Finding function/class definitions in PyTorch
I want to find out where certain classes and functions are defined within PyTorch (and other libraries).
Unfortunately, the following doesn't work:
import inspect
import torch

inspect.getsource(torch.tensor)

It throws the following error:
TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method

What's more, within PyCharm, I usually do 'gd' (in vim mode) to find a function/class definition, but this doesn't work either for PyTorch.
Please help me understand what is the problem here, and more importantly, how I can find these definitions in general.

this is actually complicated. Pytorch/libtorch is a huge project, and it relies on a lot of builtin low-level functions which have been implemented in C/Cuda. Most low-level kernels (math operations for example) even have several implementations, in order to optimize differently for the CPU and the GPU etc.
So there is a lot in this library that is not python code, and inspect is going to have a hard time.
If you want to find the source files, you are probably going to need to dive in the github repository yourself, and make good use of tools like grep and find.
However, the torch.nn module is almost entirely python, so I think inspect will work correctly on its features (like datasets, dataloaders, modules, optimizers etc)
And finally, if you need it, here is the file for torch.tensor, in which you will find python code mixed with calls to the C api : torch tensor source code
About the question in the comment below:
I cannot provide a full answer because this is reaching beyond my understanding of how exactly are python and C++ code interfaced in torch. But I'll do my best (If anyone has any correction or improvement to make to this, please do it).
There is a fundamental difference between C source code and python code : C/C++ are compiled and thus the features implemented in these languages are shipped as compiled assembly code. In other words, when your python code calls functions/objects from the underlying C code, it make calls to assembly functions that are not human-readable anymore. So the computer can make the calls, but the inspect feature that looks up for source code (for you to read) cannot work because this code does not exist anymore (at least not where inspect is looking). You would need other tools like a disassembler, a debugger etc, which are specialized in analyzing assembly instructions (you can also learn x86-64 assembly language programming if you are brave enough :D)
