llm.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package llm
  2. // #cgo CFLAGS: -Illama.cpp -Illama.cpp/include -Illama.cpp/ggml/include
  3. // #cgo LDFLAGS: -lllama -lggml -lstdc++ -lpthread
  4. // #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/build/darwin/arm64_static -L${SRCDIR}/build/darwin/arm64_static/src -L${SRCDIR}/build/darwin/arm64_static/ggml/src -framework Accelerate -framework Metal
  5. // #cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/build/darwin/x86_64_static -L${SRCDIR}/build/darwin/x86_64_static/src -L${SRCDIR}/build/darwin/x86_64_static/ggml/src
  6. // #cgo windows,amd64 LDFLAGS: -static-libstdc++ -static-libgcc -static -L${SRCDIR}/build/windows/amd64_static -L${SRCDIR}/build/windows/amd64_static/src -L${SRCDIR}/build/windows/amd64_static/ggml/src
  7. // #cgo windows,arm64 LDFLAGS: -static-libstdc++ -static-libgcc -static -L${SRCDIR}/build/windows/arm64_static -L${SRCDIR}/build/windows/arm64_static/src -L${SRCDIR}/build/windows/arm64_static/ggml/src
  8. // #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/build/linux/x86_64_static -L${SRCDIR}/build/linux/x86_64_static/src -L${SRCDIR}/build/linux/x86_64_static/ggml/src
  9. // #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/build/linux/arm64_static -L${SRCDIR}/build/linux/arm64_static/src -L${SRCDIR}/build/linux/arm64_static/ggml/src
  10. // #include <stdlib.h>
  11. // #include "llama.h"
  12. // bool update_quantize_progress(int progress, void* data) {
  13. // *((int*)data) = progress;
  14. // return true;
  15. // }
  16. import "C"
  17. import (
  18. "fmt"
  19. "unsafe"
  20. )
  21. // SystemInfo is an unused example of calling llama.cpp functions using CGo
  22. func SystemInfo() string {
  23. return C.GoString(C.llama_print_system_info())
  24. }
  25. func Quantize(infile, outfile string, ftype fileType, count *int) error {
  26. cinfile := C.CString(infile)
  27. defer C.free(unsafe.Pointer(cinfile))
  28. coutfile := C.CString(outfile)
  29. defer C.free(unsafe.Pointer(coutfile))
  30. params := C.llama_model_quantize_default_params()
  31. params.nthread = -1
  32. params.ftype = ftype.Value()
  33. // Initialize "global" to store progress
  34. store := C.malloc(C.sizeof(int))
  35. params.quantize_callback_data = store
  36. params.quantize_callback = C.update_quantize_progress
  37. go func () {
  38. for {
  39. time.Sleep(60 * time.Millisecond)
  40. if params.quantize_callback_data == nil {
  41. return
  42. } else {
  43. *count = int(*(*C.int)(store))
  44. }
  45. }
  46. }()
  47. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  48. return fmt.Errorf("llama_model_quantize: %d", rc)
  49. }
  50. return nil
  51. }