From 078f20d4238b9097580a6d5a68051b7b8b8a6897 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sat, 5 Dec 2026 09:03:00 -0600 Subject: [PATCH 1/3] mlx_tf config: refuse an unknown model_type instead of serving it as Qwen3.5 parseJson defaulted every unrecognised model_type to the Qwen3.5 hybrid; a new architecture then served until a tensor read failed somewhere far from the cause. An unknown type is now refused as UnsupportedModelType up front. qwen3_vl/qwen3_5_vl and their *_text variants (the text towers vision_config admits) count as the hybrid; everything else is refused. Also hoist build.zig's test-filter option: it was declared twice (lines 82/101), which panics zig 0.17 on every build. And register test-config-parse-host: config_json_test.zig checks every recognised model_type maps to its arch, an absent field keeps the hybrid default, and unknown types are refused. --- engines/mlx_tf/build.zig | 16 +++++++-- engines/mlx_tf/src/config_json.zig | 8 ++++- engines/mlx_tf/src/config_json_test.zig | 47 +++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 engines/mlx_tf/src/config_json_test.zig diff --git a/engines/mlx_tf/build.zig b/engines/mlx_tf/build.zig index 8121936a61e..ad434c5914f 100644 --- a/engines/mlx_tf/build.zig +++ b/engines/mlx_tf/build.zig @@ -73,18 +73,30 @@ pub fn build(b: *std.Build) void { const bounds_test = b.addTest(.{ .root_module = bounds_mod, .filters = &.{"TFP5 header:"} }); b.step("test-tfq5-bounds-host", "Run TFQ5 header bounds tests without a GPU").dependOn(&b.addRunArtifact(bounds_test).step); + const test_filter = b.option([]const u8, "test-filter", "Only tests containing this text"); const mla_fixture = b.createModule(.{ .root_source_file = b.path("../../zig/mla_published_fixture.zig") }); const mla_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/weights_mla_test.zig"), .target = b.graph.host, .optimize = .Debug, .link_libc = true, - }), .filters = if (b.option([]const u8, "test-filter", "Only tests containing this text")) |filter| &.{filter} else &.{"MLA"} }); + }), .filters = if (test_filter) |filter| &.{filter} else &.{"MLA"} }); mla_tests.root_module.addImport("mla_published_fixture", mla_fixture); b.step("test-mla-host", "Check split-MLA assembly before device upload").dependOn(&b.addRunArtifact(mla_tests).step); + const config_parse_test = b.addTest(.{ .root_module = b.createModule(.{ + .root_source_file = b.path("src/config_json_test.zig"), + .target = b.graph.host, + .optimize = .Debug, + .link_libc = true, + .imports = &.{.{ .name = "shaders", .module = shaders_mod }}, + }), .filters = if (test_filter) |filter| &.{filter} else &.{} }); + const config_parse_step = b.step("test-config-parse-host", "Check config.json arch selection and model_type refusal without Metal"); + config_parse_step.dependOn(&b.addRunArtifact(config_parse_test).step); + const test_step = b.step("test", "Run unit tests"); test_step.dependOn(vision_step); + test_step.dependOn(config_parse_step); const ergo_ref = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/ergo_ref_test.zig"), .target = b.graph.host, @@ -98,7 +110,7 @@ pub fn build(b: *std.Build) void { .link_libc = true, .target = b.graph.host, .optimize = .Debug, - }), .filters = if (b.option([]const u8, "test-filter", "Only tests containing this text")) |filter| &.{filter} else &.{} }); + }), .filters = if (test_filter) |filter| &.{filter} else &.{} }); const pack_validate_step = b.step("test-pack-validate-host", "Check byte-backed pack sources without Metal"); pack_validate_step.dependOn(&b.addRunArtifact(pack_test).step); const pack_experts_test = b.addTest(.{ .root_module = b.createModule(.{ diff --git a/engines/mlx_tf/src/config_json.zig b/engines/mlx_tf/src/config_json.zig index cd3b7d99e16..24934b9d9f7 100644 --- a/engines/mlx_tf/src/config_json.zig +++ b/engines/mlx_tf/src/config_json.zig @@ -65,7 +65,13 @@ pub const Methods = struct { const is_lfm2_moe = std.mem.eql(u8, mtype, "lfm2_moe"); const is_lfm = is_lfm2 or is_lfm2_moe; const is_dense_attn = is_qwen2 or is_llama or is_mistral or is_mixtral or is_qwen3 or is_qwen3_moe or is_gemma or is_mla or is_glm or is_lfm; - const arch: Arch = if (is_ergo) .ergo else if (is_nemotron) .nemotron_h else if (is_dense_attn) .qwen2_dense else .qwen35_hybrid; + // vision_config admits any root model_type containing "qwen3_vl"/"qwen3_5_vl"; its text + // tower then parses here under names like "qwen3_vl_text" (Qwen3-VL) rather than the root's + const is_qwen35_vl = std.mem.indexOf(u8, mtype, "qwen3_vl") != null or std.mem.indexOf(u8, mtype, "qwen3_5_vl") != null; + const is_qwen35 = std.mem.eql(u8, mtype, "qwen3_5") or std.mem.eql(u8, mtype, "qwen3_5_text") or + is_qwen35_vl or is_qwen35_moe; + // an unrecognised model_type is refused by name, not served as a Qwen3.5 hybrid: a wrong architecture shows up later as a bad tensor + const arch: Arch = if (is_ergo) .ergo else if (is_nemotron) .nemotron_h else if (is_dense_attn) .qwen2_dense else if (is_qwen35) .qwen35_hybrid else return error.UnsupportedModelType; const hidden_size = getUsize(tc, "hidden_size"); const num_heads = getUsize(tc, "num_attention_heads"); diff --git a/engines/mlx_tf/src/config_json_test.zig b/engines/mlx_tf/src/config_json_test.zig new file mode 100644 index 00000000000..02e3f68b5c7 --- /dev/null +++ b/engines/mlx_tf/src/config_json_test.zig @@ -0,0 +1,47 @@ +//! Host parse tests for config_json.zig's architecture selection: recognised model_type +//! values keep their arch, a missing field keeps the Qwen3.5 hybrid default, and a +//! model_type the engine does not know is refused instead of falling through. +const std = @import("std"); +const config_json = @import("config_json.zig"); +const config = @import("config.zig"); +const Arch = config.Arch; +const ModelConfig = config.ModelConfig; + +const fields = "\"hidden_size\":1024,\"intermediate_size\":2816,\"num_hidden_layers\":4,\"vocab_size\":32000,\"num_attention_heads\":8,\"num_key_value_heads\":4,\"rms_norm_eps\":1e-5"; + +fn parse(mtype: ?[]const u8) !ModelConfig { + const a = std.testing.allocator; + const json = if (mtype) |t| + try std.fmt.allocPrint(a, "{{\"model_type\":\"{s}\",{s}}}", .{ t, fields }) + else + try std.fmt.allocPrint(a, "{{{s}}}", .{fields}); + defer a.free(json); + return config_json.Methods.parseJson(a, json); +} + +test "config maps every recognised model_type to its arch" { + const hybrid = [_][]const u8{ "qwen3_5", "qwen3_5_text", "qwen3_5_moe", "qwen3_5_moe_text", "qwen3_next", "qwen3_6_moe", "qwen3_6_moe_text", "qwen3_vl", "qwen3_5_vl", "qwen3_vl_text", "qwen3_5_vl_text" }; + for (hybrid) |t| { + const cfg = try parse(t); + try std.testing.expectEqual(Arch.qwen35_hybrid, cfg.arch); + } + const dense_attn = [_][]const u8{ "llama", "mistral", "mixtral", "qwen2", "qwen3", "qwen3_moe", "gemma2", "gemma3_text", "gemma4_unified", "deepseek_v2", "deepseek_v32", "glm4", "glm4_moe", "glm_moe_dsa", "lfm2_vl", "lfm2_moe" }; + for (dense_attn) |t| { + const cfg = try parse(t); + try std.testing.expectEqual(Arch.qwen2_dense, cfg.arch); + } + try std.testing.expectEqual(Arch.nemotron_h, (try parse("nemotron_h")).arch); + try std.testing.expectEqual(Arch.ergo, (try parse("ergo")).arch); +} + +test "config keeps the Qwen3.5 hybrid default when model_type is absent" { + const cfg = try parse(null); + try std.testing.expectEqual(Arch.qwen35_hybrid, cfg.arch); +} + +test "config refuses a model_type it does not know, instead of serving it as Qwen3.5" { + const unknown = [_][]const u8{ "qwen4_moe", "qwen4_exp", "qwen4_exp_text", "qwen3_8", "made_up_arch" }; + for (unknown) |t| { + try std.testing.expectError(error.UnsupportedModelType, parse(t)); + } +} From 54837b761964c45ddba478a7fecd4ee07b7ddd0d Mon Sep 17 00:00:00 2001 From: "user.email" <816217+joshuaswarren@users.noreply.github.com> Date: Sat, 5 Dec 2026 09:13:24 -0600 Subject: [PATCH 2/3] changelog line for #6505 --- lines/6505.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lines/6505.txt diff --git a/lines/6505.txt b/lines/6505.txt new file mode 100644 index 00000000000..f3906e9593c --- /dev/null +++ b/lines/6505.txt @@ -0,0 +1 @@ +- mlx_tf: a `model_type` the engine does not know is refused with `UnsupportedModelType` instead of being served as a Qwen3.5 hybrid; the absent-key default and every recognised type are unchanged, and `qwen3_vl`/`qwen3_5_vl` text towers still map to the hybrid (#6505, fixes #6501). From ee0ad032565df6ff530090e50781aa3548b35582 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 5 Dec 2026 16:32:58 +0000 Subject: [PATCH 3/3] changelog: #6505 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 465ed51d952..7ed1cb58034 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- mlx_tf: a `model_type` the engine does not know is refused with `UnsupportedModelType` instead of being served as a Qwen3.5 hybrid; the absent-key default and every recognised type are unchanged, and `qwen3_vl`/`qwen3_5_vl` text towers still map to the hybrid (#6505, fixes #6501). + - The B70 contract checks Qwen sparse attention selection order, counts and flags against a host oracle. (#6425). - docs/myelin.md: the Day-0 admission and cache sections now match the code; Day-0 uses only an explicit cache directory and does not honour `--myelin-no-cache` yet (#6512).