llm.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package llm
  2. // #cgo CPPFLAGS: -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(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), tensorCount int) 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.llama_progress_callback)(C.update_quantize_progress)
  42. ticker := time.NewTicker(60 * time.Millisecond)
  43. done := make(chan struct{})
  44. defer close(done)
  45. go func() {
  46. defer ticker.Stop()
  47. for {
  48. select {
  49. case <-ticker.C:
  50. fn(api.ProgressResponse{
  51. Status: fmt.Sprintf("quantizing model tensors %d/%d", int(*((*C.float)(store))), tensorCount),
  52. Quantize: "quant",
  53. })
  54. fmt.Println("Progress: ", *((*C.float)(store)))
  55. case <-done:
  56. fn(api.ProgressResponse{
  57. Status: fmt.Sprintf("quantizing model tensors %d/%d", tensorCount, tensorCount),
  58. Quantize: "quant",
  59. })
  60. return
  61. }
  62. }
  63. }()
  64. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  65. return fmt.Errorf("llama_model_quantize: %d", rc)
  66. }
  67. return nil
  68. }