-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
136 lines (126 loc) · 3.04 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package treelite
// #cgo LDFLAGS: -ltreelite -ltreelite_runtime
// #include <stdlib.h>
// #include "treelite/c_api.h"
// #include "treelite/c_api_common.h"
// #include "treelite/c_api_runtime.h"
import "C"
import (
"errors"
"io"
"unsafe"
)
var (
errLoadModel = errors.New("load model error")
errFreeModel = errors.New("free model error")
)
// Model is a tree model
type Model struct {
pointer C.ModelHandle
}
// Close frees internally allocated model
func (m *Model) Close() error {
ret := C.TreeliteFreeModel(
m.pointer,
)
if ret == -1 {
return errFreeModel
}
return nil
}
// LoadLightGBMModel loads a model file generated by LightGBM (Microsoft/LightGBM).
// The model file must contain a decision tree ensemble.
func LoadLightGBMModel(modelPath string) (*Model, error) {
var handle C.ModelHandle
ret := C.TreeliteLoadLightGBMModel(
C.CString(modelPath),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}
// LoadXGBoostModel loads a model file generated by XGBoost (dmlc/xgboost).
// The model file must contain a decision tree ensemble.
func LoadXGBoostModel(modelPath string) (*Model, error) {
var handle C.ModelHandle
ret := C.TreeliteLoadXGBoostModel(
C.CString(modelPath),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}
// LoadXGBoostJSON loads a json model file generated by XGBoost (dmlc/xgboost).
// The model file must contain a decision tree ensemble.
func LoadXGBoostJSON(modelPath string) (*Model, error) {
var handle C.ModelHandle
ret := C.TreeliteLoadXGBoostJSON(
C.CString(modelPath),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}
// LoadXGBoostJSONString loads a model stored as JSON stringby XGBoost (dmlc/xgboost).
// The model json must contain a decision tree ensemble.
func LoadXGBoostJSONString(modelJSON string) (*Model, error) {
var handle C.ModelHandle
ret := C.TreeliteLoadXGBoostJSONString(
C.CString(modelJSON),
C.ulong(len(modelJSON)),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}
// LoadXGBoostModelFromMemoryBuffer loads XGBoost model from the given reader
// this method once loads all model to memory
func LoadXGBoostModelFromMemoryBuffer(reader io.Reader) (*Model, error) {
buf, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
var handle C.ModelHandle
ret := C.TreeliteLoadXGBoostModelFromMemoryBuffer(
unsafe.Pointer(&buf[0]),
C.ulong(len(buf)),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}
// LoadLightGBMModelFromString Loads a LightGBM model from a string.
// The string should be created with the model_to_string() method in LightGBM.
func LoadLightGBMModelFromString(modelString string) (*Model, error) {
var handle C.ModelHandle
ret := C.TreeliteLoadLightGBMModelFromString(
C.CString(modelString),
&handle,
)
if ret == -1 {
return nil, errLoadModel
}
return &Model{
pointer: handle,
}, nil
}