Skip to content

Loss Functions

XoRL supports several loss functions for local and server training. Server requests select one with loss_fn. Put per-token tensors such as targets, behavior log probabilities, and advantages in each datum’s loss_fn_inputs; put scalar options such as clipping thresholds in the request’s loss_fn_params.

loss_fn nameRequired datum inputsUse case
causallm_losstarget_tokens (or labels)Standard next-token prediction for SFT and continued pretraining
cross_entropySame as causallm_lossAlias for causallm_loss
importance_samplingtarget_tokens, logprobs, advantagesUnclipped importance-sampling policy gradient
policy_losstarget_tokens, logprobs, advantagesPPO-clipped policy gradient, with optional TIS and IcePop
drgrpotarget_tokens, logprobs or old_logprobs, advantages; ref_logprobs when beta > 0DR-GRPO with token- or sequence-level ratios and optional reference KL
opd_lossStudent targets plus teacher data selected by the OPD server configurationOnline policy distillation and optional hidden-state matching

Standard next-token prediction using cross-entropy. The loss is computed over all positions where labels != IGNORE_INDEX (-100). Loss is normalized by the total number of valid tokens globally across all ranks and micro-batches:

loss = CE_sum(logits, labels) / global_valid_tokens

This is the default loss for local training and the most common choice for SFT and continued pretraining.

PPO-style policy gradient loss for online RL training. The per-token logprobs datum field is the old or behavior-policy log probability. The trainer independently computes new_logprobs for the retained target tokens.

The per-token loss is:

ratio = exp(new_logprobs - old_logprobs)
loss_t = max(-ratio * advantages,
-clip(ratio, 1-eps_clip, 1+eps_clip_high) * advantages)

eps_clip and eps_clip_high default to 0.2. eps_clip_c enables dual clipping for negative advantages. use_tis applies a second, separately clipped correction using the optional per-token rollout_logprobs; icepop_beta hard-masks gradients outside its admitted ratio interval.

An unclipped importance-sampling policy-gradient loss. The implementation computes the ratio from the trainer and behavior log probabilities; callers do not provide a precomputed importance weight:

ratio = exp(new_logprobs - old_logprobs)
loss = mean(-(ratio * advantages))

This loss has no eps_clip parameter. Use policy_loss or drgrpo when policy-ratio clipping is required.

DR-GRPO applies a PPO-clipped policy loss using either token-level or sequence-level ratios. ratio_type is token by default; clip_low defaults to 0.2 and clip_high to 0.28. Setting beta > 0 adds a reference-policy KL term and requires per-token ref_logprobs; kl_type selects k1, k2, or k3.

OPD compares a student policy with teacher hidden states and a teacher LM head. It supports full-vocabulary forward or reverse KL, sampled-token estimators, optional teacher weights, hidden-state matching, and a policy-gradient mode. Teacher caching, transport, and normalization are configured by the server, so use the checked-in server configurations together with the implementation rather than treating OPD as a drop-in three-tensor loss.

The causal-LM and RL loss paths can return selected-token log probabilities and, where supported, per-token losses alongside the scalar loss. These are used for:

  • Computing KL divergence against a reference model
  • Logging token-level reward signals
  • Debugging training dynamics

Per-token output behavior depends on the selected loss and the request’s return_per_token option; it is not a separate loss function name.

The ce_mode setting controls the LM-head and cross-entropy implementation. When omitted, XoRL resolves it from the model: ordinary models and exact DSV4-Flash use compiled, while exact dense Qwen3, Qwen3.5-family, and GLM-5.2 numerical programs require bi_fused.

ValueDescriptionImportant constraints
compiledtorch.compile-compiled chunked cross-entropyGeneral production default
eagerStandard eager cross-entropyDebugging; may materialize the full logits tensor
bi_fusedBatch-invariant selected-token logprob/CE pathRequired by the current exact dense Qwen3, Qwen3.5-family, and GLM-5.2 programs; topology and dtype restrictions are checked at runtime
quack_linearQuack chunked linear plus scalar cross-entropyCausal-LM path; pipeline parallelism supports this mode on its last stage
fused_quackChunked matmul plus fused selected-token CEUsed by supported per-token loss paths; not supported by every loss/topology combination

The compiled backend computes cross-entropy in chunks along the sequence dimension, avoiding materializing the full [B × S, vocab_size] float32 tensor at once. This is particularly important for large vocabulary models (Qwen3 has vocab_size=151,936) where the naive logit tensor can be 2–4 GB per micro-batch.

Vocabulary-parallel cross-entropy (TP training)

Section titled “Vocabulary-parallel cross-entropy (TP training)”

When tensor_parallel_size > 1, the lm_head output is sharded across TP ranks: each rank holds logits for vocab_size / tp_size tokens. xorl computes cross-entropy directly on these sharded logits using a fused vocab-parallel CE kernel:

  1. Each TP rank computes the local log-sum-exp contribution from its vocab shard.
  2. An all-reduce aggregates the global log-sum-exp across TP ranks.
  3. Each rank computes the per-token CE using the correct global normalization.

This avoids an all-gather of the full logit tensor before CE, saving vocab_size × B × S × 4 bytes of cross-TP communication per forward pass.

Loss Computation Flowtokens[B, S]model fwdlm_headlogits[B,S,V]cross_entropycompiled / VP-CEscalar loss/ gvtgradient ← .backward()gvt = global valid tokens (all-reduced across all ranks)
FileDescription
src/xorl/ops/loss/__init__.pyPublic loss registry and cross-entropy mode names
src/xorl/ops/loss/Causal-LM, policy, importance-sampling, DR-GRPO, and OPD implementations
src/xorl/ops/loss/compiled_cross_entropy.pyCompiled chunked cross-entropy
src/xorl/ops/loss/vocab_parallel_cross_entropy.pyVocabulary-parallel cross-entropy for TP
src/xorl/distributed/gradient_accumulate_loss.pyGradientAccumulateLoss — token-normalized loss accumulation across micro-batches