Skip to content

Fix DefaultBoxGenerator CUDA device mismatch in _grid_default_boxes (#9414)#9514

Open
Nueramarcos wants to merge 2 commits into
pytorch:mainfrom
Nueramarcos:fix/default-box-generator-cuda-device
Open

Fix DefaultBoxGenerator CUDA device mismatch in _grid_default_boxes (#9414)#9514
Nueramarcos wants to merge 2 commits into
pytorch:mainfrom
Nueramarcos:fix/default-box-generator-cuda-device

Conversation

@Nueramarcos

Copy link
Copy Markdown

Summary

Fixes #9414

DefaultBoxGenerator._grid_default_boxes created shifts_x/shifts_y via torch.arange with no device= argument, and accessed self._wh_pairs[k] (always CPU from __init__), causing a CPU vs CUDA mismatch at torch.cat when the model runs on GPU.

Changes

  • Added device parameter to _grid_default_boxes (defaults to cpu for backwards compatibility)
  • Pass device=device to torch.arange for the shift tensors
  • Call .to(dtype=dtype, device=device) on _wh_pairs[k] before use
  • Updated forward() to pass device through and removed the now-redundant default_boxes.to(device) call

Testing

Added test/test_default_box_generator.py covering:

  • CPU: output shape, device, dtype, no NaNs, clip=True/False, batch size > 1, TorchScript traceability
  • CUDA (skipped when unavailable): core regression test confirming torch.cat no longer raises, CPU→CUDA value equality, batch, clip=False, TorchScript on CUDA, _wh_pairs device verification

Verified CPU path produces 8732 default boxes for SSD-300 with no NaNs.

Covers CPU shape/device/dtype/NaN/clip/TorchScript and CUDA regression
tests including the core torch.cat device-mismatch fix, CPU==CUDA value
equality, batch, and _wh_pairs device verification.
Thread the feature-map device into _grid_default_boxes so that
shifts_x/shifts_y and _wh_pairs[k] are all created/moved to the
same device before torch.cat. Removes the now-redundant .to(device)
in forward().

Fixes pytorch#9414
@pytorch-bot

pytorch-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9514

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Jun 14, 2026

Copy link
Copy Markdown

Hi @Nueramarcos!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

DefaultBoxGenerator device mismatch: torch.cat fails when model is on CUDA (CPU vs cuda:0) Body:

1 participant