Triton SiLU (Swish) Kernel
Problem Statement
Implement the SiLU / Swish activation out = x * sigmoid(x).
Background
Triton provides tl.sigmoid. SiLU is used in modern transformer MLP blocks.
Your Task
Implement silu_kernel and run(n=1024) comparing to torch.nn.functional.silu.
How it is tested
Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.
Example
n = 1024
True
- The input value
n = 1024determines the size of the input array for the SiLU kernel implementation. - The
silu_kernelfunction calculates the SiLU activation using the formula , where is the sigmoid function, for each element in the input array. - The result from the
silu_kernelfunction is compared to the result fromtorch.nn.functional.siluusingtorch.allcloseto check for numerical equivalence within a certain tolerance. - The comparison yields
Trueif the results are close enough, indicating that the customsilu_kernelimplementation matches the PyTorch reference implementation.
Constraints
- out = x * tl.sigmoid(x)
- Mask the tail block