From 577535b3875cd84b8a9ef2c82de3d2ac9e1fa5dd Mon Sep 17 00:00:00 2001 From: kingfo Date: Mon, 3 Aug 2020 17:18:29 +0800 Subject: [PATCH] fix gradients issue in pynative --- .../pipeline/pynative/pynative_execute.cc | 30 +++++++++++++------ mindspore/common/tensor.py | 6 ++++ tests/ut/python/ir/test_tensor.py | 4 +++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/mindspore/ccsrc/pipeline/pynative/pynative_execute.cc b/mindspore/ccsrc/pipeline/pynative/pynative_execute.cc index 4bfe438b2f0..4e2de1dc2c8 100644 --- a/mindspore/ccsrc/pipeline/pynative/pynative_execute.cc +++ b/mindspore/ccsrc/pipeline/pynative/pynative_execute.cc @@ -316,7 +316,10 @@ OpExecInfoPtr GenerateOpExecInfo(const py::args &args, py::list *const out_args) } op_exec_info->py_primitive = prim; op_exec_info->op_attrs = py::getattr(args[PY_PRIM], "attrs"); - op_exec_info->value = PynativeExecutor::GetInstance()->GetForwardValue(op_exec_info); + auto inst = PynativeExecutor::GetInstance(); + if (inst->grad_flag()) { + op_exec_info->value = inst->GetForwardValue(op_exec_info); + } if (op_exec_info->op_inputs.size() != op_exec_info->inputs_mask.size()) { MS_LOG(ERROR) << "Op:" << op_exec_info->op_name << " inputs size not equal op_mask"; return nullptr; @@ -1029,15 +1032,24 @@ std::vector PynativeExecutor::GetWeightsArgs(const py::object &weigh AnfNodePtr para_node = nullptr; if (graph_info_map_[df_builder_].param_map.count(param_id)) { para_node = graph_info_map_[df_builder_].param_map[param_id]; - - AnfNodePtr value = parse::GetMixedPrecisionCastHelp(df_builder_, para_node); - AnfNodePtr make_ref = NewValueNode(prim::kPrimMakeRef); - auto refkey = std::make_shared(para_node->cast()->name()); - AnfNodePtr ref_key_node = NewValueNode(refkey); - AnfNodePtr ref_node = df_builder_->NewCNode({make_ref, ref_key_node, value, para_node}); - - w_args.push_back(ref_node); + } else { + auto name_attr = mindspore::parse::python_adapter::GetPyObjAttr(param, "name"); + if (py::isinstance(name_attr)) { + MS_LOG(EXCEPTION) << "Parameter object should have name attribute"; + } + auto param_name = py::cast(name_attr); + auto free_param = df_builder_->add_parameter(); + free_param->set_name(param_name); + free_param->set_default_param(py::cast(param)); + free_param->debug_info()->set_name(param_name); + para_node = free_param; } + AnfNodePtr value = parse::GetMixedPrecisionCastHelp(df_builder_, para_node); + AnfNodePtr make_ref = NewValueNode(prim::kPrimMakeRef); + auto refkey = std::make_shared(para_node->cast()->name()); + AnfNodePtr ref_key_node = NewValueNode(refkey); + AnfNodePtr ref_node = df_builder_->NewCNode({make_ref, ref_key_node, value, para_node}); + w_args.push_back(ref_node); } } else { MS_LOG(DEBUG) << "training not paramter_tuple"; diff --git a/mindspore/common/tensor.py b/mindspore/common/tensor.py index 1ad383ad1b4..8de1c084605 100644 --- a/mindspore/common/tensor.py +++ b/mindspore/common/tensor.py @@ -185,6 +185,9 @@ class Tensor(Tensor_): def __imod__(self, other): return self.__mod__(other) + def __rmod__(self, other): + return tensor_operator_registry.get('__mod__')(other, self) + def __pow__(self, other): return tensor_operator_registry.get('__pow__')(self, other) @@ -194,6 +197,9 @@ class Tensor(Tensor_): def __ifloordiv__(self, other): return self.__floordiv__(other) + def __rfloordiv__(self, other): + return tensor_operator_registry.get('__floordiv__')(other, self) + def __str__(self): if self.dtype == mstype.type_none: return "Unknown Tensor type!" diff --git a/tests/ut/python/ir/test_tensor.py b/tests/ut/python/ir/test_tensor.py index b8631e81dc6..762e5b175ab 100644 --- a/tests/ut/python/ir/test_tensor.py +++ b/tests/ut/python/ir/test_tensor.py @@ -472,3 +472,7 @@ def test_tensor_operation(): assert np.all(x.asnumpy() == np.ones((3, 3))) with pytest.raises(ValueError): res = x * (2, 3) + res = 5 % x + assert np.all(x.asnumpy() == np.ones((3, 3))) + res = 5 // x + assert np.all(x.asnumpy() == np.ones((3, 3)))