Skip to content

Stop using c10::scalar_to_tensor in float_power.#50105

Closed
gchanan wants to merge 6 commits intogh/gchanan/348/basefrom
gh/gchanan/348/head
Closed

Stop using c10::scalar_to_tensor in float_power.#50105
gchanan wants to merge 6 commits intogh/gchanan/348/basefrom
gh/gchanan/348/head

Conversation

@gchanan
Copy link
Copy Markdown
Contributor

@gchanan gchanan commented Jan 5, 2021

Stack from ghstack:

There should be no functional change here.

A couple of reasons here:

  1. This function is generally an anti-pattern (c10::scalar_to_tensor(...) uses should be audited for performance and type promotion impact #49758) and it is good to minimize its usage in the code base.
  2. pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: D25786172

There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.
@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Jan 5, 2021

💊 CI failures summary and remediations

As of commit 6ee0044 (more details on the Dr. CI page):


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

This comment has been revised 54 times.

gchanan added a commit that referenced this pull request Jan 5, 2021
There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

ghstack-source-id: 3222bdf
Pull Request resolved: #50105
There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: [D25786172](https://our.internmc.facebook.com/intern/diff/D25786172)
There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: [D25786172](https://our.internmc.facebook.com/intern/diff/D25786172)
@gchanan gchanan requested a review from mruberry January 5, 2021 22:36
Comment thread aten/src/ATen/native/Pow.cpp Outdated
Tensor& float_power_out(Tensor& result, const Tensor& base, Scalar exp) {
return at::float_power_out(result, base, c10::scalar_to_tensor(exp, base.device()));
auto dtype = (at::isComplexType(base.scalar_type()) || exp.isComplex()) ? at::kComplexDouble : at::kDouble;
exp = dtype == at::kComplexDouble ? exp.toComplexDouble() : exp.toDouble();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Parens after the "=" might clarify this expression

Comment thread aten/src/ATen/native/Pow.cpp Outdated
return base.float_power_(c10::scalar_to_tensor(exp, base.device()));
auto dtype = (at::isComplexType(base.scalar_type()) || exp.isComplex()) ? at::kComplexDouble : at::kDouble;
TORCH_CHECK(base.scalar_type() == dtype,
"self tensor type ", base.scalar_type(), "is not the desired type ", dtype);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We're inconsistent in our checks (note: we should pick a style an be consistent) but typically want to mention the operation and use the name in the docs, not "self" which is typically an internal name. Also, "tensor type" is ambiguous and this should refer to dtype.

Maybe an error message like:

"the base given to float_power_ has dtype DTYPE, but the operation's result requires dtype DTYPE"

?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note also there's not a space between the first base.scalar_type() and "is in the string. A similar printing error can be seen in the error messages below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is this even correct behavior? Don't we generally check that the computation type is castable to the result type, not that they are the same?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good point. I extended the Developer FAQ entry to address this explicitly (see here).

So yes, a corrected version of the check would validate that the operation's common dtype is "safe castable" to base's dtype.

Copy link
Copy Markdown
Collaborator

@mruberry mruberry Jan 6, 2021

Choose a reason for hiding this comment

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

In this case, delegating to the out= variant of float_power here would probably save adding special logic for this case. That logic needs to be added to the out= variant (per other comments), anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, since this is a different issue, I filed #50213.

@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Jan 6, 2021

While many of the failures here are in upstream, some are real:

test_float_power_constant_cpu - TestAutogradDeviceTypeCPU
test_autograd.py

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 284, in instantiated_test
    raise rte
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 279, in instantiated_test
    result = test_fn(self, *args)
  File "test_autograd.py", line 5206, in do_test
    check(name)
  File "test_autograd.py", line 5177, in check
    **kwargs_variable))
RuntimeError: result type ComplexDoublecan't be cast to the desired output type Double

test_float_power_constant_cuda - TestAutogradDeviceTypeCUDA
test_autograd.py

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 889, in wrapper
    method(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 889, in wrapper
    method(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 284, in instantiated_test
    raise rte
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 279, in instantiated_test
    result = test_fn(self, *args)
  File "test_autograd.py", line 5206, in do_test
    check(name)
  File "test_autograd.py", line 5177, in check
    **kwargs_variable))
RuntimeError: result type ComplexDoublecan't be cast to the desired output type Double

test_float_power_constant_cpu - test_jit.TestJitGeneratedAutogradCPU
test_jit.py

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 284, in instantiated_test
    raise rte
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 279, in instantiated_test
    result = test_fn(self, *args)
  File "/var/lib/jenkins/workspace/test/test_jit.py", line 15849, in do_test
    check(inplace_name)
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/jit_utils.py", line 646, in wrapper
    fn(*args, **kwargs)
  File "/var/lib/jenkins/workspace/test/test_jit.py", line 15842, in check
    check_alias_annotation(name, (self_variable,) + args_variable, kwargs_variable, aten_name=name)
  File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/jit_metaprogramming_utils.py", line 477, in check_alias_annotation
    torch._C._jit_check_alias_annotation(CU.the_method.graph, tuple(tensors), aten_name)
RuntimeError: result type ComplexDoublecan't be cast to the desired output type Double

@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Jan 6, 2021

The error message, with its incorrect spacing, comes from:

TORCH_CHECK(at::can_cast(common_dtype, result.scalar_type()),

@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Jan 6, 2021

It's not obvious to me where this failure is coming from.

We could change float_power to use an OpInfo if there's a test issue.

Comment thread aten/src/ATen/native/Pow.cpp Outdated
auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ? at::kComplexDouble : at::kDouble;
return at::pow(base.to(dtype), exp.to(dtype));
auto dtype = (at::isComplexType(exp.scalar_type()) || base.isComplex()) ? at::kComplexDouble : at::kDouble;
base = dtype == at::kComplexDouble ? base.toComplexDouble() : base.toDouble();
Copy link
Copy Markdown
Collaborator

@mruberry mruberry Jan 6, 2021

Choose a reason for hiding this comment

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

Ideally we'd add an output checking helper (or maybe one exists that we can reuse) here (and in the above _out variant). Otherwise if result has the wrong dtype or the wrong device type the error will be throw in pow, and will not correctly identify float_power as the source of the issue. This could be confusing to users.

There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: [D25786172](https://our.internmc.facebook.com/intern/diff/D25786172)
There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: [D25786172](https://our.internmc.facebook.com/intern/diff/D25786172)
There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Differential Revision: [D25786172](https://our.internmc.facebook.com/intern/diff/D25786172)
@mruberry mruberry self-requested a review January 8, 2021 00:30
Copy link
Copy Markdown
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

LGTM!

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@gchanan merged this pull request in 88bd69b.

@facebook-github-bot facebook-github-bot deleted the gh/gchanan/348/head branch January 12, 2021 15:14
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this pull request Jan 14, 2021
Summary:
Pull Request resolved: pytorch#50105

There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (pytorch#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Test Plan: Imported from OSS

Reviewed By: mruberry

Differential Revision: D25786172

Pulled By: gchanan

fbshipit-source-id: 89de03aa0b900ce011a62911224a5441f15e331a
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#50105

There should be no functional change here.

A couple of reasons here:
1) This function is generally an anti-pattern (pytorch#49758) and it is good to minimize its usage in the code base.
2) pow itself has a fair amount of smarts like not broadcasting scalar/tensor combinations and we should defer to it.

Test Plan: Imported from OSS

Reviewed By: mruberry

Differential Revision: D25786172

Pulled By: gchanan

fbshipit-source-id: 89de03aa0b900ce011a62911224a5441f15e331a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants