TensorTonic
Problems
GELU in Triton
Activation FunctionsGPU Programming
Easy

Implement the exact GELU (Gaussian Error Linear Unit) activation in Triton. For each input element:

out[i]=12 x[i] (1+erf ⁣(x[i]2))\texttt{out}[i] = \tfrac{1}{2} \, x[i] \, \Big(1 + \mathrm{erf}\!\left(\tfrac{x[i]}{\sqrt{2}}\right)\Big)out[i]=21​x[i](1+erf(2​x[i]​))

GELU is the default activation in Transformer feedforward blocks. The exact form needs the Gauss error function erf\mathrm{erf}erf, which Triton exposes through a device intrinsic. This problem introduces calling transcendental intrinsics from inside a Triton kernel.

Loading visualization...

Examples

Input: x = [0.0, 1.0, -1.0, 2.0]

Output: [0.0, 0.8413, -0.1587, 1.9545]

Input: x = [-2.0, 0.5, 3.0]

Output: [-0.0455, 0.3457, 2.9960]

Hint 1

Triton exposes the error function as tl.math.erf\texttt{tl.math.erf}tl.math.erf (on older releases via tl.libdevice.erf\texttt{tl.libdevice.erf}tl.libdevice.erf). Apply it elementwise on the loaded tile.

Hint 2

Pre-multiply by 1/2≈0.70710678118654751/\sqrt{2} \approx 0.70710678118654751/2​≈0.7071067811865475 instead of dividing on each lane. The compiler keeps the constant in a register.

Hint 3

The reference is torch.nn.functional.gelu\texttt{torch.nn.functional.gelu}torch.nn.functional.gelu with default (exact) approximation. Match it within atol=10−3\text{atol}=10^{-3}atol=10−3 for every test.

Requirements

  • Use one Triton program per contiguous tile of the input
  • Implement the exact form using a device intrinsic for erf\mathrm{erf}erf
  • Pre-multiply by 1/21/\sqrt{2}1/2​ rather than dividing on every lane
  • Mask the tail on every load and store
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1061 \le N \le 10^61≤N≤106.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3 against torch.nn.functional.gelu (default exact mode).
Try Similar Problems
ReLU in TritonEasySiLU in TritonEasyVector Addition in TritonEasyFused Multiply-Add in TritonEasyOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

GELU in Triton

GELU in Triton

Activation FunctionsGPU Programming
Easy

Implement the exact GELU (Gaussian Error Linear Unit) activation in Triton. For each input element:

out[i]=12 x[i] (1+erf ⁣(x[i]2))\texttt{out}[i] = \tfrac{1}{2} \, x[i] \, \Big(1 + \mathrm{erf}\!\left(\tfrac{x[i]}{\sqrt{2}}\right)\Big)out[i]=21​x[i](1+erf(2​x[i]​))

GELU is the default activation in Transformer feedforward blocks. The exact form needs the Gauss error function erf\mathrm{erf}erf, which Triton exposes through a device intrinsic. This problem introduces calling transcendental intrinsics from inside a Triton kernel.

Loading visualization...

Examples

Input: x = [0.0, 1.0, -1.0, 2.0]

Output: [0.0, 0.8413, -0.1587, 1.9545]

Input: x = [-2.0, 0.5, 3.0]

Output: [-0.0455, 0.3457, 2.9960]

Hint 1

Triton exposes the error function as tl.math.erf\texttt{tl.math.erf}tl.math.erf (on older releases via tl.libdevice.erf\texttt{tl.libdevice.erf}tl.libdevice.erf). Apply it elementwise on the loaded tile.

Hint 2

Pre-multiply by 1/2≈0.70710678118654751/\sqrt{2} \approx 0.70710678118654751/2​≈0.7071067811865475 instead of dividing on each lane. The compiler keeps the constant in a register.

Hint 3

The reference is torch.nn.functional.gelu\texttt{torch.nn.functional.gelu}torch.nn.functional.gelu with default (exact) approximation. Match it within atol=10−3\text{atol}=10^{-3}atol=10−3 for every test.

Requirements

  • Use one Triton program per contiguous tile of the input
  • Implement the exact form using a device intrinsic for erf\mathrm{erf}erf
  • Pre-multiply by 1/21/\sqrt{2}1/2​ rather than dividing on every lane
  • Mask the tail on every load and store
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1061 \le N \le 10^61≤N≤106.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3 against torch.nn.functional.gelu (default exact mode).
Try Similar Problems
ReLU in TritonEasySiLU in TritonEasyVector Addition in TritonEasyFused Multiply-Add in TritonEasyOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1