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.

24.9 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
"""text_only = True must not send Qwen3-Omni to AutoModelForCausalLM, which rejects its config."""

import pytest
import torch
from real_accelerator import has_real_cuda  # tests/_shared, on sys.path via tests/conftest.py

transformers = pytest.importorskip("transformers")
omni_config = pytest.importorskip(
    "transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe",
    reason = "this transformers has no qwen3_omni_moe",
)
omni_modeling = pytest.importorskip("transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe")

VOCAB = 64
DEVICE = "cuda" if has_real_cuda() else "cpu"


@pytest.fixture(autouse = True)
def _stock_causal_lm_loss_on_cpu():
    """On CPU use transformers' loss and restore LOSS_MAPPING, since Unsloth's Triton loss needs a GPU."""
    if DEVICE != "cpu":
        yield
        return
    loss_utils = pytest.importorskip("transformers.loss.loss_utils")
    saved = dict(loss_utils.LOSS_MAPPING)
    for key, fn in saved.items():
        if getattr(fn, "__name__", "") != "ForCausalLMLoss" and "Unsloth" in repr(fn):
            loss_utils.LOSS_MAPPING[key] = loss_utils.ForCausalLMLoss
    try:
        yield
    finally:
        loss_utils.LOSS_MAPPING.clear()
        loss_utils.LOSS_MAPPING.update(saved)


def _tiny_config():
    text = dict(
        vocab_size = VOCAB,
        hidden_size = 16,
        intermediate_size = 32,
        moe_intermediate_size = 8,
        shared_expert_intermediate_size = 8,
        num_hidden_layers = 1,
        num_attention_heads = 2,
        num_key_value_heads = 1,
        head_dim = 8,
        num_experts = 2,
        num_experts_per_tok = 1,
        max_position_embeddings = 64,
        rope_scaling = {"rope_type": "default", "mrope_section": [1, 1, 2], "interleaved": True},
    )
    config = omni_config.Qwen3OmniMoeConfig(
        enable_audio_output = True,
        thinker_config = dict(
            text_config = text,
            # Special ids above anything the tests feed in, so a text prompt stays text.
            audio_start_token_id = 56,
            audio_end_token_id = 57,
            audio_token_id = 58,
            image_token_id = 59,
            video_token_id = 60,
            vision_start_token_id = 61,
            vision_end_token_id = 62,
            audio_config = dict(
                num_mel_bins = 8,
                encoder_layers = 1,
                encoder_attention_heads = 2,
                encoder_ffn_dim = 16,
                d_model = 16,
                output_dim = 16,
                n_window = 2,
                n_window_infer = 4,
                downsample_hidden_size = 8,
                conv_chunksize = 10,
                max_source_positions = 16,
            ),
            vision_config = dict(
                depth = 1,
                hidden_size = 16,
                intermediate_size = 16,
                num_heads = 2,
                out_hidden_size = 16,
                patch_size = 2,
                spatial_merge_size = 1,
                temporal_patch_size = 1,
                deepstack_visual_indexes = [0],
            ),
        ),
        talker_config = dict(
            text_config = text,
            code_predictor_config = dict(
                vocab_size = 16,
                hidden_size = 16,
                intermediate_size = 16,
                num_hidden_layers = 1,
                num_attention_heads = 2,
                num_key_value_heads = 1,
                head_dim = 8,
                num_code_groups = 2,
            ),
            thinker_hidden_size = 16,
            num_code_groups = 2,
            spatial_merge_size = 1,
        ),
        code2wav_config = dict(
            hidden_size = 16,
            num_hidden_layers = 1,
            num_attention_heads = 2,
            num_key_value_heads = 1,
            intermediate_size = 16,
            codebook_size = 16,
            decoder_dim = 16,
            num_quantizers = 2,
            upsample_rates = (2,),
            upsampling_ratios = (2,),
            head_dim = 8,
            sliding_window = 8,
        ),
    )
    config.architectures = ["Qwen3OmniMoeForConditionalGeneration"]
    # transformers 5.4's config lacks the field its _init_weights reads.
    if getattr(config, "initializer_range", None) is None:
        config.initializer_range = 0.02
    return config


def _tiny_omni():
    torch.manual_seed(0)
    try:
        return omni_modeling.Qwen3OmniMoeForConditionalGeneration(_tiny_config()).to(DEVICE).eval()
    except Exception as error:
        pytest.skip(f"cannot build a tiny Qwen3-Omni here: {error}")


