提交 f05d2c51 编写于 作者: T Tao Luo 提交者: Zeng Jinle

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
上级 103d0916
...@@ -41,7 +41,7 @@ void Conv2dOpConverter<TargetT, PrecisionT>::operator()( ...@@ -41,7 +41,7 @@ void Conv2dOpConverter<TargetT, PrecisionT>::operator()(
auto *filter_v = scope.FindVar(op_desc.Input("Filter").front()); auto *filter_v = scope.FindVar(op_desc.Input("Filter").front());
PADDLE_ENFORCE_NOT_NULL(filter_v); PADDLE_ENFORCE_NOT_NULL(filter_v);
auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace());
auto weight_shape = framework::vectorize2int(weight_tensor->dims()); auto weight_shape = framework::vectorize<int>(weight_tensor->dims());
PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL); PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL);
......
...@@ -43,7 +43,7 @@ void Conv2dFusionOpConverter<TargetT, PrecisionT>::operator()( ...@@ -43,7 +43,7 @@ void Conv2dFusionOpConverter<TargetT, PrecisionT>::operator()(
PADDLE_ENFORCE_NOT_NULL(filter_v); PADDLE_ENFORCE_NOT_NULL(filter_v);
auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace());
auto weight_shape = framework::vectorize2int(weight_tensor->dims()); auto weight_shape = framework::vectorize<int>(weight_tensor->dims());
auto *b_v = scope.FindVar(op_desc.Input("Bias").front()); auto *b_v = scope.FindVar(op_desc.Input("Bias").front());
PADDLE_ENFORCE_NOT_NULL(b_v); PADDLE_ENFORCE_NOT_NULL(b_v);
...@@ -99,7 +99,7 @@ void Conv2dFusionOpConverter<TargetT, PrecisionT>::operator()( ...@@ -99,7 +99,7 @@ void Conv2dFusionOpConverter<TargetT, PrecisionT>::operator()(
this->engine_->AddTensorScale(input_name, in_scale / int8_range); this->engine_->AddTensorScale(input_name, in_scale / int8_range);
} else { } else {
auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace());
auto weight_shape = framework::vectorize2int(weight_tensor->dims()); auto weight_shape = framework::vectorize<int>(weight_tensor->dims());
auto *weight1 = pblock_from_tensor<TargetT, PrecisionT>( auto *weight1 = pblock_from_tensor<TargetT, PrecisionT>(
*weight_tensor, weight_shape, this->engine_); *weight_tensor, weight_shape, this->engine_);
this->engine_->AddOpAttr(op_name, "weight_1", *weight1); this->engine_->AddOpAttr(op_name, "weight_1", *weight1);
......
...@@ -43,7 +43,7 @@ void FcBaseOpConverter<TargetT, PrecisionT>::operator()( ...@@ -43,7 +43,7 @@ void FcBaseOpConverter<TargetT, PrecisionT>::operator()(
auto *y_v = scope.FindVar(op_desc.Input(w_name).front()); auto *y_v = scope.FindVar(op_desc.Input(w_name).front());
PADDLE_ENFORCE_NOT_NULL(y_v); PADDLE_ENFORCE_NOT_NULL(y_v);
auto weight_tensor = tensor_from_var(*y_v, platform::CPUPlace()); auto weight_tensor = tensor_from_var(*y_v, platform::CPUPlace());
auto weight_shape = framework::vectorize2int(weight_tensor->dims()); auto weight_shape = framework::vectorize<int>(weight_tensor->dims());
int out_dim = weight_shape[1]; int out_dim = weight_shape[1];
const int w_m = weight_shape[0]; const int w_m = weight_shape[0];
......
...@@ -86,7 +86,7 @@ template <typename TargetT, ::anakin::Precision PrecisionT> ...@@ -86,7 +86,7 @@ template <typename TargetT, ::anakin::Precision PrecisionT>
PBlock<TargetT>* pblock_from_var(const framework::Variable& var, PBlock<TargetT>* pblock_from_var(const framework::Variable& var,
AnakinEngine<TargetT, PrecisionT>* engine) { AnakinEngine<TargetT, PrecisionT>* engine) {
auto tensor = tensor_from_var(var, platform::CPUPlace()); auto tensor = tensor_from_var(var, platform::CPUPlace());
auto shape = framework::vectorize2int(tensor->dims()); auto shape = framework::vectorize<int>(tensor->dims());
return pblock_from_tensor<TargetT, PrecisionT>(*tensor, shape, engine); return pblock_from_tensor<TargetT, PrecisionT>(*tensor, shape, engine);
} }
......
...@@ -136,7 +136,7 @@ class AnakinConvertValidation { ...@@ -136,7 +136,7 @@ class AnakinConvertValidation {
if (parameters_.count(input)) continue; if (parameters_.count(input)) continue;
auto& t = inference::analysis::GetFromScope<framework::LoDTensor>(*scope_, auto& t = inference::analysis::GetFromScope<framework::LoDTensor>(*scope_,
input); input);
auto t_shape = framework::vectorize2int(t.dims()); auto t_shape = framework::vectorize<int>(t.dims());
while (t_shape.size() < 4) { while (t_shape.size() < 4) {
t_shape.push_back(1); t_shape.push_back(1);
} }
......
...@@ -86,7 +86,7 @@ void AnakinEngine<TargetT, PrecisionType, RunType>::BindInput( ...@@ -86,7 +86,7 @@ void AnakinEngine<TargetT, PrecisionType, RunType>::BindInput(
auto *tensor = input.second; auto *tensor = input.second;
auto *data = tensor->data<float>(); auto *data = tensor->data<float>();
auto fluid_input_shape = framework::vectorize2int(tensor->dims()); auto fluid_input_shape = framework::vectorize<int>(tensor->dims());
while (fluid_input_shape.size() < 4) { while (fluid_input_shape.size() < 4) {
fluid_input_shape.push_back(1); fluid_input_shape.push_back(1);
} }
......
...@@ -48,7 +48,7 @@ PaddleTensor LodTensorToPaddleTensor(framework::LoDTensor* t) { ...@@ -48,7 +48,7 @@ PaddleTensor LodTensorToPaddleTensor(framework::LoDTensor* t) {
} else { } else {
LOG(FATAL) << "unsupported type."; LOG(FATAL) << "unsupported type.";
} }
pt.shape = framework::vectorize2int(t->dims()); pt.shape = framework::vectorize<int>(t->dims());
return pt; return pt;
} }
......
...@@ -172,7 +172,7 @@ void *ZeroCopyTensor::FindTensor() const { ...@@ -172,7 +172,7 @@ void *ZeroCopyTensor::FindTensor() const {
std::vector<int> ZeroCopyTensor::shape() const { std::vector<int> ZeroCopyTensor::shape() const {
EAGER_GET_TENSOR; EAGER_GET_TENSOR;
PADDLE_ENFORCE(tensor_, "not found tensor called %s in the scope", name_); PADDLE_ENFORCE(tensor_, "not found tensor called %s in the scope", name_);
return framework::vectorize2int(tensor->dims()); return framework::vectorize<int>(tensor->dims());
} }
void ZeroCopyTensor::SetLoD(const std::vector<std::vector<size_t>> &x) { void ZeroCopyTensor::SetLoD(const std::vector<std::vector<size_t>> &x) {
......
...@@ -61,7 +61,7 @@ class ElementwiseWeightOpConverter : public OpConverter { ...@@ -61,7 +61,7 @@ class ElementwiseWeightOpConverter : public OpConverter {
auto scale_mode = nvinfer1::ScaleMode::kELEMENTWISE; auto scale_mode = nvinfer1::ScaleMode::kELEMENTWISE;
std::vector<int> dims_y = framework::vectorize2int(Y_t->dims()); std::vector<int> dims_y = framework::vectorize<int>(Y_t->dims());
if (static_cast<int>(dims_y.size()) == dims_x.nbDims + 1) { if (static_cast<int>(dims_y.size()) == dims_x.nbDims + 1) {
if (dims_y[0] == 1) dims_y.erase(dims_y.begin()); if (dims_y[0] == 1) dims_y.erase(dims_y.begin());
} }
......
...@@ -56,7 +56,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> { ...@@ -56,7 +56,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> {
if (x_dims != y_dims_untrimed) { if (x_dims != y_dims_untrimed) {
Tensor _x; Tensor _x;
MKLDNNMemoryFormat format; MKLDNNMemoryFormat format;
std::vector<int> src_x_tz = framework::vectorize2int(x_dims); std::vector<int> src_x_tz = framework::vectorize<int>(x_dims);
if ((src_x_tz.size() == 3 && if ((src_x_tz.size() == 3 &&
x->format() != (format = MKLDNNMemoryFormat::ncw)) || x->format() != (format = MKLDNNMemoryFormat::ncw)) ||
...@@ -129,9 +129,9 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> { ...@@ -129,9 +129,9 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE_NE(y->format(), MKLDNNMemoryFormat::format_undef, PADDLE_ENFORCE_NE(y->format(), MKLDNNMemoryFormat::format_undef,
"Wrong format set for Y tensor"); "Wrong format set for Y tensor");
std::vector<int> src_x_tz = framework::vectorize2int(x_dims); std::vector<int> src_x_tz = framework::vectorize<int>(x_dims);
std::vector<int> src_y_tz = framework::vectorize2int(y_dims_untrimed); std::vector<int> src_y_tz = framework::vectorize<int>(y_dims_untrimed);
std::vector<int> dst_tz = framework::vectorize2int(z_dims); std::vector<int> dst_tz = framework::vectorize<int>(z_dims);
std::vector<memory::primitive_desc> srcs_pd; std::vector<memory::primitive_desc> srcs_pd;
std::vector<float> scales = {1.0f, 1.0f}; std::vector<float> scales = {1.0f, 1.0f};
......
...@@ -48,7 +48,7 @@ static void ReorderInput(framework::Tensor* tensor, ...@@ -48,7 +48,7 @@ static void ReorderInput(framework::Tensor* tensor,
const platform::Place& place, const platform::Place& place,
const mkldnn::engine& engine, bool isFourDim) { const mkldnn::engine& engine, bool isFourDim) {
using platform::to_void_cast; using platform::to_void_cast;
auto dims = paddle::framework::vectorize2int(tensor->dims()); auto dims = paddle::framework::vectorize<int>(tensor->dims());
framework::Tensor out_tensor; framework::Tensor out_tensor;
out_tensor.Resize(tensor->dims()); out_tensor.Resize(tensor->dims());
out_tensor.set_format(isFourDim ? MKLDNNMemoryFormat::nchw out_tensor.set_format(isFourDim ? MKLDNNMemoryFormat::nchw
...@@ -80,7 +80,7 @@ class ElementwiseMulMKLDNNKernel : public framework::OpKernel<T> { ...@@ -80,7 +80,7 @@ class ElementwiseMulMKLDNNKernel : public framework::OpKernel<T> {
auto x_dims = x->dims(); auto x_dims = x->dims();
auto y_dims_untrimmed = y->dims(); auto y_dims_untrimmed = y->dims();
auto x_int_dims = paddle::framework::vectorize2int(x_dims); auto x_int_dims = paddle::framework::vectorize<int>(x_dims);
UpdateDataFormat(ctx, const_cast<Tensor*>(x), "x_data_format"); UpdateDataFormat(ctx, const_cast<Tensor*>(x), "x_data_format");
UpdateDataFormat(ctx, const_cast<Tensor*>(y), "y_data_format"); UpdateDataFormat(ctx, const_cast<Tensor*>(y), "y_data_format");
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#pragma once #pragma once
#include <memory>
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/for_range.h" #include "paddle/fluid/platform/for_range.h"
...@@ -50,7 +51,7 @@ class StackOp : public framework::OperatorWithKernel { ...@@ -50,7 +51,7 @@ class StackOp : public framework::OperatorWithKernel {
"Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank); "Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank);
if (axis < 0) axis += (rank + 1); if (axis < 0) axis += (rank + 1);
auto vec = framework::vectorize2int(input_dims[0]); auto vec = framework::vectorize<int>(input_dims[0]);
vec.insert(vec.begin() + axis, input_dims.size()); vec.insert(vec.begin() + axis, input_dims.size());
ctx->SetOutputDim("Y", framework::make_ddim(vec)); ctx->SetOutputDim("Y", framework::make_ddim(vec));
} }
...@@ -196,7 +197,7 @@ class StackOpGrad : public framework::OperatorWithKernel { ...@@ -196,7 +197,7 @@ class StackOpGrad : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ(ctx->Outputs(framework::GradVarName("X")).size(), PADDLE_ENFORCE_EQ(ctx->Outputs(framework::GradVarName("X")).size(),
static_cast<size_t>(dy_dim[axis]), static_cast<size_t>(dy_dim[axis]),
"Number of Outputs(X@Grad) is wrong"); "Number of Outputs(X@Grad) is wrong");
auto vec = framework::vectorize2int(dy_dim); auto vec = framework::vectorize<int>(dy_dim);
vec.erase(vec.begin() + axis); vec.erase(vec.begin() + axis);
ctx->SetOutputsDim( ctx->SetOutputsDim(
framework::GradVarName("X"), framework::GradVarName("X"),
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#pragma once #pragma once
#include <memory>
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
namespace paddle { namespace paddle {
...@@ -38,7 +39,7 @@ class UnStackOpInferShape : public framework::InferShapeBase { ...@@ -38,7 +39,7 @@ class UnStackOpInferShape : public framework::InferShapeBase {
if (x_dim[axis] > 0) { if (x_dim[axis] > 0) {
PADDLE_ENFORCE_EQ(num, x_dim[axis], "Number of Outputs(Y) is wrong"); PADDLE_ENFORCE_EQ(num, x_dim[axis], "Number of Outputs(Y) is wrong");
} }
auto vec = framework::vectorize2int(x_dim); auto vec = framework::vectorize<int>(x_dim);
vec.erase(vec.begin() + axis); vec.erase(vec.begin() + axis);
ctx->SetOutputsDim("Y", std::vector<framework::DDim>( // NOLINT ctx->SetOutputsDim("Y", std::vector<framework::DDim>( // NOLINT
x_dim[axis], framework::make_ddim(vec))); x_dim[axis], framework::make_ddim(vec)));
...@@ -96,7 +97,7 @@ class UnStackOpGradInferShape : public framework::InferShapeBase { ...@@ -96,7 +97,7 @@ class UnStackOpGradInferShape : public framework::InferShapeBase {
"Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank); "Attr(axis) must be inside [-(rank+1), rank+1), where rank = %d", rank);
if (axis < 0) axis += (rank + 1); if (axis < 0) axis += (rank + 1);
auto vec = framework::vectorize2int(input_dims[0]); auto vec = framework::vectorize<int>(input_dims[0]);
vec.insert(vec.begin() + axis, input_dims.size()); vec.insert(vec.begin() + axis, input_dims.size());
ctx->SetOutputDim(framework::GradVarName("X"), framework::make_ddim(vec)); ctx->SetOutputDim(framework::GradVarName("X"), framework::make_ddim(vec));
} }
......
...@@ -102,7 +102,7 @@ class TensorDescriptor { ...@@ -102,7 +102,7 @@ class TensorDescriptor {
T* desc() { return desc_.get(); } T* desc() { return desc_.get(); }
T* desc() const { return desc_.get(); } T* desc() const { return desc_.get(); }
void set(const Tensor& tensor, const int groups = 1) { void set(const Tensor& tensor, const int groups = 1) {
auto dims = framework::vectorize2int(tensor.dims()); auto dims = framework::vectorize<int>(tensor.dims());
std::vector<int> strides(dims.size()); std::vector<int> strides(dims.size());
strides[dims.size() - 1] = 1; strides[dims.size() - 1] = 1;
for (int i = dims.size() - 2; i >= 0; i--) { for (int i = dims.size() - 2; i >= 0; i--) {
...@@ -142,7 +142,7 @@ class FilterDescriptor { ...@@ -142,7 +142,7 @@ class FilterDescriptor {
void set(const Tensor& tensor, const cudnnTensorFormat_t format, void set(const Tensor& tensor, const cudnnTensorFormat_t format,
const int groups = 1) { const int groups = 1) {
auto dims = framework::vectorize2int(tensor.dims()); auto dims = framework::vectorize<int>(tensor.dims());
if (groups > 1) { if (groups > 1) {
dims[1] = dims[1] / groups; dims[1] = dims[1] / groups;
} }
......
...@@ -290,7 +290,7 @@ void BindImperative(py::module *m_ptr) { ...@@ -290,7 +290,7 @@ void BindImperative(py::module *m_ptr) {
"shape", "shape",
[](imperative::VarBase &self) { [](imperative::VarBase &self) {
if (self.Var().IsType<framework::LoDTensor>()) { if (self.Var().IsType<framework::LoDTensor>()) {
return framework::vectorize2int( return framework::vectorize<int>(
self.Var().Get<framework::LoDTensor>().dims()); self.Var().Get<framework::LoDTensor>().dims());
} else { } else {
VLOG(2) << "It is meaningless to get shape of variable type " VLOG(2) << "It is meaningless to get shape of variable type "
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册