physics.fine_tuning
physics.fine_tuning
Fine-tuning on serialized trajectories: weighting, packing, and preference pairs.
Closed forms behind supervised and preference fine-tuning of a policy on recorded trajectories (added 2026-10-20). Each takes stated counts: selected target tokens per example, stored example lengths against a fixed buffer, or log-probability sums of two scored records. None models a trained network. The solvers in engine/solvers/fine_tuning.py compose them.
Functions
| Name | Description |
|---|---|
| calc_example_average_share | Calculate a group’s share of an example-averaged loss. |
| calc_first_fit_decreasing | Pack example lengths using the first-fit-decreasing heuristic. |
| calc_preference_pair | Evaluate the DPO loss and coefficients for one preference pair. |
| calc_token_average_shares | Calculate each example’s share of a token-averaged loss. |
calc_first_fit_decreasing
physics.fine_tuning.calc_first_fit_decreasing(lengths, capacity)Pack example lengths using the first-fit-decreasing heuristic.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| lengths | iterable of int | Nonnegative example lengths in tokens, each no larger than capacity. | required |
| capacity | int | Positive token capacity of each buffer. | required |
Returns
| Name | Type | Description |
|---|---|---|
| list of int | Token load of each allocated buffer, in buffer-creation order. |
Notes
Equation: Sort lengths in descending order; place each length n in the first buffer whose current load plus n does not exceed capacity.
Assumptions: Examples cannot be split and buffer capacities are identical.
Excludes: Optimal bin packing, sequence-boundary overhead, and attention masking.
References
Derivation: this is the explicit first-fit-decreasing packing procedure, a heuristic rather than a claim of optimal allocation.
Examples
>>> calc_first_fit_decreasing([6, 4, 3, 3], 10)
[10, 6]calc_preference_pair
physics.fine_tuning.calc_preference_pair(
policy_preferred,
reference_preferred,
policy_dispreferred,
reference_dispreferred,
beta,
)Evaluate the DPO loss and coefficients for one preference pair.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| policy_preferred | float | Policy log-probability sum for the preferred record, in natural-log units. | required |
| reference_preferred | float | Reference log-probability sum for the preferred record. | required |
| policy_dispreferred | float | Policy log-probability sum for the dispreferred record. | required |
| reference_dispreferred | float | Reference log-probability sum for the dispreferred record. | required |
| beta | float | Positive dimensionless scale of the preference margin. | required |
Returns
| Name | Type | Description |
|---|---|---|
| tuple of float | Preferred score, dispreferred score, scaled margin, logistic preference score, loss, saturation factor, and beta-scaled coefficient, in order. Scores and loss use natural-log conventions; all entries are dimensionless. |
Notes
Equation: z_w = policy_preferred-reference_preferred and z_l = policy_dispreferred-reference_dispreferred. With m=beta*(z_w-z_l), preference_score=sigmoid(m), loss=log(1+exp(-m)), saturation=sigmoid(-m), and coefficient=beta*saturation.
Assumptions: Inputs are log-probability sums for the same pair under policy and reference models, with a supplied preference ordering.
Excludes: Model training, preference-label validity, and sequence-length normalization.
References
Rafailov et al. (2023), “Direct Preference Optimization: Your Language Model is Secretly a Reward Model.” https://arxiv.org/abs/2305.18290 (bibliography key: rafailov2023dpo).
Examples
>>> pair = calc_preference_pair(-2, -2, -3, -3, 1)
>>> pair[3], round(pair[4], 6), pair[6]
(0.5, 0.693147, 0.5)