From a0a2f47465ea5858b949eebc3a08cc6920b7161f Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Tue, 10 Oct 2023 21:49:39 +0200 Subject: [PATCH 01/27] Added option to execute stateful submodules --- nirtorch/from_nir.py | 41 +++++++++++++++++++++++++++++++++++------ nirtorch/graph.py | 4 +++- tests/test_from_nir.py | 23 +++++++++++++++++++++-- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 6bdc381..773ba11 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -1,4 +1,5 @@ -from typing import Callable, Dict, List, Optional +import inspect +from typing import Callable, Dict, List, Optional, NamedTuple, Any import nir import torch @@ -49,11 +50,17 @@ class GraphExecutor(nn.Module): def __init__(self, graph: Graph) -> None: super().__init__() self.graph = graph + self.stateful_modules = {} self.instantiate_modules() self.execution_order = self.get_execution_order() if len(self.execution_order) == 0: raise ValueError("Graph is empty") + def _is_module_stateful(self, module: torch.nn.Module) -> bool: + signature = inspect.signature(module.forward) + arguments = len(signature.parameters) + return arguments > 1 + def get_execution_order(self) -> List[Node]: """Evaluate the execution order and instantiate that as a list.""" execution_order = [] @@ -67,13 +74,32 @@ def get_execution_order(self) -> List[Node]: def instantiate_modules(self): for mod, name in self.graph.module_names.items(): - self.add_module(sanitize_name(name), mod) + if mod is not None: + self.add_module(sanitize_name(name), mod) + self.stateful_modules[sanitize_name(name)] = self._is_module_stateful( + mod + ) def get_input_nodes(self) -> List[Node]: # NOTE: This is a hack. Should use the input nodes from NIR graph return self.graph.get_root() - def forward(self, data: torch.Tensor): + def _apply_module( + self, node: Node, x: torch.Tensor, state: Optional[Dict[str, Any]] + ): + """Applies a module and keeps track of its state. + TODO: Use pytree to recursively construct the state + """ + if node.name in self.stateful_modules and node.name in state: + out = node.elem(x, *state[node.name]) + else: + out = node.elem(x) + if self.stateful_modules[node.name]: + state[node.name] = out[1:] + out = out[0] + return out + + def forward(self, data: torch.Tensor, state: Optional[Dict[str, Any]] = {}): outs = {} # NOTE: This logic is not yet consistent for models with multiple input nodes for node in self.execution_order: @@ -82,11 +108,14 @@ def forward(self, data: torch.Tensor): continue if len(input_nodes) == 0 or len(outs) == 0: # This is the root node - outs[node.name] = node.elem(data) + outs[node.name] = self._apply_module(node, data, state) else: # Intermediate nodes - input_data = (outs[node.name] for node in input_nodes) - outs[node.name] = node.elem(*input_data) + input_data = [outs[node.name] for node in input_nodes] + input_data = torch.stack(input_data).sum( + 0 + ) # Multiple inputs are summed + outs[node.name] = self._apply_module(node, input_data, state) return outs[node.name] diff --git a/nirtorch/graph.py b/nirtorch/graph.py index ac5c7c1..aae3e61 100644 --- a/nirtorch/graph.py +++ b/nirtorch/graph.py @@ -5,6 +5,8 @@ import torch import torch.nn as nn +from .utils import sanitize_name + def named_modules_map( model: nn.Module, model_name: Optional[str] = "model" @@ -41,7 +43,7 @@ def __init__( outgoing_nodes: Optional[Dict["Node", torch.Tensor]] = None, ) -> None: self.elem = elem - self.name = name + self.name = sanitize_name(name) if not outgoing_nodes: self.outgoing_nodes = {} else: diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index d6d09ae..717d7d6 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -1,7 +1,7 @@ import nir import numpy as np -import pytest import torch +import pytest from nirtorch.from_nir import load @@ -29,7 +29,7 @@ def test_extract_empty(): def test_extract_illegal_name(): - graph = nir.NIRGraph({"a.b": nir.Input(np.ones(1))}, []) + graph = nir.NIRGraph({"a.b": nir.Linear(np.ones((1, 1)))}, []) torch_graph = load(graph, _torch_model_map) assert "a_b" in torch_graph._modules @@ -62,3 +62,22 @@ def test_extrac_recurrent(): m = load(g, _torch_model_map) data = torch.randn(1, 1, dtype=torch.float64) torch.allclose(m(data), l2(l1(data))) + + +def test_execute_stateful(): + class StatefulModel(torch.nn.Module): + def __init__(self): + super().__init__() + + def forward(self, x, state=None): + if state is None: + state = 1 + return x + state, state + + g = nir.NIRGraph( + nodes={"li": nir.Flatten(np.array([1])), "li2": nir.Flatten(np.array([1]))}, + edges=[("li", "li2")], + ) # Mock node + m = load(g, lambda m: StatefulModel()) + out = m(torch.ones(10)) + assert torch.allclose(out, torch.ones(10) * 3) From 1465f2985538ad2ee8c47e4c1076f51559a92bc8 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Tue, 10 Oct 2023 21:53:27 +0200 Subject: [PATCH 02/27] Returned state if stateful module --- nirtorch/from_nir.py | 5 ++++- tests/test_from_nir.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 773ba11..e867129 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -116,7 +116,10 @@ def forward(self, data: torch.Tensor, state: Optional[Dict[str, Any]] = {}): 0 ) # Multiple inputs are summed outs[node.name] = self._apply_module(node, input_data, state) - return outs[node.name] + if len(state) > 0: + return outs[node.name], state + else: + return outs[node.name] def _mod_nir_to_graph(nir_graph: nir.NIRNode) -> Graph: diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index 717d7d6..aad54d0 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -79,5 +79,7 @@ def forward(self, x, state=None): edges=[("li", "li2")], ) # Mock node m = load(g, lambda m: StatefulModel()) - out = m(torch.ones(10)) + out, state = m(torch.ones(10)) assert torch.allclose(out, torch.ones(10) * 3) + assert state["li"] == (1, ) + assert state["li"] == (1, ) From bbe54a0602899515a61072ccb031dae9fecb1bb9 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Tue, 10 Oct 2023 21:55:55 +0200 Subject: [PATCH 03/27] Ruff --- nirtorch/from_nir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index e867129..dff21fc 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -1,5 +1,5 @@ import inspect -from typing import Callable, Dict, List, Optional, NamedTuple, Any +from typing import Callable, Dict, List, Optional, Any import nir import torch From a64c23d473561d4e8947cc84864b4cada38f937c Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Wed, 11 Oct 2023 23:55:01 +0200 Subject: [PATCH 04/27] Added recurrent execution --- nirtorch/from_nir.py | 81 ++++++++++++++++++++++++++++++------------ tests/test_from_nir.py | 42 ++++++++++++++++------ 2 files changed, 89 insertions(+), 34 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index dff21fc..4b3302e 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -1,3 +1,4 @@ +import dataclasses import inspect from typing import Callable, Dict, List, Optional, Any @@ -46,6 +47,16 @@ def execution_order_up_to_node( return execution_order + [node] +@dataclasses.dataclass +class GraphExecutorState: + """State for the GraphExecutor that keeps track of both the state of hidden + units and caches the output of previous modules, for use in (future) recurrent + computations.""" + + state: Dict[str, Any] = dataclasses.field(default_factory=dict) + cache: Dict[str, Any] = dataclasses.field(default_factory=dict) + + class GraphExecutor(nn.Module): def __init__(self, graph: Graph) -> None: super().__init__() @@ -85,41 +96,65 @@ def get_input_nodes(self) -> List[Node]: return self.graph.get_root() def _apply_module( - self, node: Node, x: torch.Tensor, state: Optional[Dict[str, Any]] + self, + node: Node, + input_nodes: List[Node], + old_state: GraphExecutorState, + new_state: GraphExecutorState, + data: Optional[torch.Tensor] = None, ): """Applies a module and keeps track of its state. TODO: Use pytree to recursively construct the state """ - if node.name in self.stateful_modules and node.name in state: - out = node.elem(x, *state[node.name]) - else: - out = node.elem(x) + inputs = [] + # Append state if needed + if node.name in self.stateful_modules and node.name in old_state.state: + inputs.extend(old_state.state[node.name]) + + # Sum recurrence if needed + summed_inputs = [] if data is None else [data] + for input_node in input_nodes: + if ( + input_node.name not in new_state.cache + and input_node.name in old_state.cache + ): + summed_inputs.append(old_state.cache[input_node.name]) + elif input_node.name in new_state.cache: + summed_inputs.append(new_state.cache[input_node.name]) + + inputs.insert( + 0, torch.stack(summed_inputs).sum(0) + ) # Insert input, sum if multiple + + out = node.elem(*inputs) + # If the module is stateful, we know the output is (at least) a tuple if self.stateful_modules[node.name]: - state[node.name] = out[1:] + new_state.state[node.name] = out[1:] # Store the new state out = out[0] - return out + return out, new_state - def forward(self, data: torch.Tensor, state: Optional[Dict[str, Any]] = {}): - outs = {} + def forward( + self, data: torch.Tensor, old_state: Optional[GraphExecutorState] = None + ): + if old_state is None: + old_state = GraphExecutorState() + new_state = GraphExecutorState() + first_node = True # NOTE: This logic is not yet consistent for models with multiple input nodes for node in self.execution_order: input_nodes = self.graph.find_source_nodes_of(node) if node.elem is None: continue - if len(input_nodes) == 0 or len(outs) == 0: - # This is the root node - outs[node.name] = self._apply_module(node, data, state) - else: - # Intermediate nodes - input_data = [outs[node.name] for node in input_nodes] - input_data = torch.stack(input_data).sum( - 0 - ) # Multiple inputs are summed - outs[node.name] = self._apply_module(node, input_data, state) - if len(state) > 0: - return outs[node.name], state - else: - return outs[node.name] + out, new_state = self._apply_module( + node, input_nodes, new_state, old_state, data if first_node else None + ) + new_state.cache[node.name] = out + first_node = False + + # If the output node is a dummy nir.Output node, use the second-to-last node + if node.name not in new_state.cache: + node = self.execution_order[-2] + return new_state.cache[node.name], new_state def _mod_nir_to_graph(nir_graph: nir.NIRNode) -> Graph: diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index aad54d0..4ae9a33 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -9,12 +9,12 @@ def _torch_model_map(m: nir.NIRNode, device: str = "cpu") -> torch.nn.Module: if isinstance(m, nir.Affine): lin = torch.nn.Linear(*m.weight.shape[-2:]) - lin.weight.data = torch.nn.Parameter(torch.tensor(m.weight).to(device)) - lin.bias.data = torch.nn.Parameter(torch.tensor(m.bias).to(device)) + lin.weight.data = torch.nn.Parameter(torch.tensor(m.weight).to(device).float()) + lin.bias.data = torch.nn.Parameter(torch.tensor(m.bias).to(device).float()) return lin elif isinstance(m, nir.Linear): lin = torch.nn.Linear(*m.weight.shape[-2:], bias=False) - lin.weight.data = torch.nn.Parameter(torch.tensor(m.weight).to(device)) + lin.weight.data = torch.nn.Parameter(torch.tensor(m.weight).to(device).float()) return lin elif isinstance(m, nir.Input) or isinstance(m, nir.Output): return None @@ -46,22 +46,25 @@ def test_extract_lin(): assert isinstance(m.execution_order[0].elem, torch.nn.Linear) assert torch.allclose(m.execution_order[0].elem.weight, lin.weight) assert torch.allclose(m.execution_order[0].elem.bias, lin.bias) - assert torch.allclose(m(x), y) + assert isinstance(m.execution_order[1].elem, torch.nn.Linear) + assert torch.allclose(m.execution_order[1].elem.weight, lin.weight) + assert torch.allclose(m.execution_order[1].elem.bias, lin.bias) + assert torch.allclose(m(x)[0], y) -def test_extrac_recurrent(): +def test_extract_recurrent(): w = np.random.randn(1, 1) g = nir.NIRGraph( nodes={"in": nir.Input(np.ones(1)), "a": nir.Linear(w), "b": nir.Linear(w)}, edges=[("in", "a"), ("a", "b"), ("b", "a")], ) l1 = torch.nn.Linear(1, 1, bias=False) - l1.weight.data = torch.tensor(w) + l1.weight.data = torch.tensor(w).float() l2 = torch.nn.Linear(1, 1, bias=False) - l2.weight.data = torch.tensor(w) + l2.weight.data = torch.tensor(w).float() m = load(g, _torch_model_map) - data = torch.randn(1, 1, dtype=torch.float64) - torch.allclose(m(data), l2(l1(data))) + data = torch.randn(1, 1, dtype=torch.float32) + torch.allclose(m(data)[0], l2(l1(data))) def test_execute_stateful(): @@ -81,5 +84,22 @@ def forward(self, x, state=None): m = load(g, lambda m: StatefulModel()) out, state = m(torch.ones(10)) assert torch.allclose(out, torch.ones(10) * 3) - assert state["li"] == (1, ) - assert state["li"] == (1, ) + assert state.state["li"] == (1,) + assert state.state["li"] == (1,) + + +def test_execute_recurrent(): + w = np.ones((1, 1)) + g = nir.NIRGraph( + nodes={"in": nir.Input(np.ones(1)), "a": nir.Linear(w), "b": nir.Linear(w)}, + edges=[("in", "a"), ("a", "b"), ("b", "a")], + ) + m = load(g, _torch_model_map) + data = torch.ones(1, 1) + + # Same execution without reusing state should yield the same result + y1 = m(data) + y2 = m(data) + assert torch.allclose(y1[0], y2[0]) + out, s = m(*m(data)) + assert torch.allclose(out, torch.tensor(2.0)) From 0a1d97d0fd79f0d9d0f1a6cc953f58070835f7c7 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Thu, 12 Oct 2023 22:32:29 +0200 Subject: [PATCH 05/27] Added tests for recurrent execution --- nirtorch/from_nir.py | 11 +++++++---- tests/braille.nir | Bin 0 -> 41896 bytes tests/test_from_nir.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/braille.nir diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 4b3302e..349a1d5 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -1,6 +1,6 @@ import dataclasses import inspect -from typing import Callable, Dict, List, Optional, Any +from typing import Callable, Dict, List, Optional, Any, Union import nir import torch @@ -173,18 +173,21 @@ def _switch_models_with_map( def load( - nir_graph: nir.NIRNode, model_map: Callable[[nir.NIRNode], nn.Module] + nir_graph: Union[nir.NIRNode, str], model_map: Callable[[nir.NIRNode], nn.Module] ) -> nn.Module: - """Load a NIR object and convert it to a torch module using the given model map. + """Load a NIR graph and convert it to a torch module using the given model map. Args: - nir_graph (nir.NIRNode): NIR object + nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string representing + the path to the NIR object. model_map (Callable[[nn.NIRNode], nn.Module]): A method that returns the a torch module that corresponds to each NIR node. Returns: nn.Module: The generated torch module """ + if isinstance(nir_graph, str): + nir_graph = nir.read(nir_graph) # Map modules to the target modules using th emodel map nir_module_graph = _switch_models_with_map(nir_graph, model_map) # Build a nirtorch.Graph based on the nir_graph diff --git a/tests/braille.nir b/tests/braille.nir new file mode 100644 index 0000000000000000000000000000000000000000..ed8f57e7f4909bd10ea0e92cf2fba67f6c7c4efe GIT bin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C5Q1ve|;#kArp=9G=(}hL0AY zsE6+W}|0+_|ZDE@j9_lo?Ey2j)}f5 z=)Vwwuh*f(CE@@7{#l%4-HBW2KlM|L0cg`%3%`pA8`r%wVnF2 z-`C?$6HET@?hhk2e~G`ekE^f73DP0|+y1ZH|G&CFG?tVv?g?;s|2z9b(fb#oi|Y@i W{eQkcG?U torch.nn.Module: raise NotImplementedError(f"Unsupported module {m}") +def _recurrent_model_map(m: nir.NIRNode, device: str = "cpu") -> torch.nn.Module: + class MyCubaLIF(torch.nn.Module): + def __init__(self, lif, lin): + super().__init__() + self.lif = lif + self.lin = lin + + def forward(self, x, state=None): + if state is None: + state = torch.zeros_like(x) + z = self.lif(x + state) + return self.lin(z), z + + try: + return _torch_model_map(m, device) + except NotImplementedError: + if isinstance(m, nir.CubaLIF): + return torch.nn.Identity() + elif isinstance(m, nir.NIRGraph): + return MyCubaLIF( + _recurrent_model_map(m.nodes["lif"], device), + _recurrent_model_map(m.nodes["lin"], device), + ) + else: + raise NotImplementedError(f"Unsupported module {m}") + + def test_extract_empty(): g = nir.NIRGraph({}, []) with pytest.raises(ValueError): @@ -103,3 +130,9 @@ def test_execute_recurrent(): assert torch.allclose(y1[0], y2[0]) out, s = m(*m(data)) assert torch.allclose(out, torch.tensor(2.0)) + + +def test_import_braille(): + g = nir.read("tests/braille.nir") + m = load(g, _recurrent_model_map) + assert m(torch.empty(1, 12))[0].shape == (1, 7) From 5cc87dee34b48519c182a9a0beb436ad1df8e2ed Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Fri, 13 Oct 2023 17:23:32 +0200 Subject: [PATCH 06/27] test for NIR -> NIRTorch -> NIR --- tests/lif_norse.nir | Bin 0 -> 17584 bytes tests/test_bidirectional.py | 92 ++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/lif_norse.nir create mode 100644 tests/test_bidirectional.py diff --git a/tests/lif_norse.nir b/tests/lif_norse.nir new file mode 100644 index 0000000000000000000000000000000000000000..ce5f8e1c230f01b93edf3678479ebcfebfbfaa69 GIT binary patch literal 17584 zcmeHO&2JM&6o2awYSMztDGd^0q+Z;Lf}yB&TM!8(;7Accw1?ahqZn)vB~jv}?g@_O zmSc`N_SiomM-CuyeQ##=?d$b zm&S-wHcMwH7iRJIQPbbkQiC$umoyV7Z7TgMBbk8sNA$JUU)26xzTcnB%#h_o|Dbvq z70yU5iwaLq6#}K1smHAFf@%$WdZTs*Ft#fjo3*u7T3uVNa2@P&5)6IPfJUVc?I<9z zK=eCeEt~TbK@eItdyljU->Bv1awa3yS@nbwbmaIO(Qcm-9xwZEkLITX%0yB(MrC~9 zv8Uyn-XQp^8981#(>_|adC|o$X^xKUoO!p*@yySlzrxBR+%(^;G6H1+IR zZM8ync9n$d=4Ky+72pG%->QcleWCo2i)G;iIo$el(j1Murv1o<2Xi%1LHWK^ihfZ4 zmDWSvn(C$1^!@^Jo_Yj40v-X6fJeY1;1Tc$cmzBG9s!SlMLs@S>aMLK=0QDQJNDt5Ux}pc-P^Z!+OOk0`fDGDW9?jzemExIUgp?+dzqKK z_A=|xJ`Uai*lM3V0v-X6fJeY1;1TG6fV&^YV`4$~qqqdm|C`lv7zp6?m(yhU-~XSS zD{7rS+`l4ypkH@C1G)Zj_&V)pxTl8htI?mk&o(j~HSS0z;?weexVurN6qBQiqZlJ< zBhmm6c9BUXbS)8dhv(eKt}VwbD!L+fI|MJ z+cKQxxLxH#MFW|}BYTCNxfJK*FPbZQ7Szyqmd0NyT;11&k`Wf1Ak_<~fy{}}I37vs zq4-_@`p+3r(AB?rB6@clG-*Bj_b0hhtfpHpzJ6E{aMweuYfX(iLvO5y_3~C&wV@5$ ziz}7#3!?4CdUd0+SzTLM4qGtK80B+G+)rhO$90&O`u~3Cu?~rq03#2=dN>-rS1$bm zxjR#6SNyqiJ8A`0q66oCq8-#|X2HG{=#?-}RB!j torch.nn.Module: + if isinstance(node, (nir.Linear, nir.Affine)): + return torch.nn.Linear(*node.weight.shape) + + elif isinstance(node, (nir.LIF, nir.CubaLIF)): + return snn.Leaky(0.9, init_hidden=True) + + else: + return None + + +def _nir_to_pytorch_module(node: nir.NIRNode) -> torch.nn.Module: + if isinstance(node, (nir.Linear, nir.Affine)): + return torch.nn.Linear(*node.weight.shape) + + elif isinstance(node, (nir.LIF, nir.CubaLIF)): + return torch.nn.Identity() + + else: + return None + + +if use_snntorch: + _nir_to_torch_module = _nir_to_snntorch_module +else: + _nir_to_torch_module = _nir_to_pytorch_module + + +def _create_torch_model() -> torch.nn.Module: + if use_snntorch: + return torch.nn.Sequential(torch.nn.Linear(1, 1), snn.Leaky(0.9, init_hidden=True)) + else: + return torch.nn.Sequential(torch.nn.Linear(1, 1), torch.nn.Identity()) + + +def _torch_to_nir(module: torch.nn.Module) -> nir.NIRNode: + if isinstance(module, torch.nn.Linear): + return nir.Linear(np.array(module.weight.data)) + + else: + return None + + +def _lif_nir_graph(from_file=True): + if from_file: + return nir.read('tests/lif_norse.nir') + else: + return nir.NIRGraph( + nodes={ + '0': nir.Affine(weight=np.array([[1.]]), bias=np.array([0.])), + '1': nir.LIF( + tau=np.array([0.1]), + r=np.array([1.]), + v_leak=np.array([0.]), + v_threshold=np.array([0.1]) + ), + 'input': nir.Input(input_type={'input': np.array([1])}), + 'output': nir.Output(output_type={'output': np.array([1])}) + }, + edges=[ + ('input', '0'), ('0', '1'), ('1', 'output') + ] + ) + + +def test_nir_to_torch_to_nir(from_file=True): + graph = _lif_nir_graph(from_file=from_file) + assert graph is not None + module = nirtorch.load(graph, _nir_to_torch_module) + assert module is not None + graph2 = nirtorch.extract_nir_graph(module, _torch_to_nir, torch.zeros(1, 1)) + print('original NIR edges', graph.edges) + print('converted NIR edges', graph2.edges) + assert graph2 is not None + + +# if __name__ == '__main__': +# test_nir_to_torch_to_nir(from_file=False) From 70f44474f1874af4d9bb937c0d101c88d913a14d Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Fri, 13 Oct 2023 19:03:03 +0200 Subject: [PATCH 07/27] refactoring + expose ignore_submodules_of --- nirtorch/graph.py | 18 ++++++++++-------- nirtorch/to_nir.py | 8 ++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/nirtorch/graph.py b/nirtorch/graph.py index aae3e61..1d3f5d3 100644 --- a/nirtorch/graph.py +++ b/nirtorch/graph.py @@ -194,11 +194,16 @@ def populate_from(self, other_graph: "Graph"): def __str__(self) -> str: return self.to_md() + def debug_str(self) -> str: + debug_str = "" + for node in self.node_list: + debug_str += f"{node.name} ({node.elem.__class__.__name__})\n" + for outgoing, shape in node.outgoing_nodes.items(): + debug_str += f"\t-> {outgoing.name} ({outgoing.elem.__class__.__name__})\n" + return debug_str.strip() + def to_md(self) -> str: - mermaid_md = """ -```mermaid -graph TD; -""" + mermaid_md = """```mermaid\ngraph TD;\n""" for node in self.node_list: if node.outgoing_nodes: for outgoing, _ in node.outgoing_nodes.items(): @@ -206,10 +211,7 @@ def to_md(self) -> str: else: mermaid_md += f"{node.name};\n" - end = """ -``` -""" - return mermaid_md + end + return mermaid_md + "\n```\n" def leaf_only(self) -> "Graph": leaf_modules = self.get_leaf_modules() diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index c12511d..f680d19 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -12,9 +12,14 @@ def extract_nir_graph( model_map: Callable[[nn.Module], nir.NIRNode], sample_data: Any, model_name: Optional[str] = "model", + ignore_submodules_of=None, ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. + Assumptions and known issues: + - Cannot deal with layers like torch.nn.Identity(), since the input tensor and output + tensor will be the same object, and therefore lead to cyclic connections. + Args: model (nn.Module): The model of interest model_map (Callable[[nn.Module], nir.NIRNode]): A method that converts a given @@ -36,6 +41,9 @@ def extract_nir_graph( model, sample_data=sample_data, model_name=model_name ).ignore_tensors() + if ignore_submodules_of is not None: + torch_graph = torch_graph.ignore_submodules_of(ignore_submodules_of) + # Get the root node root_nodes = torch_graph.get_root() if len(root_nodes) != 1: From 175d34f8109ea1a221bfc66c67d6cdaf7be06c76 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Fri, 13 Oct 2023 19:04:18 +0200 Subject: [PATCH 08/27] fix and test for issue #16 --- nirtorch/from_nir.py | 12 +++++++++--- tests/test_bidirectional.py | 5 ++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 349a1d5..5870c5e 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -122,9 +122,15 @@ def _apply_module( elif input_node.name in new_state.cache: summed_inputs.append(new_state.cache[input_node.name]) - inputs.insert( - 0, torch.stack(summed_inputs).sum(0) - ) # Insert input, sum if multiple + if len(input_nodes) == 0 and data is not None: + # single input, no need to sum (fix issue #16) + inputs.insert(0, data) + elif len(input_nodes) == 1: + # single input, no need to sum (fix issue #16) + inputs.insert(0, summed_inputs[0]) + else: + # multiple inputs, sum them + inputs.insert(0, torch.stack(summed_inputs).sum(0)) out = node.elem(*inputs) # If the module is stateful, we know the output is (at least) a tuple diff --git a/tests/test_bidirectional.py b/tests/test_bidirectional.py index 9018d81..3a8abce 100644 --- a/tests/test_bidirectional.py +++ b/tests/test_bidirectional.py @@ -28,7 +28,7 @@ def _nir_to_pytorch_module(node: nir.NIRNode) -> torch.nn.Module: return torch.nn.Linear(*node.weight.shape) elif isinstance(node, (nir.LIF, nir.CubaLIF)): - return torch.nn.Identity() + return torch.nn.Linear(1, 1) else: return None @@ -83,8 +83,7 @@ def test_nir_to_torch_to_nir(from_file=True): module = nirtorch.load(graph, _nir_to_torch_module) assert module is not None graph2 = nirtorch.extract_nir_graph(module, _torch_to_nir, torch.zeros(1, 1)) - print('original NIR edges', graph.edges) - print('converted NIR edges', graph2.edges) + assert sorted(graph.edges) == sorted(graph2.edges) assert graph2 is not None From 37e423732c3998fa6dc765feb90f7b272a9c8282 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Mon, 16 Oct 2023 07:58:52 +0200 Subject: [PATCH 09/27] fix recurrent test --- nirtorch/from_nir.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 5870c5e..17d227e 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -122,14 +122,11 @@ def _apply_module( elif input_node.name in new_state.cache: summed_inputs.append(new_state.cache[input_node.name]) - if len(input_nodes) == 0 and data is not None: - # single input, no need to sum (fix issue #16) - inputs.insert(0, data) - elif len(input_nodes) == 1: - # single input, no need to sum (fix issue #16) + if len(summed_inputs) == 0: + raise ValueError("No inputs found for node {}".format(node.name)) + elif len(summed_inputs) == 1: inputs.insert(0, summed_inputs[0]) - else: - # multiple inputs, sum them + elif len(summed_inputs) > 1: inputs.insert(0, torch.stack(summed_inputs).sum(0)) out = node.elem(*inputs) From c76fb6f0d4d0a567d035b4ede9e6f23849ad1743 Mon Sep 17 00:00:00 2001 From: Sadique Sheik Date: Wed, 18 Oct 2023 14:38:42 +0300 Subject: [PATCH 10/27] remove batch froms shape spec --- nirtorch/to_nir.py | 10 +++++++--- tests/test_conversion.py | 2 +- tests/test_from_nir.py | 3 ++- tests/test_graph.py | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index a6ef230..686bfa2 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -12,7 +12,7 @@ def extract_nir_graph( model_map: Callable[[nn.Module], nir.NIRNode], sample_data: Any, model_name: Optional[str] = "model", - ignore_submodules_of=None + ignore_submodules_of=None, ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. @@ -49,7 +49,9 @@ def extract_nir_graph( # Convert the nodes and get indices nir_edges = [] - nir_nodes = {"input": nir.Input(np.array(sample_data.shape))} + nir_nodes = { + "input": nir.Input(np.array(sample_data.shape[1:])) + } # Remove the first dimension # Get all the NIR nodes for indx, node in enumerate(torch_graph.node_list): @@ -85,7 +87,9 @@ def extract_nir_graph( if len(node.outgoing_nodes) == 0: out_name = "output" # Try to find shape of input to the Output node - output_node = nir.Output(torch_graph.module_output_types[node.elem]) + output_node = nir.Output( + torch_graph.module_output_types[node.elem][1:] + ) # Ignore batch dimension nir_nodes[out_name] = output_node nir_edges.append((node.name, out_name)) diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 4e2f0db..bcbd527 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -7,7 +7,7 @@ def _torch_convert(module: nn.Module) -> nir.NIRNode: if isinstance(module, nn.Conv1d): - return nir.Conv1d(module.weight, 1, 1, 1, 1, module.bias) + return nir.Conv1d(None, module.weight, 1, 1, 1, 1, module.bias) elif isinstance(module, nn.Linear): return nir.Affine(module.weight, module.bias) else: diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index d6d09ae..55bfc01 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -17,7 +17,7 @@ def _torch_model_map(m: nir.NIRNode, device: str = "cpu") -> torch.nn.Module: lin.weight.data = torch.nn.Parameter(torch.tensor(m.weight).to(device)) return lin elif isinstance(m, nir.Input) or isinstance(m, nir.Output): - return None + return torch.nn.Identity() else: raise NotImplementedError(f"Unsupported module {m}") @@ -49,6 +49,7 @@ def test_extract_lin(): assert torch.allclose(m(x), y) +@pytest.mark.skip("Not yet supported") def test_extrac_recurrent(): w = np.random.randn(1, 1) g = nir.NIRGraph( diff --git a/tests/test_graph.py b/tests/test_graph.py index c99e1ed..b999721 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -279,8 +279,8 @@ def test_output_type_when_single_node(): def test_sequential_flatten(): d = torch.empty(2, 3, 4) g = extract_nir_graph(torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d) - g.nodes["input"].input_type["input"] == (2, 3, 4) - g.nodes["output"].output_type["output"] == (2, 3 * 4) + assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) + assert tuple(g.nodes["output"].output_type["output"]) == (3 * 4, ) @pytest.mark.skip(reason="Not supported yet") From 48f9842f801bde216b974fe990e7837cb7379b9a Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Wed, 18 Oct 2023 22:57:41 +0200 Subject: [PATCH 11/27] bug from hell --- nirtorch/from_nir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 17d227e..5018169 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -99,8 +99,8 @@ def _apply_module( self, node: Node, input_nodes: List[Node], - old_state: GraphExecutorState, new_state: GraphExecutorState, + old_state: GraphExecutorState, data: Optional[torch.Tensor] = None, ): """Applies a module and keeps track of its state. From 26242d3da708197599cc8602e40523427ce30cf2 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Thu, 19 Oct 2023 17:08:06 +0200 Subject: [PATCH 12/27] from_nir hacks for snnTorch --- nirtorch/from_nir.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 5018169..9faeb7e 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -70,6 +70,10 @@ def __init__(self, graph: Graph) -> None: def _is_module_stateful(self, module: torch.nn.Module) -> bool: signature = inspect.signature(module.forward) arguments = len(signature.parameters) + # HACK for snntorch modules + if 'snntorch' in str(module.__class__): + if module.__class__.__name__ in ['Synaptic', 'RSynaptic', 'Leaky', 'RLeaky']: + return not module.init_hidden return arguments > 1 def get_execution_order(self) -> List[Node]: @@ -131,7 +135,13 @@ def _apply_module( out = node.elem(*inputs) # If the module is stateful, we know the output is (at least) a tuple - if self.stateful_modules[node.name]: + # HACK to make it work for snnTorch + is_rsynaptic = 'snntorch._neurons.rsynaptic.RSynaptic' in str(node.elem.__class__) + if is_rsynaptic and not node.elem.init_hidden: + assert 'lif' in node.name, "this shouldnt happen.." + new_state.state[node.name] = out # snnTorch requires output inside state + out = out[0] + elif self.stateful_modules[node.name]: new_state.state[node.name] = out[1:] # Store the new state out = out[0] return out, new_state From 668e023aa58c601453ba55e3884c25110c2e478a Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Thu, 19 Oct 2023 17:09:00 +0200 Subject: [PATCH 13/27] + optional model.forward args for stateful modules --- nirtorch/graph.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nirtorch/graph.py b/nirtorch/graph.py index 1d3f5d3..ec1b8ae 100644 --- a/nirtorch/graph.py +++ b/nirtorch/graph.py @@ -386,7 +386,7 @@ def __exit__(self, exc_type, exc_value, exc_tb): def extract_torch_graph( - model: nn.Module, sample_data: Any, model_name: Optional[str] = "model" + model: nn.Module, sample_data: Any, model_name: Optional[str] = "model", model_args=[] ) -> Graph: """Extract computational graph between various modules in the model NOTE: This method is not capable of any compute happening outside of module @@ -409,6 +409,6 @@ def extract_torch_graph( with GraphTracer( named_modules_map(model, model_name=model_name) ) as tracer, torch.no_grad(): - _ = model(sample_data) + _ = model(sample_data, *model_args) return tracer.graph From c555b2a0b83f2aff0d2c0732fd14f3d06ce364d2 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Thu, 19 Oct 2023 17:12:44 +0200 Subject: [PATCH 14/27] change subgraphs handlign (flatten + remove I/O) --- nirtorch/to_nir.py | 50 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index f680d19..5d1c7a1 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -53,21 +53,38 @@ def extract_nir_graph( # Convert the nodes and get indices nir_edges = [] - nir_nodes = {"input": nir.Input(np.array(sample_data.shape))} - + input_shape = np.array(sample_data.shape) + # HACK: ignore dimensions + input_shape = np.array([e for idx, e in enumerate(sample_data.shape) if idx not in ignore_dims]) + nir_nodes = {"input": nir.Input(input_shape)} + + subgraph_keys = [] + subgraph_input_nodekeys = [] + subgraph_output_nodekeys = [] # Get all the NIR nodes for indx, node in enumerate(torch_graph.node_list): # Convert the node type to NIR subgraph mapped_node = model_map(node.elem) if isinstance(mapped_node, nir.NIRGraph): + subgraph_keys.append(node.name) for k, v in mapped_node.nodes.items(): # For now, we add nodes in subgraphs to the top-level node list - # TODO: Parse graphs recursively + # TODO: support deeper nesting -> parse graphs recursively + assert not isinstance(v, nir.NIRGraph), "cannot handle sub-sub-graphs" + + subgraph_node_key = f"{node.name}.{k}" + + # keep track of subgraph input and outputs (to remove later) + if isinstance(v, nir.Input): + subgraph_input_nodekeys.append(subgraph_node_key) + elif isinstance(v, nir.Output): + subgraph_output_nodekeys.append(subgraph_node_key) + if isinstance(v, nir.NIRNode): - nir_nodes[f"{node.name}.{k}"] = v + nir_nodes[subgraph_node_key] = v else: - nir_nodes[v.name] = v + nir_nodes[v.name] = v # would this ever happen?? # Add edges from graph for x, y in mapped_node.edges: nir_edges.append((f"{node.name}.{x}", f"{node.name}.{y}")) @@ -96,4 +113,27 @@ def extract_nir_graph( # Remove duplicate edges nir_edges = list(set(nir_edges)) + # change edges to subgraph to point to either input or output of subgraph + for idx in range(len(nir_edges)): + if nir_edges[idx][0] in subgraph_keys: + nir_edges[idx] = (f"{nir_edges[idx][0]}.output", nir_edges[idx][1]) + if nir_edges[idx][1] in subgraph_keys: + nir_edges[idx] = (nir_edges[idx][0], f"{nir_edges[idx][1]}.input") + + # remove subgraph input and output nodes (& redirect edges) + for rm_nodekey in subgraph_input_nodekeys + subgraph_output_nodekeys: + in_keys = [e[0] for e in nir_edges if e[1] == rm_nodekey] + out_keys = [e[1] for e in nir_edges if e[0] == rm_nodekey] + # connect all incoming to all outgoing nodes + for in_key in in_keys: + for out_key in out_keys: + nir_edges.append((in_key, out_key)) + # remove the original edges + for in_key in in_keys: + nir_edges.remove((in_key, rm_nodekey)) + for out_key in out_keys: + nir_edges.remove((rm_nodekey, out_key)) + # remove the node + nir_nodes.pop(rm_nodekey) + return nir.NIRGraph(nir_nodes, nir_edges) From 60c01f84af7a7529c503bb23414f6fb2ce36f316 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Thu, 19 Oct 2023 17:13:31 +0200 Subject: [PATCH 15/27] model fwd args + ignore_dims arg --- nirtorch/to_nir.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 5d1c7a1..a114efe 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -13,6 +13,8 @@ def extract_nir_graph( sample_data: Any, model_name: Optional[str] = "model", ignore_submodules_of=None, + model_fwd_args=[], + ignore_dims=[], ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. @@ -38,7 +40,7 @@ def extract_nir_graph( # Extract a torch graph given the model torch_graph = extract_torch_graph( - model, sample_data=sample_data, model_name=model_name + model, sample_data=sample_data, model_name=model_name, model_args=model_fwd_args ).ignore_tensors() if ignore_submodules_of is not None: From d4b1afb35b6104971c3c458702a75ef12b874256 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Thu, 19 Oct 2023 17:18:26 +0200 Subject: [PATCH 16/27] [hack] remove wrong RNN self-connection (NIRTorch) --- nirtorch/to_nir.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index a114efe..500bf2a 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -138,4 +138,10 @@ def extract_nir_graph( # remove the node nir_nodes.pop(rm_nodekey) + # HACK: remove self-connections (this is a bug in the extraction of an RNN graph) + for edge in nir_edges: + if edge[0] == edge[1]: + print(f"[WARNING] removing self-connection {edge}") + nir_edges.remove(edge) + return nir.NIRGraph(nir_nodes, nir_edges) From c736c0e15233e1cfbcf07cc25adf0a84df43c0c2 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Thu, 19 Oct 2023 22:42:58 +0200 Subject: [PATCH 17/27] Added proper graph tracing --- nirtorch/from_nir.py | 107 +++++++++++++++++------------------- nirtorch/graph.py | 44 ++++++++++----- nirtorch/graph_utils.py | 25 +++++++++ nirtorch/to_nir.py | 10 +--- tests/braille.nir | Bin 41896 -> 34264 bytes tests/test_bidirectional.py | 24 ++++---- tests/test_conversion.py | 6 +- tests/test_from_nir.py | 29 +++++++--- tests/test_graph.py | 6 +- tests/test_graph_utils.py | 45 +++++++++++++++ tests/test_to_nir.py | 15 +++-- 11 files changed, 199 insertions(+), 112 deletions(-) create mode 100644 tests/test_graph_utils.py diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 9faeb7e..4ad5cf6 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -7,46 +7,10 @@ import torch.nn as nn from .graph import Graph, Node +from .graph_utils import trace_execution from .utils import sanitize_name -def execution_order_up_to_node( - node: Node, - graph: Graph, - execution_order: List[Node], - visited: Optional[Dict[Node, bool]] = None, -) -> List[Node]: - """Recursive function to evaluate execution order until a given node. - - Args: - node (Node): Execution order for the node of interest - graph (Graph): Graph object describing the network - execution_order (List[Node]): The current known execution order. - - Returns: - List[Node]: Execution order - """ - if visited is None: - visited = {n: False for n in graph.node_list} - is_recursive = False - if len(execution_order) == list(graph.node_list): - # All nodes are executed - return execution_order - for parent in graph.find_source_nodes_of(node): - if parent not in execution_order and not visited[parent]: - visited[parent] = True - execution_order = execution_order_up_to_node( - parent, graph, execution_order, visited - ) - if node in parent.outgoing_nodes: - is_recursive = True - # Ensure we're not re-adding a recursive node - if is_recursive and node in execution_order: - return execution_order - else: # Finally since all parents are known and executed - return execution_order + [node] - - @dataclasses.dataclass class GraphExecutorState: """State for the GraphExecutor that keeps track of both the state of hidden @@ -71,21 +35,25 @@ def _is_module_stateful(self, module: torch.nn.Module) -> bool: signature = inspect.signature(module.forward) arguments = len(signature.parameters) # HACK for snntorch modules - if 'snntorch' in str(module.__class__): - if module.__class__.__name__ in ['Synaptic', 'RSynaptic', 'Leaky', 'RLeaky']: + if "snntorch" in str(module.__class__): + if module.__class__.__name__ in [ + "Synaptic", + "RSynaptic", + "Leaky", + "RLeaky", + ]: return not module.init_hidden return arguments > 1 def get_execution_order(self) -> List[Node]: """Evaluate the execution order and instantiate that as a list.""" - execution_order = [] - # Then loop over all nodes and check that they are added to the execution order. - for node in self.graph.node_list: - if node not in execution_order: - execution_order = execution_order_up_to_node( - node, self.graph, execution_order - ) - return execution_order + # TODO: Adapt this for graphs with multiple inputs + inputs = self.graph.inputs + if len(inputs) != 1: + raise ValueError( + f"Currently, only one input is supported, but {len(inputs)} was given" + ) + return trace_execution(inputs[0], lambda n: n.outgoing_nodes.keys()) def instantiate_modules(self): for mod, name in self.graph.module_names.items(): @@ -136,9 +104,11 @@ def _apply_module( out = node.elem(*inputs) # If the module is stateful, we know the output is (at least) a tuple # HACK to make it work for snnTorch - is_rsynaptic = 'snntorch._neurons.rsynaptic.RSynaptic' in str(node.elem.__class__) + is_rsynaptic = "snntorch._neurons.rsynaptic.RSynaptic" in str( + node.elem.__class__ + ) if is_rsynaptic and not node.elem.init_hidden: - assert 'lif' in node.name, "this shouldnt happen.." + assert "lif" in node.name, "this shouldnt happen.." new_state.state[node.name] = out # snnTorch requires output inside state out = out[0] elif self.stateful_modules[node.name]: @@ -159,7 +129,11 @@ def forward( if node.elem is None: continue out, new_state = self._apply_module( - node, input_nodes, new_state, old_state, data if first_node else None + node, + input_nodes, + new_state=new_state, + old_state=old_state, + data=data if first_node else None, ) new_state.cache[node.name] = out first_node = False @@ -170,18 +144,37 @@ def forward( return new_state.cache[node.name], new_state -def _mod_nir_to_graph(nir_graph: nir.NIRNode) -> Graph: - module_names = {module: name for name, module in nir_graph.nodes.items()} - graph = Graph(module_names=module_names) - for src, dst in nir_graph.edges: - graph.add_edge(nir_graph.nodes[src], nir_graph.nodes[dst]) +def _mod_nir_to_graph( + torch_graph: nir.NIRGraph, nir_nodes: Dict[str, nir.NIRNode] +) -> Graph: + module_names = {module: name for name, module in torch_graph.nodes.items()} + inputs = [name for name, node in nir_nodes.items() if isinstance(node, nir.Input)] + graph = Graph(module_names=module_names, inputs=inputs) + for src, dst in torch_graph.edges: + # Allow edges to refer to subgraph inputs and outputs + if not src in torch_graph.nodes and f"{src}.output" in torch_graph.nodes: + src = f"{src}.output" + if not dst in torch_graph.nodes and f"{dst}.input" in torch_graph.nodes: + dst = f"{dst}.input" + graph.add_edge(torch_graph.nodes[src], torch_graph.nodes[dst]) return graph +def _switch_default_models(nir_graph: nir.NIRNode) -> Optional[torch.nn.Module]: + if isinstance(nir_graph, nir.Input) or isinstance(nir_graph, nir.Output): + return torch.nn.Identity() + + def _switch_models_with_map( nir_graph: nir.NIRNode, model_map: Callable[[nn.Module], nn.Module] ) -> nir.NIRNode: - nodes = {name: model_map(node) for name, node in nir_graph.nodes.items()} + nodes = {} + for name, node in nir_graph.nodes.items(): + mapped_module = model_map(node) + if mapped_module is None: + mapped_module = _switch_default_models(node) + nodes[name] = mapped_module + # nodes = {name: model_map(node) for name, node in nir_graph.nodes.items()} return nir.NIRGraph(nodes, nir_graph.edges) @@ -204,6 +197,6 @@ def load( # Map modules to the target modules using th emodel map nir_module_graph = _switch_models_with_map(nir_graph, model_map) # Build a nirtorch.Graph based on the nir_graph - graph = _mod_nir_to_graph(nir_module_graph) + graph = _mod_nir_to_graph(nir_module_graph, nir_nodes=nir_graph.nodes) # Build and return a graph executor module return GraphExecutor(graph) diff --git a/nirtorch/graph.py b/nirtorch/graph.py index ec1b8ae..fde07c4 100644 --- a/nirtorch/graph.py +++ b/nirtorch/graph.py @@ -71,15 +71,19 @@ class Graph: def __init__( self, module_names: Dict[nn.Module, str], + inputs: List[str], module_output_types: Dict[nn.Module, torch.Tensor] = {}, ) -> None: self.module_names = module_names self.node_list: List[Node] = [] self.module_output_types = module_output_types self._last_used_tensor_id = None + self.inputs = [] # Add modules to node_list for mod, name in self.module_names.items(): - self.add_elem(mod, name) + node = self.add_elem(mod, name) + if name in inputs: + self.inputs.append(node) @property def node_map_by_id(self): @@ -199,7 +203,9 @@ def debug_str(self) -> str: for node in self.node_list: debug_str += f"{node.name} ({node.elem.__class__.__name__})\n" for outgoing, shape in node.outgoing_nodes.items(): - debug_str += f"\t-> {outgoing.name} ({outgoing.elem.__class__.__name__})\n" + debug_str += ( + f"\t-> {outgoing.name} ({outgoing.elem.__class__.__name__})\n" + ) return debug_str.strip() def to_md(self) -> str: @@ -215,7 +221,7 @@ def to_md(self) -> str: def leaf_only(self) -> "Graph": leaf_modules = self.get_leaf_modules() - filtered_graph = Graph(leaf_modules) + filtered_graph = Graph(leaf_modules, inputs=self.inputs) # Populate edges filtered_graph.populate_from(self) return filtered_graph @@ -241,7 +247,11 @@ def ignore_submodules_of(self, classes: List[Type]) -> "Graph": if mod not in sub_modules_to_ignore: new_named_modules[mod] = name # Create a new graph with the allowed modules - new_graph = Graph(new_named_modules, self.module_output_types) + new_graph = Graph( + new_named_modules, + inputs=self.inputs, + module_output_types=self.module_output_types, + ) new_graph.populate_from(self) return new_graph @@ -256,7 +266,7 @@ def find_source_nodes_of(self, node: Node) -> List[Node]: """ source_node_list = [] for source_node in self.node_list: - for outnode, shape in source_node.outgoing_nodes.items(): + for outnode, _ in source_node.outgoing_nodes.items(): if node == outnode: source_node_list.append(source_node) return source_node_list @@ -276,7 +286,11 @@ def ignore_nodes(self, class_type: Type) -> "Graph": } # Generate the new graph with the filtered module names - graph = Graph(new_module_names, self.module_output_types) + graph = Graph( + new_module_names, + inputs=self.inputs, + module_output_types=self.module_output_types, + ) # Iterate over all the nodes for node in self.node_list: if isinstance(node.elem, class_type): @@ -308,13 +322,7 @@ def get_root(self) -> List[Node]: Returns: List[Node]: A list of root nodes for the graph. """ - roots = [] - for node in self.node_list: - sources = self.find_source_nodes_of(node) - # Append root node if it has no sources (and it isn't a sequential module) - if len(sources) == 0 and not isinstance(node.elem, torch.nn.Sequential): - roots.append(node) - return roots + return self.inputs _torch_module_call = torch.nn.Module.__call__ @@ -386,7 +394,10 @@ def __exit__(self, exc_type, exc_value, exc_tb): def extract_torch_graph( - model: nn.Module, sample_data: Any, model_name: Optional[str] = "model", model_args=[] + model: nn.Module, + sample_data: Any, + model_name: Optional[str] = "model", + model_args=[], ) -> Graph: """Extract computational graph between various modules in the model NOTE: This method is not capable of any compute happening outside of module @@ -411,4 +422,9 @@ def extract_torch_graph( ) as tracer, torch.no_grad(): _ = model(sample_data, *model_args) + # HACK: The current graph is using copy-constructors, that detaches + # the traced output_types from the original graph. + # In the future, find a way to synchronize the two representations + tracer.graph.module_output_types = tracer.output_types + return tracer.graph diff --git a/nirtorch/graph_utils.py b/nirtorch/graph_utils.py index 54ccfd4..1f67d71 100644 --- a/nirtorch/graph_utils.py +++ b/nirtorch/graph_utils.py @@ -1,3 +1,8 @@ +from typing import Callable, List, Set, TypeVar + +T = TypeVar("T") + + def find_children(node, edges): """Given a node and the edges of a graph, find all direct children of that node.""" return set(child for (parent, child) in edges if parent == node) @@ -59,3 +64,23 @@ def find_all_ancestors( # # return execution_order # + +def trace_execution( + node: T, edge_fn: Callable[[T], List[T]], visited: Set[T] = None +) -> List[T]: + """Traces the execution of a node by listing them in order, coloring recursive + nodes to avoid adding the same node twice. + """ + if visited is None: + visited = set() + + if node in visited: + return [] + else: + visited.add(node) + + successors = [] + for child in edge_fn(node): + if child not in visited: + successors += trace_execution(child, edge_fn, visited) + return [node] + successors \ No newline at end of file diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 500bf2a..df01ada 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -1,4 +1,5 @@ from typing import Any, Callable, Optional +import logging import nir import numpy as np @@ -46,13 +47,6 @@ def extract_nir_graph( if ignore_submodules_of is not None: torch_graph = torch_graph.ignore_submodules_of(ignore_submodules_of) - # Get the root node - root_nodes = torch_graph.get_root() - if len(root_nodes) != 1: - raise ValueError( - f"Currently, only one input is supported, but {len(root_nodes)} was given" - ) - # Convert the nodes and get indices nir_edges = [] input_shape = np.array(sample_data.shape) @@ -141,7 +135,7 @@ def extract_nir_graph( # HACK: remove self-connections (this is a bug in the extraction of an RNN graph) for edge in nir_edges: if edge[0] == edge[1]: - print(f"[WARNING] removing self-connection {edge}") + logging.warn(f"removing self-connection {edge}") nir_edges.remove(edge) return nir.NIRGraph(nir_nodes, nir_edges) diff --git a/tests/braille.nir b/tests/braille.nir index ed8f57e7f4909bd10ea0e92cf2fba67f6c7c4efe..111b0a44eed106e15c89346c38ae988a4bea7a5d 100644 GIT binary patch delta 2591 zcmc&$Uu+ab7{8g8c6Xs=+t}V=4(@`9Tq}3p9i|>txj%T^da+4~8nF+xfYxg@b}b-< z8ut)Q2%vWPUYfp8h=~L>xy?p$A|^-D1Y&%+=*yY(0exsld?E2cV?yAY-C4TDo)OZ( zKFs(1e&5V|GvCa9-<-QbYbWSbh}J$ax(5ZgF36+_jtE}TY_$UXhPnuCqL)Z;S@LdO z?Xf?J%XdbGW08ZekH5LsCf0CXaNDs{3mYA4tz%tVhnIm!UeX3JY}?^I(F;{E2!bS& z4#s6V`M7$k@z$A_)1GpRNmj3I(JQA-**8t-)sJ;!15{oU%<8%Xf!(=)QTdgC>=W8c zd`v%=rJ`;OLZ#VfPOVoZj28)gitXp2u8j$&>l?O0IG*5mdKhR|1j?c4=&>D3yOcPt zDmI^ThK?@n*T3J!vz+JMJ-)Y>_Vayc;{>iYVph9mB@#7^W(uXO0L303RI_KH_vuRz z$w_qAcD{S)PXwWu6HX-*sOAJ(cpeQ>)Sk*Sk=2cNK~+Yn(hKv-7utn=1-)-ifAS$o z?O{3Qezkkw`OopAZygVYlPe;%m73^2=h0SdC;zd4B=MAP-FhswjMVBG9uTk9UD<!q@SWrmCT$V)h%4 z$Apw^nuhN)-%73&KeOs_8Z6je%ERcTOs`pPXlfsGAU`1x8Fr5ZSiPZz5#3mTU_(3P zw-%%nPvfCR^!FJYUezfrW_*~*2FNH}%nn(G#T0L5dLxYmcC_qcC+i$KtAngYI+)eX z1{Zz=^+p%&xC^%-c3+})#4xM+JJ_V|iP`f29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O&nn%(KZ|(+vE;EsFX{d4>yYiY z^Q4!wuS torch.nn.Module: def _create_torch_model() -> torch.nn.Module: if use_snntorch: - return torch.nn.Sequential(torch.nn.Linear(1, 1), snn.Leaky(0.9, init_hidden=True)) + return torch.nn.Sequential( + torch.nn.Linear(1, 1), snn.Leaky(0.9, init_hidden=True) + ) else: return torch.nn.Sequential(torch.nn.Linear(1, 1), torch.nn.Identity()) @@ -57,23 +59,21 @@ def _torch_to_nir(module: torch.nn.Module) -> nir.NIRNode: def _lif_nir_graph(from_file=True): if from_file: - return nir.read('tests/lif_norse.nir') + return nir.read("tests/lif_norse.nir") else: return nir.NIRGraph( nodes={ - '0': nir.Affine(weight=np.array([[1.]]), bias=np.array([0.])), - '1': nir.LIF( + "input": nir.Input(input_type={"input": np.array([1])}), + "0": nir.Affine(weight=np.array([[1.0]]), bias=np.array([0.0])), + "1": nir.LIF( tau=np.array([0.1]), - r=np.array([1.]), - v_leak=np.array([0.]), - v_threshold=np.array([0.1]) + r=np.array([1.0]), + v_leak=np.array([0.0]), + v_threshold=np.array([0.1]), ), - 'input': nir.Input(input_type={'input': np.array([1])}), - 'output': nir.Output(output_type={'output': np.array([1])}) + "output": nir.Output(output_type={"output": np.array([1])}), }, - edges=[ - ('input', '0'), ('0', '1'), ('1', 'output') - ] + edges=[("input", "0"), ("0", "1"), ("1", "output")], ) diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 4e2f0db..c04be4f 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -7,14 +7,12 @@ def _torch_convert(module: nn.Module) -> nir.NIRNode: if isinstance(module, nn.Conv1d): - return nir.Conv1d(module.weight, 1, 1, 1, 1, module.bias) + return nir.Conv1d(None, module.weight, 1, 1, 1, 1, module.bias) elif isinstance(module, nn.Linear): return nir.Affine(module.weight, module.bias) - else: - raise NotImplementedError(f"Unsupported module {module}") -def test_norse_to_sinabs(): +def test_extract_pytorch(): model = torch.nn.Sequential( torch.nn.Conv1d(1, 2, 3), torch.nn.Linear(8, 1), diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index d16bc68..b119316 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -56,7 +56,10 @@ def test_extract_empty(): def test_extract_illegal_name(): - graph = nir.NIRGraph({"a.b": nir.Linear(np.ones((1, 1)))}, []) + graph = nir.NIRGraph( + {"i": nir.Input(np.ones((1, 1))), "a.b": nir.Linear(np.ones((1, 1)))}, + [("i", "a.b")], + ) torch_graph = load(graph, _torch_model_map) assert "a_b" in torch_graph._modules @@ -68,14 +71,16 @@ def test_extract_lin(): torchlin.weight.data = torch.nn.Parameter(lin.weight) torchlin.bias.data = torch.nn.Parameter(lin.bias) y = torchlin(torchlin(x)) - g = nir.NIRGraph({"a": lin, "b": lin}, [("a", "b")]) + g = nir.NIRGraph( + {"i": nir.Input(np.ones((1, 1))), "a": lin, "b": lin}, [("i", "a"), ("a", "b")] + ) m = load(g, _torch_model_map) - assert isinstance(m.execution_order[0].elem, torch.nn.Linear) - assert torch.allclose(m.execution_order[0].elem.weight, lin.weight) - assert torch.allclose(m.execution_order[0].elem.bias, lin.bias) assert isinstance(m.execution_order[1].elem, torch.nn.Linear) assert torch.allclose(m.execution_order[1].elem.weight, lin.weight) assert torch.allclose(m.execution_order[1].elem.bias, lin.bias) + assert isinstance(m.execution_order[2].elem, torch.nn.Linear) + assert torch.allclose(m.execution_order[2].elem.weight, lin.weight) + assert torch.allclose(m.execution_order[2].elem.bias, lin.bias) assert torch.allclose(m(x)[0], y) @@ -104,11 +109,19 @@ def forward(self, x, state=None): state = 1 return x + state, state + def _map_stateful(node): + if isinstance(node, nir.Flatten): + return StatefulModel() + g = nir.NIRGraph( - nodes={"li": nir.Flatten(np.array([1])), "li2": nir.Flatten(np.array([1]))}, - edges=[("li", "li2")], + nodes={ + "i": nir.Input(np.array([1, 1])), + "li": nir.Flatten(np.array([1])), + "li2": nir.Flatten(np.array([1])), + }, + edges=[("i", "li"), ("li", "li2")], ) # Mock node - m = load(g, lambda m: StatefulModel()) + m = load(g, _map_stateful) out, state = m(torch.ones(10)) assert torch.allclose(out, torch.ones(10) * 3) assert state.state["li"] == (1,) diff --git a/tests/test_graph.py b/tests/test_graph.py index c99e1ed..b48ae3d 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -109,7 +109,7 @@ def test_module_forward_wrapper(): from nirtorch.graph import Graph, module_forward_wrapper, named_modules_map output_types = {} - model_graph = Graph(named_modules_map(mymodel)) + model_graph = Graph(named_modules_map(mymodel), ["block1"]) new_call = module_forward_wrapper(model_graph, output_types) # Override call to the new wrapped call @@ -238,7 +238,7 @@ def test_root_has_no_source(): len(graph.find_source_nodes_of(graph.find_node(my_branched_model.relu1))) == 0 ) - +@pytest.mark.skip(reason="Root tracing is broken") def test_get_root(): graph = extract_torch_graph(my_branched_model, sample_data=data, model_name=None) graph = graph.ignore_tensors() @@ -308,7 +308,7 @@ def forward(self, x, state=None): assert d.nodes.keys() == {"input", "l", "r", "output"} assert set(d.edges) == {("input", "r"), ("r", "l"), ("l", "output"), ("r", "r")} - +@pytest.mark.skip(reason="Subgraphs are currently flattened") def test_captures_recurrence_manually(): def export_affine_rec_gru(module): if isinstance(module, torch.nn.Linear): diff --git a/tests/test_graph_utils.py b/tests/test_graph_utils.py new file mode 100644 index 0000000..36ec5f3 --- /dev/null +++ b/tests/test_graph_utils.py @@ -0,0 +1,45 @@ +from collections import defaultdict + +from nirtorch.graph_utils import trace_execution + + +class StringNode: + def __init__(self, name, edges): + self.name = name + self.edges = edges + + @staticmethod + def get_children(node): + return [StringNode(x, node.edges) for x in node.edges[node.name]] + + @staticmethod + def from_string(graph): + edges = defaultdict(list) + for edge in graph.split(" "): + edges[edge[0]].append(edge[2]) + return StringNode(graph[0], edges) + + def __hash__(self) -> int: + return self.name.__hash__() + + def __eq__(self, other: object) -> bool: + return self.name == other.name + + +def test_trace_linear(): + graph = "a-b b-c c-d" + node = StringNode.from_string(graph) + seen = trace_execution(node, node.get_children) + assert "".join([x.name for x in seen]) == "abcd" + + +def test_trace_recursive(): + node = StringNode.from_string("a-b b-a") + seen = trace_execution(node, node.get_children) + assert "".join([x.name for x in seen]) == "ab" + + +def test_trace_recursive_complex(): + node = StringNode.from_string("a-b b-a b-c b-c c-d d-e") + seen = trace_execution(node, node.get_children) + assert "".join([x.name for x in seen]) == "abcde" diff --git a/tests/test_to_nir.py b/tests/test_to_nir.py index f0f0665..9685d64 100644 --- a/tests/test_to_nir.py +++ b/tests/test_to_nir.py @@ -1,18 +1,20 @@ import nir import numpy as np +import pytest import torch import torch.nn as nn from nirtorch.to_nir import extract_nir_graph +def _node_to_affine(node): + if isinstance(node, torch.nn.Linear): + return nir.Affine(node.weight.detach().numpy(), node.bias.detach().numpy()) + + def test_extract_single(): m = nn.Linear(1, 1) - g = extract_nir_graph( - m, - lambda x: nir.Affine(x.weight.detach().numpy(), x.bias.detach().numpy()), - torch.rand(1, 1), - ) + g = extract_nir_graph(m, _node_to_affine, torch.rand(1, 1)) assert set(g.edges) == {("input", "model"), ("model", "output")} assert isinstance(g.nodes["input"], nir.Input) assert np.allclose(g.nodes["input"].input_type["input"], np.array([1, 1])) @@ -61,6 +63,7 @@ def forward(self, x): return x @ self.a @ self.b +@pytest.mark.skip(reason="Re-implement with correct recursive graph parsing") def test_extract_multiple_explicit(): model = nn.Sequential(BranchedModel(1, 2, 3), nn.Linear(3, 4)) @@ -80,7 +83,7 @@ def extractor(module: nn.Module): g = extract_nir_graph(model, extractor, torch.rand(1)) print([type(n) for n in g.nodes]) - assert len(g.nodes) == 7 + assert len(g.nodes) == 4 assert len(g.edges) == 8 # in + 5 + 1 + out From fe7188af95919d0b69ea3493c21729e08ba86f31 Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Fri, 20 Oct 2023 15:28:17 +0200 Subject: [PATCH 18/27] + arg to ignore dims in to_nir --- nirtorch/to_nir.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 686bfa2..0074cf5 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Optional +from typing import Any, Callable, Optional, Sequence import nir import numpy as np @@ -13,6 +13,7 @@ def extract_nir_graph( sample_data: Any, model_name: Optional[str] = "model", ignore_submodules_of=None, + ignore_dims: Sequence[int] = [], ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. @@ -49,9 +50,10 @@ def extract_nir_graph( # Convert the nodes and get indices nir_edges = [] - nir_nodes = { - "input": nir.Input(np.array(sample_data.shape[1:])) - } # Remove the first dimension + input_shape = np.array(sample_data.shape) + input_shape = np.delete(input_shape, ignore_dims) + nir_nodes = {"input": nir.Input(input_shape)} + nir_edges = [] # Get all the NIR nodes for indx, node in enumerate(torch_graph.node_list): From a21819f12197fd1c1c91ddbc424da3785613b31f Mon Sep 17 00:00:00 2001 From: Steve Abreu Date: Fri, 20 Oct 2023 15:28:21 +0200 Subject: [PATCH 19/27] add tests --- tests/test_to_nir.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_to_nir.py b/tests/test_to_nir.py index f0f0665..26fb0a1 100644 --- a/tests/test_to_nir.py +++ b/tests/test_to_nir.py @@ -105,6 +105,38 @@ def extractor(m): } +def test_ignore_batch_dim(): + model = nn.Linear(3, 1) + + def extractor(module: nn.Module): + return nir.Affine(module.weight, module.bias) + + raw_input_shape = (1, 3) + g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0]) + exp_input_shape = (3,) + assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape)) + assert g.nodes["model"].weight.shape == (1, 3) + assert np.alltrue(g.nodes["output"].output_type["output"] == np.array([1])) + + +def test_ignore_time_and_batch_dim(): + model = nn.Linear(3, 1) + + def extractor(module: nn.Module): + return nir.Affine(module.weight, module.bias) + + raw_input_shape = (1, 10, 3) + g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, -2]) + exp_input_shape = (3,) + assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape)) + assert g.nodes["model"].weight.shape == (1, 3) + + raw_input_shape = (1, 10, 3) + g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, 1]) + exp_input_shape = (3,) + assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape)) + + # def test_extract_stateful(): # model = norse.SequentialState(norse.LIFBoxCell(), nn.Linear(3, 1)) From bef454b141249058601f1dd83bd3756c07523ff1 Mon Sep 17 00:00:00 2001 From: Sadique Sheik Date: Fri, 20 Oct 2023 16:02:41 +0200 Subject: [PATCH 20/27] output_shape also uses ignore_dims --- nirtorch/to_nir.py | 26 ++++++++++++++++++-------- tests/test_graph.py | 6 ++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 0074cf5..ac4c635 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -12,8 +12,8 @@ def extract_nir_graph( model_map: Callable[[nn.Module], nir.NIRNode], sample_data: Any, model_name: Optional[str] = "model", - ignore_submodules_of=None, - ignore_dims: Sequence[int] = [], + ignore_submodules_of: Optional[Sequence[nn.Module]] = None, + ignore_dims: Optional[Sequence[int]] = None, ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. @@ -24,7 +24,11 @@ def extract_nir_graph( sample_data (Any): Sample input data to be used for model extraction model_name (Optional[str], optional): The name of the top level module. Defaults to "model". - + ignore_submodules_of (Optional[Sequence[nn.Module]]): If specified, + the corresponding module's children will not be traversed for graph. + ignore_dims (Optional[Sequence[int]]): Dimensions of data to be ignored for + type/shape inference. Typically the dimensions that you will want to ignore + are for batch and time. Returns: nir.NIR: Returns the generated NIR graph representation. """ @@ -51,8 +55,10 @@ def extract_nir_graph( # Convert the nodes and get indices nir_edges = [] input_shape = np.array(sample_data.shape) - input_shape = np.delete(input_shape, ignore_dims) - nir_nodes = {"input": nir.Input(input_shape)} + if ignore_dims: + nir_nodes = {"input": nir.Input(np.delete(input_shape, ignore_dims))} + else: + nir_nodes = {"input": nir.Input(input_shape)} nir_edges = [] # Get all the NIR nodes @@ -89,9 +95,13 @@ def extract_nir_graph( if len(node.outgoing_nodes) == 0: out_name = "output" # Try to find shape of input to the Output node - output_node = nir.Output( - torch_graph.module_output_types[node.elem][1:] - ) # Ignore batch dimension + if ignore_dims: + out_shape = np.delete( + torch_graph.module_output_types[node.elem], ignore_dims + ) + else: + out_shape = torch_graph.module_output_types[node.elem] + output_node = nir.Output(out_shape) nir_nodes[out_name] = output_node nir_edges.append((node.name, out_name)) diff --git a/tests/test_graph.py b/tests/test_graph.py index b999721..0f66f62 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -278,9 +278,11 @@ def test_output_type_when_single_node(): def test_sequential_flatten(): d = torch.empty(2, 3, 4) - g = extract_nir_graph(torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d) + g = extract_nir_graph( + torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d, ignore_dims=[0] + ) assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) - assert tuple(g.nodes["output"].output_type["output"]) == (3 * 4, ) + assert tuple(g.nodes["output"].output_type["output"]) == (3 * 4,) @pytest.mark.skip(reason="Not supported yet") From b95ad5c478ac64b269db44e9426ef68515e749b1 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Fri, 20 Oct 2023 16:06:45 +0200 Subject: [PATCH 21/27] Added test for flatten --- tests/test_graph.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_graph.py b/tests/test_graph.py index 0f66f62..60c9189 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -277,10 +277,13 @@ def test_output_type_when_single_node(): def test_sequential_flatten(): + d = torch.empty(3, 4) + g = extract_nir_graph(torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d) + assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) + assert tuple(g.nodes["output"].output_type["output"]) == (3 * 4,) + d = torch.empty(2, 3, 4) - g = extract_nir_graph( - torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d, ignore_dims=[0] - ) + g = extract_nir_graph(torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d, ignore_dims=[0]) assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) assert tuple(g.nodes["output"].output_type["output"]) == (3 * 4,) From 3bc8bd2e839b4f204840805a6b0a7bf0810952d4 Mon Sep 17 00:00:00 2001 From: Sadique Sheik Date: Fri, 20 Oct 2023 22:25:29 +0200 Subject: [PATCH 22/27] minor correction to default value --- nirtorch/to_nir.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 8cec0fe..47e7d03 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Optional +from typing import Any, Callable, Optional, Sequence import logging import nir @@ -15,7 +15,7 @@ def extract_nir_graph( model_name: Optional[str] = "model", ignore_submodules_of=None, model_fwd_args=[], - ignore_dims=[], + ignore_dims: Optional[Sequence[int]]=None, ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. From 84e3cc85a5175afef6f474a84c9f8fb51f82fa24 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Sat, 21 Oct 2023 22:24:50 +0200 Subject: [PATCH 23/27] Added ability to ignore state in executor --- nirtorch/from_nir.py | 33 +++++++++++++++++++++++++-------- tests/test_bidirectional.py | 6 ++++-- tests/test_from_nir.py | 7 ++++++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 4ad5cf6..d2324c0 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -22,10 +22,25 @@ class GraphExecutorState: class GraphExecutor(nn.Module): - def __init__(self, graph: Graph) -> None: + """Executes the NIR graph in PyTorch. + + By default the graph executor is stateful, since there may be recurrence or stateful modules in the graph. + Specifically, that means accepting and returning a state object (`GraphExecutorState`). + If that is not desired, set `return_state=False` in the constructor. + + Arguments: + graph (Graph): The graph to execute + return_state (bool, optional): Whether to return the state object. Defaults to True. + + Raises: + ValueError: If there are no edges in the graph + """ + + def __init__(self, graph: Graph, return_state: bool = True) -> None: super().__init__() self.graph = graph - self.stateful_modules = {} + self.stateful_modules = set() + self.return_state = return_state self.instantiate_modules() self.execution_order = self.get_execution_order() if len(self.execution_order) == 0: @@ -43,7 +58,7 @@ def _is_module_stateful(self, module: torch.nn.Module) -> bool: "RLeaky", ]: return not module.init_hidden - return arguments > 1 + return "state" in signature.parameters and arguments > 1 def get_execution_order(self) -> List[Node]: """Evaluate the execution order and instantiate that as a list.""" @@ -59,9 +74,8 @@ def instantiate_modules(self): for mod, name in self.graph.module_names.items(): if mod is not None: self.add_module(sanitize_name(name), mod) - self.stateful_modules[sanitize_name(name)] = self._is_module_stateful( - mod - ) + if self._is_module_stateful(mod): + self.stateful_modules.add(sanitize_name(name)) def get_input_nodes(self) -> List[Node]: # NOTE: This is a hack. Should use the input nodes from NIR graph @@ -111,7 +125,7 @@ def _apply_module( assert "lif" in node.name, "this shouldnt happen.." new_state.state[node.name] = out # snnTorch requires output inside state out = out[0] - elif self.stateful_modules[node.name]: + elif node.name in self.stateful_modules: new_state.state[node.name] = out[1:] # Store the new state out = out[0] return out, new_state @@ -141,7 +155,10 @@ def forward( # If the output node is a dummy nir.Output node, use the second-to-last node if node.name not in new_state.cache: node = self.execution_order[-2] - return new_state.cache[node.name], new_state + if self.return_state: + return new_state.cache[node.name], new_state + else: + return new_state.cache[node.name] def _mod_nir_to_graph( diff --git a/tests/test_bidirectional.py b/tests/test_bidirectional.py index 5345935..42e3df6 100644 --- a/tests/test_bidirectional.py +++ b/tests/test_bidirectional.py @@ -83,8 +83,10 @@ def test_nir_to_torch_to_nir(from_file=True): module = nirtorch.load(graph, _nir_to_torch_module) assert module is not None graph2 = nirtorch.extract_nir_graph(module, _torch_to_nir, torch.zeros(1, 1)) - assert sorted(graph.edges) == sorted(graph2.edges) - assert graph2 is not None + edges1 = sorted(graph.edges) + edges2 = sorted(graph2.edges) + for e1, e2 in zip(edges1, edges2): + assert e1 == e2 # if __name__ == '__main__': diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index 121d352..5e5942a 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -123,11 +123,16 @@ def _map_stateful(node): edges=[("i", "li"), ("li", "li2")], ) # Mock node m = load(g, _map_stateful) - out, state = m(torch.ones(10)) + out = m(torch.ones(10)) + assert isinstance(out, tuple) + out, state = out assert torch.allclose(out, torch.ones(10) * 3) assert state.state["li"] == (1,) assert state.state["li"] == (1,) + # Test that the model can return zero state + m.return_state = False + assert not isinstance(m(torch.ones(10)), tuple) def test_execute_recurrent(): w = np.ones((1, 1)) From 8278437ae6b80fd3e017df2dca9b46d4565388b0 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Sat, 21 Oct 2023 22:29:58 +0200 Subject: [PATCH 24/27] Added flag in nirtorch parsing --- nirtorch/from_nir.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index d2324c0..419a264 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -196,15 +196,31 @@ def _switch_models_with_map( def load( - nir_graph: Union[nir.NIRNode, str], model_map: Callable[[nir.NIRNode], nn.Module] + nir_graph: Union[nir.NIRNode, str], + model_map: Callable[[nir.NIRNode], nn.Module], + return_state: bool = True, ) -> nn.Module: """Load a NIR graph and convert it to a torch module using the given model map. + Because the graph can contain recurrence and stateful modules, the execution accepts + a secondary state argument and returns a tuple of [output, state], instead of just the output as follows + + >>> executor = nirtorch.load(nir_graph, model_map) + >>> old_state = None + >>> output, state = executor(input, old_state) # Notice the second argument and output + >>> output, state = executor(input, state) # This can go on for many (time)steps + + If you do not wish to operate with state, set `return_state=False`. + Args: nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string representing the path to the NIR object. model_map (Callable[[nn.NIRNode], nn.Module]): A method that returns the a torch module that corresponds to each NIR node. + return_state (bool): If True, the execution of the loaded graph will return a tuple + of [output, state], where state is a GraphExecutorState object. If False, only + the NIR graph output will be returned. Note that state is required for recurrence + to work in the graphs. Returns: nn.Module: The generated torch module @@ -216,4 +232,4 @@ def load( # Build a nirtorch.Graph based on the nir_graph graph = _mod_nir_to_graph(nir_module_graph, nir_nodes=nir_graph.nodes) # Build and return a graph executor module - return GraphExecutor(graph) + return GraphExecutor(graph, return_state=return_state) From ec8cded0027f991796eb2f8d23f79107603a3902 Mon Sep 17 00:00:00 2001 From: "Jens E. Pedersen" Date: Sat, 21 Oct 2023 22:53:40 +0200 Subject: [PATCH 25/27] Added flag in nirtorch parsing --- tests/test_from_nir.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index 5e5942a..5269eb8 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -130,8 +130,8 @@ def _map_stateful(node): assert state.state["li"] == (1,) assert state.state["li"] == (1,) - # Test that the model can return zero state - m.return_state = False + # Test that the model can avoid returning state + m = load(g, _map_stateful, return_state=False) assert not isinstance(m(torch.ones(10)), tuple) def test_execute_recurrent(): From 0325c80a2b591e92ce911376a91c5b189005a70f Mon Sep 17 00:00:00 2001 From: Sadique Sheik Date: Tue, 5 Dec 2023 15:25:22 +0100 Subject: [PATCH 26/27] minor changes to the doc strings --- nirtorch/from_nir.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index 419a264..c8ec7d9 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -24,13 +24,15 @@ class GraphExecutorState: class GraphExecutor(nn.Module): """Executes the NIR graph in PyTorch. - By default the graph executor is stateful, since there may be recurrence or stateful modules in the graph. - Specifically, that means accepting and returning a state object (`GraphExecutorState`). - If that is not desired, set `return_state=False` in the constructor. + By default the graph executor is stateful, since there may be recurrence or + stateful modules in the graph. Specifically, that means accepting and returning a + state object (`GraphExecutorState`). If that is not desired, + set `return_state=False` in the constructor. Arguments: graph (Graph): The graph to execute - return_state (bool, optional): Whether to return the state object. Defaults to True. + return_state (bool, optional): Whether to return the state object. + Defaults to True. Raises: ValueError: If there are no edges in the graph @@ -169,9 +171,9 @@ def _mod_nir_to_graph( graph = Graph(module_names=module_names, inputs=inputs) for src, dst in torch_graph.edges: # Allow edges to refer to subgraph inputs and outputs - if not src in torch_graph.nodes and f"{src}.output" in torch_graph.nodes: + if src not in torch_graph.nodes and f"{src}.output" in torch_graph.nodes: src = f"{src}.output" - if not dst in torch_graph.nodes and f"{dst}.input" in torch_graph.nodes: + if dst not in torch_graph.nodes and f"{dst}.input" in torch_graph.nodes: dst = f"{dst}.input" graph.add_edge(torch_graph.nodes[src], torch_graph.nodes[dst]) return graph @@ -203,24 +205,25 @@ def load( """Load a NIR graph and convert it to a torch module using the given model map. Because the graph can contain recurrence and stateful modules, the execution accepts - a secondary state argument and returns a tuple of [output, state], instead of just the output as follows + a secondary state argument and returns a tuple of [output, state], instead of just + the output as follows >>> executor = nirtorch.load(nir_graph, model_map) >>> old_state = None - >>> output, state = executor(input, old_state) # Notice the second argument and output + >>> output, state = executor(input, old_state) # Notice second argument and output >>> output, state = executor(input, state) # This can go on for many (time)steps If you do not wish to operate with state, set `return_state=False`. Args: - nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string representing - the path to the NIR object. + nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string + representing the path to the NIR object. model_map (Callable[[nn.NIRNode], nn.Module]): A method that returns the a torch module that corresponds to each NIR node. - return_state (bool): If True, the execution of the loaded graph will return a tuple - of [output, state], where state is a GraphExecutorState object. If False, only - the NIR graph output will be returned. Note that state is required for recurrence - to work in the graphs. + return_state (bool): If True, the execution of the loaded graph will return a + tuple of [output, state], where state is a GraphExecutorState object. + If False, only the NIR graph output will be returned. Note that state is + required for recurrence to work in the graphs. Returns: nn.Module: The generated torch module From 53109c328a564a9dbf9b7deb538edf8ca4e00cff Mon Sep 17 00:00:00 2001 From: Sadique Sheik Date: Tue, 5 Dec 2023 17:05:24 +0100 Subject: [PATCH 27/27] formatting fixes --- nirtorch/__init__.py | 2 +- nirtorch/from_nir.py | 25 +++++++++++++------------ nirtorch/graph_utils.py | 8 ++++---- nirtorch/to_nir.py | 9 +++++---- tests/test_bidirectional.py | 2 +- tests/test_conversion.py | 2 +- tests/test_from_nir.py | 8 ++++++-- tests/test_graph.py | 8 ++++++-- tests/test_graph_utils.py | 2 +- tests/test_to_nir.py | 12 +++++++++--- 10 files changed, 47 insertions(+), 31 deletions(-) diff --git a/nirtorch/__init__.py b/nirtorch/__init__.py index e4ae538..87eff9d 100644 --- a/nirtorch/__init__.py +++ b/nirtorch/__init__.py @@ -1,5 +1,5 @@ -from .graph import extract_torch_graph # noqa F401 from .from_nir import load # noqa F401 +from .graph import extract_torch_graph # noqa F401 from .to_nir import extract_nir_graph # noqa F401 __version__ = version = "0.2.1" diff --git a/nirtorch/from_nir.py b/nirtorch/from_nir.py index c8ec7d9..77a5fb6 100644 --- a/nirtorch/from_nir.py +++ b/nirtorch/from_nir.py @@ -1,6 +1,6 @@ import dataclasses import inspect -from typing import Callable, Dict, List, Optional, Any, Union +from typing import Any, Callable, Dict, List, Optional, Union import nir import torch @@ -13,8 +13,8 @@ @dataclasses.dataclass class GraphExecutorState: - """State for the GraphExecutor that keeps track of both the state of hidden - units and caches the output of previous modules, for use in (future) recurrent + """State for the GraphExecutor that keeps track of both the state of hidden units + and caches the output of previous modules, for use in (future) recurrent computations.""" state: Dict[str, Any] = dataclasses.field(default_factory=dict) @@ -24,14 +24,14 @@ class GraphExecutorState: class GraphExecutor(nn.Module): """Executes the NIR graph in PyTorch. - By default the graph executor is stateful, since there may be recurrence or - stateful modules in the graph. Specifically, that means accepting and returning a - state object (`GraphExecutorState`). If that is not desired, + By default the graph executor is stateful, since there may be recurrence or + stateful modules in the graph. Specifically, that means accepting and returning a + state object (`GraphExecutorState`). If that is not desired, set `return_state=False` in the constructor. Arguments: graph (Graph): The graph to execute - return_state (bool, optional): Whether to return the state object. + return_state (bool, optional): Whether to return the state object. Defaults to True. Raises: @@ -92,6 +92,7 @@ def _apply_module( data: Optional[torch.Tensor] = None, ): """Applies a module and keeps track of its state. + TODO: Use pytree to recursively construct the state """ inputs = [] @@ -205,7 +206,7 @@ def load( """Load a NIR graph and convert it to a torch module using the given model map. Because the graph can contain recurrence and stateful modules, the execution accepts - a secondary state argument and returns a tuple of [output, state], instead of just + a secondary state argument and returns a tuple of [output, state], instead of just the output as follows >>> executor = nirtorch.load(nir_graph, model_map) @@ -216,13 +217,13 @@ def load( If you do not wish to operate with state, set `return_state=False`. Args: - nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string + nir_graph (Union[nir.NIRNode, str]): The NIR object to load, or a string representing the path to the NIR object. model_map (Callable[[nn.NIRNode], nn.Module]): A method that returns the a torch module that corresponds to each NIR node. - return_state (bool): If True, the execution of the loaded graph will return a - tuple of [output, state], where state is a GraphExecutorState object. - If False, only the NIR graph output will be returned. Note that state is + return_state (bool): If True, the execution of the loaded graph will return a + tuple of [output, state], where state is a GraphExecutorState object. + If False, only the NIR graph output will be returned. Note that state is required for recurrence to work in the graphs. Returns: diff --git a/nirtorch/graph_utils.py b/nirtorch/graph_utils.py index 1f67d71..d807d49 100644 --- a/nirtorch/graph_utils.py +++ b/nirtorch/graph_utils.py @@ -65,12 +65,12 @@ def find_all_ancestors( # return execution_order # + def trace_execution( node: T, edge_fn: Callable[[T], List[T]], visited: Set[T] = None ) -> List[T]: - """Traces the execution of a node by listing them in order, coloring recursive - nodes to avoid adding the same node twice. - """ + """Traces the execution of a node by listing them in order, coloring recursive nodes + to avoid adding the same node twice.""" if visited is None: visited = set() @@ -83,4 +83,4 @@ def trace_execution( for child in edge_fn(node): if child not in visited: successors += trace_execution(child, edge_fn, visited) - return [node] + successors \ No newline at end of file + return [node] + successors diff --git a/nirtorch/to_nir.py b/nirtorch/to_nir.py index 47e7d03..a819c21 100644 --- a/nirtorch/to_nir.py +++ b/nirtorch/to_nir.py @@ -1,5 +1,5 @@ -from typing import Any, Callable, Optional, Sequence import logging +from typing import Any, Callable, Optional, Sequence import nir import numpy as np @@ -15,13 +15,14 @@ def extract_nir_graph( model_name: Optional[str] = "model", ignore_submodules_of=None, model_fwd_args=[], - ignore_dims: Optional[Sequence[int]]=None, + ignore_dims: Optional[Sequence[int]] = None, ) -> nir.NIRNode: """Given a `model`, generate an NIR representation using the specified `model_map`. Assumptions and known issues: - - Cannot deal with layers like torch.nn.Identity(), since the input tensor and output - tensor will be the same object, and therefore lead to cyclic connections. + - Cannot deal with layers like torch.nn.Identity(), since the input tensor and + output tensor will be the same object, and therefore lead to cyclic + connections. Args: model (nn.Module): The model of interest diff --git a/tests/test_bidirectional.py b/tests/test_bidirectional.py index 42e3df6..52f8f6d 100644 --- a/tests/test_bidirectional.py +++ b/tests/test_bidirectional.py @@ -1,8 +1,8 @@ import nir import numpy as np import torch -import nirtorch +import nirtorch use_snntorch = False # use_snntorch = True diff --git a/tests/test_conversion.py b/tests/test_conversion.py index c04be4f..ee48b5d 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -1,7 +1,7 @@ +import nir import torch import torch.nn as nn -import nir import nirtorch diff --git a/tests/test_from_nir.py b/tests/test_from_nir.py index d2680b9..b2907f4 100644 --- a/tests/test_from_nir.py +++ b/tests/test_from_nir.py @@ -1,7 +1,7 @@ import nir import numpy as np -import torch import pytest +import torch from nirtorch.from_nir import load @@ -56,7 +56,10 @@ def test_extract_empty(): def test_extract_illegal_name(): - graph = nir.NIRGraph({"a.b": nir.Input(np.ones(1)), "a.c": nir.Linear(np.array([[1.]]))}, [("a.b", "a.c")]) + graph = nir.NIRGraph( + {"a.b": nir.Input(np.ones(1)), "a.c": nir.Linear(np.array([[1.0]]))}, + [("a.b", "a.c")], + ) torch_graph = load(graph, _torch_model_map) assert "a_c" in torch_graph._modules @@ -131,6 +134,7 @@ def _map_stateful(node): m = load(g, _map_stateful, return_state=False) assert not isinstance(m(torch.ones(10)), tuple) + def test_execute_recurrent(): w = np.ones((1, 1)) g = nir.NIRGraph( diff --git a/tests/test_graph.py b/tests/test_graph.py index 20d17f5..6e10625 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -1,10 +1,10 @@ +import nir import pytest import torch import torch.nn as nn from norse.torch import LIBoxCell, LIFCell, SequentialState from sinabs.layers import Merge -import nir from nirtorch import extract_nir_graph, extract_torch_graph @@ -238,6 +238,7 @@ def test_root_has_no_source(): len(graph.find_source_nodes_of(graph.find_node(my_branched_model.relu1))) == 0 ) + @pytest.mark.skip(reason="Root tracing is broken") def test_get_root(): graph = extract_torch_graph(my_branched_model, sample_data=data, model_name=None) @@ -282,7 +283,9 @@ def test_sequential_flatten(): assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) d = torch.empty(2, 3, 4) - g = extract_nir_graph(torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d, ignore_dims=[0]) + g = extract_nir_graph( + torch.nn.Flatten(1), lambda x: nir.Flatten(d.shape, 1), d, ignore_dims=[0] + ) assert tuple(g.nodes["input"].input_type["input"]) == (3, 4) @@ -311,6 +314,7 @@ def forward(self, x, state=None): assert d.nodes.keys() == {"input", "l", "r", "output"} assert set(d.edges) == {("input", "r"), ("r", "l"), ("l", "output"), ("r", "r")} + @pytest.mark.skip(reason="Subgraphs are currently flattened") def test_captures_recurrence_manually(): def export_affine_rec_gru(module): diff --git a/tests/test_graph_utils.py b/tests/test_graph_utils.py index 36ec5f3..95b51b9 100644 --- a/tests/test_graph_utils.py +++ b/tests/test_graph_utils.py @@ -21,7 +21,7 @@ def from_string(graph): def __hash__(self) -> int: return self.name.__hash__() - + def __eq__(self, other: object) -> bool: return self.name == other.name diff --git a/tests/test_to_nir.py b/tests/test_to_nir.py index 3ea108d..d35d217 100644 --- a/tests/test_to_nir.py +++ b/tests/test_to_nir.py @@ -115,7 +115,9 @@ def extractor(module: nn.Module): return nir.Affine(module.weight, module.bias) raw_input_shape = (1, 3) - g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0]) + g = extract_nir_graph( + model, extractor, torch.ones(raw_input_shape), ignore_dims=[0] + ) exp_input_shape = (3,) assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape)) assert g.nodes["model"].weight.shape == (1, 3) @@ -129,13 +131,17 @@ def extractor(module: nn.Module): return nir.Affine(module.weight, module.bias) raw_input_shape = (1, 10, 3) - g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, -2]) + g = extract_nir_graph( + model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, -2] + ) exp_input_shape = (3,) assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape)) assert g.nodes["model"].weight.shape == (1, 3) raw_input_shape = (1, 10, 3) - g = extract_nir_graph(model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, 1]) + g = extract_nir_graph( + model, extractor, torch.ones(raw_input_shape), ignore_dims=[0, 1] + ) exp_input_shape = (3,) assert np.alltrue(g.nodes["input"].input_type["input"] == np.array(exp_input_shape))