Residual Joint Model
Use this skill when implementing a residual energy-based text model where a fixed locally normalized language model proposes whole continuations and a sequence-level energy model corrects their probabilities.
Do not use this skill to train the base language model, normalize token-level vocabulary distributions, or read the original paper repository. It only handles sequence-level residual scoring and candidate-set normalization.
Inputs
prefix: context shared by all candidate continuations.candidates: records withid,text, optionallm_logprob, and requiredenergy.- Optional score mode:
joint: rank bylm_logprob - energy.energy: rank by-energywhen proposal log probabilities are unavailable.
Outputs
- Per-candidate
joint_logscorewhenlm_logprobis present. - Per-candidate unnormalized and normalized importance weights proportional to
exp(-energy). - Stable ranking and selected candidate id.
Workflow
- Keep base-LM log probabilities immutable. They represent the fixed proposal distribution from the paper.
- Compute residual joint log scores as
lm_logprob - energy. - Compute normalized importance weights with a log-sum-exp transformation over
-energy. - Select by joint score when proposal log probabilities are available; otherwise select by normalized energy weight.
- Record whether a result used full joint scoring or energy-only reweighting.
Validation
Run:
python scripts/residual_joint.py --demo
python -m pytest tests
The tests verify exact score arithmetic, stable normalization, and deterministic selection.
Limitations
Candidate-set normalization is not the full partition function over all text. It is valid as proposal-sample reweighting evidence or as a reduced proxy, not as a full perplexity claim unless the proposal sample size and estimator protocol match the paper target.