• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-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 
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 "include/backend/optimizer/optimizer.h"
25 #include "backend/common/graph_kernel/add_atomic_clean.h"
26 #include "include/backend/kernel_graph.h"
27 
28 namespace mindspore::graphkernel {
29 /*
30  * output = SubGraph(input_x, indices, update) {
31  *   %0 = TensorScatterAdd(%para1, %para2, %para3)
32  *   return %0
33  * }
34  * ---------------------------------------------------------------->
35  * // Initialize output with input_x.
36  * output = Reshape(input_x)
37  * fake_out = SubGraph'(output, indices, update) {
38  *   %0 = TensorScatterAdd(%para1, %para2, %para3)
39  *   %1 = Assign(%para1, %0, umond) //
40  *   return %1
41  * }
42  */
43 class TsaAtomicAddToFirstTensor : public AtomicCleanInserter {
44  public:
TsaAtomicAddToFirstTensor()45   TsaAtomicAddToFirstTensor() : AtomicCleanInserter("tensor_scatter_add_atomic_add_to_first_tensor") {}
46   ~TsaAtomicAddToFirstTensor() override = default;
47 
48   bool Run(const FuncGraphPtr &func_graph) override;
49 
50  private:
51   void ProcessOriginalCNode(const AnfNodePtr &composite_node,
52                             const std::vector<std::tuple<InplaceAssignerInfo, AnfNodePtr, size_t>> &outer_nodes) const;
53   void ChangeKernelBuildInfo(const AnfNodePtr &composite_node,
54                              const std::vector<std::tuple<InplaceAssignerInfo, AnfNodePtr, size_t>> &outer_infos) const;
55   void ProcessTsa(const KernelGraphPtr &main_graph, const AnfNodePtr &anf_node,
56                   const std::vector<InplaceAssignerInfo> &atomic_add_infos, const FuncGraphManagerPtr &mng) const;
57   std::pair<AnfNodePtr, size_t> GetOrCreateNewTsaFirstNode(const KernelGraphPtr &main_graph,
58                                                            const InplaceAssignerInfo &atomic_add_info,
59                                                            const AnfNodePtr &node) const;
60   std::pair<AnfNodePtr, size_t> FindTsaFirstRealInputInGraph(const KernelGraphPtr &, const CNodePtr &tsa_node,
61                                                              const AnfNodePtr &node) const;
62 
63   size_t tsa_first_input_index_{0};  // sub-graph parameter index.
64 };
65 using TsaAtomicAddToFirstTensorPtr = std::shared_ptr<TsaAtomicAddToFirstTensor>;
66 }  // namespace mindspore::graphkernel
67 
68 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_TSA_ATOMIC_ADD_TO_FIRST_TENSOR_H_
69