stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

[RFC]: Add modern scalar ML activation functions (GELU, SiLU) to `@stdlib/math/base/special`

Open AryanSharma48 opened this issue 2 months ago • 5 comments

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:

  1. GELU (Gaussian Error Linear Unit): The de facto standard activation function for modern Transformer architectures (GPT, BERT, Vision Transformers).
  2. 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/erf and @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:

  • NaN inputs will return NaN (validated via @stdlib/math/base/assert/is-nan).
  • $-\infty$ inputs will explicitly return 0.0 to avoid JavaScript's default behavior where $-\infty \times 0.0$ evaluates to NaN.
  • $+\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.GELU and torch.nn.SiLU
  • TensorFlow: tf.keras.activations.gelu and tf.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: .

AryanSharma48 avatar Sep 26 '26 23:09 AryanSharma48

In general, not opposed. However, one thing I would like to know is whether gelu and silu have other conventional names.

kgryte avatar Sep 28 '26 08:09 kgryte

@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 provides swish as 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!

AryanSharma48 avatar Sep 28 '26 08:09 AryanSharma48

@anbv29 I suggest finding other "Good First Issues" to work on, and not this one.

kgryte avatar Sep 28 '26 08:09 kgryte

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:

  1. GELU Split: The implementations will be separated to preserve the pure math namespace.
    • @stdlib/math/base/special/gelu: Exact evaluation (implemented directly via erfc to 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.
  2. SiLU Naming: Will implement strictly as @stdlib/math/base/special/silu without Swish aliases to adhere to the repository's strict naming conventions.

I'll start scaffolding these directories and getting the initial PRs ready!

AryanSharma48 avatar Oct 13 '26 22:10 AryanSharma48

All the PRs are done, ready for review when you have the bandwidth @kgryte!

AryanSharma48 avatar Oct 18 '26 10:10 AryanSharma48