A basic AutoGrad Engine written from scratch in C++. Constructs a computation graph out of operations on Tensors, computes and stores gradients in the nodes, similar to PyTorch. The API is PyTorch like.
For now, only addition, subtraction, scalar multiplication and element-wise tensor multiplication are supported, but it is easily extensible to any arbitrary differentiable operation. A separate module for such operations, similar to PyTorch's nn.Module
is planned.
Python3 bindings are also provided, however, the binding code doesn't work properly yet as the C++ code uses C style float arrays, which PyBind11 can't handle as it doesn't have pointers. The constructor function argument types may need to be changed, and/or the underlying data type of the Tensor data and gradient from raw C-style arrays to C++ vectors.
- Ensure that CMake and C++ >= 17 are installed.
- Clone the repository.
- Perform,
This will run the code and test all the operations. Both data and gradients of the outputs can be matched with PyTorch to check for correctness.
cd src mkdir build cd build cmake .. make ./tinytorch
- Install PyBind11,
brew install pybind11
- Perform,
This should output an error such as this,
cd python mkdir build cd build cmake .. -DPYTHON_LIBRARY_DIR="/usr/local/lib/python3.x/site-packages" -DPYTHON_EXECUTABLE=`which python3` make make install cd ../.. python3 tinytorch_test.py
Anything else means that the python module is not properly working. It may happen that the system can't find the appropriatea = torch.Tensor([1, 2, 3], 3) TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. tinytorch_py.Tensor(arg0: int) 2. tinytorch_py.Tensor(arg0: List[int]) 3. tinytorch_py.Tensor(arg0: float, arg1: int) 4. tinytorch_py.Tensor(arg0: float, arg1: int, arg2: bool) 5. tinytorch_py.Tensor(arg0: float, arg1: List[int]) 6. tinytorch_py.Tensor(arg0: float, arg1: List[int], arg2: bool) Invoked with: [1, 2, 3], 3
.so
file from the appropriate pythonsite-packages
directory. In that case, copy the.so
file generated in the path which appears in themake
logs to thesite-packages
directory which is being used by the system.