forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 9
Add support for cumprod #419
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
Open
DenisVieriu97
wants to merge
4
commits into
master
Choose a base branch
from
dev/denis/cumulative_ops
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -4,12 +4,16 @@ | |
| #include <ATen/native/mps/MPSGraphVenturaOps.h> | ||
|
|
||
| namespace at::native { | ||
|
|
||
| enum class MPSCumulativeOpType : uint8_t { | ||
| MPS_CUMSUM = 0, | ||
| MPS_CUMPROD = 1, | ||
| }; | ||
| namespace mps { | ||
|
|
||
| typedef MPSGraphTensor* (^UnaryOpBlock)(MPSGraph*, MPSGraphTensor*); | ||
| using is_noop_p = std::function<bool(const Tensor&)>; | ||
|
|
||
|
|
||
| bool is_empty_tensor(const Tensor& self) { | ||
| return self.numel() == 0; | ||
| } | ||
|
|
@@ -415,49 +419,74 @@ Tensor logit_mps(const Tensor& self, c10::optional<double> eps) { | |
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| TORCH_IMPL_FUNC(cumsum_out_mps) | ||
| (const Tensor& self, | ||
| int64_t dim, | ||
| c10::optional<ScalarType> dtype, | ||
| const Tensor& result) { | ||
|
|
||
| void cumulative_op_impl( | ||
| const Tensor& self, | ||
| int64_t dim, | ||
| c10::optional<ScalarType> dtype, | ||
| const Tensor& result, | ||
| MPSCumulativeOpType cumulativeOpType, | ||
| const std::string& op_name) { | ||
| bool macOS13_3_plus = is_macos_13_or_newer(MacOSVersion::MACOS_VER_13_3_PLUS); | ||
| auto nDims = self.dim(); | ||
| auto wrapped_dim = maybe_wrap_dim(dim, nDims); | ||
| TORCH_CHECK(wrapped_dim >=0 && wrapped_dim < std::max(1LL, self.ndimension()), "Expected wrapped dim to be between 0 and ", self.ndimension(), " but got ", wrapped_dim , "(original dim is ", dim, ")"); | ||
| TORCH_CHECK(wrapped_dim >= 0 && wrapped_dim < std::max(1LL, self.ndimension()), | ||
| "Expected wrapped dim to be between 0 and ", | ||
| self.ndimension(), | ||
| " but got ", | ||
| wrapped_dim, | ||
| "(original dim is ", | ||
| dim, | ||
| ")"); | ||
|
|
||
| if (!is_macos_13_or_newer()) { | ||
| TORCH_WARN_ONCE("torch.cumsum supported by MPS on MacOS 13+, please upgrade"); | ||
| TORCH_WARN_ONCE(op_name, " supported by MPS on MacOS 13+, please upgrade"); | ||
| auto cpu_result = self.to(at::Device(kCPU)).cumsum(dim, dtype); | ||
| at::_copy_from_and_resize(cpu_result, result); | ||
| return; | ||
| } | ||
| auto input = dtype.has_value() ? self.to(dtype.value()) : self; | ||
|
|
||
| // issue #103810551: cumsum is horribly broken for int8, int16 and as chances for overflow is pretty high, cast to int32 | ||
| // fixed in macOS 13.3 | ||
| bool castInputData = (isIntegralType(input.scalar_type()) && | ||
| input.scalar_type() != ScalarType::Int && | ||
| // issue #103810551: int8, int16 have a high chance of overflow, cast to int32 | ||
| bool castInputData = (isIntegralType(input.scalar_type(), false) && | ||
| input.scalar_type() != ScalarType::Int && | ||
| input.scalar_type() != ScalarType::Long); | ||
|
|
||
| TORCH_CHECK(macOS13_3_plus || input.scalar_type() != ScalarType::Long, | ||
| "MPS does not support cumsum op with int64 input. Support has been added in macOS 13.3"); | ||
| "MPS does not support ", op_name, " op with int64 input. Support has been added in macOS 13.3"); | ||
|
|
||
| mps::unary_op(input, | ||
| result, | ||
| op_name + std::to_string(dim), | ||
| ^MPSGraphTensor*(MPSGraph* mpsGraph, MPSGraphTensor* inputTensor) { | ||
| if (castInputData) { | ||
| inputTensor = mps::castMPSTensor(mpsGraph, inputTensor, ScalarType::Int); | ||
| } | ||
| MPSGraphTensor* cumulativeOpTensor = nil; | ||
| switch (cumulativeOpType) { | ||
| case MPSCumulativeOpType::MPS_CUMSUM: | ||
| cumulativeOpTensor = [mpsGraph cumulativeSumWithTensor:inputTensor axis:dim name:nil]; | ||
| break; | ||
| case MPSCumulativeOpType::MPS_CUMPROD: | ||
| cumulativeOpTensor = [mpsGraph cumulativeProductWithTensor:inputTensor axis:dim name:nil]; | ||
| break; | ||
| default: | ||
| TORCH_CHECK(false, "Undefined cumulative op type"); | ||
| } | ||
| if ((mps::getMPSDataType(result.scalar_type()) != [cumulativeOpTensor dataType]) || castInputData) { | ||
| return mps::castMPSTensor(mpsGraph, cumulativeOpTensor, result.scalar_type()); | ||
| } | ||
| return cumulativeOpTensor; | ||
| }); | ||
| } | ||
|
|
||
| mps::unary_op(input, result, "cumsum_out_mp" + std::to_string(dim), | ||
| ^ MPSGraphTensor* (MPSGraph* mpsGraph, MPSGraphTensor* inputTensor) { | ||
| TORCH_IMPL_FUNC(cumsum_out_mps) | ||
| (const Tensor& self, int64_t dim, c10::optional<ScalarType> dtype, const Tensor& result) { | ||
| cumulative_op_impl(self, dim, dtype, result, MPSCumulativeOpType::MPS_CUMSUM, "cumsum_out"); | ||
| } | ||
|
|
||
| if (castInputData) { | ||
| inputTensor = mps::castMPSTensor(mpsGraph, inputTensor, ScalarType::Int); | ||
| } | ||
| auto rc = [mpsGraph cumulativeSumWithTensor: inputTensor | ||
| axis: dim | ||
| name: nil]; | ||
| if ((mps::getMPSDataType(result.scalar_type()) != [rc dataType]) || castInputData) { | ||
| return mps::castMPSTensor(mpsGraph, rc, result.scalar_type()); | ||
| } | ||
| return rc; | ||
| }); | ||
| TORCH_IMPL_FUNC(cumprod_out_mps) | ||
|
||
| (const Tensor& self, int64_t dim, c10::optional<ScalarType> dtype, const Tensor& result) { | ||
| cumulative_op_impl(self, dim, dtype, result, MPSCumulativeOpType::MPS_CUMPROD, "cumprod_out"); | ||
| } | ||
|
|
||
| } // 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
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.
Uh oh!
There was an error while loading. Please reload this page.