stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

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

Open AryanSharma48 opened this issue 1 month ago • 7 comments

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/base/loss/float32/epsilon-insensitive
  • [ ] ml/base/loss/float32/epsilon-insensitive-gradient #16317
  • [ ] ml/base/loss/float32/hinge
  • [ ] ml/base/loss/float32/hinge-gradient #16275
  • [ ] ml/base/loss/float32/huber
  • [ ] ml/base/loss/float32/huber-gradient #16289
  • [ ] ml/base/loss/float32/log
  • [ ] ml/base/loss/float32/log-gradient
  • [ ] ml/base/loss/float32/modified-huber
  • [ ] ml/base/loss/float32/modified-huber-gradient #16313
  • [ ] ml/base/loss/float32/squared-epsilon-insensitive
  • [ ] ml/base/loss/float32/squared-epsilon-insensitive-gradient #16358
  • [ ] ml/base/loss/float32/squared-error
  • [ ] ml/base/loss/float32/squared-error-gradient #16287
  • [ ] ml/base/loss/float32/squared-hinge
  • [ ] ml/base/loss/float32/squared-hinge-gradient #16276

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

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

AryanSharma48 avatar Oct 13 '26 22:10 AryanSharma48

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.

Harsh28-raj avatar Oct 29 '26 11:10 Harsh28-raj

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

AryanSharma48 avatar Oct 29 '26 11:10 AryanSharma48

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!

Harsh28-raj avatar Oct 30 '26 04:10 Harsh28-raj

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.

devxsubh avatar Nov 11 '26 07:11 devxsubh

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!

PRIYANKA-NEGI-28 avatar Nov 21 '26 17:11 PRIYANKA-NEGI-28

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 #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 #11953 as a reference.

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

AryanSharma48 avatar Nov 21 '26 18:11 AryanSharma48

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.

PRIYANKA-NEGI-28 avatar Nov 21 '26 18:11 PRIYANKA-NEGI-28