forked from knights-analytics/hugot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhugot_training_test.go
97 lines (83 loc) · 2.31 KB
/
hugot_training_test.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
//go:build XLA || ALL
package hugot
import (
"os"
"testing"
"github.com/knights-analytics/hugot/datasets"
"github.com/knights-analytics/hugot/pipelines"
)
func TestSemanticSimilarity(t *testing.T) {
// create a new dataset to fine-tune semantic similarity of embeddings
// you need to specify the batch size for the dataset.
// For cpu low batch sizes seem to perform best.
dataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 1)
if err != nil {
t.Fatal(err)
}
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
// create a new xla training session. Currently training is only possible by loading an onnx model
// into xla, fine-tuning it with gomlx, and then writing it back to onnx. Hugot deals with the details
// for you here.
session, err := NewXLATrainingSession[*pipelines.FeatureExtractionPipeline](
TrainingConfig{
ModelPath: modelPath,
Dataset: dataset,
Epochs: 1,
Cuda: false,
Verbose: true,
},
)
if err != nil {
t.Fatal(err)
}
// train the model
if e := session.Train(); e != nil {
t.Fatal(e)
}
// we now write the fine-tuned pipeline back to disk as an onnx model
if e := session.Save("./models/testTrain.onnx"); e != nil {
t.Fatal(e)
}
if _, err := os.Stat("./models/testTrain.onnx"); err != nil {
t.Fatal(err)
}
if err = os.Remove("./models/testTrain.onnx"); err != nil {
t.Fatal(err)
}
}
func TestSemanticSimilarityCuda(t *testing.T) {
if os.Getenv("CI") != "" {
t.SkipNow()
}
dataset, err := datasets.NewSemanticSimilarityDataset("./testData/semanticSimilarityTest.jsonl", 32)
if err != nil {
t.Fatal(err)
}
modelPath := "./models/KnightsAnalytics_all-MiniLM-L6-v2"
session, err := NewXLATrainingSession[*pipelines.FeatureExtractionPipeline](
TrainingConfig{
ModelPath: modelPath,
Dataset: dataset,
Epochs: 1,
Cuda: true, // use cuda
Verbose: true,
},
)
if err != nil {
t.Fatal(err)
}
// train the model
if e := session.Train(); e != nil {
t.Fatal(e)
}
// we now write the fine-tuned pipeline back to disk as an onnx model
if e := session.Save("./models/testTrain.onnx"); e != nil {
t.Fatal(e)
}
if _, err := os.Stat("./models/testTrain.onnx"); err != nil {
t.Fatal(err)
}
if err = os.Remove("./models/testTrain.onnx"); err != nil {
t.Fatal(err)
}
}