From dc9e23c4e24df8ce4b04e5751ae3cdbb93de2da4 Mon Sep 17 00:00:00 2001 From: chengduozh Date: Mon, 29 Oct 2018 15:18:50 +0800 Subject: [PATCH 1/3] fix sum_op's GetExpectedKernelType test=develop --- paddle/fluid/operators/sum_op.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index 34dbac2ab8dcc..cc3d8db3b5a34 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -81,15 +81,17 @@ class SumOp : public framework::OperatorWithKernel { if (x_vars[0]->IsType()) { int dtype = -1; - for (auto& x_var : x_vars) { - auto& lod_tensor = x_var->Get(); - if (lod_tensor.numel() == 0) { + auto x_var_names = ctx.Inputs("X"); + for (auto& x_var_n : x_var_names) { + // FIXME(zcd): The input x_var may be SelectedRows or LoDTensor. + auto tensor = ctx.Input(x_var_n); + if (tensor->numel() == 0) { continue; } if (dtype == -1) { - dtype = framework::ToDataType(lod_tensor.type()); + dtype = framework::ToDataType(tensor->type()); } else { - PADDLE_ENFORCE_EQ(dtype, framework::ToDataType(lod_tensor.type())); + PADDLE_ENFORCE_EQ(dtype, framework::ToDataType(tensor->type())); } } PADDLE_ENFORCE_NE(dtype, -1, From 69c2acbdb925e8a37467752512f2a906206611d2 Mon Sep 17 00:00:00 2001 From: chengduozh Date: Mon, 29 Oct 2018 19:57:55 +0800 Subject: [PATCH 2/3] fix ci fail test=release/1.1 --- paddle/fluid/framework/operator.cc | 2 +- paddle/fluid/framework/operator.h | 1 + paddle/fluid/operators/sum_op.cc | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 14fcde2fe3b1c..9259bb740a8a2 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -358,7 +358,7 @@ static bool VarIsTensor(const Variable* var) { return var->IsType() || var->IsType(); } -static const Tensor* GetTensorFromVar(Variable* var) { +const Tensor* GetTensorFromVar(Variable* var) { if (var->IsType()) { return var->GetMutable(); } else if (var->IsType()) { diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index 626b50edfd394..a04d2834eb94c 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -63,6 +63,7 @@ inline std::string GradVarName(const std::string& var_name) { } proto::VarType::Type GetDataTypeOfVar(const Variable* var); +const Tensor* GetTensorFromVar(Variable* var); class OperatorBase; class ExecutionContext; diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index cc3d8db3b5a34..6fe30630e9683 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -81,10 +81,10 @@ class SumOp : public framework::OperatorWithKernel { if (x_vars[0]->IsType()) { int dtype = -1; - auto x_var_names = ctx.Inputs("X"); - for (auto& x_var_n : x_var_names) { + for (auto& x_var : x_vars) { // FIXME(zcd): The input x_var may be SelectedRows or LoDTensor. - auto tensor = ctx.Input(x_var_n); + auto tensor = framework::GetTensorFromVar( + const_cast(x_var)); if (tensor->numel() == 0) { continue; } From 1c43ef49afb2579cffd651c24bfc597113b1d549 Mon Sep 17 00:00:00 2001 From: chengduozh Date: Tue, 30 Oct 2018 16:17:26 +0800 Subject: [PATCH 3/3] fix GetTensorFromVar test=release/1.1 --- paddle/fluid/framework/operator.cc | 9 ++++----- paddle/fluid/framework/operator.h | 2 +- paddle/fluid/operators/sum_op.cc | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 9259bb740a8a2..d8251c7255320 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -358,11 +358,11 @@ static bool VarIsTensor(const Variable* var) { return var->IsType() || var->IsType(); } -const Tensor* GetTensorFromVar(Variable* var) { +const Tensor* GetTensorFromVar(const Variable* var) { if (var->IsType()) { - return var->GetMutable(); + return static_cast(&(var->Get())); } else if (var->IsType()) { - return var->GetMutable()->mutable_value(); + return &(var->Get().value()); } else { PADDLE_THROW("Variable type_id %s, expect LoDTensor/SelectedRows.", var->Type().name()); @@ -415,8 +415,7 @@ bool ExecutionContext::HasOutput(const std::string& name) const { template <> const Tensor* ExecutionContext::Input(const std::string& name) const { auto* var = InputVar(name); - return var == nullptr ? nullptr - : GetTensorFromVar(const_cast(var)); + return var == nullptr ? nullptr : GetTensorFromVar(var); } template <> diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index a04d2834eb94c..be3f06360d664 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -63,7 +63,7 @@ inline std::string GradVarName(const std::string& var_name) { } proto::VarType::Type GetDataTypeOfVar(const Variable* var); -const Tensor* GetTensorFromVar(Variable* var); +const Tensor* GetTensorFromVar(const Variable* var); class OperatorBase; class ExecutionContext; diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index 6fe30630e9683..416da3b54aa58 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -83,8 +83,7 @@ class SumOp : public framework::OperatorWithKernel { int dtype = -1; for (auto& x_var : x_vars) { // FIXME(zcd): The input x_var may be SelectedRows or LoDTensor. - auto tensor = framework::GetTensorFromVar( - const_cast(x_var)); + auto tensor = framework::GetTensorFromVar(x_var); if (tensor->numel() == 0) { continue; }