Skip to content
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

[MPS] Add native cumsum implementation #88319

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion aten/src/ATen/native/mps/operations/UnaryOps.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include <ATen/native/mps/OperationUtils.h>
#include <torch/library.h>

// TODO: Remove me when moved to MacOS 13
@interface MPSGraph (VenturaOps)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should probably move this to a header file and include all the venturaOps in the same place.

- (MPSGraphTensor *)cumulativeSumWithTensor:(MPSGraphTensor *)tensor
axis:(NSInteger)axis
name:(NSString *)name;
@end

namespace at {
namespace native {
namespace mps {
Expand All @@ -30,7 +37,7 @@ void unary_op(const Tensor& self, const Tensor& output, std::string op_name, Una
}
MPSGraphCache* cache_ = MPSGraphCache::getInstance();
@autoreleasepool {
string key = op_name + getTensorsStringKey({self}, /*use_scalar_value*/ false);
string key = op_name + getTensorsStringKey({self, output}, /*use_scalar_value*/ false);
auto cachedGraph = cache_->LookUpAs<MPSUnaryCachedGraph>(key);

if(!cachedGraph) {
Expand Down Expand Up @@ -263,5 +270,42 @@ void unary_op(const Tensor& self, const Tensor& output, std::string op_name, Una
});
}


static bool mpsSupportsCumsum() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This also probably belongs to a single place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I believe it should be property of MPSDevice and run only once. Do you mind if I'll do it as followup PR?

id mpsCD = NSClassFromString(@"MPSGraph");
return [mpsCD instancesRespondToSelector:@selector(cumulativeSumWithTensor:axis:name:)] == YES;
}


TORCH_IMPL_FUNC(cumsum_out_mps)
(const Tensor& self,
int64_t dim,
c10::optional<ScalarType> dtype,
const Tensor& result) {
TORCH_CHECK(dim >=0 && dim < std::max(1LL, self.ndimension()), "Expected dim to be between 0 and ", self.ndimension(), " but got ", dim);
if (!mpsSupportsCumsum()) {
TORCH_WARN_ONCE("torch.cumsum 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;
mps::unary_op(input, result, "cumsum_out_mp" + std::to_string(dim),
^ MPSGraphTensor* (MPSGraph* mpsGraph, MPSGraphTensor* inputTensor) {
// cumsum is horribly broken for int8, int16 and as chances for overflow is pretty high, cast to int32
if (isIntegralType(input.scalar_type()) && input.scalar_type() !=ScalarType::Int) {
inputTensor = mps::castMPSTensor(mpsGraph, inputTensor, result.scalar_type());
}
auto rc = [mpsGraph cumulativeSumWithTensor: inputTensor
axis: dim
name: nil];
if (result.scalar_type()!= input.scalar_type() ||
(isIntegralType(input.scalar_type()) && input.scalar_type() !=ScalarType::Int)) {
return mps::castMPSTensor(mpsGraph, rc, result.scalar_type());
}
return rc;
});
}

} // namespace native
} // namespace at
1 change: 1 addition & 0 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@
device_check: NoCheck # TensorIterator
dispatch:
CPU, CUDA: cumsum_out
MPS: cumsum_out_mps

- func: cumsum.dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor
device_check: NoCheck # TensorIterator
Expand Down
5 changes: 3 additions & 2 deletions test/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6592,8 +6592,7 @@ def test_index_put_accumulate_duplicate_indices(self, device="mps"):
# lots of duplicates interleaved with each other
delta = torch.empty(i, dtype=torch.float32, device=device).uniform_(-1, 1)

# cumsum not supported on 'mps', fallback on 'cpu'
indices = delta.cpu().cumsum(0).long().to("mps")
indices = delta.cumsum(0).long().to("mps")

# abs for int64 is not supported on mps, fallback on 'cpu' to calculate it
input = torch.randn(indices.cpu().abs().max().to("mps") + 1, device=device)
Expand Down Expand Up @@ -7220,6 +7219,7 @@ class TestConsistency(TestCase):
'cos': ['f32', 'i16', 'i32', 'u8', 'i64'],
'cosh': ['f32', 'i16', 'i32', 'u8', 'i64'],
'cov': ['f32'],
'cumsum': ['f16', 'f32', 'int16', 'int32'],
'deg2rad': ['b8', 'f16', 'f32', 'i16', 'i32', 'i64', 'u8'],
'diag': ['f32', 'i32'],
'diag_embed': ['b8', 'f16', 'f32', 'i16', 'i32', 'i64'],
Expand Down Expand Up @@ -7447,6 +7447,7 @@ class TestConsistency(TestCase):
'corrcoef': ['f32'],
'cos': ['f32'],
'cosh': ['f32'],
'cumsum': ['f16', 'f32'],
'deg2rad': ['f16', 'f32'],
'diag': ['f32'],
'diag_embed': ['f16', 'f32'],
Expand Down