-
Notifications
You must be signed in to change notification settings - Fork 190
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
Handling of NaN and infinity #119
Comments
One use case: dynamic scale. From TF docs:
https://www.tensorflow.org/api_docs/python/tf/mixed_precision/experimental/DynamicLossScale |
I think one option for this is to add a Obviously some functions will return infinity/NaN on certain inputs so we'll have to handle those explicitly. If the check isn't enabled, we can let it fail (I don't think this is ideal), or treat it as a no-op, unless the operation has a meaningful result for infinity/NaN. Adding support from Elixir I think we would just have to adjust some of the functions for reading/writing scalars to binaries. |
Here's MATLAB's take: https://www.mathworks.com/help/matlab/matlab_prog/infinity-and-nan.html |
And another related feature is PyTorch gradient anomaly detection: https://pytorch.org/docs/stable/autograd.html#torch.autograd.detect_anomaly Raises on any errors (such as NaN) in gradient calculations |
In my humble opinion
I think that's good idea. I haven't look at on the core of Nx so deeply yet, thus I don't know how complicated can be it in your case. In our, it was simple an implementation of IEEE754 for single and double conversion. |
Commenting here so we can keep track of this all on the same place: @dkuku found a match error in a Neural Net example which turned out to be related to Infinity. Minimal examples which fail like this on both f32 and f64: iex> Nx.tensor([1.0e32]) |> Nx.power(2) |> Nx.add(Nx.tensor([1]))
** (MatchError) no match of right hand side value: <<0, 0, 128, 127>>
(nx 0.1.0) lib/nx/binary_backend.ex:632: anonymous fn/10 in Nx.BinaryBackend.element_wise_bin_op/4
(elixir 1.13.0) lib/enum.ex:4136: Enum.reduce_range/5
(nx 0.1.0) lib/nx/binary_backend.ex:628: Nx.BinaryBackend.element_wise_bin_op/4 iex(2)> Nx.tensor([1.0e32], type: {:f, 64}) |> Nx.power(9) |> Nx.add(Nx.tensor([1.0e288]))
** (MatchError) no match of right hand side value: <<0, 0, 128, 127>>
(nx 0.1.0) lib/nx/binary_backend.ex:639: anonymous fn/10 in Nx.BinaryBackend.element_wise_bin_op/4
(elixir 1.13.0) lib/enum.ex:4136: Enum.reduce_range/5
(nx 0.1.0) lib/nx/binary_backend.ex:628: Nx.BinaryBackend.element_wise_bin_op/4 |
It's similar the error I am facing here: |
Today Nx operations fail if they find a NaN and/or Infinity (although defn behaviour will be compiler independent). Do we need to implement handling of NaN and infinity within Nx? What are the use cases?
The text was updated successfully, but these errors were encountered: