Skip to content
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

Merged
merged 7 commits into from
Aug 29, 2023

Conversation

polvalente
Copy link
Contributor

relates to #1285

@@ -129,4 +131,56 @@ defmodule Nx.TensorTest do
end
end
end

describe "inspect" do
Copy link
Contributor Author

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 Show resolved Hide resolved
Comment on lines 322 to 325
# convert number to float
n = if is_float(n), do: n, else: n * 1.0

str = float_to_string(n, precision)
Copy link
Collaborator

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.

Comment on lines 292 to 295
re_str = complex_to_string_real(re, precision)
im_str = complex_to_string_component(im, precision)

"#{re_str}#{im_str}i"
Copy link
Collaborator

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:

Suggested change
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.

Comment on lines 252 to 256
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)
Copy link
Collaborator

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?

Suggested change
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],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change correct?

Copy link
Contributor Author

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

@polvalente polvalente merged commit e082b06 into main Aug 29, 2023
9 checks passed
@polvalente polvalente deleted the pv-feat/add-custom-precision-option branch August 29, 2023 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants