Skip to content

Commit

Permalink
Returned state if stateful module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jegp committed Oct 10, 2023
1 parent a0a2f47 commit 1465f29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion nirtorch/from_nir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_from_nir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, )

0 comments on commit 1465f29

Please sign in to comment.