mixle.models.dpo_leaf module¶
Direct Preference Optimization (DPO) as a mixle leaf – alignment as a supervised preference likelihood.
Observation = (x, chosen, rejected): a context and a preferred vs dispreferred action/completion. The leaf
carries a POLICY module and a FROZEN REFERENCE module; seq_log_density returns the DPO log-sigmoid reward
log sigma( beta * [ (log pi(chosen|x) - log pi_ref(chosen|x)) - (log pi(rejected|x) - log pi_ref(rejected|x)) ] )
(higher = the policy prefers chosen over rejected, relative to the reference). The M-step gradient-steps the policy; the reference stays frozen. No reward model, no RL – the alignment stage of the LLM pipeline as a likelihood, on the same substrate as pretrain/CPT/SFT.
This is the genuinely-new paired leaf the design flagged: it couples two forward passes plus a frozen
reference, so it does not reduce to a single Categorical (the log_density contract is over a pair, not
a single token). It composes through the same estimate() driver; the M-step owns the policy optimizer.
- class DPOModel(policy, ref, beta=0.1, m_steps=100, lr=1e-3, device='cpu')[source]
Bases:
SequenceEncodableProbabilityDistributionDPO over
(x, chosen, rejected)preference triples.policyis trained,refis frozen.- seq_log_density(enc)[source]
Return per-row DPO preference log likelihoods for encoded triples.
- log_density(xcr)[source]
Return the DPO log likelihood for one
(x, chosen, rejected)triple.
- prefers(x)[source]
The policy’s argmax action at
x– what the aligned policy now picks.
- sampler(seed=None)[source]
Return the sampler for the preference-scoring leaf.
- Parameters:
seed (int | None)
- Return type:
DPOModelSampler
- estimator(pseudo_count=None)[source]
Return the DPO estimator that trains the policy while keeping the reference fixed.
- Parameters:
pseudo_count (float | None)
- Return type:
DPOModelEstimator
- dist_to_encoder()[source]
Return the encoder for preference triples.
- Return type:
DPOEncoder
- to_dict()[source]
Serialize policy/reference modules and DPO hyperparameters.
- class DPOModelSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler facade for DPO leaves, which score preference pairs rather than generating.
- Parameters:
dist (DPOModel)
seed (int | None)
- class DPOEncoder[source]
Bases:
DataSequenceEncoderEncode
(context, chosen, rejected)preference triples for DPO.
- class DPOAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulatorBuffer weighted preference triples for the DPO M-step.
- update(xcr, weight, estimate)[source]
Add one weighted preference triple to the accumulator.
- seq_update(enc, weights, estimate)[source]
Add an encoded batch of preference triples and optional weights.
- initialize(xcr, weight, rng)[source]
Initialize from one preference triple using the ordinary update path.
- seq_initialize(enc, weights, rng)[source]
Initialize from an encoded batch using the ordinary batch update path.
- combine(other)[source]
Merge the value tuple from another DPO accumulator.
- Parameters:
other (Any)
- Return type:
DPOAccumulator
- value()[source]
Return buffered contexts, chosen actions, rejected actions, and weights.
- Return type:
- from_value(v)[source]
Restore accumulator buffers from a value tuple.
- Parameters:
v (tuple)
- Return type:
DPOAccumulator
- acc_to_encoder()[source]
Return the encoder expected by this accumulator.
- Return type:
DPOEncoder
- class DPOAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactoryFactory for DPO accumulators.
- make()[source]
Create a fresh accumulator.
- Return type:
DPOAccumulator
- class DPOModelEstimator(policy, ref, beta, m_steps, lr, device)[source]
Bases:
ParameterEstimatorDPO M-step:
m_stepsof gradient on the POLICY minimizing-log sigmoid(beta * margin); ref frozen.- accumulator_factory()[source]
Return an accumulator factory for weighted preference triples.
- Return type:
DPOAccumulatorFactory
- DPOLeaf
alias of
DPOModel
- DPOLeafEstimator
alias of
DPOModelEstimator