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 vectorized log-density values for sequence-encoded observations.
- log_density(xcr)[source]
Return the log-density or log-mass at a single observation.
- prefers(x)[source]
The policy’s argmax action at
x– what the aligned policy now picks.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
DPOModelSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
DPOModelEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
DPOEncoder
- to_dict()[source]
Return a safe JSON-compatible representation of this distribution.
- class DPOModelSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (DPOModel)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class DPOEncoder[source]
Bases:
DataSequenceEncoder
- class DPOAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulator- update(xcr, weight, estimate)[source]
- seq_update(enc, weights, estimate)[source]
- initialize(xcr, weight, rng)[source]
- seq_initialize(enc, weights, rng)[source]
- acc_to_encoder()[source]
- Return type:
DPOEncoder
- class DPOAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- 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 type:
DPOAccumulatorFactory
- DPOLeaf
alias of
DPOModel
- DPOLeafEstimator
alias of
DPOModelEstimator