-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Pre)Proposal: wasi-math #587
Comments
It is possible to add math functions to WASI. It's even possible to add them to Preview 2. I expect the main challenge is to find a clear scope that can be sufficiently motivated. General-purpose CPUs today typically don't have hardware acceleration for trig/exp/log/etc. functions. x86 does have sin/cos/tan instructions, however it turns out that they're slower than just computing those functions in software. Consequently, the apparent advantage here is just toolchain convenience. There are decently high-quality open-source implementations of things like trigonometric and exp/log/pow functions available. It is less convenient for toolchains to integrate them, however many toolchains today are able to do this. And in return, there are significant advantages to having toolchains do this. Today's JavaScript engines typically don't implement correct rounding (and indeed, are not required by the spec to do so), so results will often differ in the last digit or so. And, the performance can vary significantly between platforms. Having toolchains bundle their own implementations avoids both of these problems. And, for some applications, today's JavaScript engines' trig functions compute to a level of precision that is greater than needed, and as a result are slower than they'd need to be. Having toolchains provide their own algorithms gives them the ability to make their own speed/precision tradeoffs, and in a way that's deterministic for a compiled Wasm. And it doesn't require Wasm or WASI to include a specification for reduced-precision math, which can be pretty tricky. Naive attempts can break important symmetries like In summary, what's needed here is for someone to propose a specific scope for what should be included, what the precision and determinism requirements should look like, and what the performance expectations should be. |
Regarding the balance between accuracy and speed, many languages have a mode called The following options are available: Keep bothJust like ShortcomingThere is only one correct answer, but there are many approximate answers, each with different trade-offs and application scenarios. In fact, there may not be a Moreover, Fallback MechanismWe don’t know yet how this mechanism will work, and it may add unnecessary complexity. Only keep high-precision versionPractical factorsAlthough the accuracy provided by js is already excessive in most cases, when considering applications such as physical simulation (such as three-body motion simulation demo), a high-precision correct version is still needed. Size factorCompared with the fast, accuracy-sacrifice version, the correct, high-precision algorithm will require a lot more binary code. So I think letting the host bear this cost is a better trade-off. If users need |
I have a few thoughts relating to this: What should a math library include: Does it really make sense to have this be a wasi interface: |
My consideration is mainly two application scenarios
No matter which solution is adopted, it makes sense to write a set of interfaces with wit first. I defined some of my own wit libraries here, all functions are divided into f32 and f64 versions. |
A potentially controversial point is whether to include bigint. My main considerations are:
What is less certain is whether there will be a From the perspective of polyfill, I suggest adding this interface to make it compatible with old wasm devices. By the way, I think wasi polyfill will be the norm for quite a long time It is convenient to use jco map to do this, I now use the following rules: wasi:*=@bytecodealliance/preview2-shim/*
wasi:math/*=@valkyrie-language/math-shim/*
wasi:webgpu/*=@valkyrie-language/webgpu-shim/*
valkyrie:image/*=@valkyrie-language/image-shim/*
valkyrie:dom/*=@valkyrie-language/dom-shim/* |
assuming you meant that cryptography is dependent on bigint. bigint implementations for cryptography are quite different than bigint implementations for general arithmetic, since they have to use different often-slower algorithms to prevent timing side-channels (leaking the secret information you're processing by being able to observe how long it takes over the network). |
I would like to make some comments here: Is it necessary to develop wasi-math?
Motivation
Many people want to use some slightly complex mathematical functions in wasm, but their high-performance implementation is actually not simple, especially in wasm.
So I'm wondering if it's possible to add math functions to the next stage of wasi (in preview3)?
In this case, the host platform can directly use hardware acceleration on platforms that contain corresponding instructions, and use programming implementation on platforms that do not support it.
Goals(MVP)
The initial goal is to achieve the same computing power as Javascript Math.
Including abilities such as large integers, trigonometric functions, inverse trigonometric functions, exponential logarithms, etc.
Non-Goals
To be discussed
The text was updated successfully, but these errors were encountered: