From 557a40428235e41481a3badae2b435b0e25644e2 Mon Sep 17 00:00:00 2001 From: kingfo Date: Mon, 8 Jun 2020 17:46:25 +0800 Subject: [PATCH] fix tuple args issue in pynative --- mindspore/ccsrc/pynative/pynative_execute.cc | 21 ++++++++++++++++++++ mindspore/common/tensor.py | 3 +++ mindspore/ops/operations/array_ops.py | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/mindspore/ccsrc/pynative/pynative_execute.cc b/mindspore/ccsrc/pynative/pynative_execute.cc index b9a93774a2..e95ea3b3a1 100644 --- a/mindspore/ccsrc/pynative/pynative_execute.cc +++ b/mindspore/ccsrc/pynative/pynative_execute.cc @@ -661,6 +661,20 @@ AnfNodePtr PynativeExecutor::GetInput(const py::object &obj, const py::object &o // out = op(cell1(x, y)) // out = op(cell1(x, y)[0]) node = GetObjNode(obj); + } else if (py::isinstance(obj)) { + // out = op((x, y)) + // out = cell((x, y)) + std::vector args; + args.push_back(NewValueNode(prim::kPrimMakeTuple)); + + auto tuple = obj.cast(); + auto tuple_size = static_cast(tuple.size()); + for (int i = 0; i < tuple_size; i++) { + args.push_back(GetInput(tuple[i], py::object())); + } + auto cnode = curr_g_->NewCNode(args); + set_obj_node_map(curr_g_, GetId(obj), cnode); + node = cnode; } else { // out = op(x, 1) ValuePtr converted_ret = nullptr; @@ -728,6 +742,13 @@ void PynativeExecutor::EndGraph(const py::object &cell, const py::object &out, c } auto out_cnode = curr_g_->NewCNode(inputs); set_pyobj(curr_g_, GetId(cell)); + if (py::isinstance(out)) { + auto out_list = py::cast(out); + auto out_size = static_cast(out_list.size()); + for (int i = 0; i < out_size; i++) { + set_obj_node_map(curr_g_, GetId(out_list[i]), out_cnode, i); + } + } set_obj_node_map(curr_g_, GetId(out), out_cnode); } else { parse::ResolveFuncGraph(newfg, resource_); diff --git a/mindspore/common/tensor.py b/mindspore/common/tensor.py index ed8f1be0fe..91f44ca3cc 100644 --- a/mindspore/common/tensor.py +++ b/mindspore/common/tensor.py @@ -19,6 +19,7 @@ from .._c_expression import Tensor as Tensor_ from .._c_expression import MetaTensor from .._checkparam import check_type, check_typename from . import dtype as mstype +from .. import context from ._register_for_tensor import tensor_operator_registry __all__ = ['Tensor', 'MetaTensor'] @@ -77,6 +78,8 @@ class Tensor(Tensor_): def __eq__(self, other): if not isinstance(other, Tensor): return False + if context.get_context("enable_ge") or self.dtype() == mstype.bool_ or other.dtype() == mstype.bool_: + return Tensor(np.array(self.asnumpy() == other.asnumpy())) return tensor_operator_registry.get('__eq__')(self, other) def __ne__(self, other): diff --git a/mindspore/ops/operations/array_ops.py b/mindspore/ops/operations/array_ops.py index 68e0c1b7ae..4042be84cc 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -145,8 +145,8 @@ class SameTypeShape(PrimitiveWithInfer): def __call__(self, x, y): """run in PyNative mode""" - validator.check_subclass('x', x.dtype(), mstype.tensor, self.name) - validator.check_subclass('y', y.dtype(), mstype.tensor, self.name) + validator.check_value_type("x", x, Tensor, self.name) + validator.check_value_type("y", y, Tensor, self.name) validator.check('x dtype', x.dtype(), 'y dtype', y.dtype(), Rel.EQ, self.name, TypeError) validator.check('x shape', x.shape(), 'y shape', y.shape(), Rel.EQ, self.name) return x