def _capture_fast_base_kwargs(monkeypatch, tmp_path, text_only):
    from unsloth import FastModel
    from unsloth.models.vision import FastBaseModel

    _tiny_config().save_pretrained(tmp_path)
    seen = {}

    def capture(*args, **kwargs):
        seen.update(kwargs)
        raise RuntimeError("captured")

    monkeypatch.setattr(FastBaseModel, "from_pretrained", staticmethod(capture))
    try:
        FastModel.from_pretrained(str(tmp_path), text_only = text_only, load_in_4bit = False)
    except RuntimeError as error:
        if str(error) != "captured":
            if not has_real_cuda():
                pytest.skip(f"FastModel.from_pretrained needs a GPU here: {error}")
            raise
    else:
        pytest.fail("FastModel.from_pretrained did not reach FastBaseModel.from_pretrained")
    return seen


def test_text_only_loads_qwen3_omni_through_its_own_auto_class(monkeypatch, tmp_path):
    from transformers import AutoModelForCausalLM
    from unsloth.models._utils import resolve_model_class

    seen = _capture_fast_base_kwargs(monkeypatch, tmp_path, text_only = True)
    auto_model = seen["auto_model"]
    assert auto_model is not AutoModelForCausalLM
    assert resolve_model_class(auto_model, _tiny_config()) is not None
    assert seen["text_only"] is False
    assert seen["text_intent"] is True
    assert seen["text_only_decoder"] is False


def test_the_default_load_resolves_the_same_class(monkeypatch, tmp_path):
    default = _capture_fast_base_kwargs(monkeypatch, tmp_path, text_only = False)
    text = _capture_fast_base_kwargs(monkeypatch, tmp_path, text_only = True)
    assert default["auto_model"] is text["auto_model"]
    assert default["text_intent"] is False


def test_text_intent_hands_qwen3_omni_to_its_thinker(monkeypatch):
    monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", "1")
    from unsloth.models.vision import _text_trainable_core

    model = _tiny_omni()
    core = _text_trainable_core(model, text_intent = True)
    assert type(core).__name__ == "Qwen3OmniMoeThinkerForConditionalGeneration"
    assert core.config.architectures == ["Qwen3OmniMoeThinkerForConditionalGeneration"]
    ids = torch.tensor([[1, 2, 3, 4]], device = DEVICE)
    out = core(input_ids = ids, labels = ids)
    assert out.logits.shape == (1, 4, VOCAB)
    assert torch.isfinite(out.loss)
    generated = core.generate(input_ids = ids, max_new_tokens = 2, do_sample = False)
    assert generated.shape == (1, 6)


def test_a_kept_qwen3_omni_wrapper_forwards_through_its_thinker(capsys, monkeypatch):
    monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", "1")
    from unsloth.models.vision import _text_trainable_core

    model = _tiny_omni()
    kept = _text_trainable_core(model, text_intent = False)
    assert kept is model
    assert hasattr(model, "talker") and hasattr(model, "code2wav")
    assert "text_only = True" in capsys.readouterr().out
    ids = torch.tensor([[1, 2, 3, 4]], device = DEVICE)
    with torch.no_grad():
        out = model(input_ids = ids, labels = ids)
        reference = model.thinker(input_ids = ids, labels = ids)
    assert torch.equal(out.logits, reference.logits)
    assert torch.isfinite(out.loss)
    assert model.get_output_embeddings() is model.thinker.lm_head
    assert model.get_input_embeddings() is model.thinker.get_input_embeddings()
    text = model.generate(
        input_ids = ids, return_audio = False, thinker_max_new_tokens = 2, do_sample = False
    )
    if isinstance(text, tuple):  # transformers 4.x returns (sequences, None) without audio
        text = text[0]
    assert text.shape == (1, 6)
    import copy

    clone = copy.deepcopy(model)
    assert clone.get_output_embeddings() is clone.thinker.lm_head
    assert clone.forward.__self__ is clone.thinker
    assert type(model).forward is torch.nn.Module.forward
    fresh = _tiny_omni()
    with pytest.raises(TypeError, match = "input_ids"):
        fresh(input_ids = ids)


def test_a_kept_wrapper_trains_through_peft():
    peft = pytest.importorskip("peft")
    from unsloth.models.vision import _text_trainable_core

    model = _text_trainable_core(_tiny_omni(), text_intent = False)
    # Reentrant checkpointing trains LoRA only if the embedding output requires grad.
    model.gradient_checkpointing_enable(gradient_checkpointing_kwargs = {"use_reentrant": True})
    model.enable_input_require_grads()
    config = peft.LoraConfig(
        r = 2, target_modules = r"thinker\.model\.layers\.\d+\.self_attn\.q_proj", lora_alpha = 4
    )
    peft_model = peft.get_peft_model(model, config)
    peft_model.train()
    ids = torch.tensor([[1, 2, 3, 4]], device = DEVICE)
    for name, parameter in peft_model.named_parameters():
        if "lora_B" in name:
            torch.nn.init.normal_(parameter, std = 0.1)
    loss = peft_model(input_ids = ids, labels = ids).loss
    loss.backward()
    grads = [p.grad for n, p in peft_model.named_parameters() if "lora_A" in n]
    assert grads and all(g is not None and g.abs().sum() > 0 for g in grads)
    assert peft_model.get_output_embeddings() is model.thinker.lm_head


def test_the_off_switch_leaves_the_wrapper_without_a_forward(monkeypatch):
    from unsloth.models.vision import _text_trainable_core

    monkeypatch.setenv("UNSLOTH_KEEP_COMPOSED_WRAPPER", "1")
    model = _tiny_omni()
    assert _text_trainable_core(model, text_intent = False) is model
    assert "forward" not in vars(model)


def test_a_kept_wrapper_scopes_lora_to_the_thinker_decoder():
    """get_peft_model's regex must reach thinker.model.layers, not only the vision tower."""
    import inspect
    import re
    from unsloth.models.vision import _text_core_decoder_prefix, _text_trainable_core
    from unsloth_zoo.peft_utils import get_peft_regex

    if "language_tags" not in inspect.signature(get_peft_regex).parameters:
        pytest.skip("this unsloth_zoo's get_peft_regex takes no language_tags")
    model = _text_trainable_core(_tiny_omni(), text_intent = False)
    assert _text_core_decoder_prefix(model) == "thinker.model"
    default_tags = inspect.signature(get_peft_regex).parameters["language_tags"].default
    regex = get_peft_regex(model, language_tags = list(default_tags) + [re.escape("thinker.model")])
    targets = [name for name, _ in model.named_modules() if re.fullmatch(regex, name)]
    assert any(name.startswith("thinker.model.layers.") for name in targets)
    assert not any(
        name.startswith(("talker.", "code2wav.", "thinker.audio_tower.")) for name in targets
    )
    before = get_peft_regex(model)
    assert not any(
        name.startswith("thinker.model.layers.")
        for name, _ in model.named_modules()
        if re.fullmatch(before, name)
    )
    assert _text_core_decoder_prefix(_tiny_omni()) is None


def test_get_peft_model_passes_the_thinker_decoder_as_a_language_tag(monkeypatch):
    import inspect
    from unsloth.models import vision

    if "language_tags" not in inspect.signature(vision.get_peft_regex).parameters:
        pytest.skip("this unsloth_zoo's get_peft_regex takes no language_tags")
    import functools

    seen = {}

    # functools.wraps keeps the real signature, which get_peft_model inspects.
    @functools.wraps(vision.get_peft_regex)
    def capture(model, **kwargs):
        seen.update(kwargs)
        raise RuntimeError("captured")

    monkeypatch.setattr(vision, "get_peft_regex", capture)
    model = vision._text_trainable_core(_tiny_omni(), text_intent = False)
    with pytest.raises(RuntimeError, match = "captured"):
        vision.FastBaseModel.get_peft_model(model, r = 2)
    assert "thinker\\.model" in seen["language_tags"]
    seen.clear()
    thinker = vision._text_trainable_core(_tiny_omni(), text_intent = True)
    with pytest.raises(RuntimeError, match = "captured"):
        vision.FastBaseModel.get_peft_model(thinker, r = 2)
    assert "language_tags" not in seen


def test_a_kept_wrapper_with_an_accelerate_hook_forwards_through_the_hook(monkeypatch):
    """A device-mapped load wraps forward on the instance; the hook must reach the thinker."""
    hooks = pytest.importorskip("accelerate.hooks")
    from unsloth.models.vision import _text_trainable_core

    monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", "1")
    calls = []

    class Counting(hooks.ModelHook):
        def pre_forward(self, module, *args, **kwargs):
            calls.append(type(module).__name__)
            return args, kwargs

    model = _tiny_omni()
    hooks.add_hook_to_module(model, Counting())
    model = _text_trainable_core(model, text_intent = False)
    ids = torch.tensor([[1, 2, 3, 4]], device = DEVICE)
    with torch.no_grad():
        out = model(input_ids = ids, labels = ids)
    assert torch.isfinite(out.loss)
    assert calls == ["Qwen3OmniMoeForConditionalGeneration"]


