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

Class Network

imperative/python/megengine/utils/network.py:29–532  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27
28
29class Network:
30 def __init__(self):
31 self.input_vars = [] # input var of graph
32 self._orig_inputs = []
33 self.output_vars = [] # output var of graph
34 self._orig_outputs = []
35 self.all_oprs_map = OrderedDict() # _imperative_rt.graph.VarNode.id: VarNode
36 self.all_vars_map = (
37 OrderedDict()
38 ) # _imperative_rt.graph.OperatorNode.id: OpNode
39 self.graph = ComputingGraph()
40 self._metadata = None
41
42 @property
43 def metadata(self):
44 r"""Load metadata as a dict."""
45 if not self._metadata.is_valid:
46 logger.info("metadata is not valid!")
47 return None
48 ret = dict()
49 try:
50 user_info = pickle.loads(self._metadata.user_info)
51 except: # pylint: disable=bare-except
52 logger.warning(
53 "can't parse user info by pickle, so return the original bytes object!"
54 )
55 user_info = self._metadata.user_info
56 ret["user_info"] = user_info
57 ret["graph_modified"] = self._metadata.graph_modified
58 ret["optimized_for_inference"] = self._metadata.optimized_for_inference
59 if ret["optimized_for_inference"]:
60 ret.update(G.deserialize_infer_option(self._metadata.optimize_options))
61 return ret
62
63 @classmethod
64 def load(cls, model_path: str, outspec: List[str] = None):
65 r"""Loads a computing graph as a Network object.
66
67 Args:
68 model_path: file path of mge model.
69 outspec: only load the subgraph with outspec as its endpoints.
70 """
71 self = cls()
72 ret = G.load_graph(model_path)
73 outputs, self._metadata = ret.output_vars_list, ret.metadata
74 if outspec is not None:
75 output_spec = outspec.copy()
76 all_vars = get_dep_vars(outputs) + outputs
77 new_outputs = {}
78 for i in all_vars:
79 if i.name in output_spec:
80 new_outputs[i.name] = i
81 output_spec.remove(i.name)
82 assert len(output_spec) == 0, "Can not find {} in this model".format(
83 output_spec
84 )
85 outputs = [new_outputs[i] for i in outspec]
86 self._orig_outputs = outputs

Callers 15

test_indexing_errorFunction · 0.90
test_basicFunction · 0.90
test_literal_arithFunction · 0.90
test_matmulFunction · 0.90
test_inplace_addFunction · 0.90
test_reduceFunction · 0.90
test_set_valueFunction · 0.90
test_set_subtensorFunction · 0.90
test_transposeFunction · 0.90
test_as_typeFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_indexing_errorFunction · 0.72
test_basicFunction · 0.72
test_literal_arithFunction · 0.72
test_matmulFunction · 0.72
test_inplace_addFunction · 0.72
test_reduceFunction · 0.72
test_set_valueFunction · 0.72
test_set_subtensorFunction · 0.72
test_transposeFunction · 0.72
test_as_typeFunction · 0.72