#experiment #tracking #client #logger #python #results #m-lflow

bin+lib mlflow_rs

A client library for experiment tracking with MLflow

5 releases

new 0.1.4 Apr 17, 2024
0.1.3 Mar 28, 2024
0.1.2 Mar 28, 2024
0.1.1 Mar 21, 2024
0.1.0 Mar 6, 2024

#137 in Machine learning

Download history 106/week @ 2024-03-02 23/week @ 2024-03-09 110/week @ 2024-03-16 204/week @ 2024-03-23 59/week @ 2024-03-30 8/week @ 2024-04-06

383 downloads per month

Apache-2.0

44KB
1K SLoC

MLflow_rs

This is a client library for experiment tracking with MLflow. Improvements over the official Python library:

  • uncommitted changes will be correctly handled to ensure reproducibility
  • logs from log compatible loggers can be stored with the experiment results
  • experiment code gets notified if the user wants to terminate the experiment which provides the opportunity to e.g. finish the current iteration / save the current state etc.
  • compile time configuration disable_experiment_tracking disables experiment tracking and removes most of the code which should result in minimal overhead when experiment tracking needs to be disabled temporarily

Usage

[dependencies]
mlflow_rs = "0.1"
use std::{error::Error, sync::{atomic::{AtomicBool, Ordering}, Arc}, thread::sleep, time::Duration};

use env_logger::Builder;
use log::{error, info};
use mlflow_rs::{experiment::Experiment, run::{Run, RunTag}};

/// Function that executes the experiment
fn experiment_function(run: &Run, was_killed: Arc<AtomicBool>) -> Result<(), Box<dyn Error>> {
    info!("info message");
    error!("error message");

    run.log_parameter("learning_rate", "0.001")?;
    run.log_metric("metric", 42.0, Some(0))?;
    run.log_artifact_bytes("test data".to_owned().into_bytes(), "test.txt")?;

    for _ in 0..10 {
        if was_killed.load(Ordering::Relaxed) {
            return Ok(())
        }

        sleep(Duration::from_secs(1));
    }

    u32::from_str_radix("a", 10).unwrap();  // panics, to show that panics get caught and handled

    Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
    let experiment = Experiment::new("http://localhost:5000", "test")?;

    let mut logger_builder = Builder::from_default_env();
    let logger = logger_builder.build();

    let mut run = experiment.create_run_with_git_diff(
        Some("new run"),
        vec![RunTag {
            key: "tag_name".to_owned(),
            value: "tag_value".to_owned(),
        }],
    )?;

    run.run_experiment_with_logger(experiment_function, logger)?;

    Ok(())
}

When you want to disable tracking temporarily:

Create the file .cargo/config.toml and add:

[build]
rustflags = ["--cfg", "disable_experiment_tracking"]

or run cargo with:

cargo rustc --lib -- --cfg disable_experiment_tracking

or set the RUSTFLAGS environment variable:

RUSTFLAGS="--cfg disable_experiment_tracking" cargo build --lib

other ways can be found here: https://doc.rust-lang.org/cargo/reference/config.html?highlight=rustflags#buildrustflags

Dependencies

~7–21MB
~310K SLoC