|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test script to demonstrate CUTE kernel caching""" |
| 3 | + |
| 4 | +import torch |
| 5 | +from operator import add, mul |
| 6 | +import cutlass |
| 7 | +import cutlass.cute as cute |
| 8 | +from cutlass.cute.runtime import from_dlpack |
| 9 | + |
| 10 | +from transformer_nuggets.cute import cute_compile_and_cache, get_cache_stats, clear_cute_cache |
| 11 | + |
| 12 | + |
| 13 | +@cute.kernel |
| 14 | +def simple_elementwise_kernel( |
| 15 | + op: cutlass.Constexpr, |
| 16 | + gA: cute.Tensor, |
| 17 | + gB: cute.Tensor, |
| 18 | + gC: cute.Tensor, |
| 19 | +): |
| 20 | + tidx, _, _ = cute.arch.thread_idx() |
| 21 | + bidx, _, _ = cute.arch.block_idx() |
| 22 | + bdim, _, _ = cute.arch.block_dim() |
| 23 | + |
| 24 | + thread_idx = bidx * bdim + tidx |
| 25 | + |
| 26 | + m, n = gA.shape |
| 27 | + ni = thread_idx % n |
| 28 | + mi = thread_idx // n |
| 29 | + |
| 30 | + a_val = gA[mi, ni] |
| 31 | + b_val = gB[mi, ni] |
| 32 | + |
| 33 | + gC[mi, ni] = op(a_val, b_val) |
| 34 | + |
| 35 | + |
| 36 | +def cached_elementwise( |
| 37 | + op: cutlass.Constexpr, |
| 38 | + mA: cute.Tensor, |
| 39 | + mB: cute.Tensor, |
| 40 | + mC: cute.Tensor, |
| 41 | +): |
| 42 | + # Define the kernel with @cute.jit |
| 43 | + @cute.jit |
| 44 | + def kernel(op, mA, mB, mC): |
| 45 | + num_threads_per_block = 256 |
| 46 | + m, n = mA.shape |
| 47 | + |
| 48 | + kernel_op = simple_elementwise_kernel(op, mA, mB, mC) |
| 49 | + kernel_op.launch( |
| 50 | + grid=((m * n) // num_threads_per_block, 1, 1), block=(num_threads_per_block, 1, 1) |
| 51 | + ) |
| 52 | + |
| 53 | + # Use explicit caching |
| 54 | + compiled_kernel = cute_compile_and_cache(kernel, op, mA, mB, mC) |
| 55 | + return compiled_kernel(op, mA, mB, mC) |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + print("CUTE Kernel Caching Demo") |
| 60 | + print("=" * 50) |
| 61 | + |
| 62 | + # Create test tensors |
| 63 | + M, N = 1024, 1024 |
| 64 | + a = torch.randn(M, N, device="cuda", dtype=torch.float16) |
| 65 | + b = torch.randn(M, N, device="cuda", dtype=torch.float16) |
| 66 | + c = torch.zeros(M, N, device="cuda", dtype=torch.float16) |
| 67 | + |
| 68 | + # Convert to CUTE tensors |
| 69 | + a_ = from_dlpack(a, assumed_align=16) |
| 70 | + b_ = from_dlpack(b, assumed_align=16) |
| 71 | + c_ = from_dlpack(c, assumed_align=16) |
| 72 | + |
| 73 | + print("\n1. First call with 'add' - should compile:") |
| 74 | + cached_elementwise(add, a_, b_, c_) |
| 75 | + torch.testing.assert_close(c, a + b) |
| 76 | + print("✓ Addition result correct") |
| 77 | + |
| 78 | + print("\n2. Second call with 'add' - should hit cache:") |
| 79 | + c.zero_() |
| 80 | + cached_elementwise(add, a_, b_, c_) |
| 81 | + |
| 82 | + print("\n3. First call with 'mul' - should compile (different op):") |
| 83 | + c.zero_() |
| 84 | + cached_elementwise(mul, a_, b_, c_) |
| 85 | + torch.testing.assert_close(c, a * b) |
| 86 | + print("✓ Multiplication result correct") |
| 87 | + |
| 88 | + print("\n4. Different tensor sizes - should compile:") |
| 89 | + a2 = torch.randn(512, 512, device="cuda", dtype=torch.float16) |
| 90 | + b2 = torch.randn(512, 512, device="cuda", dtype=torch.float16) |
| 91 | + c2 = torch.zeros(512, 512, device="cuda", dtype=torch.float16) |
| 92 | + |
| 93 | + a2_ = from_dlpack(a2, assumed_align=16) |
| 94 | + b2_ = from_dlpack(b2, assumed_align=16) |
| 95 | + c2_ = from_dlpack(c2, assumed_align=16) |
| 96 | + |
| 97 | + cached_elementwise(add, a2_, b2_, c2_) |
| 98 | + |
| 99 | + print("\n5. Same size as #4 - should hit cache:") |
| 100 | + cached_elementwise(add, a2_, b2_, c2_) |
| 101 | + |
| 102 | + print("\n" + "=" * 50) |
| 103 | + print("Final Cache Statistics:") |
| 104 | + stats = get_cache_stats() |
| 105 | + print(f" Total calls: {stats['total']}") |
| 106 | + print(f" Cache hits: {stats['hits']}") |
| 107 | + print(f" Cache misses: {stats['misses']}") |
| 108 | + print(f" Hit rate: {stats['hit_rate']:.2%}") |
| 109 | + print(f" Unique kernels cached: {stats['cache_size']}") |
| 110 | + |
| 111 | + print("\nClearing cache...") |
| 112 | + clear_cute_cache() |
| 113 | + stats = get_cache_stats() |
| 114 | + print(f" Cache size after clear: {stats['cache_size']}") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments