From 2f639113eeb0d006dfe7ad4b2543df3ff1df172e Mon Sep 17 00:00:00 2001 From: chengduo Date: Tue, 30 Oct 2018 15:51:37 +0800 Subject: [PATCH] Fix sum_op's GetExpectedKernelType (#14112) * fix sum_op's GetExpectedKernelType test=develop * fix ci fail test=develop --- paddle/fluid/framework/operator.cc | 2 +- paddle/fluid/framework/operator.h | 1 + paddle/fluid/operators/sum_op.cc | 10 ++++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 14fcde2fe..9259bb740 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 626b50edf..a04d2834e 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 34dbac2ab..6fe30630e 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -82,14 +82,16 @@ 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) { + // FIXME(zcd): The input x_var may be SelectedRows or LoDTensor. + auto tensor = framework::GetTensorFromVar( + const_cast(x_var)); + 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, -- GitLab