|
42 | 42 | } |
43 | 43 |
|
44 | 44 | TRITON_EXAMPLE_TEMPLATES = {"default": "See main prompt for example structure."} |
| 45 | + |
| 46 | +CUTEDSL_KERNEL_PROMPT = """Generate a CuteDSL kernel for: {op_name} |
| 47 | +
|
| 48 | +Operation: {op_signature} |
| 49 | +{op_description} |
| 50 | +
|
| 51 | +Requirements: |
| 52 | +- CuteDSL kernel function MUST be named: {op_name}_cutedsl_kernel |
| 53 | +- Launcher function MUST be named: {op_name}_kernel_launch |
| 54 | +- Wrapper function MUST be named: {op_name}_kernel_impl |
| 55 | +- Use modern CuteDSL syntax with proper grid computation |
| 56 | +- Include all necessary imports (torch, cutlass, cutlass.cute as cute) |
| 57 | +
|
| 58 | +The {op_name}_kernel_impl wrapper function MUST handle complete device management: |
| 59 | +- Move CPU tensors to GPU if needed (use .cuda() when torch.cuda.is_available()) |
| 60 | +- Raise clear errors if CUDA is not available for GPU tensors |
| 61 | +- Call the CuteDSL kernel with GPU tensors |
| 62 | +- Move results back to original device of input tensors |
| 63 | +- Handle both args and kwargs properly |
| 64 | +- Preserve original tensor devices and restore them for outputs |
| 65 | +- Avoid falling back to PyTorch implementation |
| 66 | +- Avoid using try except block |
| 67 | +
|
| 68 | +Generate complete, runnable code only - no framework will add device handling wrapper code. |
| 69 | +
|
| 70 | +Example: |
| 71 | +{example} |
| 72 | +""" |
| 73 | + |
| 74 | +CUTEDSL_OPTIMIZATIONS = { |
| 75 | + "default": "Use efficient memory access patterns and appropriate block sizes." |
| 76 | +} |
| 77 | + |
| 78 | +CUTEDSL_EXAMPLE_TEMPLATES = { |
| 79 | + "default": """import torch |
| 80 | +import cutlass |
| 81 | +import cutlass.cute as cute |
| 82 | +from cutlass.cute.runtime import from_dlpack |
| 83 | +
|
| 84 | +@cute.kernel |
| 85 | +def add_tensor_kernel( |
| 86 | + gA: cute.Tensor, |
| 87 | + gB: cute.Tensor, |
| 88 | + gC: cute.Tensor, |
| 89 | +): |
| 90 | + tidx, _, _ = cute.arch.thread_idx() |
| 91 | + bidx, _, _ = cute.arch.block_idx() |
| 92 | + bdim, _, _ = cute.arch.block_dim() |
| 93 | +
|
| 94 | + thread_idx = bidx * bdim + tidx |
| 95 | +
|
| 96 | + # Map thread index to logical index of input tensor |
| 97 | + total_elements = gA.shape[0] |
| 98 | + |
| 99 | + # Bounds checking |
| 100 | + if thread_idx < total_elements: |
| 101 | +
|
| 102 | + # Map logical index to physical address via tensor layout |
| 103 | + a_val = gA[thread_idx] |
| 104 | + b_val = gB[thread_idx] |
| 105 | +
|
| 106 | + # Perform element-wise addition |
| 107 | + gC[thread_idx] = a_val + b_val |
| 108 | +
|
| 109 | +@cute.kernel |
| 110 | +def add_scalar_kernel( |
| 111 | + gA: cute.Tensor, |
| 112 | + gC: cute.Tensor, |
| 113 | + scalar_val, |
| 114 | +): |
| 115 | + tidx, _, _ = cute.arch.thread_idx() |
| 116 | + bidx, _, _ = cute.arch.block_idx() |
| 117 | + bdim, _, _ = cute.arch.block_dim() |
| 118 | +
|
| 119 | + thread_idx = bidx * bdim + tidx |
| 120 | +
|
| 121 | + # Map thread index to logical index of input tensor |
| 122 | + total_elements = gA.shape[0] |
| 123 | + |
| 124 | + # Bounds checking |
| 125 | + if thread_idx < total_elements: |
| 126 | +
|
| 127 | + # Map logical index to physical address via tensor layout |
| 128 | + a_val = gA[thread_idx] |
| 129 | +
|
| 130 | + # Perform element-wise addition with scalar |
| 131 | + gC[thread_idx] = a_val + scalar_val |
| 132 | +
|
| 133 | +@cute.jit |
| 134 | +def add_tensor_kernel_launch( |
| 135 | + mA: cute.Tensor, |
| 136 | + mB: cute.Tensor, |
| 137 | + mC: cute.Tensor |
| 138 | +): |
| 139 | + num_threads_per_block = 1024 |
| 140 | +
|
| 141 | + total_elements = mA.shape[0] |
| 142 | + num_blocks = (total_elements + num_threads_per_block - 1) // num_threads_per_block |
| 143 | + |
| 144 | + kernel = add_tensor_kernel(mA, mB, mC) |
| 145 | + kernel.launch(grid=(num_blocks, 1, 1), |
| 146 | + block=(num_threads_per_block, 1, 1)) |
| 147 | +
|
| 148 | +@cute.jit |
| 149 | +def add_scalar_kernel_launch( |
| 150 | + mA: cute.Tensor, |
| 151 | + mC: cute.Tensor, |
| 152 | + scalar_val |
| 153 | +): |
| 154 | + num_threads_per_block = 1024 |
| 155 | +
|
| 156 | + total_elements = mA.shape[0] |
| 157 | + num_blocks = (total_elements + num_threads_per_block - 1) // num_threads_per_block |
| 158 | + |
| 159 | + kernel = add_scalar_kernel(mA, mC, scalar_val) |
| 160 | + kernel.launch(grid=(num_blocks, 1, 1), |
| 161 | + block=(num_threads_per_block, 1, 1)) |
| 162 | +
|
| 163 | +def add_kernel_impl(*args, **kwargs): |
| 164 | + |
| 165 | + # Handle both positional and keyword arguments |
| 166 | + if len(args) >= 2: |
| 167 | + input_tensor = args[0] |
| 168 | + other = args[1] |
| 169 | + elif len(args) == 1 and 'other' in kwargs: |
| 170 | + input_tensor = args[0] |
| 171 | + other = kwargs['other'] |
| 172 | + elif 'input' in kwargs and 'other' in kwargs: |
| 173 | + input_tensor = kwargs['input'] |
| 174 | + other = kwargs['other'] |
| 175 | + else: |
| 176 | + raise ValueError("add requires 'input' and 'other' arguments") |
| 177 | + |
| 178 | + if torch.is_tensor(other): |
| 179 | + input_tensor, other = torch.broadcast_tensors(input_tensor, other) |
| 180 | + |
| 181 | + if 'alpha' in kwargs: |
| 182 | + alpha = kwargs['alpha'] |
| 183 | + other = other * alpha |
| 184 | + |
| 185 | + # Remember original device |
| 186 | + original_device = input_tensor.device |
| 187 | +
|
| 188 | + # Flatten all tensors and save their shapes |
| 189 | + original_shape = input_tensor.shape |
| 190 | + input_tensor = input_tensor.flatten() |
| 191 | + if torch.is_tensor(other): |
| 192 | + other = other.flatten() |
| 193 | + |
| 194 | + # Move to GPU if needed |
| 195 | + if not input_tensor.is_cuda: |
| 196 | + if not torch.cuda.is_available(): |
| 197 | + raise RuntimeError("CUDA is not available") |
| 198 | + input_tensor = input_tensor.cuda() |
| 199 | + |
| 200 | + # Check if other is a tensor or scalar |
| 201 | + if torch.is_tensor(other): |
| 202 | + # Tensor + Tensor case |
| 203 | + if not other.is_cuda: |
| 204 | + if not torch.cuda.is_available(): |
| 205 | + raise RuntimeError("CUDA is not available") |
| 206 | + other = other.cuda() |
| 207 | + |
| 208 | + output = torch.empty_like(input_tensor) |
| 209 | + a_ = from_dlpack(input_tensor) |
| 210 | + b_ = from_dlpack(other) |
| 211 | + c_ = from_dlpack(output) |
| 212 | +
|
| 213 | + add_tensor_kernel_launch_ = cute.compile(add_tensor_kernel_launch, a_, b_, c_) |
| 214 | + add_tensor_kernel_launch_(a_, b_, c_) |
| 215 | + else: |
| 216 | + # Tensor + Scalar case |
| 217 | + # Convert scalar to Python float |
| 218 | + if hasattr(other, 'item'): |
| 219 | + scalar_val = other.item() |
| 220 | + else: |
| 221 | + scalar_val = other |
| 222 | + |
| 223 | + output = torch.empty_like(input_tensor) |
| 224 | + a_ = from_dlpack(input_tensor) |
| 225 | + c_ = from_dlpack(output) |
| 226 | +
|
| 227 | + add_scalar_kernel_launch_ = cute.compile(add_scalar_kernel_launch, a_, c_, scalar_val) |
| 228 | + add_scalar_kernel_launch_(a_, c_, scalar_val) |
| 229 | + |
| 230 | + # Move result back to original device |
| 231 | + if original_device != output.device: |
| 232 | + output = output.to(original_device) |
| 233 | + |
| 234 | + output = output.reshape(original_shape) |
| 235 | + |
| 236 | + return output""" |
| 237 | +} |
0 commit comments