This is an automated email from the ASF dual-hosted git repository.

tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 1091f2fc97 [Fix][Metal] Fit wide FP32 prefill attention in shared 
memory (#21002)
1091f2fc97 is described below

commit 1091f2fc97187bdef99272cb2c598dc3dd572d7c
Author: Julian Ng-Thow-Hing <[email protected]>
AuthorDate: Tue Nov 24 14:47:31 2026 -0500

    [Fix][Metal] Fit wide FP32 prefill attention in shared memory (#21002)
    
    Fit wide FP32 prefill attention within Metal's 32 KiB shared-memory
    limit by retrying a 16-thread configuration when the normal search fails
    for 512-wide Q/K/V heads.
---
 python/tvm/relax/frontend/nn/llm/_kernel_common.py | 52 +++++++++++++++-------
 .../relax/test_frontend_nn_llm_kernel_config.py    | 23 ++++++++++
 2 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/python/tvm/relax/frontend/nn/llm/_kernel_common.py 
b/python/tvm/relax/frontend/nn/llm/_kernel_common.py
index 2c33455bd6..3022506e9d 100644
--- a/python/tvm/relax/frontend/nn/llm/_kernel_common.py
+++ b/python/tvm/relax/frontend/nn/llm/_kernel_common.py
@@ -645,21 +645,43 @@ def _get_prefill_kernel_config(
             tile_z = 4
     if target.kind.name == "metal":
         max_shared_memory_per_block = 
int(target.attrs["max_shared_memory_per_block"])
-        tile_x, num_warps, tile_z = _fit_prefill_config_to_shared_memory(
-            tile_x,
-            tile_y,
-            tile_z,
-            original_tile_z,
-            max_tile_x,
-            d,
-            d_v,
-            dtype,
-            LOAD_VEC,
-            bdx,
-            num_warps,
-            merged_kv,
-            max_shared_memory_per_block,
-        )
+        try:
+            tile_x, num_warps, tile_z = _fit_prefill_config_to_shared_memory(
+                tile_x,
+                tile_y,
+                tile_z,
+                original_tile_z,
+                max_tile_x,
+                d,
+                d_v,
+                dtype,
+                LOAD_VEC,
+                bdx,
+                num_warps,
+                merged_kv,
+                max_shared_memory_per_block,
+            )
+        except ValueError:
+            if dtype != "float32" or d != 512 or d_v != 512 or merged_kv:
+                raise
+            # Wide FP32 heads need a 4x4 score tile to fit 32 KiB. Sixteen
+            # threads can schedule that tile; a full SIMD group cannot.
+            bdx = 16
+            tile_x, num_warps, tile_z = _fit_prefill_config_to_shared_memory(
+                tile_x,
+                tile_y,
+                tile_z,
+                original_tile_z,
+                max_tile_x,
+                d,
+                d_v,
+                dtype,
+                LOAD_VEC,
+                bdx,
+                num_warps,
+                merged_kv,
+                max_shared_memory_per_block,
+            )
     if target.kind.name == "opencl" and (
         ("android" in str(target.host)) or ("adreno" in str(target.attrs))
     ):
diff --git a/tests/python/relax/test_frontend_nn_llm_kernel_config.py 
b/tests/python/relax/test_frontend_nn_llm_kernel_config.py
index 61cad58b4c..a4d695901e 100644
--- a/tests/python/relax/test_frontend_nn_llm_kernel_config.py
+++ b/tests/python/relax/test_frontend_nn_llm_kernel_config.py
@@ -287,5 +287,28 @@ def 
test_merge_state_waits_before_overwriting_the_shared_lse(head_dim):
     )
 
 
[email protected]("sliding_window", [False, True])
+def test_wide_fp32_head_prefill_fits_metal_shared_memory(sliding_window):
+    target = tvm_tirx.target.Target("metal")
+    config = _get_prefill_kernel_config(2, 4, 512, "float32", target)
+    assert config == (16, 2, 2, 16, 1, 4, 512, 4)
+    for func in (
+        _attention_prefill(2, 4, 512, "float32", sliding_window, {}, target),
+        _attention_prefill_ragged(2, 4, 512, 512, "float32", {}, target),
+    ):
+        assert _get_allocated_shared_memory(func) == 24_688
+        assert _get_allocated_shared_memory(func) <= int(
+            target.attrs["max_shared_memory_per_block"]
+        )
+        module = tvm_tirx.tirx.compile(func.with_attr("global_symbol", 
"main"), target=target).mod
+        assert module.imports[0].inspect_source()
+
+
+def test_wide_fp32_metal_prefill_rejects_insufficient_storage():
+    target = tvm_tirx.target.Target({"kind": "metal", 
"max_shared_memory_per_block": 1024})
+    with pytest.raises(ValueError, match="target allows 1024 bytes"):
+        _get_prefill_kernel_config(2, 4, 512, "float32", target)
+
+
 if __name__ == "__main__":
     tvm_tirx.testing.main()

Reply via email to