llm.go 1.23 KB
Newer Older
1
2
package llm

3
4
5
6
// #cgo CFLAGS: -Illama.cpp
// #cgo darwin,arm64 LDFLAGS: ${SRCDIR}/build/darwin/arm64_static/libllama.a -lstdc++
// #cgo darwin,amd64 LDFLAGS: ${SRCDIR}/build/darwin/x86_64_static/libllama.a -lstdc++
// #cgo windows,amd64 LDFLAGS: ${SRCDIR}/build/windows/amd64_static/libllama.a -static -lstdc++
7
// #cgo windows,arm64 LDFLAGS: ${SRCDIR}/build/windows/arm64_static/libllama.a -static -lstdc++
8
9
// #cgo linux,amd64 LDFLAGS: ${SRCDIR}/build/linux/x86_64_static/libllama.a -lstdc++
// #cgo linux,arm64 LDFLAGS: ${SRCDIR}/build/linux/arm64_static/libllama.a -lstdc++
Michael Yang's avatar
Michael Yang committed
10
// #include <stdlib.h>
11
12
// #include "llama.h"
import "C"
Michael Yang's avatar
Michael Yang committed
13
14
15
16
import (
	"fmt"
	"unsafe"
)
17
18
19
20

// SystemInfo is an unused example of calling llama.cpp functions using CGo
func SystemInfo() string {
	return C.GoString(C.llama_print_system_info())
21
}
Michael Yang's avatar
Michael Yang committed
22

Michael Yang's avatar
Michael Yang committed
23
func Quantize(infile, outfile string, ftype fileType) error {
Michael Yang's avatar
Michael Yang committed
24
25
26
27
28
29
30
31
	cinfile := C.CString(infile)
	defer C.free(unsafe.Pointer(cinfile))

	coutfile := C.CString(outfile)
	defer C.free(unsafe.Pointer(coutfile))

	params := C.llama_model_quantize_default_params()
	params.nthread = -1
Michael Yang's avatar
Michael Yang committed
32
	params.ftype = ftype.Value()
Michael Yang's avatar
Michael Yang committed
33

Michael Yang's avatar
Michael Yang committed
34
35
	if rc := C.llama_model_quantize(cinfile, coutfile, &params); rc != 0 {
		return fmt.Errorf("llama_model_quantize: %d", rc)
Michael Yang's avatar
Michael Yang committed
36
37
38
39
	}

	return nil
}