loss_fn argument on forward_backward(). Each loss reads a different set of per-position fields from Datum.loss_fn_inputs, and some take a loss_fn_config dict to tune hyperparameters.
cross_entropy
Standard supervised fine-tuning. Each Datum carries per-token targets under target_tokens, with -100 marking positions the loss should ignore (typically the prompt). forward_backward does not shift labels internally, so shift when you tokenize: the label at position i is the token at position i+1.
cross_entropy takes no loss_fn_config. See the quickstart for the full tokenization step.
importance_sampling, ppo, dppo, cispo, dro
On-policy reinforcement learning. Each Datum’s loss_fn_inputs must include per-position logprobs (from the behavior policy at rollout time) and advantages (from your reward model or return-to-go).
loss_fn_inputs shape and differ in clipping and regularization: importance_sampling doesn’t clip the ratio; ppo clips it; dppo, cispo, and dro add further variants. All are typically driven by a rollout loop that samples from a paired SamplingClient and scores each trajectory before calling forward_backward.
The trainer accepts
dppo and dpo, but they aren’t in the SDK’s LossFnType literal yet, so type-checked code needs # type: ignore[arg-type] on those calls.dpo
Direct Preference Optimization. Trains on chosen/rejected preference pairs against a frozen reference policy. There are three additional pieces compared to SFT:
- Snapshot the reference before any
optim_step. The initial weights are the reference for offline DPO. - Interleave the batch as
[chosen_0, rejected_0, chosen_1, rejected_1, ...]. Pairing is by position, and the SDK rejects odd-length batches before submitting anything. - Score reference logprobs once with
attach_reference_logprobsand reuse across every step.
Datum needs three loss_fn_inputs fields. You build the first two yourself when tokenizing; the third comes from attach_reference_logprobs:
target_tokens: labels shifted by one,-100over the prompt positions.weights:0.0over the prompt,1.0over the response. The DPO log-ratio sums logprobs weighted by this mask.ref_logprobs: frozen reference logprobs, one per position. Populated automatically byattach_reference_logprobs.
loss_fn_config keys:
beta(float, default0.1): KL-penalty strength against the reference.label_smoothing(float, default0.0, range[0, 0.5]): enables conservative DPO (cDPO).
fb.metrics: the DPO pass reports loss, the mean DPO loss over the batch. A fresh policy starts near ln 2 (about 0.693) and decreases as the policy learns to prefer chosen responses.
Next steps
TrainingClientreference for the fullforward_backward()signature.- Helpers reference for
attach_reference_logprobs. - Loops concepts explains the paired trainer + sampler model that makes reference-policy snapshots possible.