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: SequenceEncodableProbabilityDistribution

DPO over (x, chosen, rejected) preference triples. policy is trained, ref is frozen.

Parameters:
seq_log_density(enc)[source]

Return vectorized log-density values for sequence-encoded observations.

Parameters:

enc (Any)

Return type:

ndarray

log_density(xcr)[source]

Return the log-density or log-mass at a single observation.

Parameters:

xcr (Any)

Return type:

float

prefers(x)[source]

The policy’s argmax action at x – what the aligned policy now picks.

Parameters:

x (Any)

Return type:

ndarray

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.

Return type:

dict[str, Any]

classmethod from_dict(payload)[source]

Reconstruct a distribution from to_dict output.

Parameters:

payload (dict[str, Any])

Return type:

DPOModel

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. With batched=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 independent RandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path. batched=False forces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.

Parameters:
Return type:

Any

class DPOEncoder[source]

Bases: DataSequenceEncoder

seq_encode(data)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

data (list)

Return type:

tuple[ndarray, ndarray, ndarray]

class DPOAccumulator[source]

Bases: SequenceEncodableStatisticAccumulator

update(xcr, weight, estimate)[source]
Parameters:
Return type:

None

seq_update(enc, weights, estimate)[source]
Parameters:
Return type:

None

initialize(xcr, weight, rng)[source]
Parameters:
Return type:

None

seq_initialize(enc, weights, rng)[source]
Parameters:
Return type:

None

combine(other)[source]
Parameters:

other (Any)

Return type:

DPOAccumulator

value()[source]
Return type:

tuple

from_value(v)[source]
Parameters:

v (tuple)

Return type:

DPOAccumulator

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: ParameterEstimator

DPO M-step: m_steps of gradient on the POLICY minimizing -log sigmoid(beta * margin); ref frozen.

Parameters:
accumulator_factory()[source]
Return type:

DPOAccumulatorFactory

estimate(nobs, suff_stat)[source]
Parameters:
Return type:

DPOModel

DPOLeaf

alias of DPOModel

DPOLeafEstimator

alias of DPOModelEstimator