Skip to content

Commit 170ccf4

Browse files
kyunggeunleeGitHub Enterprise
authored andcommitted
Work around torch.jit.trace bug upon Gather-ScatterElements sequence
## Problems Gather was added to SKIP_LIST_FOR_SUBGRAPH_TRACE in 7563948, but it turned out that it only creates a new problem without solving any existing problems. Two symptoms have been identified 1. Putting `Gather` in `SKIP_LIST_FOR_SUBGRAPH_TRACE` doesn't help torch.jit.trace capture the input order correctly. 2. For some reason, putting `Gather` in `SKIP_LIST_FOR_SUBGRAPH_TRACE` causes another type of mis-tracing upon Gather-ScatterElements sequence. It thinks ScatterElements and Gather takes the same input, when in fact ScatterElements takes Gather's output as input. ## Main Changes * Partially reverted 7563948 -- removed Gather from `SKIP_LIST_FOR_SUBGRAPH_TRACE`. This fixes problem 2 * Added explicit unit tests for both problem 1 and 2 --------- Signed-off-by: Kyunggeun Lee <kyunggeu@qti.qualcomm.com>
1 parent d1b9b0c commit 170ccf4

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

TrainingExtensions/torch/src/python/aimet_torch/meta/connectedgraph.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
aimet_modules.DepthToSpaceCRDMode,
109109
aimet_modules.Addmm,
110110
aimet_modules.Baddbmm,
111-
aimet_modules.Gather,
112111
aimet_modules.GridSample,
113112
]
114113

TrainingExtensions/torch/test/python/test_connectedgraph.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,11 @@ def forward(self, x, y):
14281428
(aimet_modules.Equal, (torch.randn(3),), torch.randn(3)),
14291429
(aimet_modules.FloorDivide, (torch.randn(3),), torch.randn(3)),
14301430
(aimet_modules.Fmod, (torch.randn(3),), torch.randn(3)),
1431+
(
1432+
aimet_modules.Gather,
1433+
(torch.randn(3, 3), torch.tensor(1, dtype=torch.int64)),
1434+
torch.tensor([[0, 0], [1, 1]]),
1435+
),
14311436
(aimet_modules.Greater, (torch.randn(3),), torch.randn(3)),
14321437
(aimet_modules.GreaterEqual, (torch.randn(3),), torch.randn(3)),
14331438
(aimet_modules.GridSample, (torch.randn(1, 1, 2, 2),), torch.randn(1, 2, 2, 2)),
@@ -1454,6 +1459,9 @@ def forward(self, x, y):
14541459
],
14551460
)
14561461
def test_nary_operator_input_ordering(layer, inputs, buffer):
1462+
if issubclass(layer, aimet_modules.Gather):
1463+
pytest.xfail(reason="Known failure. FIX ASAP")
1464+
14571465
class Model(torch.nn.Module):
14581466
def __init__(self, layer):
14591467
super(Model, self).__init__()
@@ -1468,3 +1476,23 @@ def forward(self, *x):
14681476
assert sim.connected_graph.ordered_ops[0].inputs[-1].is_const
14691477
for inp in sim.connected_graph.ordered_ops[0].inputs[:-1]:
14701478
assert inp.is_model_input
1479+
1480+
1481+
def test_gather_scatter_element_sequence():
1482+
class GatherScatter(torch.nn.Module):
1483+
def __init__(self):
1484+
super().__init__()
1485+
self.gather = custom.Gather()
1486+
self.scatter_elements = custom.ScatterElements(dim=3)
1487+
1488+
def forward(self, input, indices):
1489+
gather_out = self.gather(input, 3, indices)
1490+
return self.scatter_elements(input, indices, gather_out)
1491+
1492+
input = torch.randn(1, 1, 3, 128)
1493+
indices = torch.zeros(1, 1, 3, 16, dtype=torch.int64)
1494+
model = GatherScatter()
1495+
sim = aimet_torch.QuantizationSimModel(model, (input, indices))
1496+
1497+
gather, scatter_elements = sim.connected_graph.ordered_ops
1498+
assert scatter_elements.inputs[-1] is gather.outputs[0]

0 commit comments

Comments
 (0)