Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions python/tvm/topi/gpu/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity
reduction[bx] = cast(identity_value, out_dtype)
with T.Else():
nthread_tx = max_threads
nthread_bx = ceil_div(scan_axis_size, max_threads)
nthread_by = batch_size
blocks_per_batch = ceil_div(scan_axis_size, max_threads)

# Flatten the batch and scan-block axes into blockIdx.x. On CUDA,
# blockIdx.y is limited to 65535 even when blockIdx.x can be much larger.
# Copy data to output
tx = te.thread_axis("threadIdx.x")
bx = te.thread_axis("blockIdx.x")
by = te.thread_axis("blockIdx.y")
with T.frame_scope(
[
T.attr(tx, "thread_extent", nthread_tx),
T.attr(bx, "thread_extent", nthread_bx),
T.attr(by, "thread_extent", nthread_by),
T.attr(bx, "thread_extent", blocks_per_batch * batch_size),
]
):
tid = bx * nthread_tx + tx
batch = tvm.tirx.indexdiv(bx, blocks_per_batch)
tid = tvm.tirx.indexmod(bx, blocks_per_batch) * nthread_tx + tx
with T.If(tid < scan_axis_size):
with T.Then():
output[by * scan_axis_size + tid] = cast(
data[by * scan_axis_size + tid], out_dtype
output[batch * scan_axis_size + tid] = cast(
data[batch * scan_axis_size + tid], out_dtype
)

# The following algorithm performs parallel exclusive scan
Expand All @@ -132,7 +132,7 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity

tx = te.thread_axis("threadIdx.x")
bx = te.thread_axis("blockIdx.x")
by = te.thread_axis("blockIdx.y")
blocks_per_batch = cast(ceil_div(scan_axis_size, max_threads * width), "int32")
start_buf = T.decl_buffer([1], "int32", scope="local")
middle_buf = T.decl_buffer([1], "int32", scope="local")
end_buf = T.decl_buffer([1], "int32", scope="local")
Expand All @@ -142,12 +142,12 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity
T.attr(
bx,
"thread_extent",
cast(ceil_div(scan_axis_size, max_threads * width), "int32"),
blocks_per_batch * batch_size,
),
T.attr(by, "thread_extent", nthread_by),
]
):
tid = bx * nthread_tx + tx
batch = tvm.tirx.indexdiv(bx, blocks_per_batch)
tid = tvm.tirx.indexmod(bx, blocks_per_batch) * nthread_tx + tx
start = T.buffer_proxy(start_buf)
middle = T.buffer_proxy(middle_buf)
end = T.buffer_proxy(end_buf)
Expand All @@ -158,9 +158,9 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity
end[0] = tvm.te.min(start[0] + width, scan_axis_size)
with T.If(middle[0] < scan_axis_size):
with T.Then():
output[by * scan_axis_size + end[0] - 1] = binop(
output[by * scan_axis_size + end[0] - 1],
output[by * scan_axis_size + middle[0] - 1],
output[batch * scan_axis_size + end[0] - 1] = binop(
output[batch * scan_axis_size + end[0] - 1],
output[batch * scan_axis_size + middle[0] - 1],
)

# Down Sweep of exclusive scan
Expand All @@ -177,7 +177,7 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity

tx = te.thread_axis("threadIdx.x")
bx = te.thread_axis("blockIdx.x")
by = te.thread_axis("blockIdx.y")
blocks_per_batch = cast(ceil_div(scan_axis_size, max_threads * width), "int32")
start_buf = T.decl_buffer([1], "int32", scope="local")
middle_buf = T.decl_buffer([1], "int32", scope="local")
end_buf = T.decl_buffer([1], "int32", scope="local")
Expand All @@ -188,12 +188,12 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity
T.attr(
bx,
"thread_extent",
cast(ceil_div(scan_axis_size, max_threads * width), "int32"),
blocks_per_batch * batch_size,
),
T.attr(by, "thread_extent", nthread_by),
]
):
tid = bx * nthread_tx + tx
batch = tvm.tirx.indexdiv(bx, blocks_per_batch)
tid = tvm.tirx.indexmod(bx, blocks_per_batch) * nthread_tx + tx
start = T.buffer_proxy(start_buf)
middle = T.buffer_proxy(middle_buf)
end = T.buffer_proxy(end_buf)
Expand All @@ -205,12 +205,12 @@ def exclusive_scan_ir(data, output, reduction=None, binop=operator.add, identity
end[0] = tvm.tirx.min(start[0] + width, scan_axis_size)
with T.If(middle[0] < scan_axis_size):
with T.Then():
tmp[0] = output[by * scan_axis_size + middle[0] - 1]
output[by * scan_axis_size + middle[0] - 1] = output[
by * scan_axis_size + end[0] - 1
tmp[0] = output[batch * scan_axis_size + middle[0] - 1]
output[batch * scan_axis_size + middle[0] - 1] = output[
batch * scan_axis_size + end[0] - 1
]
output[by * scan_axis_size + end[0] - 1] = binop(
output[by * scan_axis_size + end[0] - 1], tmp[0]
output[batch * scan_axis_size + end[0] - 1] = binop(
output[batch * scan_axis_size + end[0] - 1], tmp[0]
)

return ib.get()
Expand Down
36 changes: 36 additions & 0 deletions tests/python/relax/test_backend_dispatch_sort_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,5 +448,41 @@ def run_and_check():
tvm.testing.run_with_gpu_lock(run_and_check)


@pytest.mark.gpu
def test_dispatch_cumprod_cuda_large_batch():
"""Test that GPU scan supports more batches than CUDA's grid-y limit."""
target = "cuda"
if not tvm.testing.device_enabled(target):
pytest.skip(f"{target} not enabled")

@I.ir_module
class Module:
@R.function
def main(x: R.Tensor(("m", "n"), "float32")):
with R.dataflow():
gv = R.cumprod(x, axis=1)
R.output(gv)
return gv

np_data = np.ones((65536, 3), dtype="float32")
np_data[:, 0] = np.arange(65536) % 7 + 1
np_data[:, 1] = 2
np_data[:, 2] = 3
np_cumprod = np.cumprod(np_data, axis=1)

with tvm.target.Target(target):
mod = DispatchSortScan()(Module)
ex = tvm.compile(mod, target)

def run_and_check():
dev = tvm.device_from_target(target)
vm = tvm.relax.VirtualMachine(ex, dev)
tvm_data = tvm.runtime.tensor(np_data, dev)
cumprod = vm["main"](tvm_data)
tvm.testing.assert_allclose(cumprod.numpy(), np_cumprod)

tvm.testing.run_with_gpu_lock(run_and_check)


if __name__ == "__main__":
tvm.testing.main()