llm.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package llm
  2. // #cgo CFLAGS: -Illama.cpp
  3. // #cgo darwin,arm64 LDFLAGS: ${SRCDIR}/build/darwin/arm64_static/libllama.a -lstdc++
  4. // #cgo darwin,amd64 LDFLAGS: ${SRCDIR}/build/darwin/x86_64_static/libllama.a -lstdc++
  5. // #cgo windows,amd64 LDFLAGS: ${SRCDIR}/build/windows/amd64_static/libllama.a -static -lstdc++
  6. // #cgo windows,arm64 LDFLAGS: ${SRCDIR}/build/windows/arm64_static/libllama.a -static -lstdc++
  7. // #cgo linux,amd64 LDFLAGS: ${SRCDIR}/build/linux/x86_64_static/libllama.a -lstdc++
  8. // #cgo linux,arm64 LDFLAGS: ${SRCDIR}/build/linux/arm64_static/libllama.a -lstdc++
  9. // #include <stdlib.h>
  10. // #include "llama.h"
  11. import "C"
  12. import (
  13. "fmt"
  14. "unsafe"
  15. )
  16. // SystemInfo is an unused example of calling llama.cpp functions using CGo
  17. func SystemInfo() string {
  18. return C.GoString(C.llama_print_system_info())
  19. }
  20. func Quantize(infile, outfile string, ftype fileType) error {
  21. cinfile := C.CString(infile)
  22. defer C.free(unsafe.Pointer(cinfile))
  23. coutfile := C.CString(outfile)
  24. defer C.free(unsafe.Pointer(coutfile))
  25. params := C.llama_model_quantize_default_params()
  26. params.nthread = -1
  27. params.ftype = ftype.Value()
  28. if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
  29. return fmt.Errorf("llama_model_quantize: %d", rc)
  30. }
  31. return nil
  32. }