Comments (2)
One proposal that would resolve #117 (before the merge), #118, #119 and possibly allow for more flexible handling of numeric types would be to introduce an Nx.Type
behaviour and implement our current types in terms of the behaviour. Right now we use the match!
, read!
, and write!
macros in pretty much every Nx.BinaryTensor
method. A behaviour would look something like:
defmodule Nx.Type.Float16 do
@impl true
def pack(scalar), do: ...pack to binary...
@impl true
def unpack(binary), do: ...unpack to scalar...
So pack and unpack are all about encoding/decoding terms to binaries and vice versa. Then, in order to extend our current implementations to any Nx.Type
module, we'd need to override the Kernel methods to work on special scalar terms. Signed/Unsigned integers need no overrides. Floats only need to override in the case of handling NaN, -Inf, Inf, -0, +0, etc. Complex numbers need to override everything. For example, a Complex128 would unpack
into a tuple of {real term, imag term}
and then :erlang.+
, :erlang.*
, etc. would be overridden to operate on those tuples.
I think a big advantage of this approach is in flexibility and rapid prototyping of research ideas. While nobody would test/research their own floating point representations in Elixir, they could test them in a native backend (like EXLA) and then implement this behavior to get full introspection from the Elixir side of things. In addition to this, backend developers aren't hamstrung to a fixed set of types. While I'm sure our current implementation covers a pretty complete set of types, it doesn't mean something won't pop up in the future (e.g. bfloat16).
A really big disadvantage is that this approach comes with a lot of overhead initially, but I think in many ways it will keep our current implementations type-agnostic.
from nx.
I think this is a good direction. The concern is indeed the overhead. One of the ways we can tackle the overhead is by compiling those clauses at compilation time: instead of invoking the behaviour at runtime, we call it at compilation time to unpack the relevant clauses, similar to how we do it today.
Unfortunately complex numbers are a bit more complex (ha) because we need to introduce a runtime representation for them too. A struct or a two-element tuple. And we likely want to make things like Nx.tensor([some_complex_number])
to work with them out of the box too - so we will need to look at every is_number clause and rewrite them to be is_number or is_complex
.
But in a nutshell, if we do add a new type, we should probably consider moving the approach that you described. 👍
from nx.
Related Issues (20)
- Need to contact julienmarie
- Matrix not rendered properly in the docs HOT 14
- Default to non-stable sorting
- EXLA does not consider the device on EXLA.Backend operations HOT 1
- `top_k` doesn't behave properly with :neg_infinity HOT 7
- Remove take_along_axis and take HOT 5
- CTranslate2 for fast model inference? HOT 1
- Explicitly broadcast in MLIR binary operations HOT 1
- Issue with using vectorized tensor in a function
- Issue with broadcast and vectorized axes on v0.6.4 HOT 1
- Convert Nx.LinAlg.lu to optional callback HOT 2
- EXLA/MLIR: Add missing Nx.LinAlg operations HOT 2
- EXLA/MLIR: run Nx and Nx.LinAlg doctests
- Convert Nx.LinAlg.eigh to optional callback
- Convert Nx.LinAlg.qr to optional callback HOT 1
- Convert Nx.LinAlg.cholesky to optional callback
- Fix sign of zero values in Nx.ceil and Nx.conjugate HOT 1
- Add executable-level caching to Nx.Defn in EXLA HOT 1
- Nx.LinAlg.solve/2 with Nx.BinaryBackend returns tensor with wrong state HOT 6
- Nx.all_close not working with EXLA HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nx.