Spaces:
Running
Running
Download compact_graph.py from simon-clmtd/autodiff-visualizer: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/simon-clmtd/autodiff-visualizer/resolve/main/compact_graph.py
- Command line
-
hf download hf://spaces/simon-clmtd/autodiff-visualizer/compact_graph.py
-
curl -L -o compact_graph.py https://huggingface.co/spaces/simon-clmtd/autodiff-visualizer/resolve/main/compact_graph.py
1.63 kB
| """Compact presentation projection; all numerical values come from the store.""" | |
| from dataclasses import dataclass | |
| from math_expression import Expr, ref | |
| class CompactNode: | |
| uid: str | |
| formula: Expr | None | |
| value: object | |
| adjoint: object | |
| role: str | |
| name: str | |
| class CompactEdge: | |
| source: str | |
| destination: str | |
| value: object | |
| role: str | |
| occurrence: int | None = None | |
| class CompactGraph: | |
| nodes: tuple[CompactNode, ...] | |
| edges: tuple[CompactEdge, ...] | |
| def compact_graph(model, backward=True): | |
| operations = {op.quantity.uid: op for op in model.operations} | |
| nodes = [] | |
| for q in model.quantities: | |
| op = operations.get(q.uid) | |
| nodes.append(CompactNode(q.uid, op.formula if op else ref(q), q.primal_value, | |
| q.adjoint_value, 'operation' if op else ('constant' if q.is_constant else 'input'), q.name)) | |
| if model.output: | |
| nodes.append(CompactNode(model.output.uid, None, None, None, 'output', '')) | |
| edges = [CompactEdge(e.source, e.destination, e.value, 'forward', e.operand_occurrence) | |
| for e in model.forward_edges] | |
| if backward and not model.forward_only: | |
| edges.extend(CompactEdge(s.source_quantity.uid, s.destination_quantity.uid, | |
| s.contribution, 'backward', s.operand_occurrence) for s in model.steps) | |
| edges.extend(CompactEdge(e.source, e.destination, e.value, 'seed') | |
| for e in model.backward_edges if e.seed) | |
| return CompactGraph(tuple(nodes), tuple(edges)) | |