TensorTonic
Problems
Fused Matmul + Bias + ReLU in Triton
Activation FunctionsLinear AlgebraGPU Programming
Medium

Implement a fused linear-layer epilogue in Triton: matmul, add a per-column bias, apply ReLU, all in one kernel. Given AAA of shape (M,K)(M, K)(M,K), BBB of shape (K,N)(K, N)(K,N), and bias\text{bias}bias of shape (N,)(N,)(N,):

out[i,j]=max⁡ ⁣(0,  ∑k=0K−1A[i,k]⋅B[k,j]+bias[j])\texttt{out}[i, j] = \max\!\left(0, \; \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j] + \text{bias}[j]\right)out[i,j]=max(0,k=0∑K−1​A[i,k]⋅B[k,j]+bias[j])

The epilogue is applied in registers on the accumulator tile, before the store. This avoids the round-trip to global memory that an unfused implementation would incur, and is the simplest concrete example of why fused kernels dominate transformer inference workloads.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[1, 0], [0, 1]], bias = [0, -10]

Output: [[1, 0], [3, 0]]

Input: A = [[1, -1], [-1, 1]], B = [[1, 1], [1, 1]], bias = [1, 1]

Output: [[1, 1], [1, 1]]

Hint 1

Start from the plain tiled matmul template. The fusion lives in the few lines between the K-loop and the final store.

Hint 2

Load the bias vector once per program for the columns of the output tile and broadcast it across the row axis when adding to the accumulator.

Hint 3

ReLU is a branch-free elementwise max against zero on the accumulator. Apply it after the bias add and before the store.

Requirements

  • Reuse the tiled matmul structure: 2D grid, K-loop, fp32 accumulator
  • Apply the bias add and ReLU in registers on the accumulator, before storing
  • Mask the loads and stores on 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≤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.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumTiled Matrix Multiplication in TritonMediumFused Tiled Matmul and ReLUHardScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasy

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Fused Matmul + Bias + ReLU in Triton

Fused Matmul + Bias + ReLU in Triton

Activation FunctionsLinear AlgebraGPU Programming
Medium

Implement a fused linear-layer epilogue in Triton: matmul, add a per-column bias, apply ReLU, all in one kernel. Given AAA of shape (M,K)(M, K)(M,K), BBB of shape (K,N)(K, N)(K,N), and bias\text{bias}bias of shape (N,)(N,)(N,):

out[i,j]=max⁡ ⁣(0,  ∑k=0K−1A[i,k]⋅B[k,j]+bias[j])\texttt{out}[i, j] = \max\!\left(0, \; \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j] + \text{bias}[j]\right)out[i,j]=max(0,k=0∑K−1​A[i,k]⋅B[k,j]+bias[j])

The epilogue is applied in registers on the accumulator tile, before the store. This avoids the round-trip to global memory that an unfused implementation would incur, and is the simplest concrete example of why fused kernels dominate transformer inference workloads.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[1, 0], [0, 1]], bias = [0, -10]

Output: [[1, 0], [3, 0]]

Input: A = [[1, -1], [-1, 1]], B = [[1, 1], [1, 1]], bias = [1, 1]

Output: [[1, 1], [1, 1]]

Hint 1

Start from the plain tiled matmul template. The fusion lives in the few lines between the K-loop and the final store.

Hint 2

Load the bias vector once per program for the columns of the output tile and broadcast it across the row axis when adding to the accumulator.

Hint 3

ReLU is a branch-free elementwise max against zero on the accumulator. Apply it after the bias add and before the store.

Requirements

  • Reuse the tiled matmul structure: 2D grid, K-loop, fp32 accumulator
  • Apply the bias add and ReLU in registers on the accumulator, before storing
  • Mask the loads and stores on 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≤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.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumGEMV: Matrix Vector Product in TritonMediumTiled Matrix Multiplication in TritonMediumFused Tiled Matmul and ReLUHardScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasy

Accepts: array

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