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_CORE_IR_PRIMAL_ATTR_H_ 18 #define MINDSPORE_CORE_IR_PRIMAL_ATTR_H_ 19 #include <string> 20 #include <memory> 21 #include <stack> 22 #include <unordered_map> 23 #include "ir/anf.h" 24 25 namespace mindspore { 26 class Value; 27 using ValuePtr = std::shared_ptr<Value>; 28 29 class PrimalAttrManager { 30 public: GetInstance()31 static PrimalAttrManager &GetInstance() noexcept { 32 static PrimalAttrManager instance; 33 return instance; 34 } 35 PrimalAttrManager(const PrimalAttrManager &) = delete; 36 PrimalAttrManager &operator=(const PrimalAttrManager &) = delete; 37 ~PrimalAttrManager() = default; SetPrimalAttr(const std::unordered_map<std::string,ValuePtr> & primal_attrs)38 void SetPrimalAttr(const std::unordered_map<std::string, ValuePtr> &primal_attrs) { primal_attrs_ = primal_attrs; } ClearPrimalAttr()39 void ClearPrimalAttr() { primal_attrs_.clear(); } GetCurrentPrimalAttr()40 std::unordered_map<std::string, ValuePtr> GetCurrentPrimalAttr() { return primal_attrs_; } 41 42 private: 43 PrimalAttrManager() = default; 44 std::unordered_map<std::string, ValuePtr> primal_attrs_; 45 }; 46 47 // PrimalAttrGuard is a class that help generate the back propagation cnode 48 // with specified primal attrs in the current c++ action scope. 49 class PrimalAttrGuard { 50 public: PrimalAttrGuard(const std::unordered_map<std::string,ValuePtr> & primal_attrs)51 explicit PrimalAttrGuard(const std::unordered_map<std::string, ValuePtr> &primal_attrs) { 52 PrimalAttrManager::GetInstance().SetPrimalAttr(primal_attrs); 53 } ~PrimalAttrGuard()54 ~PrimalAttrGuard() { PrimalAttrManager::GetInstance().ClearPrimalAttr(); } 55 }; 56 } // namespace mindspore 57 #endif // MINDSPORE_CORE_IR_PRIMAL_ATTR_H_ 58