Skip to content

Conversation

@yashwantbezawada
Copy link
Contributor

Summary

Fixes an UnboundLocalError that occurs in the RT-DETR loss computation when config.auxiliary_loss is False.

Problem

The variable auxiliary_outputs is conditionally assigned inside an if config.auxiliary_loss: block, but it's referenced later in the function regardless of whether the condition was true. When auxiliary_loss is disabled, this causes:

UnboundLocalError: local variable 'auxiliary_outputs' referenced before assignment

Solution

Initialize auxiliary_outputs = None before the conditional block to ensure it's always defined before use.

Changes

  • Added auxiliary_outputs = None initialization in src/transformers/loss/loss_rt_detr.py:450

Testing

This fix ensures the function works correctly whether config.auxiliary_loss is True or False.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, fix LGTM! Not sure how often this path is taken, but worth fixing regardless.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Rocketknight1 Rocketknight1 force-pushed the fix-rtdetr-auxiliary-loss-clean branch 2 times, most recently from cd8d1f5 to 1426ea5 Compare November 17, 2025 16:15
yashwantbezawada and others added 2 commits November 17, 2025 16:37
Initialize auxiliary_outputs to None before conditional use to prevent
UnboundLocalError when config.auxiliary_loss is False.

Fixes the error:
  UnboundLocalError: local variable 'auxiliary_outputs' referenced before assignment

This occurs when auxiliary_loss is disabled but the variable is still
referenced later in the function.
@Rocketknight1 Rocketknight1 force-pushed the fix-rtdetr-auxiliary-loss-clean branch from 1426ea5 to c208f1d Compare November 17, 2025 16:37
@Rocketknight1 Rocketknight1 merged commit 266d3b0 into huggingface:main Nov 17, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants