Repository navigation
[FEAT] Add cuTile interpreter and tracing support #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ef9b10e
f935834
5936426
d75fd95
c65543c
50db84e
c8f2a10
d796bf6
5acd690
3ce9ce5
62a8803
06ae5d4
12953de
ef389c8
508d39b
1a4ba81
5c885bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """Trace a tiled cuTile matmul with optional visualization and trace export.""" | ||
|
|
||
| import argparse | ||
| import math | ||
|
|
||
| import cuda.tile as ct | ||
| import numpy as np | ||
|
|
||
| import tilelens | ||
|
|
||
|
|
||
| TILE_M = 2 | ||
| TILE_K = 4 | ||
| TILE_N = 8 | ||
|
|
||
|
|
||
| @ct.kernel | ||
| def matmul_kernel(lhs, rhs, result): | ||
| """Compute matrix multiplication one output tile at a time. | ||
|
|
||
| Args: | ||
| lhs: Left-hand input with shape [M, K]. | ||
| rhs: Right-hand input with shape [K, N]. | ||
| result: Output with shape [M, N]. | ||
| """ | ||
| _, K = lhs.shape | ||
|
|
||
| # Compute one output tile per block. | ||
| m, n = ct.bid(0), ct.bid(1) | ||
| result_tile = ct.full((TILE_M, TILE_N), 0, ct.float32) | ||
|
|
||
| # Accumulate partial sums. | ||
| for k in range(ct.cdiv(K, TILE_K)): | ||
| lhs_tile = ct.load( | ||
| lhs, | ||
| (m, k), | ||
| (TILE_M, TILE_K), | ||
| padding_mode=ct.PaddingMode.ZERO, | ||
| ) | ||
| rhs_tile = ct.load( | ||
| rhs, | ||
| (k, n), | ||
| (TILE_K, TILE_N), | ||
| padding_mode=ct.PaddingMode.ZERO, | ||
| ) | ||
| result_tile = result_tile + ct.matmul(lhs_tile, rhs_tile) | ||
|
|
||
| ct.store(result, (m, n), result_tile) | ||
|
|
||
|
|
||
| def _run_demo(): | ||
| parser = argparse.ArgumentParser(description="Trace a cuTile matmul on the CPU") | ||
| parser.add_argument( | ||
| "--visualize", action="store_true", help="Open the local visualizer" | ||
| ) | ||
| parser.add_argument("--save", metavar="PATH", help="Save the trace as a .tvz file") | ||
| args = parser.parse_args() | ||
| M = 4 | ||
| K = 8 | ||
| N = 16 | ||
|
|
||
| lhs = np.arange(M * K, dtype=np.float32).reshape(M, K) | ||
| rhs = np.arange(K * N, dtype=np.float32).reshape(K, N) | ||
| result = np.empty((M, N), dtype=np.float32) | ||
| kernel_grid = (math.ceil(M / TILE_M), math.ceil(N / TILE_N)) | ||
|
|
||
| print("Executing matmul_kernel with the cuTile NumPy interpreter...") | ||
| traced_kernel = tilelens.trace("tracer", frontend="cutile")(matmul_kernel) | ||
| traced_kernel[kernel_grid](lhs, rhs, result) | ||
|
|
||
| expected = lhs @ rhs | ||
| print(np.max(np.abs(expected - result))) | ||
| assert np.allclose(expected, result) | ||
| if args.save: | ||
| tilelens.save(args.save) | ||
| if args.visualize: | ||
| tilelens.launch(share=False) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| _run_demo() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ dependencies = [ | |
| "flask_cloudflared", | ||
| "tqdm", | ||
| "ml_dtypes", | ||
| "cuda-tile>=1.0.0,<2", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [blocking] The code already treats cuTile as optional ( |
||
| ] | ||
|
|
||
| [tool.setuptools.packages.find] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A README note is easy to miss, and non-contiguous inputs do more than look off:
[::2]input highlights 0, 2, 4, 6 instead of 0–3.tilelens.saveraises.A runtime warning, or rejecting non-contiguous arrays in the cuTile trace path, would be safer. Related: the visualizer derives element size from the dtype name, so cuTile
bool_arrays (and int16/fp8 arrays) are treated as 4-byte and highlight the wrong cells.