1 /** 2 * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TSA_ATOMIC_ADD_TO_FIRST_TENSOR_H_ 18 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TSA_ATOMIC_ADD_TO_FIRST_TENSOR_H_ 19 20 #include <memory> 21 #include <tuple> 22 #include <utility> 23 #include <vector> 24 #include "backend/optimizer/common/optimizer.h" 25 #include "backend/optimizer/graph_kernel/add_atomic_clean.h" 26 #include "backend/session/kernel_graph.h" 27 28 namespace mindspore { 29 namespace opt { 30 /* 31 * output = SubGraph(input_x, indices, update) { 32 * %0 = TensorScatterAdd(%para1, %para2, %para3) 33 * return %0 34 * } 35 * ----------------------------------------------------------------> 36 * // Initialize output with input_x. 37 * output = Reshape(input_x) 38 * fake_out = SubGraph'(output, indices, update) { 39 * %0 = TensorScatterAdd(%para1, %para2, %para3) 40 * %1 = InplaceAssign(%para1, %0, %0) // attrs{"fake_output":true} 41 * return %1 42 * } 43 */ 44 class TsaAtomicAddToFirstTensor : public AtomicCleanInsertter { 45 public: TsaAtomicAddToFirstTensor()46 TsaAtomicAddToFirstTensor() : AtomicCleanInsertter("tensor_scatter_add_atomic_add_to_first_tensor") {} 47 ~TsaAtomicAddToFirstTensor() override = default; 48 49 bool Run(const FuncGraphPtr &func_graph) override; 50 51 private: 52 void ProcessOriginCNode(const AnfNodePtr &composite_node, const AnfNodePtr &new_input) override; 53 void CorrectKernelBuildInfo(const AnfNodePtr &composite_node, const AnfNodePtr &new_input, 54 bool bypass = true) override; 55 void ProcessTsa(const KernelGraphPtr &main_graph, const AnfNodePtr &anf_node, const FuncGraphManagerPtr &mng); 56 AnfNodePtr ProcessTsaFirstNode(const KernelGraphPtr &main_graph, const AnfNodePtr &node); 57 AnfNodePtr FindTsaFirstRealInputInGraph(const KernelGraphPtr &main_graph, const AnfNodePtr &node); 58 59 size_t tsa_first_input_index_{0}; // sub-graph parameter index. 60 }; 61 using TsaAtomicAddToFirstTensorPtr = std::shared_ptr<TsaAtomicAddToFirstTensor>; 62 } // namespace opt 63 } // namespace mindspore 64 65 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TSA_ATOMIC_ADD_TO_FIRST_TENSOR_H_ 66