Modifying the source code of Pytorch

I want to find in which C file the thread is created when I use torch.set_num_thread() . For this I did a grep in pytorch source code and then in all those places where there was an occurrence of set_num_threads, I added a printf() . I built it again and still cannot have anything on the terminal from the printf() statements added.

I got a suggestion to debug my code using gdb, but I am unaware of how to use the gdb for source code.

It would be great if someone could suggest how to use the debugger and also if someone knows why arent the printf() working.

After adding the printf statements to the C++ backend, I assume you’ve successfully rebuilt PyTorch from source? If not, these changes won’t be visible as they need to be compiled first.

This small gdb tutorial might be a good a good starter.

Yes I did rebuilt using sudo python3 develop

In that case the methods might not be called at all. Add another print statement to a call which you know will be executed and make sure you are really running your source build first.

I am really not sure about any of the methods which will definitely be called. I will attach the code which I am executing (a simpler version of that).

I also want to point out the fact that, I had done the same changes of adding printf() to certain files in my Mac and rebuilt it. At that time, the changes were being reflected and I could see the printf() executing.

Here is a simple code which does show the effect of adding printf() on mac but not on linux.

import timeit
import torch
import traceback

t = 4


r = timeit.timeit(setup = "import torch; x = torch.randn(259, 259); y = torch.randn(259, 259)", stmt=", y)", number=100)
print(t, r)