llm.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. import "C"
  13. import (
  14. "fmt"
  15. "unsafe"
  16. )
  17. // SystemInfo is an unused example of calling llama.cpp functions using CGo
  18. func SystemInfo() string {
  19. return C.GoString(C.llama_print_system_info())
  20. }
  21. func Quantize(infile, outfile string, ftype fileType) error {
  22. cinfile := C.CString(infile)
  23. defer C.free(unsafe.Pointer(cinfile))
  24. coutfile := C.CString(outfile)
  25. defer C.free(unsafe.Pointer(coutfile))
  26. params := C.llama_model_quantize_default_params()
  27. params.nthread = -1
  28. params.ftype = ftype.Value()
  29. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  30. return fmt.Errorf("llama_model_quantize: %d", rc)
  31. }
  32. return nil
  33. }