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_USS_ATOMIC_ADD_H_ 18 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_USS_ATOMIC_ADD_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, segment_ids) { 32 * %0 = UnsortedSegmentSum(%para1, %para2) 33 * return %0 34 * } 35 * ----------------------------------------------------------------> 36 * // Clean output with zero. 37 * output = broadcast_to(0.0) // attrs{"shape": [shape of origin output.]} 38 * fake_out = SubGraph'(input_x, segment_ids, output) { 39 * %0 = UnsortedSegmentSum(%para1, %para2) 40 * %1 = InplaceAssign(%para3, %0, %0) // attrs{"fake_output":true} 41 * return %1 42 * } 43 */ 44 class UssAtomicAdd : public AtomicCleanInsertter { 45 public: UssAtomicAdd()46 UssAtomicAdd() : AtomicCleanInsertter("unsorted_segment_sum_atomic_add_process") {} 47 ~UssAtomicAdd() override = default; 48 bool Run(const FuncGraphPtr &func_graph) override; 49 }; 50 using UssAtomicAddPtr = std::shared_ptr<UssAtomicAdd>; 51 } // namespace opt 52 } // namespace mindspore 53 54 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_USS_ATOMIC_ADD_H_ 55