pytorch / pytorch/vision

RegionProposalNetwork can't be AOTInductor compiled with dynamic batch size

Open
#8,285 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🐛 Describe the bug

this is a cross post of https://github.com/pytorch/pytorch/issues/121036

Just raising it here to notify the maintainers that I'm going to take a crack at fixing the RegionProposalNetwork and potentially other modules to be either traceable, AOTInductor compileable, or both. Are there any current efforts in this direction I should be aware of?

For AOTInductor I think this will at least involve changing the AnchorGenerator, which has a method that mutates an anchor attribute to instead return anchor values.

To support tracing, my plan is to address each TracerWarning (see below). First I'll be looking to remove the iteration over tensors in ImageList that prevent the model from generalziing after tracing.

[/opt/workspace/./satlas-src/satlas/model/model.py:438](http://127.0.0.1:8888/satlas-src/satlas/model/model.py#line=437): TracerWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
  image_sizes = [(image.shape[1], image.shape[2]) for image in images]
[/opt/conda/lib/python3.10/site-packages/torchvision/ops/boxes.py:166](http://127.0.0.1:8888/opt/conda/lib/python3.10/site-packages/torchvision/ops/boxes.py#line=165): UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  boxes_x = torch.min(boxes_x, torch.tensor(width, dtype=boxes.dtype, device=boxes.device))
[/opt/conda/lib/python3.10/site-packages/torchvision/ops/boxes.py:168](http://127.0.0.1:8888/opt/conda/lib/python3.10/site-packages/torchvision/ops/boxes.py#line=167): UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  boxes_y = torch.min(boxes_y, torch.tensor(height, dtype=boxes.dtype, device=boxes.device))
[/opt/conda/lib/python3.10/site-packages/torch/__init__.py:1560](http://127.0.0.1:8888/opt/conda/lib/python3.10/site-packages/torch/__init__.py#line=1559): TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  assert condition, message
[/opt/workspace/./satlas-src/satlas/model/model.py:537](http://127.0.0.1:8888/satlas-src/satlas/model/model.py#line=536): TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
  losses = {'base': torch.tensor(0, device=device, dtype=torch.float32)}
[/opt/workspace/./satlas-src/satlas/model/model.py:850](http://127.0.0.1:8888/satlas-src/satlas/model/model.py#line=849): TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
  losses = torch.tensor(0, device=batch_tensor.device, dtype=torch.float32)
Versions

I'm using the nightlies, see https://github.com/pytorch/pytorch/issues/121036

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the dynamic-batch AOTInductor issue and reviewing torchvision/models/detection/anchor_utils.py and image_list.py, along with the cited torchvision/ops/boxes.py warnings. Trace the listed TracerWarnings and determine the affected RegionProposalNetwork paths; done means the relevant detection modules support the intended tracing or AOTInductor cases without incorrect shape generalization.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision, machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.