Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.1 KB
Raw Blame
"""GRPO logit-scaling helpers must read config through DDP wrappers."""

from __future__ import annotations

import os

REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
SOURCE_PATH = os.path.join(REPO_ROOT, "unsloth", "models", "rl_replacements.py")


def _read_source() -> str:
    with open(SOURCE_PATH, "r", encoding = "utf-8") as fh:
        return fh.read()


def test_grpo_logit_scaling_uses_model_config_helper():
    src = _read_source()
    assert "def _unsloth_get_model_config(model):" in src
    assert 'getattr(model.module, "config", None)' in src
    assert "logit_softcapping = _unsloth_get_final_logit_softcapping(model)" in src
    assert "if config is None:" in src.split("def _unsloth_get_final_logit_softcapping")[1]
    # Whitespace-insensitive: the formatter wraps one of the two calls and not the other.
    flat = "".join(src.split())
    assert (
        flat.count("=_unsloth_resolve_logit_scales(model_config)") == 3
    ), "every fallback arm (two GRPO, one GKD) must resolve the scales from the unwrapped config"
    assert "_unsloth_resolve_logit_scales(model)" not in flat
    assert src.count("model_config = _unsloth_get_model_config(model)") >= 2
    assert "inspect.getsource(_unsloth_get_model_config)" in src
    assert "inspect.getsource(_unsloth_resolve_logit_scales)" in src
    assert "model.config" not in src


def test_detect_logit_transforms_reads_the_unwrapped_config():
    """Pass model_config, not the model: DDP wrappers do not forward .config, so transforms read as
    zeros."""
    src = _read_source()
    assert "detect_logit_transforms(model)" not in src
    assert src.count("detect_logit_transforms(model_config)") >= 2


def test_detect_logit_transforms_zeroes_out_on_a_wrapped_model():
    """Checks the resolved config yields the transforms, not what the helper returns for a bare wrapper."""
    torch = __import__("importlib").import_module("torch")
    transformers = __import__("importlib").import_module("transformers")
    planner = __import__("importlib").import_module("unsloth_zoo.device_map_planner")
    detect = getattr(planner, "detect_logit_transforms", None)
    if detect is None:
        return

    class _Inner(torch.nn.Module):
        def __init__(self):
            super().__init__()
            self.config = transformers.Gemma2Config()

    class _Wrapper(torch.nn.Module):
        """Same shape as DistributedDataParallel: real model under .module."""

        def __init__(self, module):
            super().__init__()
            self.module = module

    inner = _Inner()
    wrapped = _Wrapper(inner)
    assert not hasattr(wrapped, "config")
    config = _unsloth_get_model_config_reference(wrapped)
    assert config is inner.config
    assert detect(config)["logit_softcapping"] == inner.config.final_logit_softcapping


def _unsloth_get_model_config_reference(model):
    """Mirror of rl_replacements._unsloth_get_model_config, kept local to avoid
    importing the heavyweight module."""
    config = getattr(model, "config", None)
    if config is None and hasattr(model, "module"):
        config = getattr(model.module, "config", None)
    return config