From: Jerry Zhang Date: Thu, 20 Dec 2018 23:28:12 +0000 (-0800) Subject: Tensor construction codemod(ResizeLike) - 7/7 (#15087) X-Git-Tag: accepted/tizen/6.5/unified/20211028.231830~2136 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ed5b584f65eaf792761dd9e265e6cd668c11fb93;p=platform%2Fupstream%2Fpytorch.git Tensor construction codemod(ResizeLike) - 7/7 (#15087) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/15087 Codemod generated with clangr shard mode, 25 files per diff, motivation: https://github.com/pytorch/pytorch/pull/12407 Reviewed By: ezyang Differential Revision: D13419765 fbshipit-source-id: 34d695309a66723281429610a12544598c507d74 --- diff --git a/caffe2/operators/swish_op.cc b/caffe2/operators/swish_op.cc index e7a764f..18cdf32 100644 --- a/caffe2/operators/swish_op.cc +++ b/caffe2/operators/swish_op.cc @@ -24,10 +24,10 @@ bool SwishGradientOp::DoRunWithType() { auto& Xin = Input(X); auto& Yin = Input(Y); auto& DYin = Input(DY); - auto* DXout = Output(DX); + CAFFE_ENFORCE_EQ(Xin.numel(), Yin.numel()); CAFFE_ENFORCE_EQ(DYin.numel(), Yin.numel()); - DXout->ResizeLike(Yin); + auto* DXout = Output(DX, Yin.sizes(), at::dtype()); const float* Xdata = Xin.template data(); const float* Ydata = Yin.template data(); diff --git a/caffe2/operators/thresholded_relu_op.cc b/caffe2/operators/thresholded_relu_op.cc index 4d0aa51..d9ca8e6 100644 --- a/caffe2/operators/thresholded_relu_op.cc +++ b/caffe2/operators/thresholded_relu_op.cc @@ -8,8 +8,8 @@ namespace caffe2 { template <> bool ThresholdedReluOp::RunOnDevice() { auto& X = Input(0); - auto* Y = Output(0); - Y->ResizeLike(X); + + auto* Y = Output(0, X.sizes(), at::dtype()); ConstEigenVectorArrayMap Xvec(X.data(), X.numel()); EigenVectorArrayMap Yvec( @@ -30,9 +30,9 @@ template <> bool ThresholdedReluGradientOp::RunOnDevice() { auto& Y = Input(0); auto& dY = Input(1); - auto* dX = Output(0); + CAFFE_ENFORCE_EQ(dY.numel(), Y.numel()); - dX->ResizeLike(Y); + auto* dX = Output(0, Y.sizes(), at::dtype()); const float* Ydata = Y.data(); const float* dYdata = dY.data(); diff --git a/caffe2/operators/unique_ops.cc b/caffe2/operators/unique_ops.cc index 003fe13..a992342 100644 --- a/caffe2/operators/unique_ops.cc +++ b/caffe2/operators/unique_ops.cc @@ -30,8 +30,8 @@ bool UniqueOp::DoRunWithType() { int* remapping = nullptr; if (REMAPPING < OutputSize()) { - auto* remappingTensor = Output(REMAPPING); - remappingTensor->ResizeLike(inputTensor); + auto* remappingTensor = + Output(REMAPPING, inputTensor.sizes(), at::dtype()); remapping = remappingTensor->template mutable_data(); } diff --git a/caffe2/operators/utility_ops.h b/caffe2/operators/utility_ops.h index 0e03bfb..d80f4ec 100644 --- a/caffe2/operators/utility_ops.h +++ b/caffe2/operators/utility_ops.h @@ -255,12 +255,19 @@ class SumOp : public Operator { template bool DoRunWithType() { auto& input0 = Input(0); - auto* output = Output(0); + if (InputSize() == 1) { - output->CopyFrom(input0, true /*async*/); + // TODO: better TensorOptions argument passing(e.g. default argument) + OutputTensorCopyFrom( + 0, + // I'll change the order of argument in another diff, so that we don't + // need to write this + at::dtype(input0.dtype()), + input0, + true /*async*/); return true; } - output->ResizeLike(input0); + auto* output = Output(0, input0.sizes(), at::dtype()); T* output_data = output->template mutable_data(); // Dimension checking for (int i = 1; i < InputSize(); ++i) { @@ -331,10 +338,9 @@ class WeightedSumOp : public Operator { CAFFE_ENFORCE_GT(X0.numel(), 0); CAFFE_ENFORCE_EQ(weight0.numel(), 1); const int size = X0.numel(); - auto* Y = Output(0); - if (Y != &X0) { - Y->ResizeLike(X0); - } + // Note: removed Aliasing check, since Output already has + // caching capability + auto* Y = Output(0, X0.sizes(), at::dtype()); T* Y_data = Y->template mutable_data(); if (input_size == 2) { math::Scale( @@ -346,15 +352,14 @@ class WeightedSumOp : public Operator { return true; } const auto& X1 = Input(2); - CAFFE_ENFORCE_NE( - &X1, - Y, + CAFFE_ENFORCE( + !IsInputOutputAlias(2, 0), "Input #2 is the same as output. If you want to do in-place updates, " "put the output as input #0."); const auto& weight1 = Input(3); CAFFE_ENFORCE_EQ(X1.numel(), size); CAFFE_ENFORCE_EQ(weight1.numel(), 1); - if (Y != &X0) { + if (!IsInputOutputAlias(0, 0)) { context_.template CopySameDevice(size, X0.template data(), Y_data); } math::Axpby( @@ -371,7 +376,7 @@ class WeightedSumOp : public Operator { const std::string err_msg = "Input #" + to_string(i) + " is the same as output. If you want to do in-place updates, " "put the output as input #0."; - CAFFE_ENFORCE_NE(&Xi, Y, err_msg); + CAFFE_ENFORCE(!IsInputOutputAlias(i, 0), err_msg); const auto& weighti = Input(i + 1); CAFFE_ENFORCE_EQ(Xi.numel(), size); CAFFE_ENFORCE_EQ(weighti.numel(), 1); @@ -410,8 +415,8 @@ class WeightedSumGradientOp : public Operator { for (int i = 0; i < InputSize() / 2; i++) { auto& cur_w = Input(2 * i + 2); CAFFE_ENFORCE_EQ(cur_w.numel(), 1); - auto* cur_dX = Output(i); - cur_dX->ResizeLike(dY); + + auto* cur_dX = Output(i, dY.sizes(), at::dtype()); math::Scale( size, diff --git a/caffe2/operators/utility_ops_cudnn.cc b/caffe2/operators/utility_ops_cudnn.cc index b50c500..bd04f30 100644 --- a/caffe2/operators/utility_ops_cudnn.cc +++ b/caffe2/operators/utility_ops_cudnn.cc @@ -45,10 +45,10 @@ class CuDNNWeightedSumOp : public Operator { CAFFE_ENFORCE_EQ(weight0.numel(), 1); const int input_size = X0.numel(); SetTensorDescriptor(cudnnTypeWrapper::type, input_size); - auto* Y = Output(0); - if (Y != &X0) { - Y->ResizeLike(X0); - } + + // Note: removed Aliasing check, since Output already has + // caching capability + auto* Y = Output(0, X0.sizes(), at::dtype()); T* Y_data = Y->template mutable_data(); T alpha = convert::To(0.0f); T beta = convert::To(0.0f); @@ -65,9 +65,8 @@ class CuDNNWeightedSumOp : public Operator { return true; } const auto& X1 = Input(2); - CAFFE_ENFORCE_NE( - &X1, - Y, + CAFFE_ENFORCE( + !IsInputOutputAlias(2, 0), "Input #2 is the same as output. If you want to do in-place updates, " "put the output as input #0."); const auto& weight1 = Input(3); @@ -75,7 +74,7 @@ class CuDNNWeightedSumOp : public Operator { CAFFE_ENFORCE_EQ(weight1.numel(), 1); CopyWeightToHost(weight1.template data(), &alpha); CopyWeightToHost(weight0.template data(), &beta); - if (Y == &X0) { + if (IsInputOutputAlias(0, 0)) { CUDNN_ENFORCE(cudnnAddTensor( cudnn_wrapper_.inline_cudnn_handle(), &alpha, @@ -105,7 +104,7 @@ class CuDNNWeightedSumOp : public Operator { const std::string err_msg = "Input #" + to_string(i) + " is the same as output. If you want to do in-place updates, " "put the output as input #0."; - CAFFE_ENFORCE_NE(&Xi, Y, err_msg); + CAFFE_ENFORCE(!IsInputOutputAlias(i, 0), err_msg); const auto& weighti = Input(i + 1); CAFFE_ENFORCE_EQ(Xi.numel(), input_size); CAFFE_ENFORCE_EQ(weighti.numel(), 1); diff --git a/caffe2/quantization/server/conv_relu_op.cc b/caffe2/quantization/server/conv_relu_op.cc index bd41404..e3a3cd4 100644 --- a/caffe2/quantization/server/conv_relu_op.cc +++ b/caffe2/quantization/server/conv_relu_op.cc @@ -20,8 +20,8 @@ bool ConvReluOp::RunOnDeviceWithOrderNCHW() { BlobGetMutableTensor(local_output_blobs_[0], Context::GetDeviceType()); const T* output_local_data = local_output->template data(); - Tensor* output = Operator::Output(0); - output->ResizeLike(*local_output); + Tensor* output = + Operator::Output(0, local_output->sizes(), at::dtype()); T* output_data = output->template mutable_data(); #ifdef _OPENMP #pragma omp parallel for @@ -51,8 +51,8 @@ bool ConvReluOp::RunOnDeviceWithOrderNHWC() { BlobGetMutableTensor(local_output_blobs_[0], Context::GetDeviceType()); const T* output_local_data = local_output->template data(); - Tensor* output = Operator::Output(0); - output->ResizeLike(*local_output); + Tensor* output = + Operator::Output(0, local_output->sizes(), at::dtype()); T* output_data = output->template mutable_data(); #ifdef _OPENMP #pragma omp parallel for diff --git a/caffe2/quantization/server/fully_connected_fake_lowp_op.cc b/caffe2/quantization/server/fully_connected_fake_lowp_op.cc index 2380014..ae08461 100644 --- a/caffe2/quantization/server/fully_connected_fake_lowp_op.cc +++ b/caffe2/quantization/server/fully_connected_fake_lowp_op.cc @@ -179,9 +179,7 @@ bool FullyConnectedGradientFakeLowpFPOp:: CAFFE_ENFORCE(M * K == X.size()); CAFFE_ENFORCE(K * N == W.size()); - auto* dW = Output(0); - - dW->ResizeLike(W); + auto* dW = Output(0, W.sizes(), at::dtype()); auto* db = Output(1, {N}, at::dtype()); if (X.size() == 0) { @@ -198,9 +196,7 @@ bool FullyConnectedGradientFakeLowpFPOp:: &context_); if (OutputSize() == 3) { - auto* dX = Output(2); - dX->ResizeLike(X); - dX->template mutable_data(); + Output(2, X.sizes(), at::dtype()); } return true; @@ -270,8 +266,7 @@ bool FullyConnectedGradientFakeLowpFPOp:: // Compute dX if (OutputSize() == 3) { - auto* dX = Output(2); - dX->ResizeLike(X); + auto* dX = Output(2, X.sizes(), at::dtype()); math::Gemm( CblasNoTrans, TransposeWeight ? CblasNoTrans : CblasTrans, diff --git a/caffe2/sgd/clip_tensor_op.h b/caffe2/sgd/clip_tensor_op.h index 5792051..34d6686 100644 --- a/caffe2/sgd/clip_tensor_op.h +++ b/caffe2/sgd/clip_tensor_op.h @@ -29,8 +29,7 @@ class ClipTensorByScalingOp final : public Operator { const auto* input_tensor_data = input_tensor.template data(); const auto* val_data = val.template data(); - auto* clipped = Output(0); - clipped->ResizeLike(input_tensor); + auto* clipped = Output(0, input_tensor.sizes(), at::dtype()); float* clipped_tensor_data = clipped->template mutable_data(); if (InputSize() > 2) { diff --git a/caffe2/sgd/yellowfin_op.h b/caffe2/sgd/yellowfin_op.h index 5c6f67f..622caa8 100644 --- a/caffe2/sgd/yellowfin_op.h +++ b/caffe2/sgd/yellowfin_op.h @@ -157,9 +157,9 @@ for (int i = 0; i < param_tensor.dim(); ++i) { // Output data -#define CAFFE2_YF_READ_OUTPUT(OUTPUT_NAME, VAR_NAME) \ - auto VAR_NAME##_out_tensor = Output(OUTPUT_##OUTPUT_NAME); \ - VAR_NAME##_out_tensor->ResizeLike(VAR_NAME##_tensor); \ +#define CAFFE2_YF_READ_OUTPUT(OUTPUT_NAME, VAR_NAME) \ + auto VAR_NAME##_out_tensor = \ + Output(OUTPUT_##OUTPUT_NAME, VAR_NAME##_tensor.sizes(), at::dtype()); \ VAR_NAME##_out_ = VAR_NAME##_out_tensor->template mutable_data(); CAFFE2_YF_READ_OUTPUT(PARAM, param) diff --git a/modules/detectron/batch_permutation_op.cc b/modules/detectron/batch_permutation_op.cc index 4d53035..60916e5 100644 --- a/modules/detectron/batch_permutation_op.cc +++ b/modules/detectron/batch_permutation_op.cc @@ -78,7 +78,6 @@ template <> bool BatchPermutationOp::RunOnDevice() { const auto& X = Input(0); const auto& indices = Input(1); - auto* Y = Output(0); CAFFE_ENFORCE_EQ(indices.dim(), 1, "indices must be 1-d"); CAFFE_ENFORCE_EQ( @@ -90,7 +89,7 @@ bool BatchPermutationOp::RunOnDevice() { indices.dim32(0), ")"); - Y->ResizeLike(X); + auto* Y = Output(0, X.sizes(), at::dtype()); const int N = X.dim32(0); const int C = X.dim32(1);