-
Notifications
You must be signed in to change notification settings - Fork 347
/
legacy.go
287 lines (244 loc) · 8.32 KB
/
legacy.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
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
package expconf
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/determined-ai/determined/master/pkg/ptrs"
"github.com/determined-ai/determined/master/pkg/schemas"
)
// LegacyConfig represents a config that was once valid but might no longer be shimmable to the
// current version of ExperimentConfig. Fortunately, the parts of the system that need to read
// configs that are this old are either 1) only concerned with the raw config for rendering it to
// the user, or 2) only need very specific parts of the old config, so to reduce our own burden we
// only expose very specific data from it.
//
// LegacyConfig can be used to deal with configs that contain some EOL components, like searchers
// that have been removed.
type LegacyConfig struct {
CheckpointStorage CheckpointStorageConfig
BindMounts BindMountsConfig
Environment EnvironmentConfig
Hyperparameters Hyperparameters
Searcher LegacySearcher
}
// LegacySearcher represents a subset of the SearcherConfig which can be expected to be available
// for all experiments, new and old.
type LegacySearcher struct {
// Name might contain an EOL searcher name.
Name string
Metric string
SmallerIsBetter bool
}
func getCheckpointStorage(raw map[string]interface{}) (CheckpointStorageConfig, error) {
var cs CheckpointStorageConfig
csOnly := raw["checkpoint_storage"]
// Special case for hdfs.
switch m := csOnly.(type) {
case map[string]any:
if t, ok := m["type"].(string); ok && t == "hdfs" {
var saveExpBest, saveTrialBest, saveTrialLatest *int
var i float64
if i, ok = m["save_experiment_best"].(float64); ok {
saveExpBest = ptrs.Ptr(int(i))
}
if i, ok = m["save_trial_best"].(float64); ok {
saveTrialBest = ptrs.Ptr(int(i))
}
if i, ok = m["save_trial_latest"].(float64); ok {
saveTrialLatest = ptrs.Ptr(int(i))
}
// nolint: exhaustivestruct
dummyHDFSSharedFS := schemas.WithDefaults(CheckpointStorageConfig{
RawSharedFSConfig: &SharedFSConfig{
RawHostPath: ptrs.Ptr("/legacy-hdfs-checkpoint-path"),
},
RawSaveExperimentBest: saveExpBest,
RawSaveTrialBest: saveTrialBest,
RawSaveTrialLatest: saveTrialLatest,
})
if err := schemas.IsComplete(dummyHDFSSharedFS); err != nil {
return cs, fmt.Errorf("shared fs shim for hdfs is incomplete: %w", err)
}
return dummyHDFSSharedFS, nil
}
}
csByts, err := json.Marshal(csOnly)
if err != nil {
return cs, errors.Wrap(err, "unable to remarshal config bytes as json")
}
// Read the checkpoint storage.
if err = schemas.SaneBytes(&cs, csByts); err != nil {
return cs, errors.Wrap(err, "legacy checkpoint storage does not pass sanity checks")
}
if err = json.Unmarshal(csByts, &cs); err != nil {
return cs, errors.Wrap(err, "unable to unmarshal checkpoint storage bytes")
}
// Fill defaults (should be a no-op).
cs = schemas.WithDefaults(cs)
// Validate fully before passing anything out.
if err = schemas.IsComplete(cs); err != nil {
return cs, errors.Wrap(err, "legacy checkpoint storage is incomplete")
}
return cs, nil
}
func getBindMounts(raw map[string]interface{}) (BindMountsConfig, error) {
bm := BindMountsConfig{}
bmOnly := raw["bind_mounts"]
if bmOnly == nil {
// Empty bind_mounts.
return bm, nil
}
bmByts, err := json.Marshal(bmOnly)
if err != nil {
return bm, errors.Wrap(err, "unable to remarshal bind mounts as json")
}
if err = schemas.SaneBytes(&bm, bmByts); err != nil {
return bm, errors.Wrap(err, "legacy bind mounts does not pass sanity checks")
}
if err = json.Unmarshal(bmByts, &bm); err != nil {
return bm, errors.Wrap(err, "unable to unmarshal bind mounts bytes")
}
bm = schemas.WithDefaults(bm)
if err = schemas.IsComplete(bm); err != nil {
return bm, errors.Wrap(err, "legacy bind mounts is incomplete")
}
return bm, nil
}
func getEnvironment(raw map[string]interface{}) (EnvironmentConfig, error) {
var env EnvironmentConfig
envOnly := raw["environment"]
if envOnly != nil {
envByts, err := json.Marshal(envOnly)
if err != nil {
return env, errors.Wrap(err, "unable to remarshal environment as json")
}
if err = schemas.SaneBytes(&env, envByts); err != nil {
return env, errors.Wrap(err, "legacy environment does not pass sanity checks")
}
if err = json.Unmarshal(envByts, &env); err != nil {
return env, errors.Wrap(err, "unable to unmarshal environment bytes")
}
}
env = schemas.WithDefaults(env)
if err := schemas.IsComplete(env); err != nil {
return env, errors.Wrap(err, "legacy environment is incomplete")
}
return env, nil
}
func getHyperparameters(raw map[string]interface{}) (Hyperparameters, error) {
h := Hyperparameters{}
hpOnly := raw["hyperparameters"]
if hpOnly == nil {
// Empty hyperparameters.
return h, nil
}
hpBytes, err := json.Marshal(hpOnly)
if err != nil {
return h, errors.Wrap(err, "unable to remarshal hyperparameters as json")
}
if err = schemas.SaneBytes(&h, hpBytes); err != nil {
return h, errors.Wrap(err, "legacy hyperparameters do not pass sanity checks")
}
if err = json.Unmarshal(hpBytes, &h); err != nil {
return h, errors.Wrap(err, "unable to unmarshal hyperparameter bytes")
}
h = schemas.WithDefaults(h)
if err = schemas.IsComplete(h); err != nil {
return h, errors.Wrap(err, "legacy hyperparameters are incomplete")
}
return h, nil
}
func getLegacySearcher(raw map[string]interface{}) (LegacySearcher, error) {
searcher := raw["searcher"]
if searcher == nil {
return LegacySearcher{}, errors.New("searcher field missing")
}
tsearcher, ok := searcher.(map[string]interface{})
if !ok {
return LegacySearcher{}, errors.New("searcher field is not a map")
}
name, ok := tsearcher["name"]
if !ok {
return LegacySearcher{}, errors.New("searcher.name missing")
}
tname, ok := name.(string)
if !ok {
return LegacySearcher{}, errors.New("searcher.name is not a string")
}
metric, ok := tsearcher["metric"]
if !ok {
return LegacySearcher{}, errors.New("searcher.metric missing")
}
tmetric, ok := metric.(string)
if !ok {
return LegacySearcher{}, errors.New("searcher.metric is not a string")
}
// smallerIsBetter has always had a default, and always the same one
tsmallerIsBetter := true
if smallerIsBetter, ok := tsearcher["smaller_is_better"]; ok && smallerIsBetter != nil {
tsmallerIsBetter, ok = smallerIsBetter.(bool)
if !ok {
return LegacySearcher{}, errors.New("searcher.smaller_is_better is not a boolean")
}
}
return LegacySearcher{
Name: tname,
Metric: tmetric,
SmallerIsBetter: tsmallerIsBetter,
}, nil
}
// ParseLegacyConfigJSON parses bytes that represent an experiment config that was once valid
// but might no longer be shimmable to the current version of ExperimentConfig.
func ParseLegacyConfigJSON(byts []byte) (LegacyConfig, error) {
// Known difficulties (as of 0.15.5) that we are implicitly avoiding here:
// - Pre-remove-steps configs cannot be parsed as ExperimentConfig because several
// fields changed, including but not limited to batches_per_step, searcher.max_length.
// We are implicitly avoiding this by simply stripping all fields except checkpoint_storage.
// - There was a removal of adaptive and adaptive_simple. As of 0.15.5, some of that is handled
// by reading select parts of the searcher config in SQL queries. Eventually that may get
// handled here, in which case we need to be aware of that removal.
var out LegacyConfig
raw := map[string]interface{}{}
if err := json.Unmarshal(byts, &raw); err != nil {
return out, errors.Wrap(err, "unable to unmarshal bytes as json at all")
}
cs, err := getCheckpointStorage(raw)
if err != nil {
return out, err
}
out.CheckpointStorage = cs
bm, err := getBindMounts(raw)
if err != nil {
return out, err
}
out.BindMounts = bm
env, err := getEnvironment(raw)
if err != nil {
return out, err
}
out.Environment = env
hp, err := getHyperparameters(raw)
if err != nil {
return out, err
}
out.Hyperparameters = hp
searcher, err := getLegacySearcher(raw)
if err != nil {
return out, err
}
out.Searcher = searcher
return out, nil
}
// Scan implements the db.Scanner interface.
func (l *LegacyConfig) Scan(src interface{}) error {
byts, ok := src.([]byte)
if !ok {
return errors.Errorf("unable to convert to []byte: %v", src)
}
config, err := ParseLegacyConfigJSON(byts)
if err != nil {
return err
}
*l = config
return nil
}