Loading...
Searching...
No Matches
MlflowFileStore.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#ifndef GPR_AUXILIARY_MLFLOW_FILE_STORE_H_
5#define GPR_AUXILIARY_MLFLOW_FILE_STORE_H_
6
7#include <Eigen/Dense>
8#include <filesystem>
9#include <fstream>
10#include <map>
11#include <memory>
12#include <sstream>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace gpr {
18namespace track {
19
22public:
23 JsonRecord& add(std::string_view key, double value);
24 JsonRecord& add(std::string_view key, long value);
25 JsonRecord& add(std::string_view key, int value)
26 {
27 return add(key, static_cast<long>(value));
28 }
29 JsonRecord& add(std::string_view key, bool value);
30 JsonRecord& add(std::string_view key, std::string_view value);
31 JsonRecord& add(std::string_view key, const char* value)
32 {
33 return add(key, std::string_view(value));
34 }
35 JsonRecord& add(std::string_view key, const Eigen::VectorXd& value);
36 JsonRecord& add(std::string_view key, const Eigen::MatrixXd& value);
38 JsonRecord& addRaw(std::string_view key, std::string_view json);
39 [[nodiscard]] std::string str() const;
40
41private:
42 void separator(std::string_view key);
43 std::ostringstream out_;
44 bool first_ = true;
45};
46
52public:
53 struct Response {
54 int status = 0;
55 std::string body;
56 };
57
58 MlflowRestClient(std::string_view tracking_uri, double timeout_seconds);
59 [[nodiscard]] Response post(const std::string& path,
60 const std::string& json) const;
61 [[nodiscard]] Response put(const std::string& path, const std::string& body,
62 const std::string& content_type) const;
63 [[nodiscard]] Response get(const std::string& path) const;
64 [[nodiscard]] const std::string& host() const noexcept
65 {
66 return host_;
67 }
68 [[nodiscard]] int port() const noexcept
69 {
70 return port_;
71 }
72 [[nodiscard]] const std::string& prefix() const noexcept
73 {
74 return prefix_;
75 }
76
77private:
78 Response request(const std::string& method, const std::string& path,
79 const std::string& body,
80 const std::string& content_type) const;
81 std::string host_;
82 int port_ = 80;
83 std::string prefix_;
84 double timeout_seconds_ = 0.0;
85};
86
97class MlflowRun {
98public:
99 struct Options {
100 std::filesystem::path store;
101 std::string tracking_uri;
102 std::string experiment;
103 std::string run_name;
104 std::map<std::string, std::string> tags;
105 double timeout_seconds = 10.0;
106 };
107 enum class Status { Running = 1, Finished = 3, Failed = 4, Killed = 5 };
108
112 static std::unique_ptr<MlflowRun> start(const Options& options);
113 ~MlflowRun();
114 MlflowRun(const MlflowRun&) = delete;
115 MlflowRun& operator=(const MlflowRun&) = delete;
116
117 void logParam(std::string_view key, std::string_view value);
118 void logParam(std::string_view key, double value);
119 void logParam(std::string_view key, long value);
120 void logMetric(std::string_view key, double value, long step = 0);
121 void setTag(std::string_view key, std::string_view value);
123 void logArtifact(const std::filesystem::path& file,
124 const std::string& name = "");
126 void appendRecord(std::string_view stream, const JsonRecord& record);
128 void flush();
129 void finish(Status status);
130
131 [[nodiscard]] const std::string& runId() const noexcept
132 {
133 return run_id_;
134 }
135 [[nodiscard]] const std::string& experimentId() const noexcept
136 {
137 return experiment_id_;
138 }
139 [[nodiscard]] const std::filesystem::path& runDirectory() const noexcept
140 {
141 return run_dir_;
142 }
143 [[nodiscard]] bool streaming() const noexcept
144 {
145 return rest_ != nullptr;
146 }
147
149 [[nodiscard]] std::string identityJson() const;
150
151private:
152 struct PendingMetric {
153 std::string key;
154 double value;
155 long timestamp;
156 long step;
157 };
158 MlflowRun() = default;
159 void writeMeta(Status status, long end_time_ms) const;
160 void connectRemote(const Options& options);
161 void disableStreaming(const std::string& why);
162 void uploadRecordStreams();
163 void appendLocal(const std::filesystem::path& file,
164 const std::string& text);
165 std::filesystem::path store_;
166 std::filesystem::path run_dir_;
167 std::string experiment_id_;
168 std::string remote_experiment_id_;
169 std::string run_id_;
170 std::string run_name_;
171 std::string tracking_uri_;
172 std::string remote_artifact_uri_;
173 long start_time_ms_ = 0;
174 bool finished_ = false;
175 std::unique_ptr<MlflowRestClient> rest_;
176 std::vector<PendingMetric> pending_metrics_;
177 std::vector<std::pair<std::string, std::string>> pending_params_;
178 std::vector<std::pair<std::string, std::string>> pending_tags_;
179 std::vector<std::string> record_streams_;
180 // One writer owns each append stream for the lifetime of the run.
181 std::map<std::filesystem::path, std::ofstream> append_streams_;
182};
183
186void setActiveRun(MlflowRun* run) noexcept;
187[[nodiscard]] MlflowRun* activeRun() noexcept;
188
190[[nodiscard]] std::string sanitizeKey(std::string_view key);
191
194[[nodiscard]] std::string jsonStringField(std::string_view json,
195 std::string_view key);
196
197} // namespace track
198} // namespace gpr
199
200#endif // GPR_AUXILIARY_MLFLOW_FILE_STORE_H_
A JSON object assembled field by field; one line of a record stream.
JsonRecord & add(std::string_view key, double value)
JsonRecord & addRaw(std::string_view key, std::string_view json)
A nested object, already rendered.
JsonRecord & add(std::string_view key, const char *value)
std::string str() const
JsonRecord & add(std::string_view key, int value)
int port() const noexcept
Response post(const std::string &path, const std::string &json) const
MlflowRestClient(std::string_view tracking_uri, double timeout_seconds)
Throws std::invalid_argument on a URI that is not plain http.
Response put(const std::string &path, const std::string &body, const std::string &content_type) const
Response get(const std::string &path) const
const std::string & host() const noexcept
const std::string & prefix() const noexcept
void setTag(std::string_view key, std::string_view value)
void logMetric(std::string_view key, double value, long step=0)
void logArtifact(const std::filesystem::path &file, const std::string &name="")
Copy a file into artifacts/ (optionally under a relative name).
const std::filesystem::path & runDirectory() const noexcept
void logParam(std::string_view key, std::string_view value)
void finish(Status status)
void appendRecord(std::string_view stream, const JsonRecord &record)
Append one JSON line to artifacts/STREAM_NAME.jsonl.
const std::string & runId() const noexcept
std::string identityJson() const
const std::string & experimentId() const noexcept
MlflowRun(const MlflowRun &)=delete
bool streaming() const noexcept
MlflowRun & operator=(const MlflowRun &)=delete
static std::unique_ptr< MlflowRun > start(const Options &options)
void flush()
Send buffered metrics, params and tags to the tracking server.
int status
MlflowRun * activeRun() noexcept
void setActiveRun(MlflowRun *run) noexcept
std::string sanitizeKey(std::string_view key)
MLflow key alphabet: alphanumerics, underscore, dash, dot, space, slash.
std::string jsonStringField(std::string_view json, std::string_view key)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
STL namespace.
double timeout_seconds
per REST request
std::string tracking_uri
http://host:port of mlflow server
std::filesystem::path store
local file store (mlruns)
std::map< std::string, std::string > tags