TensorTonic
Problems
SiLU in Triton
Activation FunctionsGPU Programming
Easy

Implement the SiLU (sigmoid-weighted linear unit, also called Swish) activation in Triton. For each input element:

out[i]=x[i]⋅σ(x[i])=x[i]1+e−x[i]\texttt{out}[i] = x[i] \cdot \sigma(x[i]) = \frac{x[i]}{1 + e^{-x[i]}}out[i]=x[i]⋅σ(x[i])=1+e−x[i]x[i]​

SiLU is the activation in LLaMA, Mistral, and most modern Transformer feedforward gates. It is the simplest activation that involves exp⁡\expexp: one exponential, one reciprocal, one multiply. The kernel structure is the same as the other foundation maps; the new beat is calling tl.exp\texttt{tl.exp}tl.exp inside a tile.

Loading visualization...

Examples

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

Output: [0.0, 0.7311, -0.2689, 1.7616]

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

Output: [-0.2384, 0.3112, 2.8577]

Hint 1

SiLU is x⋅σ(x)x \cdot \sigma(x)x⋅σ(x) which equals x/(1+e−x)x / (1 + e^{-x})x/(1+e−x). The second form is one tl.exp\texttt{tl.exp}tl.exp, one add, one divide.

Hint 2

The exponent has a negative sign: tl.exp(−x)\texttt{tl.exp}(-x)tl.exp(−x), not tl.exp(x)\texttt{tl.exp}(x)tl.exp(x). The sign is what gives the sigmoid its shape.

Hint 3

Same tail-mask discipline as vector add. No reduction, no shared memory, one fused expression per lane.

Requirements

  • Use one Triton program per contiguous tile of the input
  • Compute x/(1+e−x)x / (1 + e^{-x})x/(1+e−x) in a single fused expression
  • 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.
  • ∣x[i]∣≤50|x[i]| \le 50∣x[i]∣≤50 in all test inputs, so e−xe^{-x}e−x stays representable.
  • Tolerance: atol=1e-3 against torch.nn.functional.silu.
Try Similar Problems
GELU in TritonEasyReLU 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.

SiLU in Triton

SiLU in Triton

Activation FunctionsGPU Programming
Easy

Implement the SiLU (sigmoid-weighted linear unit, also called Swish) activation in Triton. For each input element:

out[i]=x[i]⋅σ(x[i])=x[i]1+e−x[i]\texttt{out}[i] = x[i] \cdot \sigma(x[i]) = \frac{x[i]}{1 + e^{-x[i]}}out[i]=x[i]⋅σ(x[i])=1+e−x[i]x[i]​

SiLU is the activation in LLaMA, Mistral, and most modern Transformer feedforward gates. It is the simplest activation that involves exp⁡\expexp: one exponential, one reciprocal, one multiply. The kernel structure is the same as the other foundation maps; the new beat is calling tl.exp\texttt{tl.exp}tl.exp inside a tile.

Loading visualization...

Examples

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

Output: [0.0, 0.7311, -0.2689, 1.7616]

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

Output: [-0.2384, 0.3112, 2.8577]

Hint 1

SiLU is x⋅σ(x)x \cdot \sigma(x)x⋅σ(x) which equals x/(1+e−x)x / (1 + e^{-x})x/(1+e−x). The second form is one tl.exp\texttt{tl.exp}tl.exp, one add, one divide.

Hint 2

The exponent has a negative sign: tl.exp(−x)\texttt{tl.exp}(-x)tl.exp(−x), not tl.exp(x)\texttt{tl.exp}(x)tl.exp(x). The sign is what gives the sigmoid its shape.

Hint 3

Same tail-mask discipline as vector add. No reduction, no shared memory, one fused expression per lane.

Requirements

  • Use one Triton program per contiguous tile of the input
  • Compute x/(1+e−x)x / (1 + e^{-x})x/(1+e−x) in a single fused expression
  • 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.
  • ∣x[i]∣≤50|x[i]| \le 50∣x[i]∣≤50 in all test inputs, so e−xe^{-x}e−x stays representable.
  • Tolerance: atol=1e-3 against torch.nn.functional.silu.
Try Similar Problems
GELU in TritonEasyReLU 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