forked from knights-analytics/hugot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhugot_training_gomlx.go
More file actions
223 lines (194 loc) · 6.78 KB
/
hugot_training_gomlx.go
File metadata and controls
223 lines (194 loc) · 6.78 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
package hugot
import (
"errors"
"fmt"
"math"
"regexp"
"slices"
"strconv"
"strings"
"github.com/gomlx/exceptions"
"github.com/gomlx/gomlx/pkg/core/graph"
"github.com/gomlx/gomlx/pkg/ml/context"
"github.com/gomlx/gomlx/pkg/ml/train"
"github.com/gomlx/gomlx/pkg/ml/train/losses"
"github.com/gomlx/gomlx/pkg/ml/train/optimizers"
"github.com/gomlx/gomlx/pkg/core/tensors"
"github.com/gomlx/gopjrt/dtypes"
"github.com/knights-analytics/hugot/pipelineBackends"
"github.com/knights-analytics/hugot/pipelines"
)
type GOMLXTrainingOptions struct {
Optimizer optimizers.Interface
Loss losses.LossFn
}
type stoppingError struct{}
func (e stoppingError) Error() string {
return "stopping error"
}
func NewGoTrainingSession[T pipelineBackends.Pipeline](config TrainingConfig) (*TrainingSession, error) {
s, err := newTrainingSession[T]("GO", config)
if err != nil {
return nil, err
}
return newGoMLXTrainingSession(s)
}
func NewXLATrainingSession[T pipelineBackends.Pipeline](config TrainingConfig) (*TrainingSession, error) {
s, err := newTrainingSession[T]("XLA", config)
if err != nil {
return nil, err
}
return newGoMLXTrainingSession(s)
}
func newGoMLXTrainingSession(s *TrainingSession) (*TrainingSession, error) {
// set defaults
switch any(s.pipeline).(type) {
case *pipelines.FeatureExtractionPipeline:
if s.config.GOMLXTrainingOptions == nil {
s.config.GOMLXTrainingOptions = &GOMLXTrainingOptions{}
}
if s.config.GOMLXTrainingOptions.Optimizer == nil {
s.config.GOMLXTrainingOptions.Optimizer = optimizers.StochasticGradientDescent()
}
if s.config.GOMLXTrainingOptions.Loss == nil {
s.config.GOMLXTrainingOptions.Loss = losses.MeanSquaredError
}
default:
return nil, fmt.Errorf("loss function is required")
}
return s, nil
}
func TrainGoMLX(s *TrainingSession) error {
switch p := s.pipeline.(type) {
case *pipelines.FeatureExtractionPipeline:
GoMLXModel := p.Model.GoMLXModel
backend := GoMLXModel.Backend
ctx := GoMLXModel.Ctx
// freeze the layers if requested
freezeAllButLast := slices.Contains(s.freezeLayers, -1)
re := regexp.MustCompile(`layer\.(\d+)`) // identify the layer number in the variable name
for v := range ctx.IterVariables() {
name := v.Name()
if (s.freezeEmbeddings || freezeAllButLast) && strings.HasPrefix(name, "embeddings") {
v.SetTrainable(false)
continue
}
if matches := re.FindStringSubmatch(name); matches != nil {
layerNumStr := matches[1]
layerNum, err := strconv.Atoi(layerNumStr)
if err != nil {
return fmt.Errorf("failed to parse layer number from variable name %s: %w", name, err)
}
if freezeAllButLast || slices.Contains(s.freezeLayers, layerNum) {
v.SetTrainable(false)
continue
}
}
}
modelFn := func(ctx *context.Context, spec any, inputs []*context.Node) []*context.Node {
inputsLhs := inputs[:3] // inputIDs, attentionMask, tokenTypeIDs if present
inputsRhs := inputs[3:]
embeddingLhs := GoMLXModel.Call(ctx.Reuse(), inputsLhs)[0]
embeddingRhs := GoMLXModel.Call(ctx.Reuse(), inputsRhs)[0]
// we mean pool the results if needed e.g. if dimensions are [batch, seq, hidden]
if len(embeddingLhs.Shape().Dimensions) > 2 {
batchSize := embeddingLhs.Shape().Dim(0)
embeddingSize := embeddingLhs.Shape().Dim(-1)
embeddingLhs = graph.Reshape(embeddingLhs, batchSize, -1, embeddingSize)
embeddingRhs = graph.Reshape(embeddingRhs, batchSize, -1, embeddingSize)
maskLhs := graph.ConvertDType(graph.BroadcastToShape(graph.Reshape(inputsLhs[1], batchSize, -1, 1), embeddingLhs.Shape()), dtypes.Bool)
maskRhs := graph.ConvertDType(graph.BroadcastToShape(graph.Reshape(inputsRhs[1], batchSize, -1, 1), embeddingRhs.Shape()), dtypes.Bool)
embeddingLhs = graph.MaskedReduceMean(embeddingLhs, maskLhs, 1)
embeddingRhs = graph.MaskedReduceMean(embeddingRhs, maskRhs, 1)
}
cosineSimilarity := graph.CosineSimilarity(embeddingLhs, embeddingRhs, -1)
return []*context.Node{cosineSimilarity}
}
gomlxTrainer := train.NewTrainer(backend,
ctx,
modelFn,
s.config.GOMLXTrainingOptions.Loss,
s.config.GOMLXTrainingOptions.Optimizer,
nil,
nil)
loop := train.NewLoop(gomlxTrainer)
// Loop for given number of epochs.
if s.config.Verbose {
if s.earlyStopping != nil {
fmt.Printf("Training for %d epochs with early stopping\n", s.maxEpochs)
} else {
fmt.Printf("Training for %d epochs\n", s.maxEpochs)
}
}
var currentEpoch int
var trainLosses []float32
var evalLosses []float32
var bestLoss float32 = math.MaxFloat32
epochsWithoutImprovement := 0
var evaluateEpoch train.OnStepFn = func(loop *train.Loop, metrics []*tensors.Tensor) error {
if loop.Epoch != currentEpoch {
if s.config.TrainEvalDataset != nil {
if s.config.Verbose {
fmt.Printf("Running evaluation for epoch %d\n on trainEvalDataset", loop.Epoch)
}
lossAndMetrics := gomlxTrainer.Eval(s.config.TrainEvalDataset)
meanTrainLoss := lossAndMetrics[1].Value().(float32)
trainLosses = append(trainLosses, meanTrainLoss)
}
if s.earlyStopping != nil {
if s.config.Verbose {
fmt.Printf("Running evaluation for epoch %d\n on evalDataset", loop.Epoch)
}
lossAndMetrics := gomlxTrainer.Eval(s.config.EvalDataset)
meanLoss := lossAndMetrics[1].Value().(float32)
evalLosses = append(evalLosses, meanLoss)
if bestLoss-meanLoss > s.earlyStopping.tolerance {
bestLoss = meanLoss
epochsWithoutImprovement = 0
if s.config.Verbose {
fmt.Printf("New best loss: %.4f at epoch %d\n", bestLoss, loop.Epoch)
}
} else {
epochsWithoutImprovement++
if s.config.Verbose {
fmt.Printf("No improvement in loss, epochs without improvement: %d\n", epochsWithoutImprovement)
}
if epochsWithoutImprovement >= s.earlyStopping.patience {
if s.config.Verbose {
fmt.Printf("Early stopping triggered after %d epochs without improvement\n", s.earlyStopping.patience)
}
return stoppingError{} // trigger stopping
}
}
}
currentEpoch = loop.Epoch
}
return nil
}
loop.OnStep("evaluateAfterEpoch", train.Priority(1), evaluateEpoch)
// we rely on try catch because an error is returned if there is an initialization error but
// a panic will be thrown if e.g. dataset reset fails.
err := exceptions.TryCatch[error](func() {
if _, err := loop.RunEpochs(s.config.TrainDataset, s.maxEpochs); err != nil {
if errors.Is(err, stoppingError{}) {
if s.config.Verbose {
fmt.Printf("Training stopped after epoch %d\n", currentEpoch)
}
} else {
panic(err)
}
}
})
if err != nil {
return err
}
if s.config.Verbose {
fmt.Println("Training complete")
}
s.statistics = TrainingStatistics{
EpochTrainLosses: trainLosses,
EpochEvalLosses: evalLosses,
}
}
return nil
}