Skip to content

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

Description

@AryanSharma48

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$.

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

  • I have read and understood the Code of Conduct.
  • I have searched for existing issues and pull requests.
  • The issue title begins with RFC: .

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions