forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
207 lines (190 loc) · 5.42 KB
/
model.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
package dlframework
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/Masterminds/semver"
"github.com/Unknwon/com"
"github.com/pkg/errors"
"github.com/rai-project/config"
"golang.org/x/sync/syncmap"
)
var modelRegistry = syncmap.Map{}
func (m *ModelManifest) Validate() error {
name := m.GetName()
if name == "" {
return errors.New("model name cannot be empty")
}
version := m.GetVersion()
if version == "" {
version = "latest"
}
if version != "latest" {
if _, err := semver.NewVersion(version); err != nil {
return errors.Wrapf(err,
"the version %s for the model %s is not in semantic version format",
m.GetVersion(),
m.GetName(),
)
}
}
return nil
}
func (m ModelManifest) MustCanonicalName() string {
s, err := m.CanonicalName()
if err != nil {
log.WithField("model_name", m.GetName()).Fatal("unable to get model canonical name")
return ""
}
return s
}
func (m ModelManifest) CanonicalName() (string, error) {
if m.GetFramework() == nil {
return "", errors.Errorf("the model %s does not have a valid framework", m.GetName())
}
framework, err := m.ResolveFramework()
if err != nil {
return "", err
}
frameworkName, err := framework.CanonicalName()
if err != nil {
return "", errors.Wrapf(err, "cannot get canonical name for the framework %s and model %s in the registry", m.GetFramework().GetName(), m.GetName())
}
fm, ok := frameworkRegistry.Load(frameworkName)
if !ok {
return "", errors.Wrapf(err, "cannot get frame %s for model %s in the registry", frameworkName, m.GetName())
}
if _, ok := fm.(FrameworkManifest); !ok {
return "", errors.Errorf("invalid framework %s registered for model %s in the registry", frameworkName, m.GetName())
}
modelName := strings.ToLower(m.GetName())
if modelName == "" {
return "", errors.New("model name must not be empty")
}
modelVersion := m.GetVersion()
if modelVersion == "" {
modelVersion = "latest"
}
return frameworkName + "/" + modelName + ":" + modelVersion, nil
}
func (m ModelManifest) FrameworkConstraint() (*semver.Constraints, error) {
return semver.NewConstraint(m.GetFramework().GetVersion())
}
func (m ModelManifest) ResolveFramework() (FrameworkManifest, error) {
frameworks, err := Frameworks()
if err != nil {
return FrameworkManifest{}, err
}
if len(frameworks) == 0 {
return FrameworkManifest{}, errors.New("framework not found")
}
if m.GetFramework().GetVersion() == "lastest" {
sort.Slice(frameworks, func(ii, jj int) bool {
f1, _ := semver.NewVersion(frameworks[ii].GetVersion())
f2, _ := semver.NewVersion(frameworks[jj].GetVersion())
return f1.LessThan(f2)
})
return frameworks[len(frameworks)-1], nil
}
modelFrameworkConstraint, err := m.FrameworkConstraint()
if err != nil {
return FrameworkManifest{},
errors.Wrapf(err, "cannot get framework constraints for model %v", m.GetName())
}
filtered := []FrameworkManifest{}
for _, framework := range frameworks {
frameworkVersion, err := semver.NewVersion(framework.GetVersion())
if err != nil {
continue
}
if !modelFrameworkConstraint.Check(frameworkVersion) {
continue
}
filtered = append(filtered, framework)
}
if len(frameworks) == 0 {
return FrameworkManifest{}, errors.New("framework not found")
}
sort.Slice(filtered, func(ii, jj int) bool {
f1, _ := semver.NewVersion(filtered[ii].GetVersion())
f2, _ := semver.NewVersion(filtered[jj].GetVersion())
return f1.LessThan(f2)
})
return filtered[len(filtered)-1], nil
}
func (m ModelManifest) Register() error {
n, err := m.CanonicalName()
if err != nil {
return err
}
return m.RegisterNamed(n)
}
func (m ModelManifest) RegisterNamed(s string) error {
name := strings.ToLower(s)
if _, ok := modelRegistry.Load(name); ok {
return errors.Errorf("the %s model has already been registered", s)
}
modelRegistry.Store(s, m)
return nil
}
func RegisteredModelNames() []string {
return syncMapKeys(modelRegistry)
}
func Models() ([]ModelManifest, error) {
names := RegisteredModelNames()
models := make([]ModelManifest, len(names))
for ii, name := range names {
m, ok := modelRegistry.Load(name)
if !ok {
return nil, errors.Errorf("model %s was not found", name)
}
model, ok := m.(ModelManifest)
if !ok {
return nil, errors.Errorf("model %s was found but not of type ModelManifest", name)
}
models[ii] = model
}
sort.Slice(models, func(ii, jj int) bool {
return models[ii].GetName() < models[jj].GetName()
})
return models, nil
}
func FindModel(name string) (*ModelManifest, error) {
var model *ModelManifest
modelRegistry.Range(func(key0 interface{}, value interface{}) bool {
key, ok := key0.(string)
if !ok {
return true
}
if key != name {
return true
}
m, ok := value.(ModelManifest)
if !ok {
return true
}
model = &m
return false
})
if model == nil {
return nil, errors.Errorf("model %s not found in registry", name)
}
return model, nil
}
func (m *ModelManifest) WorkDir() (string, error) {
cannonicalName, err := m.CanonicalName()
if err != nil {
return "", err
}
cannonicalName = strings.Replace(cannonicalName, ":", "_", -1)
cannonicalName = strings.Replace(cannonicalName, " ", "_", -1)
cannonicalName = strings.Replace(cannonicalName, "-", "_", -1)
workDir := filepath.Join(config.App.TempDir, "dlframework", cannonicalName)
if !com.IsDir(workDir) {
if err := os.MkdirAll(workDir, 0700); err != nil {
return "", errors.Wrapf(err, "failed to create model work directory %v", workDir)
}
}
return workDir, nil
}