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

[GPU] Calculate strides for metal tensors #50309

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions aten/src/ATen/native/metal/MetalTensor.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <ATen/native/metal/MetalTensor.h>
#import <ATen/native/metal/MetalTensorImpl.h>
#import <ATen/native/metal/MetalUtils.h>
#import <ATen/native/metal/mpscnn/MPSImageWrapper.h>

namespace at {
Expand All @@ -8,9 +9,6 @@

class API_AVAILABLE(ios(10.0), macos(10.13)) MetalTensor::Impl {
public:
Impl(const std::vector<int64_t>& sizes)
: Impl(sizes, std::vector<int64_t>(sizes.size())) {}

Impl(const std::vector<int64_t>& sizes, const std::vector<int64_t>& strides)
: _sizes(sizes),
_strides(strides),
Expand Down Expand Up @@ -51,7 +49,7 @@ void copy_data_to_host(float* host) {
};

MetalTensor::MetalTensor(const std::vector<int64_t>& sizes)
: MetalTensor(sizes, std::vector<int64_t>(sizes.size())) {} // fake strides
: MetalTensor(sizes, compute_strides(sizes)) {}

MetalTensor::MetalTensor(
const std::vector<int64_t>& sizes,
Expand Down
14 changes: 14 additions & 0 deletions aten/src/ATen/native/metal/MetalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ std::vector<float> NC4_to_NCHW(
const float* src,
const std::vector<int64_t>& sizes);

// When copying to a CPU tensor, the memory format becomes NCHW.
// Thus,we compute the strides based on contiguous memory format.
static inline std::vector<int64_t> compute_strides(std::vector<int64_t> sizes) {
const auto dim = sizes.size();
std::vector<int64_t> strides(dim, 0);
if (dim > 0) {
const auto last_idx = dim - 1;
strides[last_idx] = 1;
for (int i = last_idx - 1; i >= 0; --i) {
strides[i] = strides[i + 1] * std::max<int64_t>(sizes[i + 1], 1);
}
}
return strides;
}

} // namespace metal
} // namespace native
Expand Down
4 changes: 1 addition & 3 deletions aten/src/ATen/native/metal/mpscnn/MPSCNNOps.mm
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,7 @@ Tensor copy_to_host(const Tensor& input) {
MPSImage* X = imageFromTensor(input);
MetalCommandBuffer* commandBuffer = commandBufferFromInputTensor(input);
auto&& sizes = [X sizes];
auto dummy = at::zeros(input.sizes()).contiguous();
auto strides = dummy.strides();
MetalTensor mt{sizes, strides.vec()};
MetalTensor mt{sizes};
mt.texture()->setCommandBuffer(commandBuffer);
mt.texture()->allocateTextureStorage(sizes);
MPSImage* Y = imageFromMetalTensor(mt);
Expand Down