Skip to content

[RFC]: add native float32 support for ML loss functions and activation primitives #16124

Open

Description

@AryanSharma48

Description

This RFC proposes adding native single-precision (float32) support for the core machine learning loss functions and activation primitives.

Currently, the lack of float32 packages forces the engine to cast Float32Array buffers up to float64 to compute mathematical operations before casting back down. This doubles memory bandwidth usage and hinders SIMD vectorization. These proposed packages rely on standard algebraic arithmetic and can be efficiently implemented via single-precision C kernels without requiring new polynomial derivations.

Core ML Loss Functions (Forward & Gradients)

ML Activations

  • math/base/special/siluf
  • math/base/special/geluf
  • math/base/special/fast/gelu-tanhf
  • math/base/special/fast/gelu-quickf

Prerequisites

The above ML pipeline relies on the following core single-precision math primitives, which are already being tracked in the #649:

  • math/base/special/erfcf (Required for geluf)
  • math/base/special/expitf (Required for siluf and gelu-tanhf)
  • math/base/special/log1pexpf (Required for log loss functions)

Related Issues

None.

Questions

None.

Other

No.

Checklist

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

Activity

  1. added
    RFC
    Feature
    Math
    Machine Learning
    Accepted
    JavaScript
    C
    on Oct 20, 2026
  2. Harsh28-raj commented on Oct 29, 2026

    @Harsh28-raj

    Hi @AryanSharma48, I'd like to help with this RFC. Would it be okay if I took ml/base/loss/float32/squared-error and ml/base/loss/float32/squared-hinge? I'll make sure not to overlap with anything you're already working on.

  3. AryanSharma48 commented on Oct 29, 2026

    @AryanSharma48
    ContributorAuthor

    Go ahead @Harsh28-raj! You can use these for references #16016 #16093

  4. Harsh28-raj commented on Oct 30, 2026

    @Harsh28-raj

    Thanks @AryanSharma48 ! Quick update: since squared-error and squared-hinge didn't have float64 implementations to port from when I started, I began with ml/base/loss/float32/squared-epsilon-insensitive-gradient instead, which wasn't claimed in the list above: #16358. I'll use #16016 and #16093 as references for squared-error and squared-hinge next.

    @kgryte whenever you get a chance, #16358 is ready for review. Thanks!

  5. devxsubh commented on Nov 11, 2026

    @devxsubh
    Contributor

    Hi @AryanSharma48, I'm taking ml/base/loss/float32/log-gradient, which isn't claimed in the list above. I'll port it from ml/base/loss/float64/log-gradient (#12241) using expf, with the C implementation, following the layout of #16358, and link the PR here.

  6. PRIYANKA-NEGI-28 commented on Nov 21, 2026

    @PRIYANKA-NEGI-28

    Hi @AryanSharma48 — I'd like to contribute an unclaimed package from the checklist: ml/base/loss/float32/hinge (starting with one focused PR following the layout of #16358 and #16275).

    Before I start scaffolding the JS + C implementation and tests, two quick checks:

    1. Is ml/base/loss/float32/hinge still unclaimed?
    2. For the IEEE-754 float32 edge cases (specifically NaN propagation in max(0.0f, 1.0f - y*yhat), ±Inf, and -0.0f), I plan to explicitly guard isnanf(y) || isnanf(yhat) in C and isnan(y) || isnan(yhat) in JS so NaN never collapses to 0.0, and verify JS/C parity against float32 fixtures.

    Let me know if there's any recent template/convention change since #16358 I should follow!

  7. AryanSharma48 commented on Nov 21, 2026

    @AryanSharma48
    ContributorAuthor

    Hey @PRIYANKA-NEGI-28!

    1. Yes, ml/base/loss/float32/hinge is unclaimed. However, I'd hold off on opening a PR for non-gradient loss functions for now, since the reference implementations tracked in [RFC]: Add batch machine learning algorithms in Javascript and C (tracking issue) #12875 haven't been reviewed or merged yet. I'll bring this up in the next office hours.
    2. Your edge-case plan looks solid. Two quick notes for JS/C parity in float32:
      • In JS, use isnanf instead of isnan, and wrap inputs and intermediate arithmetic with f32 so rounding matches C.
      • Watch out for 0 * ±Inf producing NaN in y * yhat, which your NaN handling after or alongside the multiplication should cover cleanly.
    3. No major template change for hinge specifically, you can also check feat: add ml/base/loss/float64/hinge #11953 as a reference.

    Feel free to start planning or prototyping locally in the meantime, and ping me anytime if you want to discuss!

  8. PRIYANKA-NEGI-28 commented on Nov 21, 2026

    @PRIYANKA-NEGI-28

    Thanks @AryanSharma48, that's really helpful! I'll start exploring the prerequisites in #649 — particularly expitf, erfcf, and log1pexpf — while waiting for the office-hours update on #12875. I'll verify their status and prepare a local implementation plan before proceeding.

  9. Sharman1661 commented on Nov 26, 2026

    @Sharman1661
    Contributor

    Hey @AryanSharma48 , I'd like to contribute to this issue. I'll work on ml/base/loss/float32/huber, I see it hasn't been claimed yet. I'll make a PR regarding the same soon.
    Thanks!

  10. AryanSharma48 commented on Nov 26, 2026

    @AryanSharma48
    ContributorAuthor

    @kgryte Quick question on runner.jl fixture generation for the ml/base/loss/float32/* and float32/*-gradient packages:

    In log1pexpf (#16235), we quantized inputs to Float32 in runner.jl, promoted them to Float64 to evaluate the reference formula without intermediate Float32 rounding drift, and cast e = f32( expected[ i ] ) in the tests with a 1 ULP tolerance.

    For the float32 loss/gradient packages here:

    1. Algebraic functions (hinge, squared-hinge, squared-error, huber, modified-huber, epsilon-insensitive, squared-epsilon-insensitive and their gradients): Should runner.jl evaluate directly in Float32 (matching the step-by-step f32(...) / float arithmetic at 0 ULP), or evaluate in Float64 on Float32-quantized inputs and test with e = f32( expected[ i ] ) at 1 ULP?
    2. Transcendental functions (float32/log and float32/log-gradient): Should these use the Float64-reference approach (1 ULP) like log1pexpf to avoid intermediate Float32 exp / log1p rounding drift in the fixtures?
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

    AcceptedCFeatureJavaScriptMachine LearningMathRFC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions