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

Perf. improvement - save the gather result into dst directly if dst is contiguous (copy_kernel_mps) #44

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions aten/src/ATen/native/mps/OperationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ double getMPSScalarValue(const Tensor& t);
std::string getArrayRefString(const IntArrayRef s);
// use has_storage() on the returned tensor to determine if src actually is a view
Tensor gatherViewTensor(const at::Tensor& src);
Tensor gatherViewTensorWithOutput(const at::Tensor& src, at::Tensor& dst);
Tensor& scatterViewTensor(const at::Tensor& src, at::Tensor& output);

MPSShape* getMPSShape(const Tensor& t);
Expand Down
16 changes: 14 additions & 2 deletions aten/src/ATen/native/mps/operations/Copy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,23 @@ void copy_blit_mps(void* dst, const void* src, size_t size) {
static at::Tensor& copy_kernel_mps(at::Tensor& dst_, const at::Tensor& src_, bool non_blocking)
{
auto src_byte_offset = src_.storage_offset() * src_.itemsize();
auto dst_byte_offset = dst_.storage_offset() * dst_.itemsize();

// If dst is contiguous and there is no byte offset, we can save directly the result of
// gather into dst. This reduces the overhead of doing an additional blit for most cases
bool returnGatherOutput = (dst_.is_contiguous() && !dst_byte_offset);
Tensor src;

if (!src_.is_contiguous()) {
src = gatherViewTensor(src_);
src = returnGatherOutput ?
gatherViewTensorWithOutput(src_, dst_) :
gatherViewTensor(src_);

if (src.has_storage()) {

if (returnGatherOutput)
return dst_;

src_byte_offset = 0;
} else {
src = src_.expand_as(dst_).contiguous();
Expand All @@ -271,7 +284,6 @@ void copy_blit_mps(void* dst, const void* src, size_t size) {
src._set_conj(src_.is_conj());
src._set_neg(src_.is_neg());

auto dst_byte_offset = dst_.storage_offset() * dst_.itemsize();
id<MTLBuffer> destBuffer = getMTLBufferStorage(dst_);
id<MTLBuffer> sourceBuffer = getMTLBufferStorage(src);
const size_t src_size = src.nbytes();
Expand Down
20 changes: 18 additions & 2 deletions aten/src/ATen/native/mps/operations/View.mm
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,21 @@
}
}

Tensor gatherViewTensor(const at::Tensor& src)
{
static ViewCachedGraph* _getCachedGraph(const at::Tensor& src) {
ViewCachedGraph* cachedGraph = nullptr;

const IntArrayRef& base_shape = get_buffer_shape(src.storage().data());
if (base_shape.size() > 0) {
string key = getStridedKey(src.scalar_type(), base_shape, src.sizes(), /*is_scatter*/ false);
cachedGraph = static_cast<ViewCachedGraph *>(MPSGraphCache::getInstance()->LookUp(key));
}

return cachedGraph;
}

Tensor gatherViewTensor(const at::Tensor& src)
razarmehr marked this conversation as resolved.
Show resolved Hide resolved
{
ViewCachedGraph* cachedGraph = _getCachedGraph(src);
// there are cases where gatherViewTensor() is called without having as_strided() called beforehand.
// this typically may come from copy_mps variants. In such cases, when the base_shape isn't found the
// callers would resort to make the tensor contiguous in an alternative code path.
Expand All @@ -229,6 +235,16 @@ Tensor gatherViewTensor(const at::Tensor& src)
return runViewGraph(cachedGraph, src, output, /*needsScatter*/ false);
}

Tensor gatherViewTensorWithOutput(const at::Tensor& src, at::Tensor& dst)
{
ViewCachedGraph* cachedGraph = _getCachedGraph(src);
if (!cachedGraph) {
return Tensor();
}

return runViewGraph(cachedGraph, src, dst, /*needsScatter*/ false);
}

Tensor& scatterViewTensor(const at::Tensor& src, at::Tensor& output)
{
ViewCachedGraph* cachedGraph = createViewGraph(output, output.sizes(), output.strides(),
Expand Down
2 changes: 1 addition & 1 deletion test/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4641,7 +4641,7 @@ def test_slicing_with_step(self):
x_mps = torch.zeros(10, dtype=torch.float32, device="mps")
x_mps[::2] = 1.0

x_cpu = torch.zeros(10, dtype=torch.float32, device="mps")
x_cpu = torch.zeros(10, dtype=torch.float32, device="cpu")
x_cpu[::2] = 1.0

self.assertEqual(x_cpu, x_mps)
Expand Down