1 /** 2 * Copyright 2019-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 #ifndef MINDSPORE_CCSRC_PLUGIN_DEVICE_CPU_OPTIMIZER_INSERT_CAST_TO_PYEXECUTE_H_ 17 #define MINDSPORE_CCSRC_PLUGIN_DEVICE_CPU_OPTIMIZER_INSERT_CAST_TO_PYEXECUTE_H_ 18 19 #include <string> 20 #include <utility> 21 #include "plugin/device/cpu/optimizer/cpu_pass_utils.h" 22 #include "include/backend/optimizer/optimizer.h" 23 #include "ir/anf.h" 24 25 namespace mindspore { 26 namespace opt { 27 using InsertCastFunction = std::function<AnfNodePtr(const FuncGraphPtr &, const AnfNodePtr &, const std::string &, 28 const TypeId &, const TypeId &, const abstract::BaseShapePtr &)>; 29 class InsertCastToPyExecute : public PatternProcessPass { 30 public: 31 explicit InsertCastToPyExecute(bool multigraph = true) 32 : PatternProcessPass("insert_cast_to_pyexecute", multigraph), insert_cast_function_(AddCastOpNodeToGraph) {} 33 explicit InsertCastToPyExecute(InsertCastFunction func, bool multigraph = true) 34 : PatternProcessPass("insert_cast_to_pyexecute", multigraph), insert_cast_function_(std::move(func)) {} 35 ~InsertCastToPyExecute() override = default; 36 const BaseRef DefinePattern() const override; 37 const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &node, const EquivPtr &) const override; 38 39 private: 40 std::function<AnfNodePtr(const FuncGraphPtr &, const AnfNodePtr &, const std::string &, const TypeId &, 41 const TypeId &, const abstract::BaseShapePtr &)> 42 insert_cast_function_; 43 }; 44 } // namespace opt 45 } // namespace mindspore 46 47 #endif // MINDSPORE_CCSRC_PLUGIN_DEVICE_CPU_OPTIMIZER_INSERT_CAST_TO_PYEXECUTE_H_ 48