@@ -875,6 +875,136 @@ TEST_CASE("test Gemma 4 expert QMM pure route table") {
875875 Gemma4ExpertQMMRoute::fallback_nax);
876876}
877877
878+ TEST_CASE (" test Qwen 3.6 expert QMM pure route table" ) {
879+ using metal::Gemma4ExpertQMMRoute;
880+ using metal::Gemma4ExpertQMMRouteInput;
881+ using metal::classify_gemma4_expert_qmm;
882+
883+ // Base input: Qwen 3.5/3.6 35B-A3B expert projection at W4/g64,
884+ // parametrized by whole-projection [E=256, n, k].
885+ auto qwen = [](int assignments, int k, int n) {
886+ Gemma4ExpertQMMRouteInput input;
887+ input.requested = true ;
888+ input.aot_available = true ;
889+ input.outer_route = true ;
890+ input.affine = true ;
891+ input.transpose = true ;
892+ input.has_bias = true ;
893+ input.indices_uint32 = true ;
894+ input.indices_contiguous = true ;
895+ input.x_bfloat16 = true ;
896+ input.x_contiguous = true ;
897+ input.w_uint32 = true ;
898+ input.w_contiguous = true ;
899+ input.scales_bfloat16 = true ;
900+ input.scales_contiguous = true ;
901+ input.biases_bfloat16 = true ;
902+ input.biases_contiguous = true ;
903+ input.group_size = 64 ;
904+ input.bits = 4 ;
905+ input.expert_count = 256 ;
906+ input.assignments = assignments;
907+ input.index_count = assignments;
908+ input.k = k;
909+ input.n = n;
910+ input.x_rank = 3 ;
911+ input.x_dim0 = assignments;
912+ input.x_dim1 = 1 ;
913+ input.x_dim2 = k;
914+ input.w_rank = 3 ;
915+ input.w_dim0 = 256 ;
916+ input.w_dim1 = n;
917+ input.w_dim2 = k / 8 ;
918+ input.scales_rank = 3 ;
919+ input.scales_dim0 = 256 ;
920+ input.scales_dim1 = n;
921+ input.scales_dim2 = k / 64 ;
922+ input.biases_rank = 3 ;
923+ input.biases_dim0 = 256 ;
924+ input.biases_dim1 = n;
925+ input.biases_dim2 = k / 64 ;
926+ return input;
927+ };
928+
929+ // Fused gate_up, split gate/up, and down projections hit at the chunked
930+ // prefill assignment counts (T x top-8 for T in {512, 1024, 2048}).
931+ for (int assignments : {4096 , 8192 , 16384 }) {
932+ CHECK (
933+ classify_gemma4_expert_qmm (qwen (assignments, 2048 , 1024 )) ==
934+ Gemma4ExpertQMMRoute::hit);
935+ CHECK (
936+ classify_gemma4_expert_qmm (qwen (assignments, 2048 , 512 )) ==
937+ Gemma4ExpertQMMRoute::hit);
938+ CHECK (
939+ classify_gemma4_expert_qmm (qwen (assignments, 512 , 2048 )) ==
940+ Gemma4ExpertQMMRoute::hit);
941+ }
942+
943+ auto exact = qwen (4096 , 2048 , 1024 );
944+ auto check_miss = [&exact](
945+ auto mutate, Gemma4ExpertQMMRoute expected) {
946+ auto input = exact;
947+ mutate (input);
948+ CHECK (classify_gemma4_expert_qmm (input) == expected);
949+ };
950+ // Expert counts other than the two instantiated builders miss on topology.
951+ check_miss (
952+ [](auto & x) {
953+ x.expert_count = 255 ;
954+ x.w_dim0 = 255 ;
955+ x.scales_dim0 = 255 ;
956+ x.biases_dim0 = 255 ;
957+ },
958+ Gemma4ExpertQMMRoute::fallback_topology);
959+ // E=256 with Gemma geometry (and vice versa) must miss on geometry: the
960+ // shape table is tied to the expert count, never mixed.
961+ check_miss (
962+ [](auto & x) {
963+ x.k = 2816 ;
964+ x.n = 1408 ;
965+ x.x_dim2 = 2816 ;
966+ x.w_dim1 = 1408 ;
967+ x.w_dim2 = 352 ;
968+ x.scales_dim1 = 1408 ;
969+ x.scales_dim2 = 44 ;
970+ x.biases_dim1 = 1408 ;
971+ x.biases_dim2 = 44 ;
972+ },
973+ Gemma4ExpertQMMRoute::fallback_geometry);
974+ check_miss (
975+ [](auto & x) { x.w_dim2 = 128 ; },
976+ Gemma4ExpertQMMRoute::fallback_geometry);
977+ check_miss (
978+ [](auto & x) { x.n -= 32 ; },
979+ Gemma4ExpertQMMRoute::fallback_geometry);
980+ // T=128 chunks (1024 assignments) intentionally stay on the legacy path.
981+ for (int assignments : {8 , 1024 , 4095 , 4097 }) {
982+ check_miss (
983+ [assignments](auto & x) {
984+ x.assignments = assignments;
985+ x.index_count = assignments;
986+ x.x_dim0 = assignments;
987+ },
988+ Gemma4ExpertQMMRoute::fallback_assignment_count);
989+ }
990+ check_miss (
991+ [](auto & x) { x.bits = 8 ; },
992+ Gemma4ExpertQMMRoute::fallback_quantization);
993+ check_miss (
994+ [](auto & x) { x.aot_available = false ; },
995+ Gemma4ExpertQMMRoute::fallback_metallib_unavailable);
996+
997+ // The Gemma table must also reject Qwen geometry under E=128.
998+ auto gemma_with_qwen_geometry = exact;
999+ gemma_with_qwen_geometry.expert_count = 128 ;
1000+ gemma_with_qwen_geometry.w_dim0 = 128 ;
1001+ gemma_with_qwen_geometry.scales_dim0 = 128 ;
1002+ gemma_with_qwen_geometry.biases_dim0 = 128 ;
1003+ CHECK (
1004+ classify_gemma4_expert_qmm (gemma_with_qwen_geometry) ==
1005+ Gemma4ExpertQMMRoute::fallback_geometry);
1006+ }
1007+
8781008TEST_CASE (" test Gemma 4 expert QMM counter invariant" ) {
8791009 metal::Gemma4ExpertQMMCounters counters;
8801010 using Route = metal::Gemma4ExpertQMMRoute;
0 commit comments