From 7f682a052869b14efc992ed22abb1d437e9f9327 Mon Sep 17 00:00:00 2001 From: Qubitium Date: Sat, 21 Nov 2026 02:07:34 +0000 Subject: [PATCH 1/2] feat: register inkling2_loop routed experts for defusion Inkling 2 Mini keeps its routed experts as 3-D gate_up_proj/down_proj parameters, so convert_model() returned False and downstream quantizers silently left every expert in the source dtype. Register the native Inkling2LoopExperts class with the linear_loop forward and cover it with a real tiny-model defusion test that compares logits before and after. The test skips with a precise reason unless transformers.models.inkling2_loop is importable (open huggingface/transformers#51050). --- defuser/model_registry.py | 12 ++++++ tests/test_convert_model.py | 78 ++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/defuser/model_registry.py b/defuser/model_registry.py index e94ea42..2110053 100644 --- a/defuser/model_registry.py +++ b/defuser/model_registry.py @@ -52,6 +52,18 @@ class PATCH(str, Enum): "inkling_mm_model": { "min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION, }, + "inkling2_loop": { + "min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION, + PATCH.EXPERTS_DEFUSE: [ + { + # Inkling 2 Mini keeps routed experts as 3-D gate_up_proj / + # down_proj parameters. Expand them into per-expert Linear + # modules so quantizers can discover and quantize each expert. + "module_class": "transformers.models.inkling2_loop.modeling_inkling2_loop.Inkling2LoopExperts", + "forward_impl": "linear_loop", + } + ], + }, "deepseek_v3": { "min_transformers_version": MIN_SUPPORTED_TRANSFORMERS_VERSION, }, diff --git a/tests/test_convert_model.py b/tests/test_convert_model.py index 3a6582e..21e965a 100644 --- a/tests/test_convert_model.py +++ b/tests/test_convert_model.py @@ -1292,7 +1292,9 @@ def test_defused_glm4_model_forward_accepts_router_tuple(model_type, config_fact assert torch.isfinite(logits).all() -@pytest.mark.parametrize("model_type", ["kimi_k25", "kimi_linear", "mimo_v2_flash", "unlimited_ocr"]) +@pytest.mark.parametrize( + "model_type", ["kimi_k25", "kimi_linear", "mimo_v2_flash", "unlimited_ocr", "inkling2_loop"] +) def test_native_moe_model_types_are_registered(model_type): """Native Transformers MoE backbones must not fall through the registry. @@ -1335,6 +1337,80 @@ def test_native_moe_defusion_preserves_forward(model_type, config_factory, model torch.testing.assert_close(actual, expected, atol=1e-6, rtol=0) +def test_inkling2_loop_experts_defuse_preserves_forward(): + """Inkling 2 Mini routed experts are 3-D parameters that must be expanded. + + The implementation only exists on the open huggingface/transformers#51050 + branch, so this test skips with a precise reason on a released Transformers + that has no ``inkling2_loop`` module. + """ + + modeling = pytest.importorskip( + "transformers.models.inkling2_loop.modeling_inkling2_loop", + reason="Inkling2Loop only exists on the open huggingface/transformers#51050 branch.", + ) + configuration = pytest.importorskip( + "transformers.models.inkling2_loop.configuration_inkling2_loop", + reason="Inkling2Loop only exists on the open huggingface/transformers#51050 branch.", + ) + + torch.manual_seed(0) + config = configuration.Inkling2LoopConfig( + text_config={ + "hidden_size": 32, + "num_hidden_layers": 4, + "num_attention_heads": 2, + "num_key_value_heads": 1, + "head_dim": 8, + "intermediate_size": 16, + "n_routed_experts": 3, + "num_experts_per_tok": 2, + "moe_intermediate_size": 8, + "vocab_size": 64, + "unpadded_vocab_size": 64, + "n_layers_per_attn_res_block": 2, + "d_rel": 2, + "rel_extent": 4, + "sliding_window_size": 4, + "loop_config": {"core_start": 2, "core_end": 4, "num_passes": 2, "gate_rank": 4}, + }, + vision_config={ + "hidden_size": 16, + "num_hidden_layers": 1, + "num_attention_heads": 2, + "projection_hidden_dim": 16, + "text_hidden_size": 32, + }, + ) + model = modeling.Inkling2LoopForConditionalGeneration(config).eval() + input_ids = torch.tensor([[1, 2, 3, 4, 5]]) + + with torch.no_grad(): + expected = model(input_ids=input_ids, use_cache=False).logits + + containers = [module for name, module in model.named_modules() if name.endswith(".mlp.experts")] + assert len(containers) == config.text_config.num_hidden_layers + assert all(hasattr(container, "gate_up_proj") for container in containers) + + assert convert_model(model, cleanup_original=False) is True + + containers = [module for name, module in model.named_modules() if name.endswith(".mlp.experts")] + assert len(containers) == config.text_config.num_hidden_layers + for container in containers: + assert not hasattr(container, "gate_up_proj") + assert not hasattr(container, "down_proj") + for index in range(config.text_config.n_routed_experts): + expert = container.get_submodule(str(index)) + assert isinstance(expert.gate_proj, nn.Linear) + assert isinstance(expert.up_proj, nn.Linear) + assert isinstance(expert.down_proj, nn.Linear) + + with torch.no_grad(): + actual = model(input_ids=input_ids, use_cache=False).logits + + torch.testing.assert_close(actual, expected, atol=1e-6, rtol=0) + + def test_glm4v_checkpoint_mapping_splits_gate_up_proj(): from defuser.defuser import get_checkpoint_conversion_mapping From ed69b3b2695cec0ca9fe5d9a9fbfcc0c1dd531cb Mon Sep 17 00:00:00 2001 From: Qubitium Date: Sat, 21 Nov 2026 02:09:54 +0000 Subject: [PATCH 2/2] chore: bump version to 0.0.33 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3971786..c122f7a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta" [project] name = "Defuser" -version = "0.0.32" +version = "0.0.33" description = "Model defuser helper for HF Transformers." readme = "README.md" requires-python = ">=3.9"