TensorTonic
Problems
Block-Pointer Matmul in Triton
Linear AlgebraGPU Programming
Medium

Reimplement tiled matmul using the block-pointer abstraction instead of raw pointer arithmetic. For each output entry:

C[i,j]=∑k=0K−1A[i,k]⋅B[k,j]C[i, j] = \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j]C[i,j]=k=0∑K−1​A[i,k]⋅B[k,j]

The arithmetic is unchanged. The differentiator is the pointer abstraction. The standard tile matmul builds 2D pointer tensors by hand and uses explicit per-axis masks. \texttt{tl.make_block_ptr} packages the base, shape, strides, current offsets, block shape, and traversal order into a single descriptor, and tl.advance\texttt{tl.advance}tl.advance walks it along the KKK axis.

Why it matters: the compiler sees the full tensor descriptor and can lower it directly to TMA descriptors on Hopper, emit out-of-bounds masking implicitly via boundary_check\texttt{boundary\_check}boundary_check, and pick a layout that matches the tl.dot\texttt{tl.dot}tl.dot MMA tiles. The user-side code becomes shorter and the generated PTX tends to be cleaner because the bounds reasoning is centralized.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]

Output: [[19, 22], [43, 50]]

Input: A = [[1, 0, 1], [0, 1, 0]], B = [[1, 2], [3, 4], [5, 6]]

Output: [[6, 8], [3, 4]]

Hint 1

Build a_block_ptr\texttt{a\_block\_ptr}a_block_ptr with shape=(M,K)\texttt{shape}=(M, K)shape=(M,K), strides=(stride_am,stride_ak)\texttt{strides}=(\text{stride\_am}, \text{stride\_ak})strides=(stride_am,stride_ak), offsets=(pid_m⋅BLOCK_M,0)\texttt{offsets}=(\text{pid\_m} \cdot \text{BLOCK\_M}, 0)offsets=(pid_m⋅BLOCK_M,0), and block_shape=(BLOCK_M,BLOCK_K)\texttt{block\_shape}=(\text{BLOCK\_M}, \text{BLOCK\_K})block_shape=(BLOCK_M,BLOCK_K). Mirror it for BBB and CCC.

Hint 2

Inside the KKK-loop, advance the block pointers by the KKK-block size instead of recomputing offsets. tl.advance\texttt{tl.advance}tl.advance returns a new block pointer at the next position.

Hint 3

With boundary_check\texttt{boundary\_check}boundary_check on each load and the store, no manual MMM or NNN mask is needed - the block-pointer mechanism handles partial tiles implicitly.

Requirements

  • Build AAA, BBB, and CCC tile pointers with \texttt{tl.make_block_ptr}
  • Walk the KKK dimension with tl.advance\texttt{tl.advance}tl.advance once per iteration
  • Use boundary_check\texttt{boundary\_check}boundary_check on every load and store so the block-pointer mechanism handles the MMM, NNN, and KKK tails
  • The solve\texttt{solve}solve signature must not change
  • use ieee precision for consistent float32 accuracy across gpus.

Constraints

  • 1≤M,N,K≤7681 \le M, N, K \le 7681≤M,N,K≤768.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-2 + rtol=1e-2 * abs(expected) for K≥128K \ge 128K≥128 to absorb fp32 accumulation drift.
Try Similar Problems
Split-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHardScaled Dot-Product Attention in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumVector Addition in TritonEasyFused Multiply-Add in TritonEasyL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Block-Pointer Matmul in Triton

Block-Pointer Matmul in Triton

Linear AlgebraGPU Programming
Medium

Reimplement tiled matmul using the block-pointer abstraction instead of raw pointer arithmetic. For each output entry:

C[i,j]=∑k=0K−1A[i,k]⋅B[k,j]C[i, j] = \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j]C[i,j]=k=0∑K−1​A[i,k]⋅B[k,j]

The arithmetic is unchanged. The differentiator is the pointer abstraction. The standard tile matmul builds 2D pointer tensors by hand and uses explicit per-axis masks. \texttt{tl.make_block_ptr} packages the base, shape, strides, current offsets, block shape, and traversal order into a single descriptor, and tl.advance\texttt{tl.advance}tl.advance walks it along the KKK axis.

Why it matters: the compiler sees the full tensor descriptor and can lower it directly to TMA descriptors on Hopper, emit out-of-bounds masking implicitly via boundary_check\texttt{boundary\_check}boundary_check, and pick a layout that matches the tl.dot\texttt{tl.dot}tl.dot MMA tiles. The user-side code becomes shorter and the generated PTX tends to be cleaner because the bounds reasoning is centralized.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]

Output: [[19, 22], [43, 50]]

Input: A = [[1, 0, 1], [0, 1, 0]], B = [[1, 2], [3, 4], [5, 6]]

Output: [[6, 8], [3, 4]]

Hint 1

Build a_block_ptr\texttt{a\_block\_ptr}a_block_ptr with shape=(M,K)\texttt{shape}=(M, K)shape=(M,K), strides=(stride_am,stride_ak)\texttt{strides}=(\text{stride\_am}, \text{stride\_ak})strides=(stride_am,stride_ak), offsets=(pid_m⋅BLOCK_M,0)\texttt{offsets}=(\text{pid\_m} \cdot \text{BLOCK\_M}, 0)offsets=(pid_m⋅BLOCK_M,0), and block_shape=(BLOCK_M,BLOCK_K)\texttt{block\_shape}=(\text{BLOCK\_M}, \text{BLOCK\_K})block_shape=(BLOCK_M,BLOCK_K). Mirror it for BBB and CCC.

Hint 2

Inside the KKK-loop, advance the block pointers by the KKK-block size instead of recomputing offsets. tl.advance\texttt{tl.advance}tl.advance returns a new block pointer at the next position.

Hint 3

With boundary_check\texttt{boundary\_check}boundary_check on each load and the store, no manual MMM or NNN mask is needed - the block-pointer mechanism handles partial tiles implicitly.

Requirements

  • Build AAA, BBB, and CCC tile pointers with \texttt{tl.make_block_ptr}
  • Walk the KKK dimension with tl.advance\texttt{tl.advance}tl.advance once per iteration
  • Use boundary_check\texttt{boundary\_check}boundary_check on every load and store so the block-pointer mechanism handles the MMM, NNN, and KKK tails
  • The solve\texttt{solve}solve signature must not change
  • use ieee precision for consistent float32 accuracy across gpus.

Constraints

  • 1≤M,N,K≤7681 \le M, N, K \le 7681≤M,N,K≤768.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-2 + rtol=1e-2 * abs(expected) for K≥128K \ge 128K≥128 to absorb fp32 accumulation drift.
Try Similar Problems
Split-K Matmul in TritonHardGrouped Program-ID Matmul in TritonHardScaled Dot-Product Attention in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumVector Addition in TritonEasyFused Multiply-Add in TritonEasyL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1