How and when to use `torch.set_num_threads`?

I have a laptop with 4 cores (4 CPUs I assume?). If I don’t set the number of threads (with torch.set_num_threads), the performance is really bad. I have two questions:

  • which number should I give to torch.set_num_threads? I don’t really understand what “number of threads” means. I have 4 cores, should I set it to 4 or to 1?
  • when should I set the number of threads? At the beginning of all my Pytorch programs?

Thank you in advance for your answers :slight_smile:

5 Likes

I also want to know the answer to this question.

Also want to know + 1

I also want to know the answer to this question.

I think if you use GPU, you can set it 1, but if you use CPUs for training, you can set it with 4 since there are 4 cores. It depends. But you can test the speedup.

1 Like

+1. And as Willems said the performance is really bad without set_num_threads, why we need set_num_threads? How it works?