1 /**
2 * Copyright 2022-2023 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ops/py_execute.h"
18
19 #include <algorithm>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <vector>
24 #include "mindspore/core/ops/framework_ops.h"
25
26 namespace mindspore {
27 namespace ops {
28 MIND_API_OPERATOR_IMPL(PyExecute, BaseOperator);
29
InferPy(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const30 AbstractBasePtr PyExecuteInfer::InferPy(const PrimitivePtr &primitive,
31 const std::vector<AbstractBasePtr> &input_args) const {
32 MS_EXCEPTION_IF_NULL(primitive);
33 for (const auto &item : input_args) {
34 MS_EXCEPTION_IF_NULL(item);
35 MS_LOG(DEBUG) << "item: " << item->ToString();
36 }
37
38 if (infer_handler_ == nullptr) {
39 MS_LOG(EXCEPTION) << "infer_handler_ should not be null.";
40 }
41 std::vector<abstract::AbstractBase *> real_args;
42 std::transform(input_args.begin(), input_args.end(), std::back_inserter(real_args),
43 [](auto &input_arg) { return input_arg.get(); });
44 const auto &abs = infer_handler_(primitive, real_args);
45 MS_LOG(DEBUG) << "output abstract: " << abs;
46 return abs;
47 }
48
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const49 BaseShapePtr PyExecuteInfer::InferShape(const PrimitivePtr &primitive,
50 const std::vector<AbstractBasePtr> &input_args) const {
51 const auto &abs = InferPy(primitive, input_args);
52 MS_EXCEPTION_IF_NULL(abs);
53 return abs->GetShape();
54 }
55
InferType(const PrimitivePtr &,const std::vector<AbstractBasePtr> &) const56 TypePtr PyExecuteInfer::InferType(const PrimitivePtr &, const std::vector<AbstractBasePtr> &) const {
57 MS_LOG(EXCEPTION) << "Should not invoke InferType().";
58 }
59
InferShapeAndType(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const60 AbstractBasePtr PyExecuteInfer::InferShapeAndType(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
61 const std::vector<AbstractBasePtr> &input_args) const {
62 return InferPy(primitive, input_args);
63 }
64
GetValueDependArgIndices() const65 std::set<int64_t> PyExecuteInfer::GetValueDependArgIndices() const { return {-1}; }
66
67 REGISTER_PRIMITIVE_OP_INFER_IMPL(PyExecute, prim::kPrimPyExecute, PyExecuteInfer, false);
68 } // namespace ops
69 } // namespace mindspore
70