Skip to main content
The Loops SDK ships helper functions alongside the core clients, grouped by what they operate on. baseten.loops.helpers itself exports nothing; import each helper from its submodule, except loops_log_kl_sample_train, which is exported from the top-level package:
list[Datum]
Return a copy of data with each Datum carrying a new ref_logprobs field in loss_fn_inputs, scored by reference.
  • data (list[Datum]): the preference batch, with each datum holding the full prompt + response sequence in model_input. For DPO batches, these are interleaved chosen/rejected pairs.
  • reference (SamplingClient): a version-pinned snapshot of the reference policy, typically returned by TrainingClient.save_weights_and_get_sampling_client before any optimizer steps.
Each datum’s sequence is scored once via a single compute_logprobs call. Scoring is sequential.
dict[str, Any]
Compute sampler-vs-trainer logprob divergence diagnostics for an RL step and log them to Weights & Biases. Over the action tokens (mask > 0), it compares the sampler logprobs saved on each Datum against the trainer logprobs from forward_backward, and logs KL estimators (kl_sample_train_v1_loops, kl_sample_train_v2_loops, kl_sample_train_k3), importance-ratio and effective-sample-size stats, a per-token-diff histogram, and a worst-divergent-tokens table under optim/train-sample-div/.
  • wandb_run: a W&B run (anything with .log(metrics, step=...)), or None to skip logging; the metrics dict is returned either way.
  • data (list[Datum]): the batch passed to forward_backward, with sampler logprobs attached.
  • Pass either forward_backward_output (a ForwardBackwardOutput) or training_logprobs; passing neither raises ValueError.
For the scalar metrics without W&B objects, call loops_compute_kl_sample_train(data, training_logprobs) from baseten.loops.rl_metrics.