-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
- (MPSGraphTensor *)cumulativeSumWithTensor:(MPSGraphTensor *)tensor | ||
axis:(NSInteger)axis | ||
name:(NSString *)name; | ||
@end | ||
|
||
namespace at { | ||
namespace native { | ||
namespace mps { | ||
|
@@ -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) { | ||
|
@@ -263,5 +270,42 @@ void unary_op(const Tensor& self, const Tensor& output, std::string op_name, Una | |
}); | ||
} | ||
|
||
|
||
static bool mpsSupportsCumsum() { | ||
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. This also probably belongs to a single place. 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. 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 |
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.
I think we should probably move this to a header file and include all the venturaOps in the same place.