llm.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <stdatomic.h>
  12. // #include "llama.h"
  13. // bool update_quantize_progress(float progress, void* data) {
  14. // atomic_int* atomicData = (atomic_int*)data;
  15. // int intProgress = *((int*)&progress);
  16. // atomic_store(atomicData, intProgress);
  17. // return true;
  18. // }
  19. import "C"
  20. import (
  21. "errors"
  22. "fmt"
  23. "sync/atomic"
  24. "time"
  25. "unsafe"
  26. "github.com/ollama/ollama/api"
  27. )
  28. // SystemInfo is an unused example of calling llama.cpp functions using CGo
  29. func SystemInfo() string {
  30. return C.GoString(C.llama_print_system_info())
  31. }
  32. func Quantize(infile, outfile string, ftype fileType, fn func(resp api.ProgressResponse), tensorCount int) error {
  33. cinfile := C.CString(infile)
  34. defer C.free(unsafe.Pointer(cinfile))
  35. coutfile := C.CString(outfile)
  36. defer C.free(unsafe.Pointer(coutfile))
  37. params := C.llama_model_quantize_default_params()
  38. params.nthread = -1
  39. params.ftype = ftype.Value()
  40. // Initialize "global" to store progress
  41. store := (*int32)(C.malloc(C.sizeof_int))
  42. defer C.free(unsafe.Pointer(store))
  43. // Initialize store value, e.g., setting initial progress to 0
  44. atomic.StoreInt32(store, 0)
  45. params.quantize_callback_data = unsafe.Pointer(store)
  46. params.quantize_callback = (C.llama_progress_callback)(C.update_quantize_progress)
  47. ticker := time.NewTicker(30 * time.Millisecond)
  48. done := make(chan struct{})
  49. defer close(done)
  50. go func() {
  51. defer ticker.Stop()
  52. for {
  53. select {
  54. case <-ticker.C:
  55. progressInt := atomic.LoadInt32(store)
  56. progress := *(*float32)(unsafe.Pointer(&progressInt))
  57. fn(api.ProgressResponse{
  58. Status: fmt.Sprintf("quantizing model %d%%", 100*int(progress)/tensorCount),
  59. })
  60. case <-done:
  61. fn(api.ProgressResponse{
  62. Status: fmt.Sprintf("quantizing model 100%%"),
  63. })
  64. return
  65. }
  66. }
  67. }()
  68. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  69. return errors.New("failed to quantize model. This model architecture may not be supported, or you may need to upgrade Ollama to the latest version")
  70. }
  71. return nil
  72. }