Last active
April 12, 2019 15:10
-
-
Save vene/5a49670f97bb2df55177579acbda2b8f to your computer and use it in GitHub Desktop.
mlflow and crayon from cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/* MLFlow interaction */ | |
#include <string> | |
#include <chrono> | |
#include <cpr/cpr.h> | |
#include <nlohmann/json.hpp> | |
//#include<iostream> | |
using nlohmann::json; | |
struct Crayon | |
{ | |
std::string hostname; | |
std::string port; | |
std::string run_uuid; | |
Crayon( | |
const std::string& run_uuid, | |
const std::string& hostname = std::string("localhost"), | |
const std::string& port = std::string("8889")) | |
: hostname(hostname) | |
, port(port) | |
, run_uuid(run_uuid) | |
{ | |
//std::cerr << "run_uuid" << run_uuid << "\n"; | |
std::stringstream url; | |
url << "http://" << hostname << ":" << port << "/data"; | |
json payload = run_uuid; | |
auto r = cpr::Post(cpr::Url(url.str()), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
//std::cerr << r.text << "\n"; | |
} | |
cpr::Url get_url(const std::string& name) | |
{ | |
std::stringstream url; | |
url << "http://" << hostname << ":" << port | |
<< "/data/scalars?xp=" << run_uuid << "&name=" << name; | |
auto cpurl = cpr::Url(url.str()); | |
return cpurl; | |
} | |
void log_metric(const std::string& key, const double& val, const unsigned& step) | |
{ | |
auto payload = json{seconds_since_epoch(), step, val}; | |
auto r = cpr::Post(get_url(key), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
//std::cerr << r.text << "\n"; | |
} | |
private: | |
double seconds_since_epoch() | |
{ | |
auto t = std::chrono::system_clock::now().time_since_epoch(); | |
auto tms = std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | |
return tms / 1000; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/* MLFlow interaction */ | |
#include <string> | |
#include <chrono> | |
#include <cpr/cpr.h> | |
#include <nlohmann/json.hpp> | |
#define GIT_COMMIT "0" | |
#include "git_commit.h" | |
using nlohmann::json; | |
struct MLFlowRun | |
{ | |
std::string hostname; | |
std::string port; | |
unsigned exp_id; | |
std::string run_uuid; | |
const std::string user_id = "vlad"; | |
MLFlowRun( | |
unsigned exp_id, | |
const std::string& hostname = std::string("localhost"), | |
const std::string& port = std::string("5000")) | |
: hostname(hostname) | |
, port(port) | |
, exp_id(exp_id) | |
{ | |
auto payload = json{ | |
{"user_id", user_id}, | |
{"start_time", milliseconds_since_epoch()}, | |
{"experiment_id", exp_id}, | |
{"source_type", "PROJECT"}, | |
{"source_version", GIT_COMMIT} | |
}; | |
auto r = cpr::Post(get_url("runs/create"), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
auto jr = json::parse(r.text); | |
run_uuid = jr["run"]["info"]["run_uuid"]; | |
//set_tag("mlflow.source.type", "PROJECT"); | |
//set_tag("mlflow.source.git.commit", GIT_COMMIT); | |
}; | |
cpr::Url get_url(const std::string& path) | |
{ | |
std::stringstream url; | |
url << "http://" << hostname << ":" << port | |
<< "/api/2.0/preview/mlflow/" << path; | |
auto cpurl = cpr::Url(url.str()); | |
return cpurl; | |
} | |
void set_tag(const std::string& key, const std::string& val) | |
{ | |
auto payload = json{ | |
{"run_uuid", run_uuid}, | |
{"key", key}, | |
{"value", val}, | |
}; | |
auto r = cpr::Post(get_url("runs/set-tag"), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
} | |
void log_parameter(const std::string& key, const std::string& val) | |
{ | |
auto payload = json{ | |
{"run_uuid", run_uuid}, | |
{"key", key}, | |
{"value", val}, | |
}; | |
auto r = cpr::Post(get_url("runs/log-parameter"), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
} | |
void log_metric(const std::string& key, const double& val) | |
{ | |
auto payload = json{ | |
{"run_uuid", run_uuid}, | |
{"timestamp", milliseconds_since_epoch()}, | |
{"key", key}, | |
{"value", val}, | |
}; | |
auto r = cpr::Post(get_url("runs/log-metric"), | |
cpr::Header{{"Content-Type", "application/json"}}, | |
cpr::Body{payload.dump()}); | |
} | |
private: | |
long milliseconds_since_epoch() | |
{ | |
auto t = std::chrono::system_clock::now().time_since_epoch(); | |
return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment