Skip to content

Commit

Permalink
Fix non-deterministic dynapcnn-network test
Browse files Browse the repository at this point in the history
  • Loading branch information
bauerfe committed Nov 13, 2024
1 parent 1954b61 commit 9a194ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
40 changes: 36 additions & 4 deletions tests/test_dynapcnnnetwork/model_dummy_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ def forward(self, x):

snn = SNN(batch_size)

# TODO: This test sometimes fails because the layer that has ID 1
# sometimes gets ID 2, and the layer with ID 3 gets ID 4
# This is not a bug in sinabs itself but an issue with the test, becuase
# the IDs that the layers are assigned do not always have to be the same.
expected_output = {
"dcnnl_edges": {
(0, 1),
Expand Down Expand Up @@ -152,3 +148,39 @@ def forward(self, x):
"output_shape": torch.Size([2, 10, 1, 1]),
"entry_points": {0},
}

# Sometimes the layer that usually gets assgined ID1, gets ID2, and the
# layer with ID 3 gets ID 4. Therefore an alternative solution is defined.
# This is not a bug in sinabs itself but an issue with the test, becuase
# the IDs that the layers are assigned do not always have to be the same.
expected_output["alternative"] = {
"dcnnl_edges": {
(0, 1),
(0, 2),
(1, 3),
(1, 4),
(2, 4),
(3, 5),
(4, 5),
("input", 0),
},
"node_source_map": {
0: {"input"},
1: {0},
2: {0},
3: {1},
4: {1, 2},
5: {3, 4},
},
"destination_map": {
0: {1, 2},
1: {3, 4},
2: {4},
3: {5},
4: {5},
5: {-1},
},
"sorted_nodes": [0, 1, 2, 3, 4, 5],
"output_shape": torch.Size([2, 10, 1, 1]),
"entry_points": {0},
}
10 changes: 10 additions & 0 deletions tests/test_dynapcnnnetwork/test_dynapcnnnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def test_DynapcnnNetwork(snn, input_shape, batch_size, expected_output):
output = dcnnnet(x)

module = dcnnnet.dynapcnn_module
# For some models there are multiple possible topological sortings,
# such that the assigned node IDs are not always the same.
# To prevent the following tests from failing, alternative expected
# outputs are defined which correspond to different assigned IDs.
if (
expected_output["dcnnl_edges"] != module._dynapcnnlayer_edges
and "alternative" in expected_output
):
expected_output = expected_output["alternative"]
print("Using algernative node ID assignment")
assert (
expected_output["dcnnl_edges"] == module._dynapcnnlayer_edges
), "wrong list of edges describing DynapcnnLayer connectivity."
Expand Down

0 comments on commit 9a194ad

Please sign in to comment.