-
Notifications
You must be signed in to change notification settings - Fork 1
Hacking the codebase
- https://pytorch.org/blog/a-tour-of-pytorch-internals-1/
- https://pytorch.org/blog/a-tour-of-pytorch-internals-2/
- http://blog.ezyang.com/2019/05/pytorch-internals/
- https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md
- Libtorch => https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst Note: Method2 is more viable here.
Setup.py ==>
Initialization of torch._C Link Notice the method list defined here and how methods are appended to the module Link
Other modules/objects from C extension:
- torch._C._functions
- torch._C._EngineBase
- torch._C._FunctionBase
- torch._C._LegacyVariableBase
- torch._C._CudaEventBase
- torch._C._CudaStreamBase
- torch._C.Generator
- "torch._C." THPStorageBaseStr // Note the ""
- torch._C._PtrWrapper
Check implementation of torch.tensor() i.e. (init())
- Tensor https://github.com/pytorch/pytorch/blob/e8ad167211e09b1939dcb4f462d3f03aa6a6f08a/torch/tensor.py#L20
- _TensorBase : Note this is an object added via
PyModule_AddObject
https://github.com/pytorch/pytorch/blob/e8ad167211e09b1939dcb4f462d3f03aa6a6f08a/torch/csrc/autograd/python_variable.cpp#L588
Note:
torch.autograd.Variable
class was used before PyTorch v0.4.0. Now Variable class has been deprecated. torch.autograd.Variable and torch.Tensor and the same now.
https://pytorch.org/blog/pytorch-0_4_0-migration-guide/
See the section on torch._C.VariableFunctions.add. THPVariable_add
in Edward's post
Adding to this take a look at https://github.com/pytorch/pytorch/tree/master/torch/csrc/autograd
In the torch/csrc/autograd
directory another folder called generated
is created that contains all Python methods associated with torch.Tensor
.
Torch Random Number Generators
https://github.com/pytorch/pytorch/blob/master/docs/source/notes/autograd.rst
THPModule_initNames THPModule_initExtension => Callback for python part. Used for additional initialization of python classes
void THPAutograd_initFunctions()
What is there in copy_utils.h
?
Check THPInsertStorageCopyFunction
- THPDtypeType
- THPDeviceType
- THPMemoryFormatType
- THPLayoutType
- THPGeneratorType
- THPWrapperType
- THPQSchemeType
- THPSizeType
- THPFInfoType
tools
directory is the most important one if you want to hack the Pytorch codebase. A lot of magic happens here i.e. code generation
.
Module.cpp
lists the functions to be injected(link). Note the method lists are marked extern
.
Also look at line
- This line
${py_methods}
is replaced by the generated code. - The code above
${py_methods}
dictates how the code is generated. - The code below
${${py_methods}
dictates the list ofvariable_methods[]
that would be injected intotorch
module and_tensorImpl
type. - Note all the code in directory
tools/autograd/templates
gets placed incsrc/autograd/generated
directory. So whenever, you are not sure where the code comes from incsrc
directory, check forgenerated
in headers. This means you need to look intotools
directory.