Tensara Logo

tensara

ProblemsLeaderboardsBlog

GELU

EASY

Perform the GELU (Gaussian Error Linear Unit) activation function on an input matrix:

C[i][j]=GELU(A[i][j])C[i][j] = \text{GELU}(A[i][j])

The GELU function is defined as:

GELU(x)=xΦ(x)\text{GELU}(x) = x \cdot \Phi(x)

where Φ(x)\Phi(x) is the cumulative distribution function of the standard normal distribution.

A common approximation for GELU is:

GELU(x)0.5x(1+tanh(2/π(x+0.044715x3)))\text{GELU}(x) \approx 0.5x \cdot (1 + \tanh(\sqrt{2/\pi} \cdot (x + 0.044715x^3)))

Input:

  • Matrix AA of size M×NM \times N containing floating-point values

Output:

  • Matrix CC of size M×NM \times N containing the GELU activation values

Notes:

  • Both matrices A\text{A} and C\text{C} are stored in row-major order
  • You should implement the approximation formula for GELU defined above
  • GELU is commonly used in modern transformer-based neural networks like BERT and GPT

GPU Type

Language

Data Type

Loading...

Desktop Required for Code Submission

For the best coding experience, please switch to a desktop device to write and submit your solution.