1 /** 2 * Copyright 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_DYNAMIC_OBFUSCATION_H 18 #define MINDSPORE_DYNAMIC_OBFUSCATION_H 19 20 #include <vector> 21 #include <string> 22 #include <map> 23 #include <stack> 24 #include <set> 25 #include "load_mindir/load_model.h" 26 #include "ops/conv_pool_op_name.h" 27 #include "ops/nn_optimizer_ops.h" 28 #include "ops/nn_ops.h" 29 #include "ops/math_ops.h" 30 #include "ops/lite_ops.h" 31 #include "include/common/visible.h" 32 #include "include/common/utils/utils.h" 33 34 namespace mindspore { 35 enum struct ObfCase : unsigned int { NotObfNode, OneInputNoWeightNode, OneInputWithWeightNode }; 36 class COMMON_EXPORT DynamicObfuscator { 37 public: DynamicObfuscator(const float obf_ratio,const int branch_control_input)38 DynamicObfuscator(const float obf_ratio, const int branch_control_input) 39 : obf_ratio_(obf_ratio), branch_control_input_(branch_control_input) {} 40 41 ~DynamicObfuscator() = default; 42 43 FuncGraphPtr ObfuscateMindIR(const FuncGraphPtr &func_graph); 44 45 private: 46 void SubGraphFakeBranch(const FuncGraphPtr func_graph); 47 std::string ObfuscateOpType(const AnfNodePtr &node); 48 ObfCase ObfuscateOpCase(const std::string obf_type); 49 CNodePtr GetControlNode(const FuncGraphPtr &func_graph, const AnfNodePtr &prev_node); 50 CNodePtr RandomSeedModeControl(const FuncGraphPtr func_graph); 51 CNodePtr CustomOpModeControl(const FuncGraphPtr func_graph, const AnfNodePtr &prev_node) const; 52 53 bool IsTarget(const std::string &cnode_name); 54 void UpdateDict(const AnfNodePtr &node, const bool isParent); 55 void CheckDuplicatedParent(const AnfNodePtr &node); 56 CNodePtr CheckInputNodes(const CNodePtr &node); 57 void AddSwitchNode(const FuncGraphPtr fg); 58 FuncGraphPtr CloneSubGraph(const std::vector<CNodePtr> &node_arr, const AnfNodePtr &parent_node); 59 FuncGraphPtr BuildFakeGraph(const std::vector<CNodePtr> &node_arr, const AnfNodePtr &parent_node); 60 CNodePtr BuildOneInputNoWeightNode(const FuncGraphPtr &fg, const mindspore::AnfNodePtr &input_node, 61 const mindspore::PrimitivePtr prim_node) const; 62 CNodePtr BuildOneInputWithWeightNode(const FuncGraphPtr &fg, const AnfNodePtr &input_node, const CNodePtr &conv_node, 63 const AnfNodePtr &weights) const; 64 CNodePtr AddPartialBranch(const FuncGraphPtr fg, FuncGraphPtr fg_sub, const std::vector<mindspore::CNodePtr> &nodes); 65 PrimitivePtr get_random_prim(const std::string &obf_type, const mindspore::CNodePtr &node); 66 bool IsValidOpNum(const int ¤t_num, const int &compa_num) const; 67 const float obf_ratio_ = 0.01; 68 const int branch_control_input_; 69 bool has_build_appended_input = false; 70 std::vector<bool> customized_func_results_; 71 std::map<std::string, AnfNodePtr> node_dict_; 72 std::stack<std::string> node_names_; 73 std::stack<std::string> parent_names_; 74 int used_control_node_ = 0; 75 int subgraph_obf_num_ = 0; 76 bool switch_branch_ = true; 77 const std::vector<std::string> single_input_target_op_ = { 78 kReLUOpName, kSigmoidOpName, kReLU6OpName, kSoftplusOpName, kHSigmoidOpName, kFastGeLUOpName, 79 kHSwishOpName, kSoftsignOpName, kSeLUOpName, kTanhOpName, kSquareOpName}; 80 const std::vector<std::string> single_input_with_weight_target_op_ = {kConv2DOpName, kMatMulOpName}; 81 const std::vector<PrimitivePtr> one_input_prim_ = { 82 mindspore::prim::kPrimReLU, mindspore::prim::kPrimSigmoid, mindspore::prim::kPrimReLU6, 83 mindspore::prim::kPrimSoftplus, mindspore::prim::kPrimHSigmoid, mindspore::prim::kPrimFastGeLU, 84 mindspore::prim::kPrimHSwish, mindspore::prim::kPrimSoftsign, mindspore::prim::kPrimSeLU, 85 mindspore::prim::kPrimTanh, mindspore::prim::kPrimSquare}; 86 }; 87 } // namespace mindspore 88 #endif // MINDSPORE_DYNAMIC_OBFUSCATION_H 89