llm.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // static bool update_quantize_progress(float progress, void* data) {
  13. // *((float*)data) = progress;
  14. // return true;
  15. // }
  16. import "C"
  17. import (
  18. "fmt"
  19. "unsafe"
  20. "time"
  21. "github.com/ollama/ollama/api"
  22. )
  23. // SystemInfo is an unused example of calling llama.cpp functions using CGo
  24. func SystemInfo() string {
  25. return C.GoString(C.llama_print_system_info())
  26. }
  27. func Quantize(infile, outfile string, ftype fileType, fn func(resp api.ProgressResponse) ) error {
  28. cinfile := C.CString(infile)
  29. defer C.free(unsafe.Pointer(cinfile))
  30. coutfile := C.CString(outfile)
  31. defer C.free(unsafe.Pointer(coutfile))
  32. params := C.llama_model_quantize_default_params()
  33. params.nthread = -1
  34. params.ftype = ftype.Value()
  35. // Initialize "global" to store progress
  36. store := C.malloc(C.sizeof_float)
  37. defer C.free(unsafe.Pointer(store))
  38. // Initialize store value, e.g., setting initial progress to 0
  39. *(*C.float)(store) = 0.0
  40. params.quantize_callback_data = store
  41. params.quantize_callback = C.update_quantize_progress
  42. go func () {
  43. for {
  44. time.Sleep(60 * time.Millisecond)
  45. if params.quantize_callback_data == nil {
  46. return
  47. } else {
  48. progress := *((*C.float)(store))
  49. fn(api.ProgressResponse{
  50. Status: fmt.Sprintf("quantizing model %d%%", int(progress*100)),
  51. Quantize: "quant",
  52. })
  53. }
  54. }
  55. }()
  56. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  57. return fmt.Errorf("llama_model_quantize: %d", rc)
  58. }
  59. return nil
  60. }