-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhugot_training.go
177 lines (152 loc) · 4.35 KB
/
hugot_training.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
package hugot
import (
"context"
"errors"
"fmt"
"io"
"os"
"github.com/knights-analytics/hugot/datasets"
"github.com/knights-analytics/hugot/options"
"github.com/knights-analytics/hugot/pipelineBackends"
"github.com/knights-analytics/hugot/pipelines"
"github.com/knights-analytics/hugot/util"
)
type TrainingSession struct {
runtime string
pipeline pipelineBackends.Pipeline
config TrainingConfig
}
// GetPipeline returns the pipeline used in the training session.
func (s *TrainingSession) GetPipeline() pipelineBackends.Pipeline {
return s.pipeline
}
func (s *TrainingSession) Destroy() error {
err := s.pipeline.GetModel().Destroy()
if err != nil {
return err
}
s.pipeline = nil
return nil
}
type TrainingOption func(eo *TrainingSession) error
type TrainingConfig struct {
ModelPath string
OnnxFilename string
Cuda bool
Epochs int
XlaTrainingOptions *XLATrainingOptions
Dataset datasets.Dataset
Verbose bool
}
func newTrainingSession[T pipelineBackends.Pipeline](runtime string, config TrainingConfig) (*TrainingSession, error) {
session := &TrainingSession{
config: config,
runtime: runtime,
}
var trainingPipeline T
var model *pipelineBackends.Model
var err error
opts := options.Defaults()
opts.Runtime = runtime
switch runtime {
case "XLA":
opts.XLAOptions.Cuda = config.Cuda
default:
return nil, fmt.Errorf("runtime %s is not supported", runtime)
}
if config.Epochs <= 0 {
config.Epochs = 1
}
model, err = pipelineBackends.LoadModel(config.ModelPath, config.OnnxFilename, opts)
if err != nil {
return nil, err
}
switch any(trainingPipeline).(type) {
case *pipelines.FeatureExtractionPipeline:
pipelineConfig := FeatureExtractionConfig{}
pipeline := any(trainingPipeline).(*pipelines.FeatureExtractionPipeline)
pipeline, _, err = InitializePipeline(pipeline, pipelineConfig, opts, model)
if err != nil {
return nil, err
}
session.pipeline = pipeline
// hook the dataset up with the pipeline for tokenization
if d, ok := session.config.Dataset.(*datasets.SemanticSimilarityDataset); !ok {
return nil, fmt.Errorf("expected SemanticSimilarityDataset, got %T", d)
} else {
if e := d.SetTokenizationPipeline(pipeline); e != nil {
return nil, e
}
}
default:
return nil, fmt.Errorf("training for pipeline type is not supported")
}
if session.config.Verbose {
session.config.Dataset.SetVerbose(true)
}
return session, nil
}
func (s *TrainingSession) Train() error {
switch s.runtime {
case "XLA":
return TrainXLA(s)
default:
return fmt.Errorf("training runtime %s is not supported", s.runtime)
}
}
// Save serializes the trained model as an onnx model.
// If a tokenizer is present, the tokenizer files are copied from the untrained model directory to the trained model.
// Path is the full path to the directory where the model will be saved.
func (s *TrainingSession) Save(path string) error {
if path == "" {
return fmt.Errorf("path is required")
}
var writeErr error
model := s.pipeline.GetModel()
if model != nil {
if s.runtime == "XLA" {
xlaModel := model.XLAModel
if xlaModel != nil {
w, err := util.NewFileWriter(util.PathJoinSafe(path, "model.onnx"), "")
if err != nil {
return err
}
defer func() {
writeErr = errors.Join(writeErr, w.Close())
}()
if writeErr = xlaModel.Save(w); writeErr != nil {
return writeErr
}
if model.Tokenizer != nil {
// copy tokenizer files from original model
if writeErr = copyTokenizer(model.Path, path); writeErr != nil {
return writeErr
}
}
return writeErr
}
return fmt.Errorf("XLA model is nil")
} else {
return fmt.Errorf("XLA runtime is required for saving a training model")
}
} else {
return fmt.Errorf("pipeline model is nil")
}
}
func copyTokenizer(from, to string) error {
toCopy := map[string]bool{
"special_tokens_map.json": true,
"tokenizer_config.json": true,
"tokenizer.json": true,
"vocab.txt": true,
}
walker := func(_ context.Context, _ string, parent string, info os.FileInfo, _ io.Reader) (toContinue bool, err error) {
if toCopy[info.Name()] {
if err := util.CopyFile(util.PathJoinSafe(from, parent, info.Name()), to); err != nil {
return false, err
}
}
return true, nil
}
return util.WalkDir()(context.Background(), from, walker)
}