def _capture_adapter_reload(monkeypatch, tmp_path, target_modules):
    peft = pytest.importorskip("peft")
    from unsloth import FastModel
    from unsloth.models.vision import FastBaseModel

    base = tmp_path / "base"
    adapter = tmp_path / "adapter"
    _tiny_config().save_pretrained(base)
    peft.LoraConfig(
        r = 2, lora_alpha = 2, target_modules = target_modules, base_model_name_or_path = str(base)
    ).save_pretrained(adapter)
    seen = {}

    def capture(*args, **kwargs):
        seen.update(kwargs)
        raise RuntimeError("captured")

    monkeypatch.setattr(FastBaseModel, "from_pretrained", staticmethod(capture))
    try:
        FastModel.from_pretrained(str(adapter), load_in_4bit = False)
    except RuntimeError as error:
        if str(error) != "captured":
            if not has_real_cuda():
                pytest.skip(f"FastModel.from_pretrained needs a GPU here: {error}")
            raise
    return seen


def test_an_adapter_trained_on_the_thinker_reloads_onto_the_thinker(monkeypatch, tmp_path):
    seen = _capture_adapter_reload(
        monkeypatch, tmp_path, r"(?:\bmodel\.layers\.[\d]{1,}\.(?:self_attn)\.(?:q_proj))"
    )
    assert seen["text_intent"] is True


def test_an_adapter_trained_on_the_kept_wrapper_keeps_the_wrapper(monkeypatch, tmp_path):
    seen = _capture_adapter_reload(
        monkeypatch, tmp_path, r"(?:.*?(?:thinker\.model).*?(?:self_attn).*?(?:q_proj))"
    )
    assert seen["text_intent"] is False


def _tiny_omni_checkpoint(path):
    tokenizers = pytest.importorskip("tokenizers")
    _tiny_omni().save_pretrained(path)
    vocab = {f"w{i}": i for i in range(48)}
    vocab.update({"<unk>": 48, "<pad>": 49, "<eos>": 50})
    backend = tokenizers.Tokenizer(tokenizers.models.WordLevel(vocab, unk_token = "<unk>"))
    backend.pre_tokenizer = tokenizers.pre_tokenizers.Whitespace()
    transformers.PreTrainedTokenizerFast(
        tokenizer_object = backend, unk_token = "<unk>", pad_token = "<pad>", eos_token = "<eos>"
    ).save_pretrained(path)


@pytest.mark.parametrize("text_only", [True, False], ids = ["thinker", "kept_wrapper"])
@pytest.mark.parametrize(
    "target_modules", [None, ["q_proj", "v_proj"]], ids = ["unsloth_regex", "leaf_list"]
)
def test_an_omni_adapter_reloads_onto_the_model_it_was_trained_on(
    tmp_path, text_only, target_modules
):
    if not has_real_cuda():
        pytest.skip("FastModel needs a GPU")
    from unsloth import FastModel

    base = tmp_path / "base"
    _tiny_omni_checkpoint(base)
    model, _ = FastModel.from_pretrained(str(base), text_only = text_only, load_in_4bit = False)
    kwargs = {} if target_modules is None else {"target_modules": target_modules}
    model = FastModel.get_peft_model(model, r = 2, lora_alpha = 2, random_state = 0, **kwargs)
    trained_on = type(model.get_base_model()).__name__
    with torch.no_grad():
        for name, param in model.named_parameters():
            if "lora_B" in name:
                param.normal_(0, 0.1)
    saved = {
        n: p.detach().float().cpu().clone() for n, p in model.named_parameters() if "lora_" in n
    }
    assert saved
    model.save_pretrained(tmp_path / "adapter")
    del model
    reloaded, _ = FastModel.from_pretrained(str(tmp_path / "adapter"), load_in_4bit = False)
    assert type(reloaded.get_base_model()).__name__ == trained_on
    loaded = {n: p.detach().float().cpu() for n, p in reloaded.named_parameters() if "lora_" in n}
    assert loaded.keys() == saved.keys()
    for name, value in saved.items():
        assert torch.equal(loaded[name], value), name


