MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / replace_dev_input_pass

Method replace_dev_input_pass

lite/src/mge/network_impl.cpp:367–429  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

365}
366
367void NetworkImplDft::replace_dev_input_pass() {
368 mgb::CompNode::Locator locator;
369 m_load_config.comp_node_mapper(locator);
370 //! CPU is not need use device input
371 if (locator.type == mgb::CompNode::DeviceType::CPU) {
372 return;
373 }
374 //! repalce the H2D with VolatileSharedDeviceTensor, and keep the dev tensor
375 //! in m_network_io.input, user can directly change the dev tensor
376 //! storage through m_network_io.input.lite_tensor->reset() befor forward
377 using DeviceTensorMap =
378 std::unordered_map<std::string, std::shared_ptr<mgb::DeviceTensorND>>;
379 DeviceTensorMap name2dev_tensor;
380
381 mgb::ThinHashMap<mgb::HostTensorND*, mgb::SymbolVar> host_val2var;
382
383 //! construct host_val2var that maps from host tensor to corresponding var
384 auto on_opr = [&](mgb::cg::OperatorNodeBase* opr) {
385 if (opr->same_type<mgb::opr::Host2DeviceCopy>()) {
386 mgb::HostTensorND* tensor =
387 opr->cast_final<mgb::opr::Host2DeviceCopy>().host_data().get();
388 host_val2var[tensor] = opr->output(0);
389 }
390 };
391 mgb::cg::DepOprIter dep_iter{on_opr};
392 for (auto i : m_load_result.output_var_list) {
393 dep_iter.add(i.node()->owner_opr());
394 }
395
396 mgb::ThinHashMap<mgb::SymbolVar, mgb::SymbolVar> inp_var_map, out_var_map;
397
398 mgb::SmallVector<std::string> to_clear;
399 for (auto&& config_in : m_network_io->inputs) {
400 if (!config_in.is_host) {
401 auto host_val = m_load_result.tensor_map[config_in.name];
402 auto dev_val = TensorHelper::implement(config_in.lite_tensor)
403 ->cast_final_safe<TensorImplDft>()
404 .m_dev_tensor;
405 auto dev_var = mgb::opr::VolatileSharedDeviceTensor::make(
406 *m_load_result.graph, dev_val, {config_in.name});
407 inp_var_map[host_val2var.at(host_val.get())] = dev_var;
408 name2dev_tensor[config_in.name] = dev_val;
409 }
410 //! reset lite_tensor in discrete mode
411 if (config_in.name == m_user_config->discrete_input_name) {
412 config_in.lite_tensor.reset();
413 }
414 }
415 auto new_ovar = mgb::cg::replace_vars(m_load_result.output_var_list, inp_var_map);
416 for (size_t i = 0; i < new_ovar.size(); ++i) {
417 out_var_map[m_load_result.output_var_list[i]] = new_ovar[i];
418 }
419 for (auto&& i : m_load_result.output_var_map) {
420 i.second = out_var_map.at(i.second);
421 }
422 for (auto&& i : m_load_result.output_var_map_id) {
423 i.second = out_var_map.at(i.second);
424 }

Callers

nothing calls this directly

Calls 12

replace_varsFunction · 0.85
renameMethod · 0.80
makeFunction · 0.50
getMethod · 0.45
outputMethod · 0.45
addMethod · 0.45
owner_oprMethod · 0.45
nodeMethod · 0.45
atMethod · 0.45
resetMethod · 0.45
sizeMethod · 0.45
nameMethod · 0.45

Tested by

no test coverage detected