Implement the tanh approximation of GELU in a Triton kernel. For each input value x, the output is:
GELU(x)=2x(1+tanh(π2(x+0.044715x3))).Here, π is pi and tanh is the hyperbolic tangent. Complete only the empty kernel body; the supplied solve launcher is already implemented. The pointers identify the input and output buffers, n_elements is their flattened length, and BLOCK_SIZE is the compile-time number of lanes per program block. Return no value; write GELU values into the supplied output tensor with the same shape and storage dtype as the input.
Input: x = [-2, -1, 0, 1, 2], dtype = float32
Output: [-0.045402, -0.158808, 0, 0.841192, 1.954598]
Explanation: GELU scales each value independently, attenuating negative values.
Input: x = [-3, -0.5, 0, 0.25, 1, 3, 8], dtype = float16
Output: [-0.003637, -0.154297, 0, 0.149658, 0.841309, 2.996094, 8]
Input: x = [1.5], dtype = bfloat16
Output: [1.398438]
tl.program_id identifies the current program block.
tl.load and tl.store accept masks for partial blocks.
libdevice.tanh evaluates the hyperbolic tangent.
Accepts: array
Accepts: string
Submit a correct solution to see your kernel performance.
Implement the tanh approximation of GELU in a Triton kernel. For each input value x, the output is:
GELU(x)=2x(1+tanh(π2(x+0.044715x3))).Here, π is pi and tanh is the hyperbolic tangent. Complete only the empty kernel body; the supplied solve launcher is already implemented. The pointers identify the input and output buffers, n_elements is their flattened length, and BLOCK_SIZE is the compile-time number of lanes per program block. Return no value; write GELU values into the supplied output tensor with the same shape and storage dtype as the input.
Input: x = [-2, -1, 0, 1, 2], dtype = float32
Output: [-0.045402, -0.158808, 0, 0.841192, 1.954598]
Explanation: GELU scales each value independently, attenuating negative values.
Input: x = [-3, -0.5, 0, 0.25, 1, 3, 8], dtype = float16
Output: [-0.003637, -0.154297, 0, 0.149658, 0.841309, 2.996094, 8]
Input: x = [1.5], dtype = bfloat16
Output: [1.398438]
tl.program_id identifies the current program block.
tl.load and tl.store accept masks for partial blocks.
libdevice.tanh evaluates the hyperbolic tangent.
Accepts: array
Accepts: string
Submit a correct solution to see your kernel performance.