-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
282 lines (257 loc) · 8.52 KB
/
interface.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
// Package mlflow implements an MLFlow client.
//
// It supports the Tracking API, with local files and HTTP.
// The API is modeled after the official Python client, so the [official MLFlow docs] may be useful.
//
// Authentication to Databricks-hosted MLFlow is only supported via access token, not via Databricks username and password.
// Follow the [personal acess token instructions] to get one.
//
// The API is organized into a hierarchy of interfaces:
// - [Tracking]: represents an MLFlow tracking server.
// - [Experiment]: represents an MLFlow experiment.
// - [Run]: represents an MLFlow run.
//
// [official MLFlow docs]: https://mlflow.org/docs/latest/tracking.html
// [personal acess token instructions]: https://docs.databricks.com/dev-tools/api/latest/authentication.html#generate-a-personal-access-token
package mlflow
import (
"errors"
"fmt"
"log"
"net/url"
"os"
"reflect"
"strings"
"sync"
)
const (
TrackingURIEnvName = "MLFLOW_TRACKING_URI"
ExperimentIDEnvName = "MLFLOW_EXPERIMENT_ID"
RunIDEnvName = "MLFLOW_RUN_ID"
BearerTokenEnvName = "MLFLOW_TRACKING_TOKEN"
// https://www.mlflow.org/docs/latest/tracking.html#system-tags
GitCommitTagKey = "mlflow.source.git.commit"
ParentRunIDTagKey = "mlflow.parentRunId"
UserTagKey = "mlflow.user"
SourceNameTagKey = "mlflow.source.name"
SourceTypeTagKey = "mlflow.source.type"
SourceTypeJob = "JOB"
SourceTypeLocal = "LOCAL"
HostTagKey = "host"
// https://github.com/mlflow/mlflow/blob/da4fe0f1509ff5062016b2efc05e73876db118c2/mlflow/tracking/default_experiment/__init__.py#L1
defaultExperimentID = "0"
// https://github.com/mlflow/mlflow/blob/da4fe0f1509ff5062016b2efc05e73876db118c2/mlflow/entities/experiment.py#L14
defaultName = "Default"
)
var (
ErrUnsupported = errors.New("this operation not supported by this tracking client")
)
// Tracking is an interface for an MLFlow tracking server.
// It is generally used indirectly via [ActiveRunFromEnv], or you can create one with
// [NewTracking].
type Tracking interface {
ExperimentsByName() (map[string]Experiment, error)
CreateExperiment(name string) (Experiment, error)
GetOrCreateExperimentWithName(name string) (Experiment, error)
GetExperiment(id string) (Experiment, error)
URI() string
UIURL() string
// Returns (matching runs, next page token, error)
SearchRuns(experimentIDs []string, filter string, orderBy []string, pageToken string) ([]Run, string, error)
}
type Experiment interface {
CreateRun(name string) (Run, error)
GetRun(runId string) (Run, error)
ID() string
}
type Metric struct {
Key string
Val float64
}
type Param struct {
Key string
Val string
}
type Tag struct {
Key string
Val string
}
type Run interface {
SetName(name string) error
Name() string
SetTag(key, value string) error
SetTags(tags []Tag) error
GetTag(key string) (string, error)
LogArtifact(localPath, artifactPath string) error
LogMetric(key string, val float64, step int64) error
LogMetrics(metrics []Metric, step int64) error
LogParam(key, value string) error
LogParams(params []Param) error
GetParam(key string) (string, error)
End() error
Fail() error
UIURL() string
ID() string
ExperimentID() string
}
// ArtifactRepo is an interface for logging artifacts.
// It is generally used indirectly via [Run.LogArtifact].
type ArtifactRepo interface {
// localPath is the path to the file on the local filesystem.
// artifactPath is the directory in the artifact repo to upload the file to.
// Kinda weird, but this is how the python client does it.
LogArtifact(localPath, artifactPath string) error
// LogArtifacts logs all of the files in a directory tree as artifacts.
// localPath is the path to the directory on the local filesystem.
// artifactPath is the directory in the artifact repo to upload to.
LogArtifacts(localDir, artifactPath string) error
}
func NewTracking(uri, bearerToken string, l *log.Logger) (Tracking, error) {
if uri == "" {
uri = os.Getenv(TrackingURIEnvName)
}
if uri == "" {
return nil, fmt.Errorf("uri not specified and no %q found, but it's required", TrackingURIEnvName)
}
parsed, err := url.Parse(uri)
if err != nil {
return nil, err
}
if bearerToken == "" {
bearerToken = os.Getenv(BearerTokenEnvName)
}
switch parsed.Scheme {
case "file", "":
if bearerToken != "" && l != nil {
l.Println("Bearer token ignored for local file tracking URI")
}
return NewFileStore(parsed.Path)
case "http", "https":
return NewRESTStore(uri, bearerToken)
}
return nil, fmt.Errorf("support for tracking service with URI scheme %s not implemented", parsed.Scheme)
}
var activeRunMtx sync.Mutex
var activeRun Run = nil
// Returns the singleton active run. If it has not been set,
// a new run will be created in the experiment named experimentName.
// If experimentName is not set, falls back to:
// 1. The value of the [ExperimentIDEnvName] environment variable.
// 2. The experiment with ID "0".
//
// Differences from the python client:
// - Doesn't create nested runs.
// - No automatic switching to a new run if the active run finishes.
func ActiveRunFromEnv(experimentName string, l *log.Logger) (Run, error) {
return getActiveRun(experimentName, l, os.Getenv)
}
// Same as ActiveRunFromEnv, but uses the given struct as the source of config values.
// The struct must have string fields named that match the environment variable names,
// e.g. [TrackingURIEnvName].
func ActiveRunFromConfig(experimentName string, l *log.Logger, config interface{}) (Run, error) {
return getActiveRun(experimentName, l, func(key string) string {
return stringFieldFromStruct(key, config)
})
}
func getActiveRun(experimentName string, l *log.Logger, getConfig func(string) string) (Run, error) {
activeRunMtx.Lock()
defer activeRunMtx.Unlock()
if activeRun != nil && l != nil && experimentName != "" {
l.Println("Active run already exists, ignoring experiment name")
} else {
tracking, err := NewTracking("", getConfig(BearerTokenEnvName), l)
if err != nil {
return nil, err
}
var exp Experiment
expID := getConfig(ExperimentIDEnvName)
if expID != "" {
exp, err = tracking.GetExperiment(expID)
if experimentName != "" && l != nil {
l.Printf("Ignoring experiment name %q, using experiment ID %q", experimentName, expID)
}
} else if experimentName != "" {
exp, err = tracking.GetOrCreateExperimentWithName(experimentName)
} else {
exp, err = tracking.GetExperiment("")
}
if err != nil {
return nil, err
}
runID := getConfig(RunIDEnvName)
if runID != "" {
// In theory we could create the run here, but to match
// the behavior of the Python client, we just fail.
activeRun, err = exp.GetRun(runID)
if err != nil {
return nil, err
}
} else {
activeRun, err = exp.CreateRun("")
if err != nil {
return nil, err
}
host, _ := os.Hostname()
tags := []Tag{{SourceTypeTagKey, SourceTypeLocal}, {HostTagKey, host}}
// Note: UserTagKey may noly be set during CreateRun, hence not set here.
if err = activeRun.SetTags(tags); err != nil {
return nil, err
}
}
if l != nil {
uri := tracking.URI()
if strings.HasPrefix(uri, "file:") || !strings.Contains(uri, ":") {
l.Println("MLFlow logging to local files only. To view, run: mlflow ui --backend-store-uri", uri, "--port 0")
} else {
l.Println("To view MLFlow, open", activeRun.UIURL())
}
}
}
return activeRun, nil
}
func endIfActive(run Run) {
activeRunMtx.Lock()
if activeRun == run {
activeRun = nil
}
activeRunMtx.Unlock()
}
func stringFieldFromStruct(key string, config interface{}) string {
val := reflect.ValueOf(config)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return ""
}
field := val.FieldByName(key)
if field.Kind() != reflect.String {
return ""
}
return field.String()
}
// LogStructAsParams logs the fields of the given obj as params.
func LogStructAsParams(run Run, obj interface{}) error {
objVal := reflect.ValueOf(obj)
if objVal.Kind() == reflect.Ptr {
objVal = objVal.Elem()
}
if objVal.Kind() != reflect.Struct {
return fmt.Errorf("LogStructAsParams expected struct, got %v", objVal.Kind())
}
params := make([]Param, 0)
for _, field := range reflect.VisibleFields(objVal.Type()) {
fieldName := field.Name
value := objVal.FieldByName(fieldName)
if value.Kind() == reflect.Slice {
for i := 0; i < value.Len(); i++ {
idx := i
params = append(params, Param{
Key: fmt.Sprintf("%s_%d", fieldName, idx), Val: fmt.Sprintf("%v", value.Index(idx))})
}
} else {
params = append(params, Param{Key: fieldName, Val: fmt.Sprintf("%v", value)})
}
}
return run.LogParams(params)
}