Skip to content

Your first training run

In this tutorial you will connect a Jax or Torch training job to DVBench to attest what datasets you used during training.

1. Install DVBench

pip install "dvbench[torch]"

2. Connect a Client

Begin by connecting to DVBench.

from dvbench import Client  # import before Jax/Torch
client = Client("https://api.prod.dvbench.org/api/v1")

This connects to the public DVBench production API.

3. Wire up a Neural Network

We can't help you with this part, but we trust you've got it :)

import torch
from torch import nn
from torch.nn import functional as F

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = nn.Linear(10, 2).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)

3. Configure your proof tape

This is where you configure the proof tape, and you'll have to make some decisions.

  • name: run names must be kebab-case, with a dash like/this-example.
  • rolling_window: every rolling_window number of tokens is considered a sample.
# register an experiment
experiment = client.experiment("your-name/your-run")

# initialize your zkdv tape
proof = experiment.proof(backend="torch")
proof.start(rolling_window=32)

# prepare your Torch models for tracing
model, optimizer = proof.prepare(model, optimizer)
Tap for more on rolling_window

A rolling_window of 4 means that every 4 tokens is considered a "sample." For instance, the quick brown fox and brown fox the quick are considered two distinct samples when rolling_window=4, but considered a shuffling of the same sample when rolling_window=2. A longer window results in a shorter tape, but offers less flexibility in shuffling.

3. Declare the replayable update

We need a deterministic description of how you got from one parameter to the next.

@proof.attest
def attested_step(model, optimizer, batch):  # you must keep this signature
    logits, loss = proof.forward(model, **batch)  # instead of model(**batch)
    proof.backward(loss)  # instead of loss.backward()
    optimizer.step()
    optimizer.zero_grad()

This function has to be deterministically serializable. Common culprits that prevent this include custom kernels. Rest assured, we don't call this function that many times (usually <1% of training), so you can definitely wire up a "slow-path" here without loosing MFUs.

4. Annotate your training

Your training loop remains largely the same, and can contain any custom logic:

for _ in range(...):
    batch = ...

    with proof.transaction(batch=batch):
        logits, loss = model(**batch)
        proof.backward(loss)  # instead of loss.backward()
        optimizer.step()
        optimizer.zero_grad()

5. Finish the proof

Call finish() before evaluation.

proof.finish()
client.close()

save_your(model.state_dict())  # do so after proof is finished

A complete working example is in examples/jax/proof_jax.py and examples/torch/proof.py.

Next: evaluate the checkpoint.