Skip to content

How to Version Models and Data

Summary

In regular software development, code is versioned using Git. In machine learning, this is not enough: models also have datasets, weights, and experiment results. Here's how to keep everything organized with DVC, MLflow, and Hugging Face Hub.

Why Is This Necessary

Imagine this: you fine-tune a model, get a good result, but a month later you can't remember which dataset it was trained on or with what parameters. Or a colleague asks, "Can you reproduce your experiment?"—and you only have a file model_final_v2_really_final.pt.

Versioning solves three problems:

  • Reproducibility—always possible to return to any state;
  • Collaboration—others understand what and when changed;
  • Rollback—if a new model is worse, roll back to the previous one.

Three Layers of Versioning

flowchart TD
    A[Code and Configs] --> D[Git]
    B[Data and Datasets] --> E[DVC]
    C[Experiments and Models] --> F[MLflow / W&B / HF Hub]
  • Git—code, prompts, configs;
  • DVC—large data files that Git doesn't store;
  • MLflow / W&B—experiment metrics and artifacts;
  • Hugging Face Hub—ready-to-use models for sharing.

Step 1. Data with DVC

DVC (Data Version Control) stores heavy files separately from Git, leaving only lightweight pointers in the repository.

pip install dvc
dvc init
git commit -m "init dvc"

Add a dataset under version control:

dvc add data/dataset.csv

In Git, only the pointer file data/dataset.csv.dvc is committed, while the dataset itself is sent to storage:

dvc remote add -d storage s3://my-bucket/datasets
dvc push

Now, any change to the dataset is a new version. Returning to an old version is done with one command:

git checkout <commit>
dvc checkout

Step 2. Experiments with MLflow

MLflow records parameters, metrics, and artifacts for each run.

import mlflow

mlflow.set_experiment("fine-tuning")

with mlflow.start_run():
    mlflow.log_param("learning_rate", 1e-4)
    mlflow.log_param("epochs", 3)
    mlflow.log_metric("eval_loss", 0.42)
    mlflow.log_artifact("checkpoints/model.pt")

Next, in the dashboard, you can see all runs: parameters, metrics, which was the best. Starting the dashboard:

mlflow ui

This answers the question "what did I try and what worked"—the history of all experiments is right in front of you.

Step 3. Models with Hugging Face Hub

A ready-to-use model is conveniently stored on Hugging Face Hub: there are versions (revisions) like commits in Git.

from huggingface_hub import HfApi

api = HfApi()
api.create_repo(repo_id="my-org/my-model", repo_type="model")

api.upload_file(
    path_or_fileobj="model.pt",
    path_in_repo="model.pt",
    repo_id="my-org/my-model",
)

Each upload is a new version. Downloading a specific version is done by its revision:

huggingface-cli download my-org/my-model --revision <hash>

Full Workflow Diagram

flowchart LR
    A[Dataset] -->|dvc add| B[DVC]
    B -->|pointer in Git| C[Git]
    D[Training] -->|log_params, log_metrics| E[MLflow]
    D -->|upload_file| F[Hugging Face Hub]
    C --> G[Reproducible Experiment]
    B --> G
    E --> G

The setup is as follows: Git remembers code and data versions, DVC stores the actual data, MLflow—run history, and Hub—final models. Together, this provides full reproducibility.

Practical Rules

  1. Don't put data in Git. Files larger than a couple of megabytes—use DVC or external storage.
  2. One experiment—one run. Don't overwrite metrics, log a new run instead.
  3. Give models meaningful names (my-org/support-bot-v1), not final_v2.
  4. Store configs with the code—they are part of reproducibility.
  5. Pin the dataset version before training—otherwise, it's unclear on what it was trained.

Checklist Before 'Done'

  • code and configs in Git;
  • dataset in DVC, pointer in Git;
  • parameters and metrics in MLflow or W&B;
  • final model uploaded to Hugging Face Hub;
  • colleague can reproduce the result following the instructions.

See Also