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_FRONTEND_OPTIMIZER_IRPASS_LESS_BATCH_NORMALIZATION_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_LESS_BATCH_NORMALIZATION_H_ 19 20 #include <utility> 21 #include <vector> 22 #include <tuple> 23 #include <string> 24 25 #include "utils/hash_set.h" 26 #include "frontend/optimizer/irpass.h" 27 #include "frontend/optimizer/optimizer.h" 28 #include "frontend/optimizer/anf_visitor.h" 29 #include "frontend/operator/ops.h" 30 31 namespace mindspore { 32 namespace opt { 33 namespace irpass { 34 using kStructureTuple = std::tuple<size_t, std::vector<PrimitivePtr>, std::pair<size_t, size_t>>; 35 class LessBatchNormalization : public AnfVisitor { 36 public: 37 AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override; 38 void Visit(const CNodePtr &cnode) override; 39 void Reset(); 40 void IsRemoveNode(const CNodePtr &cnode, const std::vector<kStructureTuple> &match_pattern); 41 bool MatchStructureNode(const CNodePtr &cnode, const int32_t index, const kStructureTuple &patternTuple) const; 42 bool MatchGraphStructure(const CNodePtr &cnode, const std::vector<kStructureTuple> &match_pattern); 43 44 private: 45 mindspore::HashSet<CNodePtr> remove_node_list_{}; 46 std::vector<size_t> total_match_node_{0}; 47 size_t match_node_{0}; 48 size_t match_branch_{0}; 49 size_t match_pattern_{0}; 50 bool is_match_{false}; 51 }; 52 } // namespace irpass 53 } // namespace opt 54 } // namespace mindspore 55 #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_LESS_BATCH_NORMALIZATION_H_ 56