Replace resize_dim() with set_sizes_and_strides() in (#15348)
authorRuiyang Liu <rayliu@fb.com>
Wed, 19 Dec 2018 00:28:14 +0000 (16:28 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 19 Dec 2018 00:38:36 +0000 (16:38 -0800)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15348

We have a function resize_dim() on TensorImpl in c10/core/TensorImpl.h which lets you change the dimensionality of a tensor, resizing both sizes and strides. Unfortunately, this API is fairly easy to misuse, because it fills in the new entries with garbage when you size it larger. We want to refactor the call sites to use set_sizes_and_strides() instead, so that there is never an intermediate tensor state where the sizes/strides don't make sense. In this diff, resize_dim() is
replaced with set_sizes_and_strides() in aten/src/TH/THTensor.hpp.

Reviewed By: ezyang

Differential Revision: D13505512

fbshipit-source-id: 193bab89f0018c13ca07488be336d8e967746b76

aten/src/TH/THTensor.hpp

index 1a868cd..4001bc4 100644 (file)
@@ -43,7 +43,7 @@ inline THStorage* THTensor_getStoragePtr(const THTensor* tensor) {
 inline void THTensor_maybe_zero_dim(THTensor *tensor, bool condition_when_zero_dim) {
   bool set_zero_dim = condition_when_zero_dim && tensor->sizes().size() == 1 && tensor->size(0) == 1;
   if (set_zero_dim) {
-    tensor->resize_dim(0);
+    tensor->set_sizes_and_strides({}, {});
   }
 }