Skip to content

Commit fe06728

Browse files
committed
KTO: decorate MLXKTOConfig @DataClass(init=False)
Bare @DataClass regenerated __init__, bypassing MLXTrainingConfig.__init__ and its setup (e.g. _unsloth_mlx_warmup_steps_explicit). init=False inherits the parent init while fields() still registers the KTO-specific fields, which the parent __init__ reads via fields(type(self)). Mirrors unslothai#830's MLXORPOConfig/MLXDPOConfig. Regression test asserts the inherited init and intact KTO fields.
1 parent f3fc0dc commit fe06728

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

tests/test_mlx_kto_loss.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,16 @@ def trainable_parameters(self):
324324
monkeypatch.setattr(T, "_kto_model_has_non_lora_trainable_params", lambda m: True)
325325
with pytest.raises(ValueError, match="structural limit"):
326326
tr.train()
327+
328+
329+
def test_kto_config_inherits_parent_init_not_a_generated_one():
330+
# Bare @dataclass regenerates __init__ and bypasses MLXTrainingConfig.__init__
331+
# (dropping e.g. _unsloth_mlx_warmup_steps_explicit). @dataclass(init=False)
332+
# inherits the parent init while keeping the KTO-specific fields.
333+
from unsloth_zoo.mlx.trainer import MLXKTOConfig, MLXTrainingConfig
334+
assert MLXKTOConfig.__init__ is MLXTrainingConfig.__init__
335+
c = MLXKTOConfig()
336+
assert hasattr(c, "_unsloth_mlx_warmup_steps_explicit") # parent init ran
337+
assert c.beta == 0.1 and c.loss_type == "kto" # KTO fields intact
338+
c2 = MLXKTOConfig(beta=0.5, desirable_weight=2.0)
339+
assert c2.beta == 0.5 and c2.desirable_weight == 2.0

unsloth_zoo/mlx/trainer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8155,10 +8155,16 @@ def train_on_responses_only(
81558155
# ============================================================================
81568156

81578157

8158-
@dataclass
8158+
@dataclass(init=False)
81598159
class MLXKTOConfig(MLXTrainingConfig):
81608160
"""KTO configuration mirroring TRL's KTOConfig field names, on top of the
8161-
shared MLX training knobs (optimizer, schedule, clipping, save)."""
8161+
shared MLX training knobs (optimizer, schedule, clipping, save).
8162+
8163+
init=False (not a bare @dataclass) so the subclass inherits
8164+
MLXTrainingConfig.__init__ rather than a generated one that would bypass the
8165+
parent's setup (e.g. the warmup-steps-explicit initialization). Mirrors
8166+
#830's MLXORPOConfig/MLXDPOConfig. fields() still registers the KTO-specific
8167+
fields below, which the inherited __init__ reads via fields(type(self))."""
81628168

81638169
beta: float = 0.1
81648170
desirable_weight: float = 1.0

0 commit comments

Comments
 (0)