Skip to content

Commit f2ae8da

Browse files
kaixuanliuguarin
andauthored
Fix UnboundLocalError in SuperPoint forward under torch.compile (#49409)
* fix bug for superpoint under compile mode Signed-off-by: kaixuanliu <kaixuan.liu@intel.com> * Update tests/models/superpoint/test_modeling_superpoint.py Co-authored-by: guarin <43336610+guarin@users.noreply.github.com> * update Signed-off-by: kaixuanliu <kaixuan.liu@intel.com> * update Signed-off-by: kaixuanliu <kaixuan.liu@intel.com> --------- Signed-off-by: kaixuanliu <kaixuan.liu@intel.com> Co-authored-by: guarin <43336610+guarin@users.noreply.github.com>
1 parent 307a9dd commit f2ae8da

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎src/transformers/models/superpoint/modeling_superpoint.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ def forward(
421421
last_hidden_state = encoder_outputs[0]
422422

423423
list_keypoints_scores = [
424-
self.keypoint_decoder(last_hidden_state[None, ...]) for last_hidden_state in last_hidden_state
424+
self.keypoint_decoder(image_hidden_state[None, ...]) for image_hidden_state in last_hidden_state
425425
]
426426

427427
list_keypoints = [keypoints_scores[0] for keypoints_scores in list_keypoints_scores]
428428
list_scores = [keypoints_scores[1] for keypoints_scores in list_keypoints_scores]
429429

430430
list_descriptors = [
431-
self.descriptor_decoder(last_hidden_state[None, ...], keypoints[None, ...])
432-
for last_hidden_state, keypoints in zip(last_hidden_state, list_keypoints)
431+
self.descriptor_decoder(image_hidden_state[None, ...], keypoints[None, ...])
432+
for image_hidden_state, keypoints in zip(last_hidden_state, list_keypoints)
433433
]
434434

435435
maximum_num_keypoints = max(keypoints.shape[0] for keypoints in list_keypoints)

‎tests/models/superpoint/test_modeling_superpoint.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515
import unittest
1616
from functools import cached_property
1717

18+
import pytest
19+
1820
from transformers.models.superpoint.configuration_superpoint import SuperPointConfig
19-
from transformers.testing_utils import is_flaky, require_torch, require_vision, slow, torch_device
21+
from transformers.testing_utils import (
22+
is_flaky,
23+
require_torch,
24+
require_torch_accelerator,
25+
require_vision,
26+
slow,
27+
torch_device,
28+
)
2029
from transformers.utils import is_torch_available, is_vision_available
2130

2231
from ...test_configuration_common import ConfigTester
@@ -239,6 +248,28 @@ def test_forward_labels_should_be_none(self):
239248
model(**model_inputs)
240249
self.assertEqual(ValueError, cm.exception.__class__)
241250

251+
@slow
252+
@require_torch_accelerator
253+
@pytest.mark.torch_compile_test
254+
def test_compile_forward(self):
255+
# Regression test for https://github.com/huggingface/transformers/issues/49561: the list comprehensions
256+
# in the forward pass used to shadow `last_hidden_state`, which raised an `UnboundLocalError` in the
257+
# resume function generated after a dynamo graph break.
258+
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
259+
for model_class in self.all_model_classes:
260+
model = model_class(config)
261+
model.to(torch_device)
262+
model.eval()
263+
264+
torch._dynamo.reset()
265+
model.forward = torch.compile(model.forward)
266+
267+
with torch.no_grad():
268+
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
269+
270+
self.assertIsNotNone(outputs.keypoints)
271+
self.assertIsNotNone(outputs.descriptors)
272+
242273

243274
def prepare_imgs():
244275
image1 = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")

0 commit comments

Comments
 (0)