MCPcopy Create free account
hub / github.com/apache/tvm / test_crossthread_reduction2

Function test_crossthread_reduction2

tests/python/codegen/test_target_codegen_cuda.py:376–428  ·  view source on GitHub ↗
(target, dev)

Source from the content-addressed store, hash-verified

374
375@tvm.testing.parametrize_targets("cuda", "rocm")
376def test_crossthread_reduction2(target, dev):
377 def sched(nthdx, nthdy):
378 @I.ir_module(s_tir=True)
379 class Module:
380 @T.prim_func(s_tir=True)
381 def main(var_A: T.handle, var_B: T.handle):
382 T.func_attr({"tirx.noalias": True})
383 n, k0, k1 = T.int32(), T.int32(), T.int32()
384 A = T.match_buffer(var_A, (n, k0, k1))
385 B = T.match_buffer(var_B, (n,))
386 for i in T.thread_binding(n, thread="blockIdx.x"):
387 for k0_0 in T.thread_binding(nthdx, thread="threadIdx.x"):
388 for k0_1 in range((k0 + nthdx - 1) // nthdx):
389 for k1_0 in T.thread_binding(nthdy, thread="threadIdx.y"):
390 for k1_1 in range((k1 + nthdy - 1) // nthdy):
391 with T.sblock("B"):
392 v_i = T.axis.spatial(n, i)
393 v_k0 = T.axis.reduce(
394 k0, k0_0 * ((k0 + nthdx - 1) // nthdx) + k0_1
395 )
396 v_k1 = T.axis.reduce(
397 k1, k1_0 * ((k1 + nthdy - 1) // nthdy) + k1_1
398 )
399 T.where(
400 k0_0 * ((k0 + nthdx - 1) // nthdx) + k0_1 < k0
401 and k1_0 * ((k1 + nthdy - 1) // nthdy) + k1_1 < k1
402 )
403 T.reads(A[v_i, v_k0, v_k1])
404 T.writes(B[v_i])
405 with T.init():
406 B[v_i] = T.float32(0.0)
407 B[v_i] = B[v_i] + A[v_i, v_k0, v_k1]
408
409 func = tvm.compile(Module, target="cuda")
410 return func
411
412 def verify(nthdx, nthdy):
413 func = sched(nthdx, nthdy)
414 nn = 3
415 # checks three typical cases
416 vx = [nthdx - 1, nthdx, nthdx + 1]
417 vy = [nthdy - 1, nthdy, nthdy + 1]
418 for kk0, kk1 in [(x, y) for x in vx for y in vy]:
419 size = (nn, kk0, kk1)
420 a = tvm.runtime.tensor(np.random.uniform(size=size).astype("float32"), dev)
421 b = tvm.runtime.tensor(np.zeros(nn, dtype="float32"), dev)
422 func(a, b)
423 tvm.testing.assert_allclose(b.numpy(), np.sum(a.numpy(), axis=(1, 2)), rtol=1e-3)
424
425 verify(16, 16)
426 verify(32, 32)
427 verify(16, 32)
428 verify(32, 16)
429
430
431@pytest.mark.gpu

Callers

nothing calls this directly

Calls 1

verifyFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…