-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadapter.go
More file actions
342 lines (284 loc) · 9.73 KB
/
adapter.go
File metadata and controls
342 lines (284 loc) · 9.73 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
// Package training provides adapter implementations for bridging existing and new interfaces.
package training
import (
"context"
"fmt"
"os"
ztensorgguf "github.com/zerfoo/ztensor/gguf"
"github.com/zerfoo/ztensor/graph"
"github.com/zerfoo/ztensor/tensor"
"github.com/zerfoo/zerfoo/training/optimizer"
)
// TrainerWorkflowAdapter adapts the existing Trainer interface to the new TrainingWorkflow interface.
// This allows legacy trainer implementations to work with the new generic workflow system.
type TrainerWorkflowAdapter[T tensor.Numeric] struct {
trainer Trainer[T]
optimizer optimizer.Optimizer[T]
config WorkflowConfig
metrics map[string]interface{}
}
// NewTrainerWorkflowAdapter creates a new adapter for legacy trainers.
func NewTrainerWorkflowAdapter[T tensor.Numeric](trainer Trainer[T], opt optimizer.Optimizer[T]) *TrainerWorkflowAdapter[T] {
return &TrainerWorkflowAdapter[T]{
trainer: trainer,
optimizer: opt,
metrics: make(map[string]interface{}),
}
}
// Initialize implements TrainingWorkflow.Initialize
func (a *TrainerWorkflowAdapter[T]) Initialize(ctx context.Context, config WorkflowConfig) error {
a.config = config
return nil
}
// Train implements TrainingWorkflow.Train by adapting to the legacy Trainer interface
func (a *TrainerWorkflowAdapter[T]) Train(ctx context.Context, dataset DataProvider[T], modelProvider ModelProvider[T]) (*TrainingResult[T], error) {
// Create model
model, err := modelProvider.CreateModel(ctx, a.config.ModelConfig)
if err != nil {
return nil, fmt.Errorf("failed to create model: %w", err)
}
// Get training data
dataIter, err := dataset.GetTrainingData(ctx, a.config.BatchConfig)
if err != nil {
return nil, fmt.Errorf("failed to get training data: %w", err)
}
defer func() { _ = dataIter.Close() }()
var totalLoss T
var bestLoss T
bestEpoch := 0
epoch := 0
// Training loop
for epoch < a.config.NumEpochs {
epochLoss := T(0)
batchCount := 0
// Reset iterator for new epoch
if err := dataIter.Reset(); err != nil {
return nil, fmt.Errorf("failed to reset data iterator: %w", err)
}
// Process all batches in epoch
for dataIter.Next(ctx) {
batch := dataIter.Batch()
if batch == nil {
break
}
// Convert batch targets to the required format for legacy trainer
targets := batch.Targets
// Perform training step using legacy trainer
stepLoss, err := a.trainer.TrainStep(ctx, model, a.optimizer, batch.Inputs, targets)
if err != nil {
return nil, fmt.Errorf("training step failed at epoch %d: %w", epoch, err)
}
epochLoss += stepLoss
batchCount++
}
if err := dataIter.Error(); err != nil {
return nil, fmt.Errorf("data iteration failed at epoch %d: %w", epoch, err)
}
// Calculate average loss for epoch
if batchCount > 0 {
epochLoss /= T(batchCount)
}
totalLoss = epochLoss
// Track best loss
if epoch == 0 || epochLoss < bestLoss {
bestLoss = epochLoss
bestEpoch = epoch
}
// Store metrics
a.metrics[fmt.Sprintf("epoch_%d_loss", epoch)] = float64(epochLoss)
epoch++
}
// Return training result
result := &TrainingResult[T]{
FinalLoss: totalLoss,
BestLoss: bestLoss,
BestEpoch: bestEpoch,
TotalEpochs: epoch,
Metrics: make(map[string]float64),
Extensions: make(map[string]interface{}),
}
// Convert metrics to float64 for result
for key, value := range a.metrics {
if floatVal, ok := value.(float64); ok {
result.Metrics[key] = floatVal
}
}
return result, nil
}
// Validate implements TrainingWorkflow.Validate
func (a *TrainerWorkflowAdapter[T]) Validate(ctx context.Context, dataset DataProvider[T], modelProvider ModelProvider[T]) (*ValidationResult[T], error) {
// For the adapter, validation is simplified - we just run through validation data
// without updating model parameters
dataIter, err := dataset.GetValidationData(ctx, a.config.BatchConfig)
if err != nil {
return nil, fmt.Errorf("failed to get validation data: %w", err)
}
defer func() { _ = dataIter.Close() }()
var totalLoss T
sampleCount := 0
for dataIter.Next(ctx) {
batch := dataIter.Batch()
if batch == nil {
break
}
sampleCount++
// Note: For validation, we would typically run forward pass only,
// but legacy trainer interface doesn't provide this.
// This is a limitation of the adapter approach.
totalLoss += T(0) // Placeholder - actual validation would need model forward pass
}
if err := dataIter.Error(); err != nil {
return nil, fmt.Errorf("validation data iteration failed: %w", err)
}
avgLoss := T(0)
if sampleCount > 0 {
avgLoss = totalLoss / T(sampleCount)
}
result := &ValidationResult[T]{
Loss: avgLoss,
Metrics: make(map[string]float64),
SampleCount: sampleCount,
Extensions: make(map[string]interface{}),
}
return result, nil
}
// GetMetrics implements TrainingWorkflow.GetMetrics
func (a *TrainerWorkflowAdapter[T]) GetMetrics() map[string]interface{} {
return a.metrics
}
// Shutdown implements TrainingWorkflow.Shutdown
func (a *TrainerWorkflowAdapter[T]) Shutdown(ctx context.Context) error {
// Clear metrics
a.metrics = make(map[string]interface{})
return nil
}
// GradientStrategyAdapter adapts GradientStrategy to work with the new interface system.
type GradientStrategyAdapter[T tensor.Numeric] struct {
strategy GradientStrategy[T]
graph *graph.Graph[T]
lossNode graph.Node[T]
}
// NewGradientStrategyAdapter creates a new gradient strategy adapter.
func NewGradientStrategyAdapter[T tensor.Numeric](strategy GradientStrategy[T], g *graph.Graph[T], lossNode graph.Node[T]) *GradientStrategyAdapter[T] {
return &GradientStrategyAdapter[T]{
strategy: strategy,
graph: g,
lossNode: lossNode,
}
}
// ComputeGradientsFromBatch adapts batch processing to the legacy GradientStrategy interface.
func (a *GradientStrategyAdapter[T]) ComputeGradientsFromBatch(ctx context.Context, batch *Batch[T]) (T, error) {
return a.strategy.ComputeGradients(ctx, a.graph, a.lossNode, *batch)
}
// DataIteratorAdapter provides a simple iterator implementation over static data.
type DataIteratorAdapter[T tensor.Numeric] struct {
batches []*Batch[T]
currentIdx int
err error
}
// NewDataIteratorAdapter creates a new data iterator from a slice of batches.
func NewDataIteratorAdapter[T tensor.Numeric](batches []*Batch[T]) *DataIteratorAdapter[T] {
return &DataIteratorAdapter[T]{
batches: batches,
currentIdx: -1,
}
}
// Next implements DataIterator.Next
func (d *DataIteratorAdapter[T]) Next(ctx context.Context) bool {
d.currentIdx++
return d.currentIdx < len(d.batches)
}
// Batch implements DataIterator.Batch
func (d *DataIteratorAdapter[T]) Batch() *Batch[T] {
if d.currentIdx < 0 || d.currentIdx >= len(d.batches) {
return nil
}
return d.batches[d.currentIdx]
}
// Error implements DataIterator.Error
func (d *DataIteratorAdapter[T]) Error() error {
return d.err
}
// Close implements DataIterator.Close
func (d *DataIteratorAdapter[T]) Close() error {
return nil
}
// Reset implements DataIterator.Reset
func (d *DataIteratorAdapter[T]) Reset() error {
d.currentIdx = -1
d.err = nil
return nil
}
// SimpleModelProvider provides a basic model provider implementation.
type SimpleModelProvider[T tensor.Numeric] struct {
modelFactory func(ctx context.Context, config ModelConfig) (*graph.Graph[T], error)
modelInfo ModelInfo
}
// NewSimpleModelProvider creates a new simple model provider.
func NewSimpleModelProvider[T tensor.Numeric](
factory func(ctx context.Context, config ModelConfig) (*graph.Graph[T], error),
info ModelInfo,
) *SimpleModelProvider[T] {
return &SimpleModelProvider[T]{
modelFactory: factory,
modelInfo: info,
}
}
// CreateModel implements ModelProvider.CreateModel
func (s *SimpleModelProvider[T]) CreateModel(ctx context.Context, config ModelConfig) (*graph.Graph[T], error) {
if s.modelFactory == nil {
return nil, fmt.Errorf("no model factory configured")
}
return s.modelFactory(ctx, config)
}
// LoadModel implements ModelProvider.LoadModel
func (s *SimpleModelProvider[T]) LoadModel(ctx context.Context, path string) (*graph.Graph[T], error) {
return nil, fmt.Errorf("model loading not implemented in SimpleModelProvider")
}
// SaveModel implements ModelProvider.SaveModel by writing model parameters to
// a GGUF file using the shared ztensor/gguf writer. Each graph parameter is
// stored as a float32 tensor. The model info architecture name is written as
// the general.architecture metadata key.
func (s *SimpleModelProvider[T]) SaveModel(ctx context.Context, model *graph.Graph[T], path string) error {
if model == nil {
return fmt.Errorf("training: SaveModel: model is nil")
}
params := model.Parameters()
if len(params) == 0 {
return fmt.Errorf("training: SaveModel: model has no parameters")
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("training: SaveModel: create file: %w", err)
}
defer f.Close()
w := ztensorgguf.NewWriter()
arch := s.modelInfo.Architecture
if arch == "" {
arch = "generic"
}
w.AddMetadataString("general.architecture", arch)
if s.modelInfo.Name != "" {
w.AddMetadataString("general.name", s.modelInfo.Name)
}
for _, p := range params {
data := p.Value.Data()
f32Data := make([]float32, len(data))
for i, v := range data {
f32Data[i] = float32(v)
}
w.AddTensorF32(p.Name, p.Value.Shape(), f32Data)
}
if err := w.Write(f); err != nil {
return fmt.Errorf("training: SaveModel: write GGUF: %w", err)
}
return nil
}
// GetModelInfo implements ModelProvider.GetModelInfo
func (s *SimpleModelProvider[T]) GetModelInfo() ModelInfo {
return s.modelInfo
}
// Ensure adapters implement their respective interfaces
var _ TrainingWorkflow[float32] = (*TrainerWorkflowAdapter[float32])(nil)
var _ DataIterator[float32] = (*DataIteratorAdapter[float32])(nil)
var _ ModelProvider[float32] = (*SimpleModelProvider[float32])(nil)