Skip to content

Commit

Permalink
Add meta support for [un]squeeze(), fix bug with set_()
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhirsh authored and pytorchmergebot committed Mar 24, 2022
1 parent 4025ca8 commit 23383b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/torch/optim/ @albanD
/test/test_public_bindings.py @albanD
/docs/source/conf.py @albanD
/aten/src/ATen/native/native_functions.yaml @bdhirsh

# Tensorpipe RPC Agent.
/torch/csrc/distributed/rpc/tensorpipe_agent.cpp @jiayisuse @osalpekar @lw @beauby
Expand Down
17 changes: 16 additions & 1 deletion aten/src/ATen/native/TensorShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Tensor& set_storage_cpu_(Tensor& result, Storage storage, int64_t storage_offset
result.unsafeGetTensorImpl()->set_storage_offset(storage_offset);
c10::optional<IntArrayRef> stride_opt = stride.data() != nullptr ?
c10::optional<IntArrayRef>(stride) : c10::nullopt;
at::native::resize_impl_cpu_(result.unsafeGetTensorImpl(), size, stride_opt);
// We can re-use this kernel for the meta device.
// We just need to make sure we don't actually try to resize the (null) storage.
at::native::resize_impl_cpu_(result.unsafeGetTensorImpl(), size, stride_opt, /*resize_storage=*/!result.is_meta());
return result;
}

Expand All @@ -87,6 +89,19 @@ Tensor& set_cpu_(Tensor& result) {
return result;
}

// We can't re-use the cpu kernel here because we don't want to use the cpu allocator.
Tensor& set_meta_(Tensor& result) {
caffe2::TypeMeta dtype = result.dtype();
Storage storage(
Storage::use_byte_size_t(),
0,
c10::GetAllocator(kMeta),
true);
result.set_(storage, 0, {0}, {});
TORCH_INTERNAL_ASSERT(dtype == result.dtype());
return result;
}

Tensor sparse_broadcast_to(const Tensor& self, IntArrayRef size) {
TORCH_CHECK(self.is_sparse(), "input must be sparse tensor");
int64_t sparse_extra_ndim = size.size() - self.dim();
Expand Down
13 changes: 7 additions & 6 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4162,15 +4162,15 @@
device_check: NoCheck
device_guard: False
dispatch:
CPU, CUDA: squeeze
CompositeExplicitAutograd: squeeze
QuantizedCPU, QuantizedCUDA: squeeze_quantized

- func: squeeze.dim(Tensor(a) self, int dim) -> Tensor(a)
variants: function, method
device_check: NoCheck
device_guard: False
dispatch:
CPU, CUDA: squeeze
CompositeExplicitAutograd: squeeze
QuantizedCPU, QuantizedCUDA: squeeze_quantized

- func: squeeze.dimname(Tensor(a) self, Dimname dim) -> Tensor(a)
Expand Down Expand Up @@ -4694,7 +4694,7 @@
device_check: NoCheck
device_guard: False
dispatch:
CPU, CUDA: unsqueeze
CompositeExplicitAutograd: unsqueeze
SparseCPU, SparseCUDA: unsqueeze_sparse
QuantizedCPU, QuantizedCUDA: unsqueeze_quantized

Expand Down Expand Up @@ -5830,14 +5830,14 @@
device_check: NoCheck
device_guard: False
dispatch:
CPU, CUDA: set_
CPU, CUDA, Meta: set_

- func: set_.source_Storage_storage_offset(Tensor(a!) self, Storage source, int storage_offset, int[] size, int[] stride=[]) -> Tensor(a!)
variants: method
device_check: NoCheck
device_guard: False
dispatch:
CPU: set_storage_cpu_
CPU, Meta: set_storage_cpu_
CUDA: set_storage_cuda_
QuantizedCPU, QuantizedCUDA: set_storage_quantized_

Expand All @@ -5846,13 +5846,14 @@
device_check: NoCheck
device_guard: False
dispatch:
CPU, CUDA: set_tensor_
CPU, CUDA, Meta: set_tensor_

- func: set_(Tensor(a!) self) -> Tensor(a!)
variants: method
dispatch:
CPU: set_cpu_
CUDA: set_cuda_
Meta: set_meta_

- func: is_set_to(Tensor self, Tensor tensor) -> bool
variants: method
Expand Down

0 comments on commit 23383b1

Please sign in to comment.