diff --git a/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc b/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc index d207028a73..7bdf69bf88 100644 --- a/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc +++ b/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc @@ -435,7 +435,7 @@ FuncGraphPtr DFunctor::KUserDefined(const FuncGraphPtr &primal) { FuncGraphPtr bprop_graph = bprop->second.func_graph(); resources_->manager()->AddFuncGraph(bprop_graph); - if (bprop_graph->free_variables_nodes().size() != 0 || primal->free_variables_nodes().size() != 0) { + if (!bprop_graph->free_variables_nodes().empty() || !primal->free_variables_nodes().empty()) { MS_LOG(EXCEPTION) << "User defined Cell bprop " << primal->ToString() << " in scope " << primal->output()->scope()->name() << " does not support Parameter data type."; } diff --git a/mindspore/ccsrc/pipeline/jit/pipeline.cc b/mindspore/ccsrc/pipeline/jit/pipeline.cc index dd4b521274..56c288ace5 100644 --- a/mindspore/ccsrc/pipeline/jit/pipeline.cc +++ b/mindspore/ccsrc/pipeline/jit/pipeline.cc @@ -101,7 +101,7 @@ py::tuple GenerateKey(const std::string &name, const std::unordered_map(arg.second)) { MS_LOG(EXCEPTION) << "GenerateKey failed, argument input should not be py::module"; } @@ -122,7 +122,7 @@ py::tuple GenerateKey(const std::string &name, const std::unordered_map(arg_obj)) { MS_LOG(DEBUG) << "Verify Tensor"; - std::shared_ptr m_tensor = arg_obj.cast>(); + auto m_tensor = arg_obj.cast>(); if (m_tensor == nullptr) { MS_LOG(ERROR) << "Verify Tensor error, get ptr is null"; return false; } - std::shared_ptr sig = input_signature[count].cast>(); + auto sig = input_signature[count].cast>(); ShapeVector sig_shape = sig->shape(); TypePtr sig_type = sig->Dtype(); @@ -303,16 +303,16 @@ std::map> ExecutorPy::FetchI MS_EXCEPTION_IF_NULL(func_graph); MS_LOG(DEBUG) << "FetchInfoForQuantExport func graph(" << func_graph->ToString() << ") phase(" << phase_s << ")!"; std::map> fake_quant_table; - auto filter = [](AnfNodePtr node) { + auto filter = [](const AnfNodePtr &node) { return !(IsPrimitiveCNode(node, prim::kPrimConv2D) || IsPrimitiveCNode(node, prim::kPrimMatMul) || IsPrimitiveCNode(node, prim::kPrimDepthwiseConv2dNative)); }; std::vector nodes = DeepScopedGraphSearchWithFilter(func_graph->get_return(), AlwaysInclude, filter); - auto is_quant_cnode = [](AnfNodePtr node) { + auto is_quant_cnode = [](const AnfNodePtr &node) { return IsPrimitiveCNode(node, prim::kPrimFakeQuantPerLayer) || IsPrimitiveCNode(node, prim::kPrimFakeQuantPerChannel); }; - for (auto node : nodes) { + for (const auto &node : nodes) { auto cnode = node->cast(); if (cnode == nullptr || cnode->size() != 3) { continue; @@ -758,11 +758,24 @@ py::object ExecutorPy::Run(const py::tuple &args, const py::object &phase) { auto ret_val = std::make_shared(); if (info_.count(phase_s) != 0 && info_[phase_s]->func_graph != nullptr) { if (IsGraphOutputValueNodeOrParameter(info_[phase_s]->func_graph->output(), args, ret_val)) { + // Check the input arg must be Tensor when backend is "ms". + if (MsContext::GetInstance()->backend_policy() == kMsConvert) { + for (std::size_t i = 0; i < size; i++) { + ValuePtr converted = nullptr; + if (!parse::ConvertData(args[i], &converted)) { + MS_LOG(EXCEPTION) << "The " << i << "th arg convert failed."; + } + if (!converted->isa()) { + MS_EXCEPTION(TypeError) << "The " << i << "th arg: " << converted->ToString() << " is not tensor."; + } + } + } return *ret_val; } } if (backend == "ge") { - if (args.size() > 0) { + // Virtual output constructed for test cases. + if (!args.empty()) { return args[0]; } return args; @@ -911,9 +924,9 @@ bool InitExecDatasetVm(const std::string &queue_name, int64_t size, int64_t batc return true; } -bool InitRandomNormal(float mean, float stddev, std::vector out_shape, int64_t seed, +bool InitRandomNormal(float mean, float stddev, const std::vector &out_shape, int64_t seed, const py::object &output_tensor) { - if (out_shape.size() == 0) { + if (out_shape.empty()) { std::cout << "output data shape is error" << std::endl; } int64_t total_count = 1; @@ -925,14 +938,14 @@ bool InitRandomNormal(float mean, float stddev, std::vector out_shape, thread_num = 1; } auto temp = py::cast>(output_tensor); - float *start_ptr = reinterpret_cast(temp->data_c()); + auto *start_ptr = reinterpret_cast(temp->data_c()); if (start_ptr == nullptr) { std::cout << "start_ptr is nullptr" << std::endl; return false; } int64_t batchSize = total_count / thread_num; std::vector threads(thread_num); - mindspore::kernel::PhiloxGenerator generator = mindspore::kernel::PhiloxGenerator(seed); + auto generator = mindspore::kernel::PhiloxGenerator(seed); if (thread_num != 1) { for (uint32_t i = 0; i < thread_num - 1; i++) { float *offset_ptr = start_ptr + batchSize * i; diff --git a/mindspore/ccsrc/pipeline/jit/pipeline.h b/mindspore/ccsrc/pipeline/jit/pipeline.h index dc880dd53f..7421520376 100644 --- a/mindspore/ccsrc/pipeline/jit/pipeline.h +++ b/mindspore/ccsrc/pipeline/jit/pipeline.h @@ -116,7 +116,7 @@ using ExecutorPyPtr = std::shared_ptr; // Generate a key for mapping function graph py::tuple GenerateKey(const std::string &name, const std::unordered_map &defaults); -py::bool_ VerifyInputSignature(const py::list input_signature, const py::tuple inputs); +py::bool_ VerifyInputSignature(const py::list &input_signature, const py::tuple &inputs); bool InitDistribute(const std::map &options); @@ -142,7 +142,7 @@ bool InitExecDatasetVm(const std::string &queue_name, int64_t size, int64_t batc const std::vector &input_indexes, bool need_run); // init random normal -bool InitRandomNormal(float mean, float stddev, std::vector outshape, int64_t seed, +bool InitRandomNormal(float mean, float stddev, const std::vector &outshape, int64_t seed, const py::object &outputTensor); void ProcessVmArgInner(const py::tuple &args, const ResourcePtr &res, VectorRef *const arg_list); diff --git a/tests/ut/python/pipeline/infer/test_net_infer.py b/tests/ut/python/pipeline/infer/test_net_infer.py index 51bfcf87cd..719ad294a7 100644 --- a/tests/ut/python/pipeline/infer/test_net_infer.py +++ b/tests/ut/python/pipeline/infer/test_net_infer.py @@ -70,9 +70,9 @@ def test_assign_in_while(): def test_dup_context(): - ''' different func_with_fv in net1 and net2 should produce 2 different FuncGraphAbstractClosure and + """ different func_with_fv in net1 and net2 should produce 2 different FuncGraphAbstractClosure and Evaluator. - ''' + """ context.set_context(mode=context.GRAPH_MODE) class Net(nn.Cell): @@ -102,7 +102,7 @@ def test_dup_context(): def test_maybe_poly_func(): - ''' different func_with_fv in net1 and net2 may produce poly node. ''' + """ different func_with_fv in net1 and net2 may produce poly node. """ context.set_context(mode=context.GRAPH_MODE) class Net(nn.Cell): diff --git a/tests/ut/python/nn/test_structure_output.py b/tests/ut/python/pipeline/parse/test_structure_output.py similarity index 97% rename from tests/ut/python/nn/test_structure_output.py rename to tests/ut/python/pipeline/parse/test_structure_output.py index 5f83dd0aa7..b3ec662c54 100644 --- a/tests/ut/python/nn/test_structure_output.py +++ b/tests/ut/python/pipeline/parse/test_structure_output.py @@ -89,8 +89,7 @@ def test_output_parameter_tuple(): super(Net, self).__init__() def construct(self, x): - ret = x - return ret + return x x = (1, 2, 3) net = Net() @@ -103,8 +102,7 @@ def test_output_parameter_list(): super(Net, self).__init__() def construct(self, x): - ret = x - return ret + return x x = [1, 2, 3] net = Net() @@ -117,8 +115,7 @@ def test_output_parameter_int(): super(Net, self).__init__() def construct(self, x): - ret = x - return ret + return x x = 88 net = Net() @@ -131,8 +128,7 @@ def test_output_parameter_str(): super(Net, self).__init__() def construct(self, x): - ret = x - return ret + return x x = "hello world" net = Net()