From f05d2c519d76b43fe50fdb005c2094b6664a010e Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Mon, 9 Sep 2019 10:22:01 +0800 Subject: [PATCH] paddle::framework::vectorize() templatization [PART3] (#19643) * paddle::framework::vectorize() templatization test=develop * update pybind/imperative.cc test=develop * revert update on unsqueeze_op.cc and warpctc_cudnn_op.cu.cc test=develop --- paddle/fluid/inference/anakin/convert/conv2d.cc | 2 +- paddle/fluid/inference/anakin/convert/conv2d_fusion.cc | 4 ++-- paddle/fluid/inference/anakin/convert/fc.cc | 2 +- paddle/fluid/inference/anakin/convert/helper.h | 2 +- paddle/fluid/inference/anakin/convert/ut_helper.h | 2 +- paddle/fluid/inference/anakin/engine.cc | 2 +- paddle/fluid/inference/api/api_impl_tester.cc | 2 +- paddle/fluid/inference/api/details/zero_copy_tensor.cc | 2 +- paddle/fluid/inference/tensorrt/convert/elementwise_op.cc | 2 +- .../elementwise/mkldnn/elementwise_add_mkldnn_op.cc | 8 ++++---- .../elementwise/mkldnn/elementwise_mul_mkldnn_op.cc | 4 ++-- paddle/fluid/operators/stack_op.h | 5 +++-- paddle/fluid/operators/unstack_op.h | 5 +++-- paddle/fluid/platform/cudnn_desc.h | 4 ++-- paddle/fluid/pybind/imperative.cc | 2 +- 15 files changed, 25 insertions(+), 23 deletions(-) diff --git a/paddle/fluid/inference/anakin/convert/conv2d.cc b/paddle/fluid/inference/anakin/convert/conv2d.cc index 26f78efa61c..7904d407c36 100644 --- a/paddle/fluid/inference/anakin/convert/conv2d.cc +++ b/paddle/fluid/inference/anakin/convert/conv2d.cc @@ -41,7 +41,7 @@ void Conv2dOpConverter::operator()( auto *filter_v = scope.FindVar(op_desc.Input("Filter").front()); PADDLE_ENFORCE_NOT_NULL(filter_v); auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize2int(weight_tensor->dims()); + auto weight_shape = framework::vectorize(weight_tensor->dims()); PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL); diff --git a/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc b/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc index f2e6003aa68..4d7a502dee3 100644 --- a/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc +++ b/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc @@ -43,7 +43,7 @@ void Conv2dFusionOpConverter::operator()( PADDLE_ENFORCE_NOT_NULL(filter_v); auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize2int(weight_tensor->dims()); + auto weight_shape = framework::vectorize(weight_tensor->dims()); auto *b_v = scope.FindVar(op_desc.Input("Bias").front()); PADDLE_ENFORCE_NOT_NULL(b_v); @@ -99,7 +99,7 @@ void Conv2dFusionOpConverter::operator()( this->engine_->AddTensorScale(input_name, in_scale / int8_range); } else { auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize2int(weight_tensor->dims()); + auto weight_shape = framework::vectorize(weight_tensor->dims()); auto *weight1 = pblock_from_tensor( *weight_tensor, weight_shape, this->engine_); this->engine_->AddOpAttr(op_name, "weight_1", *weight1); diff --git a/paddle/fluid/inference/anakin/convert/fc.cc b/paddle/fluid/inference/anakin/convert/fc.cc index b64d0b84fd4..265d318aaee 100644 --- a/paddle/fluid/inference/anakin/convert/fc.cc +++ b/paddle/fluid/inference/anakin/convert/fc.cc @@ -43,7 +43,7 @@ void FcBaseOpConverter::operator()( auto *y_v = scope.FindVar(op_desc.Input(w_name).front()); PADDLE_ENFORCE_NOT_NULL(y_v); auto weight_tensor = tensor_from_var(*y_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize2int(weight_tensor->dims()); + auto weight_shape = framework::vectorize(weight_tensor->dims()); int out_dim = weight_shape[1]; const int w_m = weight_shape[0]; diff --git a/paddle/fluid/inference/anakin/convert/helper.h b/paddle/fluid/inference/anakin/convert/helper.h index 7b0fb211dcd..6ba8fd6ceb5 100644 --- a/paddle/fluid/inference/anakin/convert/helper.h +++ b/paddle/fluid/inference/anakin/convert/helper.h @@ -86,7 +86,7 @@ template PBlock* pblock_from_var(const framework::Variable& var, AnakinEngine* engine) { auto tensor = tensor_from_var(var, platform::CPUPlace()); - auto shape = framework::vectorize2int(tensor->dims()); + auto shape = framework::vectorize(tensor->dims()); return pblock_from_tensor(*tensor, shape, engine); } diff --git a/paddle/fluid/inference/anakin/convert/ut_helper.h b/paddle/fluid/inference/anakin/convert/ut_helper.h index 92441f2560f..f2b56a2569f 100644 --- a/paddle/fluid/inference/anakin/convert/ut_helper.h +++ b/paddle/fluid/inference/anakin/convert/ut_helper.h @@ -136,7 +136,7 @@ class AnakinConvertValidation { if (parameters_.count(input)) continue; auto& t = inference::analysis::GetFromScope(*scope_, input); - auto t_shape = framework::vectorize2int(t.dims()); + auto t_shape = framework::vectorize(t.dims()); while (t_shape.size() < 4) { t_shape.push_back(1); } diff --git a/paddle/fluid/inference/anakin/engine.cc b/paddle/fluid/inference/anakin/engine.cc index 13f16c4c898..d57952db0d3 100644 --- a/paddle/fluid/inference/anakin/engine.cc +++ b/paddle/fluid/inference/anakin/engine.cc @@ -86,7 +86,7 @@ void AnakinEngine::BindInput( auto *tensor = input.second; auto *data = tensor->data(); - auto fluid_input_shape = framework::vectorize2int(tensor->dims()); + auto fluid_input_shape = framework::vectorize(tensor->dims()); while (fluid_input_shape.size() < 4) { fluid_input_shape.push_back(1); } diff --git a/paddle/fluid/inference/api/api_impl_tester.cc b/paddle/fluid/inference/api/api_impl_tester.cc index 2dc5dda34d0..c80187adfa7 100644 --- a/paddle/fluid/inference/api/api_impl_tester.cc +++ b/paddle/fluid/inference/api/api_impl_tester.cc @@ -48,7 +48,7 @@ PaddleTensor LodTensorToPaddleTensor(framework::LoDTensor* t) { } else { LOG(FATAL) << "unsupported type."; } - pt.shape = framework::vectorize2int(t->dims()); + pt.shape = framework::vectorize(t->dims()); return pt; } diff --git a/paddle/fluid/inference/api/details/zero_copy_tensor.cc b/paddle/fluid/inference/api/details/zero_copy_tensor.cc index 937b6398f81..88489078eae 100644 --- a/paddle/fluid/inference/api/details/zero_copy_tensor.cc +++ b/paddle/fluid/inference/api/details/zero_copy_tensor.cc @@ -172,7 +172,7 @@ void *ZeroCopyTensor::FindTensor() const { std::vector ZeroCopyTensor::shape() const { EAGER_GET_TENSOR; PADDLE_ENFORCE(tensor_, "not found tensor called %s in the scope", name_); - return framework::vectorize2int(tensor->dims()); + return framework::vectorize(tensor->dims()); } void ZeroCopyTensor::SetLoD(const std::vector> &x) { diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index a888b0803df..c61dd753a33 100644 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -61,7 +61,7 @@ class ElementwiseWeightOpConverter : public OpConverter { auto scale_mode = nvinfer1::ScaleMode::kELEMENTWISE; - std::vector dims_y = framework::vectorize2int(Y_t->dims()); + std::vector dims_y = framework::vectorize(Y_t->dims()); if (static_cast(dims_y.size()) == dims_x.nbDims + 1) { if (dims_y[0] == 1) dims_y.erase(dims_y.begin()); } diff --git a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc index 505294bad77..542b31790f4 100644 --- a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc +++ b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc @@ -56,7 +56,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { if (x_dims != y_dims_untrimed) { Tensor _x; MKLDNNMemoryFormat format; - std::vector src_x_tz = framework::vectorize2int(x_dims); + std::vector src_x_tz = framework::vectorize(x_dims); if ((src_x_tz.size() == 3 && x->format() != (format = MKLDNNMemoryFormat::ncw)) || @@ -129,9 +129,9 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { PADDLE_ENFORCE_NE(y->format(), MKLDNNMemoryFormat::format_undef, "Wrong format set for Y tensor"); - std::vector src_x_tz = framework::vectorize2int(x_dims); - std::vector src_y_tz = framework::vectorize2int(y_dims_untrimed); - std::vector dst_tz = framework::vectorize2int(z_dims); + std::vector src_x_tz = framework::vectorize(x_dims); + std::vector src_y_tz = framework::vectorize(y_dims_untrimed); + std::vector dst_tz = framework::vectorize(z_dims); std::vector srcs_pd; std::vector scales = {1.0f, 1.0f}; diff --git a/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc b/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc index 43aa1fcfb1f..cc723844278 100644 --- a/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc +++ b/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc @@ -48,7 +48,7 @@ static void ReorderInput(framework::Tensor* tensor, const platform::Place& place, const mkldnn::engine& engine, bool isFourDim) { using platform::to_void_cast; - auto dims = paddle::framework::vectorize2int(tensor->dims()); + auto dims = paddle::framework::vectorize(tensor->dims()); framework::Tensor out_tensor; out_tensor.Resize(tensor->dims()); out_tensor.set_format(isFourDim ? MKLDNNMemoryFormat::nchw @@ -80,7 +80,7 @@ class ElementwiseMulMKLDNNKernel : public framework::OpKernel { auto x_dims = x->dims(); auto y_dims_untrimmed = y->dims(); - auto x_int_dims = paddle::framework::vectorize2int(x_dims); + auto x_int_dims = paddle::framework::vectorize(x_dims); UpdateDataFormat(ctx, const_cast(x), "x_data_format"); UpdateDataFormat(ctx, const_cast(y), "y_data_format"); diff --git a/paddle/fluid/operators/stack_op.h b/paddle/fluid/operators/stack_op.h index 3d132e4397e..9ebf166d703 100644 --- a/paddle/fluid/operators/stack_op.h +++ b/paddle/fluid/operators/stack_op.h @@ -14,6 +14,7 @@ #pragma once +#include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/platform/for_range.h" @@ -50,7 +51,7 @@ class StackOp : public framework::OperatorWithKernel { "Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank); if (axis < 0) axis += (rank + 1); - auto vec = framework::vectorize2int(input_dims[0]); + auto vec = framework::vectorize(input_dims[0]); vec.insert(vec.begin() + axis, input_dims.size()); ctx->SetOutputDim("Y", framework::make_ddim(vec)); } @@ -196,7 +197,7 @@ class StackOpGrad : public framework::OperatorWithKernel { PADDLE_ENFORCE_EQ(ctx->Outputs(framework::GradVarName("X")).size(), static_cast(dy_dim[axis]), "Number of Outputs(X@Grad) is wrong"); - auto vec = framework::vectorize2int(dy_dim); + auto vec = framework::vectorize(dy_dim); vec.erase(vec.begin() + axis); ctx->SetOutputsDim( framework::GradVarName("X"), diff --git a/paddle/fluid/operators/unstack_op.h b/paddle/fluid/operators/unstack_op.h index 348a1038804..6247a1f1d94 100644 --- a/paddle/fluid/operators/unstack_op.h +++ b/paddle/fluid/operators/unstack_op.h @@ -14,6 +14,7 @@ #pragma once +#include #include "paddle/fluid/framework/op_registry.h" namespace paddle { @@ -38,7 +39,7 @@ class UnStackOpInferShape : public framework::InferShapeBase { if (x_dim[axis] > 0) { PADDLE_ENFORCE_EQ(num, x_dim[axis], "Number of Outputs(Y) is wrong"); } - auto vec = framework::vectorize2int(x_dim); + auto vec = framework::vectorize(x_dim); vec.erase(vec.begin() + axis); ctx->SetOutputsDim("Y", std::vector( // NOLINT x_dim[axis], framework::make_ddim(vec))); @@ -96,7 +97,7 @@ class UnStackOpGradInferShape : public framework::InferShapeBase { "Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank); if (axis < 0) axis += (rank + 1); - auto vec = framework::vectorize2int(input_dims[0]); + auto vec = framework::vectorize(input_dims[0]); vec.insert(vec.begin() + axis, input_dims.size()); ctx->SetOutputDim(framework::GradVarName("X"), framework::make_ddim(vec)); } diff --git a/paddle/fluid/platform/cudnn_desc.h b/paddle/fluid/platform/cudnn_desc.h index 39a50b3bc99..f503dfb3878 100644 --- a/paddle/fluid/platform/cudnn_desc.h +++ b/paddle/fluid/platform/cudnn_desc.h @@ -102,7 +102,7 @@ class TensorDescriptor { T* desc() { return desc_.get(); } T* desc() const { return desc_.get(); } void set(const Tensor& tensor, const int groups = 1) { - auto dims = framework::vectorize2int(tensor.dims()); + auto dims = framework::vectorize(tensor.dims()); std::vector strides(dims.size()); strides[dims.size() - 1] = 1; for (int i = dims.size() - 2; i >= 0; i--) { @@ -142,7 +142,7 @@ class FilterDescriptor { void set(const Tensor& tensor, const cudnnTensorFormat_t format, const int groups = 1) { - auto dims = framework::vectorize2int(tensor.dims()); + auto dims = framework::vectorize(tensor.dims()); if (groups > 1) { dims[1] = dims[1] / groups; } diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 999251ff57b..82867faff82 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -290,7 +290,7 @@ void BindImperative(py::module *m_ptr) { "shape", [](imperative::VarBase &self) { if (self.Var().IsType()) { - return framework::vectorize2int( + return framework::vectorize( self.Var().Get().dims()); } else { VLOG(2) << "It is meaningless to get shape of variable type " -- GitLab