Repository navigation
Open
Labels
AcceptedRFC feature request which has been accepted.CIssue involves or relates to C.FeatureIssue or pull request for adding a new feature.JavaScriptIssue involves or relates to JavaScript.Machine LearningIssue or pull request specific to machine learning functionality.MathIssue or pull request specific to math functionality.RFCRequest for comments. Feature requests and proposed changes.
Description
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-gradientfeat: addml/base/loss/float32/epsilon-insensitive-gradient#16317 -
ml/base/loss/float32/hinge -
ml/base/loss/float32/hinge-gradientfeat: addml/base/loss/float32/hinge-gradient#16275 -
ml/base/loss/float32/huber -
ml/base/loss/float32/huber-gradientfeat: addml/base/loss/float32/huber-gradient#16289 -
ml/base/loss/float32/log -
ml/base/loss/float32/log-gradientfeat: addml/base/loss/float32/log-gradient#16594 -
ml/base/loss/float32/modified-huber -
ml/base/loss/float32/modified-huber-gradientfeat: addml/base/loss/float32/modified-huber-gradient#16313 -
ml/base/loss/float32/squared-epsilon-insensitive -
ml/base/loss/float32/squared-epsilon-insensitive-gradientfeat: add ml/base/loss/float32/squared-epsilon-insensitive-gradient #16358 -
ml/base/loss/float32/squared-error -
ml/base/loss/float32/squared-error-gradientfeat: addml/base/loss/float32/squared-error-gradient#16287 -
ml/base/loss/float32/squared-hinge -
ml/base/loss/float32/squared-hinge-gradientfeat: addml/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
- I have read and understood the Code of Conduct.
- Searched for existing issues and pull requests.
- The issue name begins with
RFC:.
Metadata
Metadata
Assignees
Labels
AcceptedRFC feature request which has been accepted.CIssue involves or relates to C.FeatureIssue or pull request for adding a new feature.JavaScriptIssue involves or relates to JavaScript.Machine LearningIssue or pull request specific to machine learning functionality.MathIssue or pull request specific to math functionality.RFCRequest for comments. Feature requests and proposed changes.
Activity
Harsh28-raj commentedon Oct 29, 2026
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.
AryanSharma48 commentedon Oct 29, 2026
Go ahead @Harsh28-raj! You can use these for references #16016 #16093
Harsh28-raj commentedon Oct 30, 2026
Thanks @AryanSharma48 ! Quick update: since
squared-errorandsquared-hingedidn't have float64 implementations to port from when I started, I began withml/base/loss/float32/squared-epsilon-insensitive-gradientinstead, which wasn't claimed in the list above: #16358. I'll use #16016 and #16093 as references forsquared-errorandsquared-hingenext.@kgryte whenever you get a chance, #16358 is ready for review. Thanks!
devxsubh commentedon Nov 11, 2026
Hi @AryanSharma48, I'm taking
ml/base/loss/float32/log-gradient, which isn't claimed in the list above. I'll port it fromml/base/loss/float64/log-gradient(#12241) usingexpf, with the C implementation, following the layout of #16358, and link the PR here.PRIYANKA-NEGI-28 commentedon Nov 21, 2026
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:
ml/base/loss/float32/hingestill unclaimed?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!
AryanSharma48 commentedon Nov 21, 2026
Hey @PRIYANKA-NEGI-28!
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 [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.float32:isnanfinstead ofisnan, and wrap inputs and intermediate arithmetic withf32so rounding matches C.0 * ±InfproducingNaNiny * yhat, which yourNaNhandling after or alongside the multiplication should cover cleanly.hingespecifically, you can also check feat: addml/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!
PRIYANKA-NEGI-28 commentedon Nov 21, 2026
Thanks @AryanSharma48, that's really helpful! I'll start exploring the prerequisites in #649 — particularly
expitf,erfcf, andlog1pexpf— while waiting for the office-hours update on #12875. I'll verify their status and prepare a local implementation plan before proceeding.Sharman1661 commentedon Nov 26, 2026
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!
AryanSharma48 commentedon Nov 26, 2026
@kgryte Quick question on
runner.jlfixture generation for theml/base/loss/float32/*andfloat32/*-gradientpackages:In
log1pexpf(#16235), we quantized inputs toFloat32inrunner.jl, promoted them toFloat64to evaluate the reference formula without intermediateFloat32rounding drift, and caste = f32( expected[ i ] )in the tests with a1ULP tolerance.For the
float32loss/gradient packages here:hinge,squared-hinge,squared-error,huber,modified-huber,epsilon-insensitive,squared-epsilon-insensitiveand their gradients): Shouldrunner.jlevaluate directly inFloat32(matching the step-by-stepf32(...)/floatarithmetic at0ULP), or evaluate inFloat64onFloat32-quantized inputs and test withe = f32( expected[ i ] )at1ULP?float32/logandfloat32/log-gradient): Should these use theFloat64-reference approach (1ULP) likelog1pexpfto avoid intermediateFloat32exp/log1prounding drift in the fixtures?