Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect(inplace=True) multithreaded detection RuntimeError #8565

Closed
1 task done
mynameischaos opened this issue Jul 13, 2022 · 10 comments · Fixed by #8801
Closed
1 task done

Detect(inplace=True) multithreaded detection RuntimeError #8565

mynameischaos opened this issue Jul 13, 2022 · 10 comments · Fixed by #8801
Labels
question Further information is requested

Comments

@mynameischaos
Copy link

mynameischaos commented Jul 13, 2022

Search before asking

Question

torch version: 1.9.1

I use this scripts load model:

self.model = torch.hub.load('ultralytics/yolov5', 'custom', path=self.model_path)

then, run model(image) with single thread is ok.

then, run model(image) with multithread, but report this error:

File models/yolo.py", line 67, in forward
y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy
RuntimeError: The size of tensor a (48) must match the size of tensor b (60) at non-singleton dimension 3

I follow the similar issues by set self.inplace = False, but it does not work.

Please help me.

Additional

No response

@mynameischaos mynameischaos added the question Further information is requested label Jul 13, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jul 13, 2022

👋 Hello @mynameischaos, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@mynameischaos yes multiple models require inplace=False

@glenn-jocher
Copy link
Member

@mynameischaos I'm thinking it may make sense to make inplace=False the default. There are speed improvements in the head with inplace=True but several users have raised multithreading issues so it may be worthwhile to improve the compatibility at the expense of a bit of speed. I think the speed improvement is very slight, but I'll add a TODO to quantify this to determine the best course of action here.

@glenn-jocher glenn-jocher changed the title multithread to predict error Detect(inplace=True) multithreaded detection RuntimeError Jul 13, 2022
@glenn-jocher glenn-jocher removed the TODO label Jul 30, 2022
@glenn-jocher
Copy link
Member

@mynameischaos good news 😃! Your original issue may now be fixed ✅ in PR #8801. This PR sets Detect.inplace=False for safe multithread inference when loading YOLOv5 AutoShape models with PyTorch Hub.

To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

@fernandoTB
Copy link

Hi @glenn-jocher ! im still getting Runtime Error with multithread, even setting inplace=False manually.
The error:
line 72, in forward
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
RuntimeError: The size of tensor a (80) must match the size of tensor b (60) at non-singleton dimension 3

@glenn-jocher
Copy link
Member

glenn-jocher commented Aug 8, 2022

@fernandoTB 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! 😃

@fernandoTB
Copy link

@glenn-jocher I was facing problems when serving Yolov5 with torch hub + FastAPI, I believe is the same problem reported @mynameischaos. When the client sends multiple requests, FastAPI handles it with multithread. Randomly some inferences crash raising runtime error on self.grid multiplication.

I tried to force reload to the newest version with inplace=False, but the error just change to another line, where multiplies self.anchor_grid[i].

Apparently, I just solved my problem using threading mutex, allowing only one thread to perform inference at a time.

@glenn-jocher
Copy link
Member

@fernandoTB got it, thanks! Is there any way to reproduce this with torch hub? i.e. maybe loading two models and dispatching inference in daemon threads? If I could reproduce it it would help debug it.

@boreas-l
Copy link

@glenn-jocher I was facing problems when serving Yolov5 with torch hub + FastAPI, I believe is the same problem reported @mynameischaos. When the client sends multiple requests, FastAPI handles it with multithread. Randomly some inferences crash raising runtime error on self.grid multiplication.

I tried to force reload to the newest version with inplace=False, but the error just change to another line, where multiplies self.anchor_grid[i].

Apparently, I just solved my problem using threading mutex, allowing only one thread to perform inference at a time.

Can you describe in detail how you handled it??I need your help. Thankyou.

@glenn-jocher
Copy link
Member

@boreas-l It's great to hear that you were able to find a workaround by using threading mutex to control the inference thread execution. This approach ensures that only one thread performs the inference at a time, resolving the issue of crashes during concurrent inference. If other users encounter similar problems with concurrent inference in FastAPI, your solution could be helpful to them as well. Thank you for sharing your experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants