-
Notifications
You must be signed in to change notification settings - Fork 14
/
hugot.go
246 lines (215 loc) · 8.92 KB
/
hugot.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package hugot
import (
"errors"
"fmt"
"slices"
"github.com/knights-analytics/hugot/options"
"github.com/knights-analytics/hugot/pipelineBackends"
"github.com/knights-analytics/hugot/pipelines"
)
// Session allows for the creation of new pipelines and holds the pipeline already created.
type Session struct {
featureExtractionPipelines pipelineMap[*pipelines.FeatureExtractionPipeline]
tokenClassificationPipelines pipelineMap[*pipelines.TokenClassificationPipeline]
textClassificationPipelines pipelineMap[*pipelines.TextClassificationPipeline]
zeroShotClassificationPipelines pipelineMap[*pipelines.ZeroShotClassificationPipeline]
models map[string]*pipelineBackends.Model
options *options.Options
environmentDestroy func() error
}
func newSession(runtime string, opts ...options.WithOption) (*Session, error) {
parsedOptions := options.Defaults()
parsedOptions.Runtime = runtime
// Collect options into a struct, so they can be applied in the correct order later
for _, option := range opts {
err := option(parsedOptions)
if err != nil {
return nil, err
}
}
session := &Session{
featureExtractionPipelines: map[string]*pipelines.FeatureExtractionPipeline{},
textClassificationPipelines: map[string]*pipelines.TextClassificationPipeline{},
tokenClassificationPipelines: map[string]*pipelines.TokenClassificationPipeline{},
zeroShotClassificationPipelines: map[string]*pipelines.ZeroShotClassificationPipeline{},
models: map[string]*pipelineBackends.Model{},
options: parsedOptions,
environmentDestroy: func() error {
return nil
},
}
return session, nil
}
type pipelineMap[T pipelineBackends.Pipeline] map[string]T
func (m pipelineMap[T]) GetStats() []string {
var stats []string
for _, p := range m {
stats = append(stats, p.GetStats()...)
}
return stats
}
// FeatureExtractionConfig is the configuration for a feature extraction pipeline
type FeatureExtractionConfig = pipelineBackends.PipelineConfig[*pipelines.FeatureExtractionPipeline]
// FeatureExtractionOption is an option for a feature extraction pipeline
type FeatureExtractionOption = pipelineBackends.PipelineOption[*pipelines.FeatureExtractionPipeline]
// TextClassificationConfig is the configuration for a text classification pipeline
type TextClassificationConfig = pipelineBackends.PipelineConfig[*pipelines.TextClassificationPipeline]
// type ZSCConfig = pipelines.PipelineConfig[*pipelines.ZeroShotClassificationPipeline]
type ZeroShotClassificationConfig = pipelineBackends.PipelineConfig[*pipelines.ZeroShotClassificationPipeline]
// TextClassificationOption is an option for a text classification pipeline
type TextClassificationOption = pipelineBackends.PipelineOption[*pipelines.TextClassificationPipeline]
// TokenClassificationConfig is the configuration for a token classification pipeline
type TokenClassificationConfig = pipelineBackends.PipelineConfig[*pipelines.TokenClassificationPipeline]
// TokenClassificationOption is an option for a token classification pipeline
type TokenClassificationOption = pipelineBackends.PipelineOption[*pipelines.TokenClassificationPipeline]
// NewPipeline can be used to create a new pipeline of type T. The initialised pipeline will be returned and it
// will also be stored in the session object so that all created pipelines can be destroyed with session.Destroy()
// at once.
func NewPipeline[T pipelineBackends.Pipeline](s *Session, pipelineConfig pipelineBackends.PipelineConfig[T]) (T, error) {
var pipeline T
if pipelineConfig.Name == "" {
return pipeline, errors.New("a name for the pipeline is required")
}
_, getError := GetPipeline[T](s, pipelineConfig.Name)
var notFoundError *pipelineNotFoundError
if getError == nil {
return pipeline, fmt.Errorf("pipeline %s has already been initialised", pipelineConfig.Name)
} else if !errors.As(getError, ¬FoundError) {
return pipeline, getError
}
// Load model if it has not been loaded already
model, ok := s.models[pipelineConfig.ModelPath]
if !ok {
model = &pipelineBackends.Model{
Path: pipelineConfig.ModelPath,
OnnxFilename: pipelineConfig.OnnxFilename,
}
err := pipelineBackends.LoadOnnxModelBytes(model)
if err != nil {
return pipeline, err
}
err = pipelineBackends.CreateModelBackend(model, s.options)
if err != nil {
return pipeline, err
}
tkErr := pipelineBackends.LoadTokenizer(model, s.options)
if tkErr != nil {
return pipeline, tkErr
}
model.Destroy = func() error {
destroyErr := model.Tokenizer.Destroy()
switch s.options.Runtime {
case "ORT":
destroyErr = errors.Join(destroyErr, model.ORTModel.Destroy())
case "XLA":
model.XLAModel.Destroy()
}
return destroyErr
}
s.models[pipelineConfig.ModelPath] = model
}
switch any(pipeline).(type) {
case *pipelines.TokenClassificationPipeline:
config := any(pipelineConfig).(pipelineBackends.PipelineConfig[*pipelines.TokenClassificationPipeline])
pipelineInitialised, err := pipelines.NewTokenClassificationPipeline(config, s.options, model)
if err != nil {
return pipeline, err
}
s.tokenClassificationPipelines[config.Name] = pipelineInitialised
pipeline = any(pipelineInitialised).(T)
case *pipelines.TextClassificationPipeline:
config := any(pipelineConfig).(pipelineBackends.PipelineConfig[*pipelines.TextClassificationPipeline])
pipelineInitialised, err := pipelines.NewTextClassificationPipeline(config, s.options, model)
if err != nil {
return pipeline, err
}
s.textClassificationPipelines[config.Name] = pipelineInitialised
pipeline = any(pipelineInitialised).(T)
case *pipelines.FeatureExtractionPipeline:
config := any(pipelineConfig).(pipelineBackends.PipelineConfig[*pipelines.FeatureExtractionPipeline])
pipelineInitialised, err := pipelines.NewFeatureExtractionPipeline(config, s.options, model)
if err != nil {
return pipeline, err
}
s.featureExtractionPipelines[config.Name] = pipelineInitialised
pipeline = any(pipelineInitialised).(T)
case *pipelines.ZeroShotClassificationPipeline:
config := any(pipelineConfig).(pipelineBackends.PipelineConfig[*pipelines.ZeroShotClassificationPipeline])
pipelineInitialised, err := pipelines.NewZeroShotClassificationPipeline(config, s.options, model)
if err != nil {
return pipeline, err
}
s.zeroShotClassificationPipelines[config.Name] = pipelineInitialised
pipeline = any(pipelineInitialised).(T)
default:
return pipeline, fmt.Errorf("not implemented")
}
return pipeline, nil
}
// GetPipeline can be used to retrieve a pipeline of type T with the given name from the session
func GetPipeline[T pipelineBackends.Pipeline](s *Session, name string) (T, error) {
var pipeline T
switch any(pipeline).(type) {
case *pipelines.TokenClassificationPipeline:
p, ok := s.tokenClassificationPipelines[name]
if !ok {
return pipeline, &pipelineNotFoundError{pipelineName: name}
}
return any(p).(T), nil
case *pipelines.TextClassificationPipeline:
p, ok := s.textClassificationPipelines[name]
if !ok {
return pipeline, &pipelineNotFoundError{pipelineName: name}
}
return any(p).(T), nil
case *pipelines.FeatureExtractionPipeline:
p, ok := s.featureExtractionPipelines[name]
if !ok {
return pipeline, &pipelineNotFoundError{pipelineName: name}
}
return any(p).(T), nil
case *pipelines.ZeroShotClassificationPipeline:
p, ok := s.zeroShotClassificationPipelines[name]
if !ok {
return pipeline, &pipelineNotFoundError{pipelineName: name}
}
return any(p).(T), nil
default:
return pipeline, errors.New("pipeline type not supported")
}
}
type pipelineNotFoundError struct {
pipelineName string
}
func (e *pipelineNotFoundError) Error() string {
return fmt.Sprintf("Pipeline with name %s not found", e.pipelineName)
}
// GetStats returns runtime statistics for all initialized pipelines for profiling purposes. We currently record for each pipeline:
// the total runtime of the tokenization step
// the number of batch calls to the tokenization step
// the average time per tokenization batch call
// the total runtime of the inference (i.e. onnxruntime) step
// the number of batch calls to the onnxruntime inference
// the average time per onnxruntime inference batch call
func (s *Session) GetStats() []string {
// slices.Concat() is not implemented in experimental x/exp/slices package
return slices.Concat(
s.tokenClassificationPipelines.GetStats(),
s.textClassificationPipelines.GetStats(),
s.featureExtractionPipelines.GetStats(),
s.zeroShotClassificationPipelines.GetStats(),
)
}
// Destroy deletes the hugot session and onnxruntime environment and all initialized pipelines, freeing memory.
// A hugot session should be destroyed when not neeeded any more, preferably with a defer() call.
func (s *Session) Destroy() error {
var err error
for _, model := range s.models {
err = errors.Join(err, model.Destroy())
}
err = errors.Join(
s.options.Destroy(),
s.environmentDestroy(),
)
return err
}