[RFC]: Add modern scalar ML activation functions (GELU, SiLU) to `@stdlib/math/base/special`
Description
As @stdlib continues to expand its machine learning and statistical primitives, the repository currently provides classical scalar activations like expit (Sigmoid), relu, and log1pexp (Softplus). However, it is missing the standard activation functions that power modern deep learning architectures.
To support developers building JS-based ML inference engines natively in @stdlib, I propose adding GELU and SiLU to the @stdlib/math/base/special namespace.
Motivation:
- GELU (Gaussian Error Linear Unit): The de facto standard activation function for modern Transformer architectures (GPT, BERT, Vision Transformers).
- SiLU (Sigmoid Linear Unit / Swish): Heavily utilized in modern vision models (YOLO, EfficientNet) and modern LLMs (LLaMA).
Proposed Solution: I propose creating two new purely scalar mathematical packages:
1. @stdlib/math/base/special/gelu
- Formula: $x \cdot \Phi(x) = \frac{x}{2} \left[ 1 + \text{erf}\left( x \cdot \sqrt{0.5} \right) \right]$
- Implementation: Pure JavaScript scalar composition relying on the existing
@stdlib/math/base/special/erfand@stdlib/constants/float64/sqrt-half.
2. @stdlib/math/base/special/silu
- Formula: $x \cdot \sigma(x) = \frac{x}{1 + e^{-x}}$
- Implementation: Pure JavaScript scalar composition relying on the existing
@stdlib/math/base/special/expit.
Edge Case Handling (IEEE 754 Compliance):
Both implementations will strictly adhere to standard @stdlib floating-point handling:
NaNinputs will returnNaN(validated via@stdlib/math/base/assert/is-nan).- $-\infty$ inputs will explicitly return
0.0to avoid JavaScript's default behavior where $-\infty \times 0.0$ evaluates toNaN. - $+\infty$ inputs will return $+\infty$.
Tracking
@stdlib/math/base/special/gelu: #15754@stdlib/math/base/special/silu: #16219@stdlib/math/base/special/fast/gelu-tanh: #16174@stdlib/math/base/special/fast/gelu-quick: #16180
Related Issues
No.
Questions
No.
Other
Prior Art:
- PyTorch:
torch.nn.GELUandtorch.nn.SiLU - TensorFlow:
tf.keras.activations.geluandtf.keras.activations.swish
Implementation Notes:
This RFC strictly covers the exact mathematical definitions for the primary namespace. Fast approximations (e.g., the standard tanh approximation for GELU) can be addressed in separate, future PRs targeting math/base/special/fast/gelu to keep this initial scope tight.
If this proposal is accepted, I am ready to handle the implementations, generate the test fixtures, and write the JSDoc documentation for both packages.
Checklist
- [x] I have read and understood the Code of Conduct.
- [x] I have searched for existing issues and pull requests.
- [x] The issue title begins with
RFC:.
In general, not opposed. However, one thing I would like to know is whether gelu and silu have other conventional names.
@kgryte To check the standard conventions, I mainly compared how PyTorch, TensorFlow, and JAX handle these.
For GELU, there aren't really any other conventional names. It is universally referred to as gelu across all three major libraries and in the academic literature.
For SiLU, it does have one major alternative name: Swish. Looking at how the major frameworks handle the split:
- PyTorch: Natively uses
SiLU. - TensorFlow: Uses
swish. - JAX: Natively uses
silu(and providesswishas a direct alias).
I leaned towards silu because "Sigmoid Linear Unit" describes the actual mathematical composition of the function, which seemed to align better with @stdlib's naming philosophy than the branded "Swish" name.
I already have the local code and tests scaffolded for both, so just let me know which naming convention you prefer for the namespace and I can open the PRs!
@anbv29 I suggest finding other "Good First Issues" to work on, and not this one.
Following up on this after office hours on 13th Oct 26! We aligned on the following namespace architecture, so I will proceed with this structure:
- GELU Split: The implementations will be separated to preserve the pure math namespace.
@stdlib/math/base/special/gelu: Exact evaluation (implemented directly viaerfcto avoid negative-tail cancellation).@stdlib/math/base/special/fast/gelu-tanh: The algebraically stable $2 \cdot \text{expit}(2z)$ approximation.@stdlib/math/base/special/fast/gelu-quick: The 1.702 sigmoidal approximation.
- SiLU Naming: Will implement strictly as
@stdlib/math/base/special/siluwithout Swish aliases to adhere to the repository's strict naming conventions.
I'll start scaffolding these directories and getting the initial PRs ready!
All the PRs are done, ready for review when you have the bandwidth @kgryte!