-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_run.go
179 lines (156 loc) · 4.37 KB
/
file_run.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
package mlflow
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
)
type runMeta struct {
ArtifactURI string `yaml:"artifact_uri"`
ExperimentID string `yaml:"experiment_id"`
LifecycleStage string `yaml:"lifecycle_stage"`
EndTime int64 `yaml:"end_time"`
EntryPointName string `yaml:"entry_point_name"`
RunID string `yaml:"run_id"`
RunName string `yaml:"run_name"`
RunUUID string `yaml:"run_uuid"`
SourceName string `yaml:"source_name"`
// https://github.com/mlflow/mlflow/blob/8cd2eb0f7975decefb88af60ac5cc4f968458ab3/mlflow/protos/service.proto#L421
SourceType int `yaml:"source_type"`
SourceVersion string `yaml:"source_version"`
StartTime int64 `yaml:"start_time"`
// https://github.com/mlflow/mlflow/blob/8cd2eb0f7975decefb88af60ac5cc4f968458ab3/mlflow/protos/service.proto#L439
Status int `yaml:"status"`
Tags []string `yaml:"tags"`
UserID string `yaml:"user_id"`
}
type fileRun struct {
runMeta
rootDir string
}
func (r *fileRun) ID() string {
return r.RunID
}
func (r *fileRun) UIURL() string {
// Assumes UI is running on default port.
return fmt.Sprintf("http://127.0.0.1:5000/#/experiments/%s/runs/%s", r.runMeta.ExperimentID, r.RunID)
}
func (r *fileRun) syncMeta() error {
metaBytes, err := yaml.Marshal(r.runMeta)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(r.rootDir, metaDataFileName), metaBytes, 0644)
}
func (r *fileRun) SetTag(key, value string) error {
return os.WriteFile(filepath.Join(r.rootDir, tagsFolderName, key), []byte(value), 0644)
}
func (r *fileRun) SetTags(tags []Tag) error {
for _, tag := range tags {
if err := r.SetTag(tag.Key, tag.Val); err != nil {
return err
}
}
return nil
}
func (r *fileRun) GetTag(key string) (string, error) {
valBytes, err := os.ReadFile(filepath.Join(r.rootDir, tagsFolderName, key))
if err != nil {
return "", err
}
return string(valBytes), nil
}
func (r *fileRun) ArtifactDir() string {
return filepath.Join(r.rootDir, artifactsFolderName)
}
func (r *fileRun) LogArtifact(localPath, artifactPath string) error {
repo, err := NewFileArtifactRepo(r.ArtifactDir())
if err != nil {
return err
}
return repo.LogArtifact(localPath, artifactPath)
}
func (r *fileRun) LogMetric(key string, val float64, step int64) error {
if r.LifecycleStage != LifecycleStageActive {
return fmt.Errorf("run %s is not active", r.RunName)
}
if r.Status != runStatusRunning {
return fmt.Errorf("run %s is not running", r.RunName)
}
path := filepath.Join(r.rootDir, metricsFolderName, key)
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
line := fmt.Sprintf("%d %g %d\n", time.Now().UnixMilli(), val, step)
if _, err := f.Write([]byte(line)); err != nil {
f.Close() // ignore error; Write error takes precedence
return err
}
return f.Close()
}
func (r *fileRun) LogMetrics(metrics []Metric, step int64) error {
for _, metric := range metrics {
if err := r.LogMetric(metric.Key, metric.Val, step); err != nil {
return err
}
}
return nil
}
func (r *fileRun) paramPath(key string) string {
return filepath.Join(r.rootDir, paramsFolderName, key)
}
func (r *fileRun) LogParam(key, value string) error {
return os.WriteFile(r.paramPath(key), []byte(value), 0644)
}
func (r *fileRun) LogParams(params []Param) error {
for _, param := range params {
if err := r.LogParam(param.Key, param.Val); err != nil {
return err
}
}
return nil
}
func (r *fileRun) End() error {
r.EndTime = time.Now().UnixMilli()
r.Status = runStatusFinished
if err := r.syncMeta(); err != nil {
return err
}
endIfActive(r)
return nil
}
func (r *fileRun) Fail() error {
r.EndTime = time.Now().UnixMilli()
r.Status = runStatusFailed
if err := r.syncMeta(); err != nil {
return err
}
endIfActive(r)
return nil
}
func (r *fileRun) ExperimentID() string {
return r.runMeta.ExperimentID
}
func (r *fileRun) SetName(name string) error {
r.RunName = name
return r.syncMeta()
}
func (r *fileRun) Name() string {
return r.RunName
}
func (r *fileRun) GetParam(key string) (string, error) {
f, err := os.Open(r.paramPath(key))
if err != nil {
return "", fmt.Errorf("param with key %s not found", key)
}
defer f.Close()
valBytes, err := io.ReadAll(f)
if err != nil {
return "", err
}
return string(valBytes), nil
}