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_CORE_IR_PRIMAL_DEBUG_INFO_H 18 #define MINDSPORE_CORE_IR_PRIMAL_DEBUG_INFO_H 19 #include <string> 20 #include <memory> 21 #include <stack> 22 #include <vector> 23 #include <set> 24 #include "utils/hash_map.h" 25 #include "utils/info.h" 26 #include "mindapi/base/macros.h" 27 28 namespace mindspore { 29 class MS_CORE_API PrimalDebugInfoManager { 30 public: 31 static PrimalDebugInfoManager &GetInstance() noexcept; 32 PrimalDebugInfoManager(const PrimalDebugInfoManager &) = delete; 33 PrimalDebugInfoManager &operator=(const PrimalDebugInfoManager &) = delete; 34 ~PrimalDebugInfoManager() = default; SetPrimalDebugInfo(const std::vector<NodeDebugInfoPtr> & primal_debug_infos)35 void SetPrimalDebugInfo(const std::vector<NodeDebugInfoPtr> &primal_debug_infos) { 36 (void)std::for_each(primal_debug_infos.begin(), primal_debug_infos.end(), 37 [this](const NodeDebugInfoPtr &debug_info) { (void)primal_debug_infos_.emplace(debug_info); }); 38 } ClearPrimalDebugInfo()39 void ClearPrimalDebugInfo() noexcept { primal_debug_infos_.clear(); } GetCurrentPrimalDebugInfo()40 std::set<NodeDebugInfoPtr, DebugInfoCompare> GetCurrentPrimalDebugInfo() const { return primal_debug_infos_; } 41 42 private: 43 PrimalDebugInfoManager() = default; 44 std::set<NodeDebugInfoPtr, DebugInfoCompare> primal_debug_infos_; 45 }; 46 47 // PrimalDebugInfoGuard is a class that help generate the back propagation cnode 48 // with specified primal_debug_infos in the current c++ action scope. 49 class PrimalDebugInfoGuard { 50 public: PrimalDebugInfoGuard(const std::vector<NodeDebugInfoPtr> & primal_debug_infos)51 explicit PrimalDebugInfoGuard(const std::vector<NodeDebugInfoPtr> &primal_debug_infos) { 52 PrimalDebugInfoManager::GetInstance().SetPrimalDebugInfo(primal_debug_infos); 53 } ~PrimalDebugInfoGuard()54 ~PrimalDebugInfoGuard() { PrimalDebugInfoManager::GetInstance().ClearPrimalDebugInfo(); } 55 }; 56 } // namespace mindspore 57 #endif // MINDSPORE_CORE_IR_PRIMAL_DEBUG_INFO_H 58