-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Fix type promotion for trace on CPU. #47305
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8268813
Fix type promotion for trace on CPU.
gchanan 6e47233
Update on "Fix type promotion for trace on CPU."
gchanan 8665d70
Update on "Fix type promotion for trace on CPU."
gchanan f06fa57
Update on "Fix type promotion for trace on CPU."
gchanan 848af86
Update on "Fix type promotion for trace on CPU."
gchanan 07ea4f0
Update on "Fix type promotion for trace on CPU."
gchanan f72d684
Update on "Fix type promotion for trace on CPU."
gchanan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/native/ReduceOps.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/ATen.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/AccumulateType.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/ExpandUtils.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/NativeFunctions.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <ATen/WrapDimUtils.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -473,6 +474,40 @@ static Tensor& prod_out_impl(Tensor& result, const Tensor& self, IntArrayRef dim | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // NOTE: this could be implemented via diag and sum, but this has perf problems, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // see https://github.com/pytorch/pytorch/pull/47305, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tensor trace_cpu(const Tensor& self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tensor result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ScalarType dtype = get_dtype(result, self, c10::nullopt, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = at::empty({}, self.options().dtype(dtype)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AT_DISPATCH_ALL_TYPES(self.scalar_type(), "trace", [&] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using accscalar_t = at::acc_type<scalar_t, false>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accscalar_t sum = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto* t_data = self.data_ptr<scalar_t>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int64_t t_stride_0, t_stride_1, t_diag_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TORCH_CHECK(self.dim() == 2, "trace: expected a matrix, but got tensor with dim ", self.dim()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t_stride_0 = self.stride(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t_stride_1 = self.stride(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t_diag_size = std::min(self.size(0), self.size(1)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int64_t i = 0; i < t_diag_size; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sum += t_data[i * (t_stride_0 + t_stride_1)]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // all integer types get promoted to kLong | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (result.scalar_type() == at::kLong) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *result.data_ptr<int64_t>() = sum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *result.data_ptr<scalar_t>() = sum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. We're going to have to update test_trace: Lines 5996 to 6029 in 774b638
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tensor prod(const Tensor& self, int64_t dim, bool keepdim, c10::optional<ScalarType> dtype) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tensor result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| native::prod_out_impl(result, self, dim, keepdim, dtype); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: The best place to put this might be TriangularOps.cpp (trace_cuda is in TriangularOps.cu). Alternatively we can move trace_cuda out of TriangularOps.cu for consistency
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.
the problem is you need the specialized promotion logic from here. In theory you could split things up but this doesn't seem worth it given the promotion logic is really specialized for reductions.