-
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
feat: add custom float precision inspect option #1286
Conversation
@@ -129,4 +131,56 @@ defmodule Nx.TensorTest do | |||
end | |||
end | |||
end | |||
|
|||
describe "inspect" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a-alhusaini This PR will introduce a paliative solution for this. The printing format (and maybe the actual option) will change in the future, but it should solve your problem in the short-term.
The changes below contain some examples on the inspected tensors in the "prints with configured precision" test.
For usage in IEx, you can use IEx.configure(inspect: [custom_options: [nx_precision: 5]])
for example.
nx/lib/nx/backend.ex
Outdated
# convert number to float | ||
n = if is_float(n), do: n, else: n * 1.0 | ||
|
||
str = float_to_string(n, precision) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code means that, if precision is enabled, 1+2i
will always be printed as 1.0+2.0i
.
nx/lib/nx/backend.ex
Outdated
re_str = complex_to_string_real(re, precision) | ||
im_str = complex_to_string_component(im, precision) | ||
|
||
"#{re_str}#{im_str}i" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some duplication here. Could this instead be written as:
re_str = complex_to_string_real(re, precision) | |
im_str = complex_to_string_component(im, precision) | |
"#{re_str}#{im_str}i" | |
re_str = inspect_value(re, precision) | |
case inspect_value(im, precision) do | |
"-" <> _ = im_str -> re_str <> im_str <> "i" | |
"+" <> _ = im_str -> re_str <> im_str <> "i" | |
im_str -> re_str <> "+" <> im_str <> "i" | |
end |
The second clause may not be necessary.
nx/lib/nx/backend.ex
Outdated
defp inspect_value(float, precision) when is_float(float) and is_integer(precision) do | ||
float_to_string(float, precision) | ||
end | ||
|
||
defp inspect_value(float, nil) when is_float(float), do: Float.to_string(float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick. For consistency with complex_to_string, move the precision handling inside?
defp inspect_value(float, precision) when is_float(float) and is_integer(precision) do | |
float_to_string(float, precision) | |
end | |
defp inspect_value(float, nil) when is_float(float), do: Float.to_string(float) | |
defp inspect_value(float, precision) when is_float(float), do: float_to_string(float, precision) |
nx/lib/nx.ex
Outdated
@@ -6133,7 +6133,7 @@ defmodule Nx do | |||
#Nx.Tensor< | |||
f64[2][2] | |||
[ | |||
[-3.141592653589793, -0.0], | |||
[-3.141592653589793, 0.0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was intentional, for consistency with the custom handling of -0.0 in complex_to_string
relates to #1285