def test_saved_weight_keys_decide_over_the_target_regex():
    from unsloth.models.loader import _adapter_targets_text_core

    config = type("Config", (), {"target_modules": {"q_proj", "v_proj"}})()
    thinker_keys = ["base_model.model.model.layers.0.self_attn.q_proj.lora_A.weight"]
    wrapper_keys = ["base_model.model.thinker.model.layers.0.self_attn.q_proj.lora_A.weight"]
    assert _adapter_targets_text_core(config, thinker_keys) is True
    assert _adapter_targets_text_core(config, wrapper_keys) is False
    assert _adapter_targets_text_core(config, None) is False
    regex = type("Config", (), {"target_modules": r".*\.q_proj"})()
    assert _adapter_targets_text_core(regex, wrapper_keys) is False


@pytest.mark.parametrize("saved_as", ["adapter_model.safetensors", "adapter_model.bin"])
def test_hub_adapter_keys_are_read_from_the_saved_file(monkeypatch, tmp_path, saved_as):
    huggingface_hub = pytest.importorskip("huggingface_hub")
    safetensors_torch = pytest.importorskip("safetensors.torch")
    from unsloth.models.loader import _adapter_weight_keys

    key = "base_model.model.model.layers.0.self_attn.q_proj.lora_A.weight"
    path = tmp_path / saved_as
    if saved_as.endswith(".safetensors"):
        safetensors_torch.save_file({key: torch.ones(2, 2)}, str(path))
    else:
        torch.save({key: torch.ones(2, 2)}, path)
    asked = []

    def fake_download(repo_id, filename, **kwargs):
        asked.append((repo_id, filename, kwargs.get("local_files_only"), kwargs.get("revision")))
        if filename != saved_as:
            raise FileNotFoundError(filename)
        return str(path)

    monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_download)
    assert _adapter_weight_keys("someone/omni-adapter", revision = "r", local_files_only = True) == [
        key
    ]
    assert asked[0] == ("someone/omni-adapter", "adapter_model.safetensors", True, "r")
    assert asked[-1][1] == saved_as


def test_local_bin_adapter_keys_are_read_without_weights(tmp_path):
    from unsloth.models.loader import _adapter_weight_keys

    key = "base_model.model.thinker.model.layers.0.q_proj.lora_A.weight"
    torch.save({key: torch.ones(2, 2)}, tmp_path / "adapter_model.bin")
    assert _adapter_weight_keys(str(tmp_path)) == [key]
    assert _adapter_weight_keys(str(tmp_path / "missing")) is None


def test_an_audio_only_wrapper_adapter_stays_on_the_wrapper():
    from unsloth.models.loader import _adapter_targets_text_core, _composition_children

    children = _composition_children(_tiny_config())
    assert set(children) == {"thinker", "talker", "code2wav"}
    config = type("Config", (), {"target_modules": {"q_proj"}})()
    talker = ["base_model.model.talker.model.layers.0.self_attn.q_proj.lora_A.weight"]
    code2wav = ["base_model.model.code2wav.decoder.0.conv.lora_A.weight"]
    thinker_only = ["base_model.model.model.layers.0.self_attn.q_proj.lora_A.weight"]
    assert _adapter_targets_text_core(config, talker, children) is False
    assert _adapter_targets_text_core(config, code2wav, children) is False
    assert _adapter_targets_text_core(config, thinker_only, children) is True
    regex = type("Config", (), {"target_modules": r"(?:.*?(?:talker).*?(?:q_proj))"})()
    assert _adapter_targets_text_core(regex, None, children) is False


def test_only_a_forwardless_composition_redirects_adapter_reloads():
    """Every VLM maps to an image-text auto class; routing their adapters to text_only dropped the vision tower."""
    from transformers import Gemma3Config, Qwen2_5_VLConfig
    from unsloth.models.loader import _is_forwardless_composition

    assert _is_forwardless_composition(_tiny_config()) is True
    assert _is_forwardless_composition(Gemma3Config()) is False
    assert _is_forwardless_composition(Qwen2_5_VLConfig()) is False


def test_a_kept_wrapper_freezes_the_siblings_its_forward_never_uses():
    """DDP (find_unused_parameters=False) fails on trainable talker / code2wav weights with no gradient."""
    from unsloth.models.vision import _text_trainable_core

    model = _text_trainable_core(_tiny_omni(), text_intent = False)
    assert all(not p.requires_grad for p in model.talker.parameters())
    assert all(not p.requires_grad for p in model.code2wav.parameters())
    assert any(p.requires_grad for p in model.thinker.parameters())


def test_a_kept_wrapper_resolves_its_layer_count_through_the_thinker():
    from unsloth.models.vision import _get_total_transformer_layers, _text_trainable_core

    model = _text_trainable_core(_tiny_omni(), text_intent = False)
    assert _get_total_transformer_layers(model) is None
    assert (
        _get_total_transformer_layers(model.thinker)
        == model.thinker.config.text_config.num_hidden_layers
    )


def test_an_explicit_leaf_list_on_a_kept_wrapper_is_scoped_to_the_thinker(monkeypatch):
    """PEFT suffix-matches ["q_proj", "v_proj"] in the talker too; those adapters never train."""
    import functools
    from unsloth.models import vision

    seen = {}

    @functools.wraps(vision.get_peft_regex)
    def capture(model, **kwargs):
        seen.update(kwargs)
        raise RuntimeError("captured")

    monkeypatch.setattr(vision, "get_peft_regex", capture)
    model = vision._text_trainable_core(_tiny_omni(), text_intent = False)
    with pytest.raises(RuntimeError, match = "captured"):
        vision.FastBaseModel.get_peft_model(model, r = 2, target_modules = ["q_proj", "v_proj"])
    assert seen["target_modules"] == ["q_proj", "v_proj"]


def test_a_kept_wrapper_resizes_its_vocabulary_through_the_thinker():
    from unsloth.models.vision import _text_trainable_core

    model = _text_trainable_core(_tiny_omni(), text_intent = False)
    old = model.get_input_embeddings().weight.shape[0]
    model.resize_token_embeddings(old + 8)
    assert model.thinker.get_input_embeddings().weight.shape[0] == old + 8
    assert model.thinker.lm_head.weight.shape[0] == old + 8


def _kept_wrapper_peft(monkeypatch, **peft_kwargs):
    from unsloth.models import vision

    monkeypatch.setattr(
        vision.FastBaseModel, "post_patch_model", staticmethod(lambda model, *a, **k: model)
    )
    model = vision._text_trainable_core(_tiny_omni(), text_intent = False)
    model.max_seq_length = 64
    return vision.FastBaseModel.get_peft_model(model, r = 2, **peft_kwargs)


def test_a_qualified_target_on_a_kept_wrapper_reaches_peft_unchanged(monkeypatch):
    """get_peft_regex treats entries as leaves, so a full path raised "No layers to finetune"."""
    target = "thinker.model.layers.0.self_attn.q_proj"
    peft_model = _kept_wrapper_peft(monkeypatch, target_modules = [target])
    trained = {n for n, p in peft_model.named_parameters() if p.requires_grad}
    assert trained and all(target in n for n in trained)


def test_saved_modules_on_a_kept_wrapper_skip_the_talker(monkeypatch):
    """An unqualified "lm_head" also suffix-matched talker.code_predictor.lm_head, a ModuleList PEFT refuses."""
    peft_model = _kept_wrapper_peft(
        monkeypatch, target_modules = ["q_proj"], modules_to_save = ["lm_head"]
    )
    trained = [n for n, p in peft_model.named_parameters() if p.requires_grad]
    assert any("thinker.lm_head" in n for n in trained)
    assert not any(".talker." in n for n in trained)


def test_full_finetuning_keeps_a_kept_wrappers_siblings_frozen(monkeypatch):
    """prepare_model_for_training(full_finetuning = True) turned every parameter back on."""
    from unsloth.models import vision

    monkeypatch.setenv("UNSLOTH_ENABLE_FULL_FINETUNING", "1")
    model = vision._text_trainable_core(_tiny_omni(), text_intent = False)
    try:
        vision.FastBaseModel.post_patch_model(model, use_gradient_checkpointing = False)
    except RuntimeError as error:
        if "inner_training_loop" not in str(error):
            raise
    assert any(p.requires_grad for p in model.thinker.parameters())
    assert not any(p.requires_grad for p in model.talker.parameters())


def test_suffix_target_parameters_on_a_kept_wrapper_skip_the_talker(monkeypatch):
    """PEFT suffix-matches target_parameters; bare expert names also adapted the talker's experts."""
    peft_model = _kept_wrapper_peft(
        monkeypatch, target_modules = ["q_proj"], target_parameters = ["mlp.experts.gate_up_proj"]
    )
    trained = [n for n, p in peft_model.named_parameters() if p.requires_grad]
    assert any("thinker." in n and "experts" in n for n in trained)
    assert not any(".talker." in n for n in trained)