TensorTonic
Problems
Tiled Matrix Multiplication in Triton
Linear AlgebraGPU Programming
Medium

Implement a tiled matrix multiplication kernel in Triton. Given AAA of shape (M,K)(M, K)(M,K) and BBB of shape (K,N)(K, N)(K,N), compute C=A⋅BC = A \cdot BC=A⋅B of shape (M,N)(M, N)(M,N):

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]

This is the canonical 2D-tile parallel pattern. Each program owns a BLOCK_M×BLOCK_N\texttt{BLOCK\_M} \times \texttt{BLOCK\_N}BLOCK_M×BLOCK_N output tile and walks the shared KKK dimension in chunks, accumulating partial products in an fp32 register tile before storing the result.

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

Use a 2D launch grid sized (⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉)(\lceil M / \text{BLOCK\_M} \rceil, \lceil N / \text{BLOCK\_N} \rceil)(⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉). Each program reads its two program IDs to locate its output tile.

Hint 2

Initialize the accumulator with tl.zeros((BLOCK_M,BLOCK_N),dtype=tl.float32)\texttt{tl.zeros}((\text{BLOCK\_M}, \text{BLOCK\_N}), \text{dtype}=\texttt{tl.float32})tl.zeros((BLOCK_M,BLOCK_N),dtype=tl.float32), loop over KKK in steps of BLOCK_K\text{BLOCK\_K}BLOCK_K, and cast to the output dtype only on the final store.

Hint 3

Use tl.dot\texttt{tl.dot}tl.dot on the loaded AAA and BBB tiles. The mask on the KKK tail prevents partial products from leaking past the last full chunk.

Requirements

  • Use one Triton program per output tile on a 2D grid
  • Walk the KKK dimension in tile-sized chunks, accumulating in fp32
  • Mask the MMM, NNN, and KKK tails so non-power-of-two shapes are correct
  • The solve\texttt{solve}solve signature must not change
  • use ieee precision for consistent float32 accuracy across gpus.

Constraints

  • 1≤M,N,K≤5121 \le M, N, K \le 5121≤M,N,K≤512.
  • 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
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 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.

Tiled Matrix Multiplication in Triton

Tiled Matrix Multiplication in Triton

Linear AlgebraGPU Programming
Medium

Implement a tiled matrix multiplication kernel in Triton. Given AAA of shape (M,K)(M, K)(M,K) and BBB of shape (K,N)(K, N)(K,N), compute C=A⋅BC = A \cdot BC=A⋅B of shape (M,N)(M, N)(M,N):

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]

This is the canonical 2D-tile parallel pattern. Each program owns a BLOCK_M×BLOCK_N\texttt{BLOCK\_M} \times \texttt{BLOCK\_N}BLOCK_M×BLOCK_N output tile and walks the shared KKK dimension in chunks, accumulating partial products in an fp32 register tile before storing the result.

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

Use a 2D launch grid sized (⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉)(\lceil M / \text{BLOCK\_M} \rceil, \lceil N / \text{BLOCK\_N} \rceil)(⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉). Each program reads its two program IDs to locate its output tile.

Hint 2

Initialize the accumulator with tl.zeros((BLOCK_M,BLOCK_N),dtype=tl.float32)\texttt{tl.zeros}((\text{BLOCK\_M}, \text{BLOCK\_N}), \text{dtype}=\texttt{tl.float32})tl.zeros((BLOCK_M,BLOCK_N),dtype=tl.float32), loop over KKK in steps of BLOCK_K\text{BLOCK\_K}BLOCK_K, and cast to the output dtype only on the final store.

Hint 3

Use tl.dot\texttt{tl.dot}tl.dot on the loaded AAA and BBB tiles. The mask on the KKK tail prevents partial products from leaking past the last full chunk.

Requirements

  • Use one Triton program per output tile on a 2D grid
  • Walk the KKK dimension in tile-sized chunks, accumulating in fp32
  • Mask the MMM, NNN, and KKK tails so non-power-of-two shapes are correct
  • The solve\texttt{solve}solve signature must not change
  • use ieee precision for consistent float32 accuracy across gpus.

Constraints

  • 1≤M,N,K≤5121 \le M, N, K \le 5121≤M,N,K≤512.
  • 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
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 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