| 25 | super().__init__() |
| 26 | |
| 27 | def build(self, file_path: str, **kwargs) -> BaseGraph: |
| 28 | def load_elements_from_file(file, num_of_elements: int) -> list: |
| 29 | try: return [load(file) for _ in range(num_of_elements)] |
| 30 | except EOFError as e: |
| 31 | raise Exception('File format parsing error. Unexpected EOF found.') |
| 32 | |
| 33 | with open(file_path, 'rb') as file: |
| 34 | signature, version, graph = load_elements_from_file(file, 3) |
| 35 | if signature != 'PPQ GRAPH DEFINITION': |
| 36 | raise Exception('File format parsing error. Graph Signature has been damaged.') |
| 37 | if str(version) > PPQ_CONFIG.VERSION: |
| 38 | print(f'\033[31mWarning: Dump file is created by PPQ({str(version)}), ' |
| 39 | f'however you are using PPQ({PPQ_CONFIG.VERSION}).\033[0m') |
| 40 | |
| 41 | assert isinstance(graph, BaseGraph), ( |
| 42 | 'File format parsing error. Graph Definition has been damaged.') |
| 43 | try: |
| 44 | for op in graph.operations.values(): |
| 45 | input_copy, _ = op.inputs.copy(), op.inputs.clear() |
| 46 | for name in input_copy: op.inputs.append(graph.variables[name]) |
| 47 | output_copy, _ = op.outputs.copy(), op.outputs.clear() |
| 48 | for name in output_copy: op.outputs.append(graph.variables[name]) |
| 49 | |
| 50 | for var in graph.variables.values(): |
| 51 | dest_copy, _ = var.dest_ops.copy(), var.dest_ops.clear() |
| 52 | for name in dest_copy: var.dest_ops.append(graph.operations[name]) |
| 53 | if var.source_op is not None: |
| 54 | var.source_op = graph.operations[var.source_op] |
| 55 | |
| 56 | graph._graph_inputs = {name: graph.variables[name] for name in graph._graph_inputs} |
| 57 | graph._graph_outputs = {name: graph.variables[name] for name in graph._graph_outputs} |
| 58 | except Exception as e: |
| 59 | raise Exception('File format parsing error. Graph Definition has been damaged.') |
| 60 | return graph |