| 181 | } |
| 182 | |
| 183 | void ModelMdl::update_io() { |
| 184 | //! update output varlist when input shape maybe change(some pass excution |
| 185 | //! time depends on the shape of init input) |
| 186 | mgb::thin_hash_table::ThinHashMap<mgb::cg::SymbolVar, mgb::cg::SymbolVar> varmap; |
| 187 | auto&& network = m_load_result; |
| 188 | std::unordered_map<void*, std::string> tensor_name_map; |
| 189 | for (auto& input : network.tensor_map) { |
| 190 | tensor_name_map.insert({input.second->raw_ptr(), input.first}); |
| 191 | } |
| 192 | mgb::cg::DepOprIter dep([&](mgb::cg::OperatorNodeBase* opr) { |
| 193 | if (auto h2d = opr->try_cast_final<mgb::opr::Host2DeviceCopy>()) { |
| 194 | if (tensor_name_map.find(h2d->host_data()->raw_ptr()) != |
| 195 | tensor_name_map.end()) { |
| 196 | //! make new h2d opr with new host tensor shape |
| 197 | std::string name = tensor_name_map[h2d->host_data()->raw_ptr()]; |
| 198 | std::shared_ptr<mgb::HostTensorND> new_tensor = |
| 199 | std::make_shared<mgb::HostTensorND>(); |
| 200 | new_tensor->copy_from(*h2d->host_data()); |
| 201 | |
| 202 | auto h2d_opr = mgb::opr::Host2DeviceCopy::make( |
| 203 | *h2d->owner_graph(), new_tensor, h2d->param(), h2d->config()); |
| 204 | //! rename new h2d with given name |
| 205 | h2d_opr.node()->owner_opr()->name(name); |
| 206 | varmap[h2d->output(0)] = h2d_opr; |
| 207 | } |
| 208 | } |
| 209 | }); |
| 210 | //! get replace var map |
| 211 | for (auto&& i : network.output_var_list) |
| 212 | dep.add(i); |
| 213 | //! replace new h2d and update related var shape |
| 214 | if (!varmap.empty()) { |
| 215 | auto output_vars = mgb::cg::replace_vars(network.output_var_list, varmap); |
| 216 | network.output_var_list = output_vars; |
| 217 | } |
| 218 | } |
no test coverage detected