Implement GeLU (exact form) in PyTorch using primitive tensor ops only.
Signature: def gelu(x: torch.Tensor) -> torch.Tensor
The rule: you may NOT call F.gelu, nn.GELU, or torch.nn.functional.gelu. We verify your output matches F.gelu(x) (the exact erf form, the PyTorch default).
Allowed primitives: torch.special.erf, basic arithmetic, math.sqrt.
Formula (exact):
GeLU(x) = 0.5 * x * (1 + erf(x / sqrt(2)))
PyTorch idioms vs the NumPy version:
from scipy.special import erf. In PyTorch, the same function lives at torch.special.erf — note the .special namespace, mirroring SciPy. There is no top-level torch.erf shortcut for the version you want; torch.erf exists but torch.special.erf is the canonical name and matches SciPy semantics exactly.math.sqrt(2) (a Python float) rather than torch.sqrt(torch.tensor(2.0)) — the math module gives a constant the JIT compiler can fold.Math
Related problems
Asked at
import numpy as np
def gelu(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?