forked from knights-analytics/hugot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhugot_training_test.go
More file actions
375 lines (320 loc) · 10.9 KB
/
hugot_training_test.go
File metadata and controls
375 lines (320 loc) · 10.9 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//go:build (ORT && XLA && TRAINING) || (ALL && TRAINING)
package hugot
import (
"bytes"
"encoding/json"
"fmt"
"math"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/knights-analytics/hugot/datasets"
"github.com/knights-analytics/hugot/options"
"github.com/knights-analytics/hugot/pipelines"
"github.com/knights-analytics/hugot/util"
)
func cosineSimilarityTester(x []float32, y []float32) float64 {
var sum, s1, s2 float64
for i := range x {
sum += float64(x[i]) * float64(y[i])
s1 += math.Pow(float64(x[i]), 2)
s2 += math.Pow(float64(y[i]), 2)
}
if s1 == 0 || s2 == 0 {
return 0.0
}
return sum / (math.Sqrt(s1) * math.Sqrt(s2))
}
func runModel(t *testing.T, runtime string, examplesLeft, examplesRight []string, modelPath string) []float64 {
t.Helper()
var session *Session
var err error
switch runtime {
case "ORT":
session, err = NewORTSession(options.WithOnnxLibraryPath(onnxRuntimeSharedLibrary))
checkT(t, err)
case "GO":
session, err = NewGoSession()
checkT(t, err)
case "XLA":
session, err = NewXLASession()
checkT(t, err)
default:
t.Fatal("unknown runtime")
}
defer func() {
checkT(t, session.Destroy())
}()
config := FeatureExtractionConfig{
ModelPath: modelPath,
Name: "testPipeline",
OnnxFilename: "model.onnx",
}
pipeline, err := NewPipeline(session, config)
checkT(t, err)
resultsLeft, err := pipeline.RunPipeline(examplesLeft)
checkT(t, err)
resultsRight, err := pipeline.RunPipeline(examplesRight)
checkT(t, err)
// calculate cosine similarity between the embeddings
var similarities []float64
for i := 0; i < len(resultsLeft.Embeddings); i++ {
similarities = append(similarities, cosineSimilarityTester(resultsLeft.Embeddings[i], resultsRight.Embeddings[i]))
}
return similarities
}
func round3decimals(x float64) float64 {
return math.Round(x*1000) / 1000
}
func trainSimilarity(t *testing.T,
config TrainingConfig,
examplesLhs,
examplesRhs []string) []float64 {
// Create a new GoMLX training session. Currently, training is only possible by loading an onnx model
// into GoMLX, fine-tuning it, and then writing it back to onnx. Hugot deals with the details
// for you here.
trainingSession, err := NewXLATrainingSession[*pipelines.FeatureExtractionPipeline](config)
if err != nil {
t.Fatal(err)
}
defer func() {
checkT(t, trainingSession.Destroy())
}()
// train the model
if e := trainingSession.Train(); e != nil {
t.Fatal(e)
}
// we now write the fine-tuned pipeline back to disk as an onnx model.
// This will also copy the tokenizer files for you. If your models are on s3
// this can also work (see documentation).
if e := trainingSession.Save("./models/testTrain"); e != nil {
t.Fatal(e)
}
if _, err := os.Stat("./models/testTrain"); err != nil {
t.Fatal(err)
}
defer func() {
if err = os.RemoveAll("./models/testTrain"); err != nil {
t.Fatal(err)
}
}()
// we now load the newly trained onnx model and generate the predictions with onnxruntime backend
return runModel(t, "ORT", examplesLhs, examplesRhs, "./models/testTrain")
}
func TestTrainSemanticSimilarity(t *testing.T) {
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
// each line in this dataset is an example. In training we will use the dataset object but for inference
// we just load the strings here.
data, err := os.ReadFile("./testData/semanticSimilarityTest.jsonl")
checkT(t, err)
lines := bytes.Split(data, []byte("\n"))
var examplesLhs []string
var examplesRhs []string
var scores []float64
for _, line := range lines {
var example map[string]any
err = json.Unmarshal(line, &example)
if err != nil {
t.Fatal(err)
}
examplesLhs = append(examplesLhs, example["sentence1"].(string))
examplesRhs = append(examplesRhs, example["sentence2"].(string))
scores = append(scores, example["score"].(float64))
}
// first we run the untrained onnx model with onnxruntime backend
similaritiesOnnxruntime := runModel(t, "ORT", examplesLhs, examplesRhs, modelPath)
// we do the same for GoMLX and check the forward pass results match
similaritiesGoMLX := runModel(t, "XLA", examplesLhs, examplesRhs, modelPath)
for i := range similaritiesOnnxruntime {
assert.Equal(t, round3decimals(similaritiesOnnxruntime[i]), round3decimals(similaritiesGoMLX[i]))
}
// TRAINING: We now fine-tune the model
// first we create a train dataset object. This allows us to loop over the dataset for potentially multiple epochs.
// We need to specify the batch size. For cpu lower batches seem to be faster.
// The datasets.NewSemanticSimilarityDataset function also accepts a custom function that will be applied
// to all examples in a batch before they are passed to the model. This can be used to apply whatever preprocessing
// you need.
trainDataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 1, nil)
if err != nil {
t.Fatal(err)
}
// next we create a trainEvalDataset. This is the same as the train dataset, but it will be used to evaluate the model on
// in-sample data at the end of each epoch.
// We can also specify an eval dataset with early stopping (see test below).
trainEvalDataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 1, nil)
if err != nil {
t.Fatal(err)
}
// we now train the model with the dataset
trainingConfig := TrainingConfig{
ModelPath: modelPath,
TrainDataset: trainDataset,
TrainEvalDataset: trainEvalDataset,
Options: []TrainingOption{
WithEpochs(2),
},
Verbose: true,
}
similaritiesGoMLXTrained := trainSimilarity(t, trainingConfig, examplesLhs, examplesRhs)
fmt.Println("GoMLX trained model predictions:")
for i := range similaritiesGoMLXTrained {
fmt.Printf("Example %d: untrained similarity %f, trained similarity %f, label %f\n", i, similaritiesGoMLX[i], similaritiesGoMLXTrained[i], scores[i])
}
// on the training set, we expect the model to have improved
rmseUntrained := rmse(similaritiesGoMLX, scores)
rmseTrained := rmse(similaritiesGoMLXTrained, scores)
fmt.Printf("RMSE untrained is: %f\n", rmseUntrained)
fmt.Printf("RMSE trained is: %f\n", rmseTrained)
assert.Less(t, rmseTrained, rmseUntrained)
assert.Equal(t, 0.047, round3decimals(rmseTrained))
// we can also train a model using an in memory dataset. For this we create the slice of examples manually.
var examples []datasets.SemanticSimilarityExample
for i := 0; i < len(examplesLhs); i++ {
examples = append(examples, datasets.SemanticSimilarityExample{
Sentence1: examplesLhs[i],
Sentence2: examplesRhs[i],
Score: float32(scores[i]),
})
}
inMemoryDataset, err := datasets.NewInMemorySemanticSimilarityDataset(examples, 1, nil)
checkT(t, err)
trainingConfig.TrainDataset = inMemoryDataset
similaritiesGoMLXTrainedInMemory := trainSimilarity(t, trainingConfig, examplesLhs, examplesRhs)
for i := range similaritiesGoMLXTrainedInMemory {
assert.Equal(t, round3decimals(similaritiesGoMLXTrained[i]), round3decimals(similaritiesGoMLXTrainedInMemory[i]))
}
// we can also freeze layers
trainingConfig.Options = append(trainingConfig.Options, WithFreezeLayers([]int{-1})) // freeze all layers but the last one
similaritiesGoMLXTrainedFrozen := trainSimilarity(t, trainingConfig, examplesLhs, examplesRhs)
fmt.Println("GoMLX trained model predictions freezing all layers but the last one:")
for i := range similaritiesGoMLXTrainedFrozen {
fmt.Printf("Example %d: untrained similarity %f, trained similarity with frozen weights %f, label %f\n", i, similaritiesGoMLX[i], similaritiesGoMLXTrainedFrozen[i], scores[i])
}
}
func rmse(predictions []float64, labels []float64) float64 {
var sum float64
for i := 0; i < len(predictions); i++ {
sum += math.Pow(predictions[i]-labels[i], 2)
}
return math.Sqrt(sum / float64(len(predictions)))
}
func TestTrainSemanticSimilarityCuda(t *testing.T) {
if os.Getenv("CI") != "" {
t.SkipNow()
}
dataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 32, nil)
if err != nil {
t.Fatal(err)
}
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
session, err := NewXLATrainingSession[*pipelines.FeatureExtractionPipeline](
TrainingConfig{
ModelPath: modelPath,
TrainDataset: dataset,
Options: []TrainingOption{
WithEpochs(1),
WithCuda(), // enable cuda
},
Verbose: true,
},
)
if err != nil {
t.Fatal(err)
}
// train the model
if err = session.Train(); err != nil {
t.Fatal(err)
}
// we now write the fine-tuned pipeline back to disk as an onnx model
if e := session.Save("./models/testTrain"); e != nil {
t.Fatal(e)
}
if exists, existsErr := util.FileExists("./models/testTrain"); existsErr != nil {
t.Fatal(err)
} else if !exists {
t.Fatal("model file ./models/testTrain does not exist")
}
if err = util.DeleteFile("./models/testTrain"); err != nil {
t.Fatal(err)
}
}
func TestTrainSemanticSimilarityGo(t *testing.T) {
if os.Getenv("CI") != "" {
t.SkipNow()
}
dataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 1, nil)
if err != nil {
t.Fatal(err)
}
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
session, err := NewGoTrainingSession[*pipelines.FeatureExtractionPipeline](
TrainingConfig{
ModelPath: modelPath,
TrainDataset: dataset,
Options: []TrainingOption{
WithEpochs(1),
},
Verbose: true,
},
)
if err != nil {
t.Fatal(err)
}
// train the model
if err = session.Train(); err != nil {
t.Fatal(err)
}
// we now write the fine-tuned pipeline back to disk as an onnx model
if e := session.Save("./models/testTrain"); e != nil {
t.Fatal(e)
}
if exists, existsErr := util.FileExists("./models/testTrain"); existsErr != nil {
t.Fatal(err)
} else if !exists {
t.Fatal("model file ./models/testTrain does not exist")
}
if err = util.DeleteFile("./models/testTrain"); err != nil {
t.Fatal(err)
}
}
func TestEarlyStopping(t *testing.T) {
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
trainDataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 1, nil)
if err != nil {
t.Fatal(err)
}
evalDataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTestEval.jsonl", 1, nil)
if err != nil {
t.Fatal(err)
}
defer func() {
err := os.RemoveAll("./models/testTrainEval")
if err != nil {
t.Fatal(err)
}
}()
trainingConfig := TrainingConfig{
ModelPath: modelPath,
TrainDataset: trainDataset,
EvalDataset: evalDataset,
Options: []TrainingOption{
WithEarlyStoppingParams(2, 1e-4),
},
Verbose: true,
}
trainingSession, err := NewXLATrainingSession[*pipelines.FeatureExtractionPipeline](trainingConfig)
if err != nil {
t.Fatal(err)
}
defer func() {
checkT(t, trainingSession.Destroy())
}()
// train the model
if trainErr := trainingSession.Train(); trainErr != nil {
t.Fatal(trainErr)
}
// save the model
if saveErr := trainingSession.Save("./models/testTrainEval"); saveErr != nil {
t.Fatal(saveErr)
}
}