-
Notifications
You must be signed in to change notification settings - Fork 565
Allow mean return nan for 0dim #996
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#include "torch_xla/csrc/reduction.h" | ||
|
||
#include <cmath> | ||
|
||
#include "tensorflow/compiler/xla/client/lib/arithmetic.h" | ||
#include "tensorflow/compiler/xla/literal_util.h" | ||
#include "tensorflow/compiler/xla/xla_client/debug_macros.h" | ||
|
@@ -31,7 +33,6 @@ ReductionInfo GetReductionInfo( | |
rinfo.new_dimensions.push_back(shape.dimensions(i)); | ||
} | ||
} | ||
XLA_CHECK_GT(rinfo.element_count, 0); | ||
return rinfo; | ||
} | ||
|
||
|
@@ -73,10 +74,11 @@ xla::XlaOp CreateSummation( | |
xla::XlaOp result = xla::Reduce( | ||
input, init_value, XlaHelpers::CreateAddComputation(shape.element_type()), | ||
dimensions); | ||
if (scale && rinfo.element_count > 1) { | ||
if (scale) { | ||
xla::XlaOp scale = XlaHelpers::ScalarValue<float>( | ||
1.0f / static_cast<float>(rinfo.element_count), shape.element_type(), | ||
input.builder()); | ||
rinfo.element_count > 0 ? 1.0f / static_cast<float>(rinfo.element_count) | ||
: NAN, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I know it compiles. But relies on some other include to pull the proper include, which is wrong. |
||
shape.element_type(), input.builder()); | ||
result = xla::Mul(result, scale); | ||
} | ||
if (keep_reduced_dimensions) { | ||
|
@@ -146,6 +148,7 @@ xla::XlaOp BuildMaxInDim(const xla::XlaOp& input, xla::int64 dim, | |
xla::XlaOp init_value = XlaHelpers::ScalarValue( | ||
min_max.min, shape.element_type(), input.builder()); | ||
ReductionInfo rinfo = GetReductionInfo(shape, {dim}, keep_reduced_dimensions); | ||
XLA_CHECK_GT(rinfo.element_count, 0); | ||
xla::XlaOp result = xla::Reduce( | ||
input, init_value, XlaHelpers::CreateMaxComputation(shape.element_type()), | ||
{dim}); | ||
|
@@ -162,6 +165,7 @@ xla::XlaOp BuildMinInDim(const xla::XlaOp& input, xla::int64 dim, | |
xla::XlaOp init_value = XlaHelpers::ScalarValue( | ||
min_max.max, shape.element_type(), input.builder()); | ||
ReductionInfo rinfo = GetReductionInfo(shape, {dim}, keep_reduced_dimensions); | ||
XLA_CHECK_GT(rinfo.element_count, 0); | ||
xla::XlaOp result = xla::Reduce( | ||
input, init_value, XlaHelpers::CreateMinComputation(shape.element_type()), | ||
{dim}); | ||
|
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.
Are you sure?
Coming in with count==0 means division by zero below...
Uh oh!
There was an error while loading. Please reload this page.
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.
yea
nan
is the expected output for this case: in the sense that numpy and pytorch both return nan...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.
What about the line:
?
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.
sorry I didn't get this - what's the issue about that line? It produces a nan which is then multiplied to result.
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.
Division by zero is Undefined Behavior, and many times depends on compiler options (whether to SIGFPE or not).
So better not rely on it returning NaN.
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.
ehhh what's a better way to get nan in this case?
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.
Not sure what the most reliable way of construction NaN in XLA. I assume
ConstantR0
with a NaN float value should work.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.
ScalarValue already does that.
I am not sure how XLA "feels" about NaN uploaded to TPU device. We can check.
In any case, that should be: