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

Give clearer guidance about multithreading in PyTorch, and how to disable it #16899

Open
ezyang opened this issue Feb 8, 2019 · 5 comments
Open
Labels
module: docs Related to our documentation, both in docs/ and docblocks module: multithreading Related to issues that occur when running on multiple CPU threads triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@ezyang
Copy link
Contributor

ezyang commented Feb 8, 2019

e.g. #16894

There's a lot of people asking about this, but no canonical source of information about it.

cc @jlin27 @mruberry

@vadimkantorov
Copy link
Contributor

vadimkantorov commented Feb 8, 2019

I think it would be nice to have a smart switch for limiting the number of CPU threads (activated from code and by a new torch-consumed env variable) that would set all the needed environment variables for OpenMP and known BLAS implementations.

It's important for the usecase of academic clusters managed by old deprecated software where a machine can be shared by several people and no rock-solid CPU-time/thread quotas exist. E.g. multiple data-loading threads and BLAS/OpenMP threads contribute to the CPU-affinity and CPU-cache thrashing issues. A single switch or guidance will be very helpful. An easy default way allowing to have good CPU-affinity without manual generating a mask and using the Linux command would also be very helpful.

@ailzhang ailzhang added the module: docs Related to our documentation, both in docs/ and docblocks label Feb 10, 2019
@soumith
Copy link
Member

soumith commented Feb 12, 2019

@vadimkantorov as of v1.0.1, torch.set_num_threads(1) covers everything, including the BLAS libraries.

@vadimkantorov
Copy link
Contributor

@soumith CPU affinity is the next step :)

@fisakhan
Copy link

@soumith @vadimkantorov using torch.set_num_threads(1) in Python I get in total 3 threads, where 1 thread is generated by Python. I use os.system( "top -H -b -n1 | grep python | wc -l") command to count the number of threads. Why do I get 2 (rather than 1) additional threads?
Can I make the total number of threads to 2 or 1 (including thread for python)?

@bdhirsh bdhirsh added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 13, 2020
@mruberry mruberry added the module: multithreading Related to issues that occur when running on multiple CPU threads label Oct 13, 2020
@WillemvJ
Copy link

WillemvJ commented Dec 2, 2021

I am calling into PyTorch from c++. I have:

	at::set_num_interop_threads(1);
	at::set_num_threads(1);
	torch::set_num_threads(1);

Still, when I start inferencing a (small) neural network (with nogradguard) on CPU, I see 16 out of 128 processors which are using 100% capacity. Shouldn't torch::set_num_threads(1) ensure that only one thread is used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: docs Related to our documentation, both in docs/ and docblocks module: multithreading Related to issues that occur when running on multiple CPU threads triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

8 participants