diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 9259bb740a8a2408e4dc7be21711560fdf250752..d8251c7255320f89bca15f860b1b7559aa54ffd6 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 a04d2834eb94c2d8df9c6e48782d10bb3254a6dd..be3f06360d66453ae18fdc9abbf6ea4b29491248 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 6fe30630e9683f59044b216b9e9b1f7dd647b1e2..416da3b54aa58e92f9e9fc0112680f07d11159ff 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; }