1 /** 2 * Copyright 2022 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_BACKEND_OPTIMIZER_GRAPH_KERNEL_TENSOR_INPLACE_H_ 17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TENSOR_INPLACE_H_ 18 19 #include <string> 20 #include "include/backend/optimizer/pass.h" 21 #include "backend/common/graph_kernel/graph_kernel_helper.h" 22 #include "backend/common/graph_kernel/inplace_assign_builder.h" 23 namespace mindspore::graphkernel { 24 /** 25 * @brief Inplace output tensor of a graph kernel to its input tensor if requirements satisfied 26 * @example 27 * main(x, y, z){ 28 * %1 = Op1(x, y) 29 * %2 = subgraph(%1, z) 30 * %3 = Op2(%2) 31 * return %3 32 * } 33 * ---- 34 * subgraph(a, b){ 35 * %1 = Add(a, b) 36 * %2 = Exp(%1) 37 * %3 = Mul(%2, b) 38 * return %3 39 * } 40 * ---------------> 41 * main(x, y, z){ 42 * %1 = Op1(x, y) 43 * %2 = subgraph(%1, z) 44 * %3 = Depend(%1, %2) 45 * %4 = Op2(%3) 46 * return %4 47 * } 48 * ---- 49 * subgraph(a, b){ 50 * %1 = Add(a, b) 51 * %2 = Exp(%1) 52 * %3 = Mul(%2, b) 53 * %4 = Assign(a, %3) 54 * return %4 55 * } 56 */ 57 class TensorInplace : public InplaceAssignBuilder { 58 public: InplaceAssignBuilder(name)59 explicit TensorInplace(const std::string &name = "tensor_inplace") : InplaceAssignBuilder(name) {} 60 ~TensorInplace() override = default; 61 bool Run(const FuncGraphPtr &func_graph) override; 62 63 protected: SetTargetAttrs(const CNodePtr & cnode)64 void SetTargetAttrs(const CNodePtr &cnode) override { 65 SetNodeAttrSafely("enable_auto_inplace", MakeValue(true), cnode); 66 } 67 }; 68 } // namespace mindspore::graphkernel 69 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TENSOR_INPLACE_H_ 70