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

Class GraphInference

imperative/python/megengine/utils/comp_graph_tools.py:434–505  ·  view source on GitHub ↗

r"""Loads a serialized computing graph as a GraphInference object which can be used to execute the computing graph. Args: file: could be file object or filename. outputs: only compile the subgraph with outputs as its endpoints.

Source from the content-addressed store, hash-verified

432
433
434class GraphInference:
435 r"""Loads a serialized computing graph as a GraphInference object which can be used
436 to execute the computing graph.
437
438 Args:
439 file: could be file object or filename.
440 outputs: only compile the subgraph with outputs as its endpoints.
441 """
442
443 def __init__(
444 self,
445 file,
446 outputs: List[str] = None,
447 profiling: bool = False,
448 optimize_for_inference: bool = False,
449 **kwargs
450 ):
451 ret = G.load_graph(file)
452 self._graph, output_nodes = ret.graph, ret.output_vars_list
453 if outputs is not None:
454 output_nodes = find_vars_by_name(output_nodes, outputs)
455 self._origin_outputs = output_nodes
456
457 # replace inputs with `InputNode`
458 output_nodes, self._inp_dict = convert_inputs(output_nodes)
459
460 # replace outputs with `OutputNode`
461 output_nodes, self._oup_dict = convert_outputs(output_nodes)
462
463 self._func = self._graph.compile(output_nodes)
464
465 def run(
466 self, *inp_args: np.ndarray, inp_dict: Dict[str, np.ndarray] = None
467 ) -> Dict[str, np.ndarray]:
468 r"""
469
470 Args:
471 inp_args: list of input datas.
472 inp_dict: dict of named input datas.
473
474 Returns:
475 a dict {output_name: output_value}.
476
477 Note:
478 Note that the order of the Graph's input nodes may be different from the order of the origin traced function's arguments.
479 It is recommended to use ``inp_dict`` to provide input data by name.
480 """
481 assert len(inp_args) <= len(
482 self._inp_dict
483 ), "This model expects {} inputs".format(len(self._inp_dict))
484 inputs = {}
485 inp_keys = list(self._inp_dict.keys())
486 for ind, data in enumerate(inp_args):
487 inputs[inp_keys[ind]] = data
488 if inp_dict is not None:
489 inputs.update(inp_dict)
490 assert (
491 inputs.keys() == self._inp_dict.keys()

Callers 10

test_replace_varFunction · 0.90
test_replace_oprFunction · 0.90
test_modify_paramsFunction · 0.90
test_make_constFunction · 0.90
test_add_inputFunction · 0.90
test_add_remove_outputFunction · 0.90
test_dump_cond_takeFunction · 0.90
check_pygraph_dumpFunction · 0.90
test_assert_equalFunction · 0.90

Calls

no outgoing calls

Tested by 10

test_replace_varFunction · 0.72
test_replace_oprFunction · 0.72
test_modify_paramsFunction · 0.72
test_make_constFunction · 0.72
test_add_inputFunction · 0.72
test_add_remove_outputFunction · 0.72
test_dump_cond_takeFunction · 0.72
check_pygraph_dumpFunction · 0.72
test_assert_equalFunction · 0.72