[RFC]: add native float32 support for ML loss functions and activation primitives
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 forgeluf) - [ ]
math/base/special/expitf(Required forsilufandgelu-tanhf) - [ ]
math/base/special/log1pexpf(Required forlogloss 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:.
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.
Go ahead @Harsh28-raj! You can use these for references #16016 #16093
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!
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.
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:
- Is
ml/base/loss/float32/hingestill unclaimed? - For the IEEE-754
float32edge cases (specificallyNaNpropagation inmax(0.0f, 1.0f - y*yhat),±Inf, and-0.0f), I plan to explicitly guardisnanf(y) || isnanf(yhat)in C andisnan(y) || isnan(yhat)in JS soNaNnever collapses to0.0, and verify JS/C parity againstfloat32fixtures.
Let me know if there's any recent template/convention change since #16358 I should follow!
Hey @PRIYANKA-NEGI-28!
- Yes,
ml/base/loss/float32/hingeis 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. - Your edge-case plan looks solid. Two quick notes for JS/C parity in
float32:- In JS, use
isnanfinstead ofisnan, and wrap inputs and intermediate arithmetic withf32so rounding matches C. - Watch out for
0 * ±InfproducingNaNiny * yhat, which yourNaNhandling after or alongside the multiplication should cover cleanly.
- In JS, use
- No major template change for
hingespecifically, 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!
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.