-
Notifications
You must be signed in to change notification settings - Fork 6k
[Cpp API Compatibility] Align some compat APIs with libtorch #78099
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <ATen/AccumulateType.h> | ||
| #include <c10/core/Scalar.h> | ||
| #include <limits> | ||
|
|
||
| namespace at::native { | ||
|
|
||
| inline void arange_check_bounds(const c10::Scalar& start, | ||
| const c10::Scalar& end, | ||
| const c10::Scalar& step) { | ||
| // use double precision for validation to avoid precision issues | ||
| double dstart = start.to<double>(); | ||
| double dend = end.to<double>(); | ||
| double dstep = step.to<double>(); | ||
|
|
||
| TORCH_CHECK(dstep > 0 || dstep < 0, "step must be nonzero"); | ||
| TORCH_CHECK(std::isfinite(dstart) && std::isfinite(dend), | ||
| "unsupported range: ", | ||
| dstart, | ||
| " -> ", | ||
| dend); | ||
| TORCH_CHECK( | ||
| ((dstep > 0) && (dend >= dstart)) || ((dstep < 0) && (dend <= dstart)), | ||
| "upper bound and lower bound inconsistent with step sign"); | ||
| } | ||
|
|
||
| template <typename scalar_t> | ||
| int64_t compute_arange_size(const Scalar& start, | ||
| const Scalar& end, | ||
| const Scalar& step) { | ||
| arange_check_bounds(start, end, step); | ||
|
|
||
| // we use double precision for (start - end) / step | ||
| // to compute size_d for consistency across devices. | ||
| // The problem with using accscalar_t is that accscalar_t might be float32 on | ||
| // gpu for a float32 scalar_t, but double on cpu for the same, and the | ||
| // effective output size starts differing on CPU vs GPU because of precision | ||
| // issues, which we dont want. the corner-case we do want to take into account | ||
| // is int64_t, which has higher precision than double | ||
| double size_d; | ||
| if constexpr (std::is_same_v<scalar_t, int64_t>) { | ||
| using accscalar_t = at::acc_type<scalar_t, false>; | ||
| auto xstart = start.to<accscalar_t>(); | ||
| auto xend = end.to<accscalar_t>(); | ||
| auto xstep = step.to<accscalar_t>(); | ||
| int64_t sgn = (xstep > 0) - (xstep < 0); | ||
| size_d = std::ceil((xend - xstart + xstep - sgn) / xstep); | ||
| } else { | ||
| size_d = | ||
| std::ceil((end.to<double>() - start.to<double>()) / step.to<double>()); | ||
| } | ||
|
|
||
| TORCH_CHECK(size_d >= 0 && size_d <= static_cast<double>( | ||
| std::numeric_limits<int64_t>::max()), | ||
| "invalid size, possible overflow?"); | ||
|
|
||
| return static_cast<int64_t>(size_d); | ||
| } | ||
|
|
||
| } // namespace at::native |
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
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 |
|---|---|---|
|
|
@@ -26,24 +26,44 @@ namespace at { | |
|
|
||
| inline at::Tensor sum(const at::Tensor& self, | ||
| ::std::optional<at::ScalarType> dtype = ::std::nullopt) { | ||
| // Match PyTorch promotion: integer inputs -> int64; others -> keep input | ||
| // dtype. | ||
| at::ScalarType resolved_dtype; | ||
| if (dtype.has_value()) { | ||
| resolved_dtype = dtype.value(); | ||
| } else { | ||
| at::ScalarType input_dtype = self.scalar_type(); | ||
| resolved_dtype = at::isIntegralType(input_dtype, /*includeBool=*/true) | ||
| ? at::kLong | ||
| : input_dtype; | ||
| } | ||
|
Comment on lines
+29
to
+39
|
||
| return paddle::experimental::sum( | ||
| self._PD_GetInner(), | ||
| {}, | ||
| compat::_PD_AtenScalarTypeToPhiDataType( | ||
| dtype.value_or(c10::get_default_dtype())), | ||
| compat::_PD_AtenScalarTypeToPhiDataType(resolved_dtype), | ||
| /*keepdim=*/false); | ||
| } | ||
|
|
||
| inline at::Tensor sum(const at::Tensor& self, | ||
| at::OptionalIntArrayRef dim, | ||
| bool keepdim = false, | ||
| ::std::optional<at::ScalarType> dtype = ::std::nullopt) { | ||
| // Match PyTorch promotion: integer inputs -> int64; others -> keep input | ||
| // dtype. | ||
| at::ScalarType resolved_dtype; | ||
| if (dtype.has_value()) { | ||
| resolved_dtype = dtype.value(); | ||
| } else { | ||
| at::ScalarType input_dtype = self.scalar_type(); | ||
| resolved_dtype = at::isIntegralType(input_dtype, /*includeBool=*/true) | ||
| ? at::kLong | ||
| : input_dtype; | ||
| } | ||
| return paddle::experimental::sum( | ||
| self._PD_GetInner(), | ||
| dim.has_value() ? dim.value()._PD_ToPaddleIntArray() | ||
| : paddle::experimental::IntArray(), | ||
| compat::_PD_AtenScalarTypeToPhiDataType( | ||
| dtype.value_or(c10::get_default_dtype())), | ||
| compat::_PD_AtenScalarTypeToPhiDataType(resolved_dtype), | ||
| keepdim); | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
This change alters
sum's implicit dtype promotion rules (integral -> int64, otherwise keep input dtype). There are existing C++ compat tests forsum, but none cover integral inputs or bool promotion. Adding/adjusting tests to assert the new promotion behavior (e.g., int32/bool inputs sum to int64 when dtype is not provided) would help prevent regressions.