Skip to content

Commit 1bc0ce8

Browse files
authored
[Fix][DLight] Decline GEMV reductions with only batch spatial axes (#20985)
Weighted centered reductions can look like GEMV when a broadcast operand uses every spatial axis, leaving only batch axes and triggering an assertion; decline those blocks so the reduction schedule handles them instead. The regression verifies rejection and fallback, all 15 GEMV tests pass using the current pass wrapper with the retained compiler, and the patched compiler builds ECAPA for WebGPU with eight HF parity cases passing in Chrome.
1 parent 23a6dbe commit 1bc0ce8

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎python/tvm/s_tir/dlight/analysis/gemv.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ def normalize(
158158
else:
159159
s_loops.append(loop)
160160

161-
assert s_loops
162-
assert r_loops
161+
# Broadcast operands can make a general reduction look like GEMV.
162+
# GEMV needs a non-batch spatial axis as well as a reduction axis.
163+
if not s_loops or not r_loops:
164+
return None
163165
if not c_loops:
164166
c_loops = [sch.add_unit_loop(block_info.block_rv)]
165167
if not batch_loops:

‎tests/python/s_tir/dlight/test_gpu_gemv.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,41 @@ def before(
5252
tvm_tirx.ir.assert_structural_equal(scheduled["main"], before)
5353

5454

55+
def test_gemv_rejects_all_batch_spatial_axes():
56+
frames = T.dynamic("frames", "int64")
57+
58+
@Ts.function(private=True)
59+
def before(
60+
data: T.Tensor((T.int64(768), frames), "float32"),
61+
mean: T.Tensor((T.int64(768),), "float32"),
62+
weights: T.Tensor((T.int64(768), frames), "float32"),
63+
result: T.Tensor((T.int64(768),), "float32"),
64+
):
65+
for c, t in T.grid(T.int64(768), frames):
66+
with Ts.sblock("weighted_variance"):
67+
vc, vt = Ts.axis.remap("SR", [c, t])
68+
with Ts.init():
69+
result[vc] = T.float32(0)
70+
result[vc] += weights[vc, vt] * (
71+
(data[vc, vt] - mean[vc]) * (data[vc, vt] - mean[vc])
72+
)
73+
74+
mod = tvm_tirx.IRModule({"main": before})
75+
target = Target(
76+
{
77+
"kind": "webgpu",
78+
"keys": ["webgpu", "gpu"],
79+
"max_num_threads": 256,
80+
"thread_warp_size": 1,
81+
"max_shared_memory_per_block": 32768,
82+
}
83+
)
84+
rejected = dl.ApplyDefaultSchedule(dl.gpu.GEMV(), target=target)(mod)
85+
tvm_tirx.ir.assert_structural_equal(rejected["main"], before)
86+
scheduled = dl.ApplyDefaultSchedule(dl.gpu.GEMV(), dl.gpu.Reduction(), target=target)(mod)
87+
assert scheduled["main"].attrs["tirx.is_scheduled"]
88+
89+
5590
def test_gemv_basic():
5691
# fmt: off
5792
n = T.dynamic("n", "int32")

0 commit comments

Comments
 (0)