| 91 | } |
| 92 | |
| 93 | class MMNetwork { |
| 94 | public: |
| 95 | template <typename T> |
| 96 | using MagicMindUniquePtr = magicmind_intl::MagicMindUniquePtr<T>; |
| 97 | using IModelPtr = MagicMindRuntimeOpr::IModelPtr; |
| 98 | using IContextPtr = MagicMindRuntimeOpr::IContextPtr; |
| 99 | using IEnginePtr = MagicMindRuntimeOpr::IEnginePtr; |
| 100 | |
| 101 | const CompNode& cn_; |
| 102 | magicmind::DataType op_datatype_; |
| 103 | IModelPtr model_; |
| 104 | bool graph_shape_mutable_; |
| 105 | bool built_; |
| 106 | |
| 107 | template <typename T> |
| 108 | static MagicMindUniquePtr<T> make_mm_unique_ptr(T* ptr) { |
| 109 | return {ptr, magicmind_intl::MagicMindDeleter<T>()}; |
| 110 | } |
| 111 | |
| 112 | MMNetwork( |
| 113 | const CompNode& cn, magicmind::DataType op_datatype, |
| 114 | bool graph_shape_mutable = false) |
| 115 | : cn_{cn}, |
| 116 | op_datatype_{op_datatype}, |
| 117 | model_{nullptr}, |
| 118 | graph_shape_mutable_{graph_shape_mutable}, |
| 119 | built_{false} {} |
| 120 | void build() { |
| 121 | auto&& cnrt_env = CompNodeEnv::from_comp_node(cn_).cnrt_env(); |
| 122 | cnrt_env.activate(); |
| 123 | constexpr int ni = 16, ci = 64, hi = 32, wi = 32; |
| 124 | constexpr int no = 16, co = 64, ho = 32, wo = 32; |
| 125 | constexpr int kh = 3, kw = 3; |
| 126 | constexpr int stride_h = 1, stride_w = 1; |
| 127 | constexpr int pad_h = 1, pad_w = 1; |
| 128 | magicmind::Dims input_dim{{ni, hi, wi, ci}}; |
| 129 | magicmind::Dims filter_dim{{co, kh, kw, ci}}; |
| 130 | magicmind::Dims bias_dim{{co}}; |
| 131 | magicmind::Dims add_dim{{no, ho, wo, co}}; |
| 132 | magicmind::DataType output_datatype = magicmind::DataType::FLOAT32; |
| 133 | |
| 134 | // init |
| 135 | auto builder = make_mm_unique_ptr(magicmind::CreateIBuilder()); |
| 136 | auto config = make_mm_unique_ptr(magicmind::CreateIBuilderConfig()); |
| 137 | std::string user_json_config = R"( |
| 138 | { |
| 139 | "graph_shape_mutable": {{GRAPH_SHAPE_MUTABLE}}, |
| 140 | "precision_config": { |
| 141 | "precision_mode": "qint8_mixed_float32" |
| 142 | } |
| 143 | } |
| 144 | )"; |
| 145 | replace_all_pairs_inplace( |
| 146 | user_json_config, |
| 147 | {{"{{GRAPH_SHAPE_MUTABLE}}", graph_shape_mutable_ ? "true" : "false"}}); |
| 148 | config->ParseFromString(user_json_config); |
| 149 | auto network = make_mm_unique_ptr(magicmind::CreateINetwork()); |
| 150 | magicmind::Range filter_range = {0.0f, 0.0f}; |