MCPcopy
hub / github.com/OpenPPL/ppq / RunnableGraph

Class RunnableGraph

ppq/IR/deploy.py:13–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class RunnableGraph(GraphCommandProcessor):
14 def __init__(self, graph: BaseGraph, device: str = None):
15 """RunnableGraph deals with values related with graph executing.
16
17 Literally it helps you move values of your graph towards device and vice versa.
18 And give an executable order of all operations in your graph which actual executor will follow.
19 Args:
20 graph (BaseGraph): BaseGraph instance.
21 device (str, optional): This attribute is only used by with RunnableGraph(graph, device) syntactic.
22 next_command_processor (Callable, optional): next processor in processing chain.
23 """
24 super().__init__(graph_or_processor=graph)
25 self._device = device # only used in "with RunnableGraph(graph, device):"
26
27 def process(self, command: GraphCommand) -> Any:
28
29 if command.command_type == GraphCommandType.DEPLOY_TO_CPU:
30 return self.deploy('cpu')
31
32 elif command.command_type == GraphCommandType.DEPLOY_TO_CUDA:
33 if isinstance(command, GraphDeployCommand):
34 device = command._device
35 return self.deploy(device)
36 else:
37 return self.deploy('cuda')
38
39 elif command.command_type == GraphCommandType.DEPLOY_TO_NUMPY:
40 return self.retrieve()
41
42 def __enter__(self):
43 self.deploy(self._device)
44
45 def __exit__(self, exc_type, exc_val, exc_tb):
46 self.retrieve()
47
48 def _acceptable_command_types(self) -> List[GraphCommandType]:
49 return [
50 GraphCommandType.DEPLOY_TO_CPU,
51 GraphCommandType.DEPLOY_TO_CUDA,
52 GraphCommandType.DEPLOY_TO_NUMPY
53 ]
54
55 def retrieve(self):
56
57 for _, operator in self._graph.operations.items():
58
59 assert isinstance(operator, Operation), \
60 f'Failed to retrieve graph to numpy, incorrect operator {operator} found.'
61
62 # in onnx format, some constant values are warpped with operation's attributes['value']
63 # To move those constant value from numpy to device,
64 # we have to move all the attributes['value'] of operation to device(if there is any).
65 if operator.type == 'Constant':
66 operator.attributes['value'] = \
67 convert_any_to_numpy(operator.attributes['value'])
68
69 for _, variable in self._graph.variables.items():
70 assert isinstance(variable, Variable), \

Callers 2

__init__Method · 0.90
load_graphMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected