Giter Club home page Giter Club logo

tinytorch's Introduction

TinyTorch

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.

Steps to build and run the C++ code (Mac OSX Big Sur)

  1. Ensure that CMake and C++ >= 17 are installed.
  2. Clone the repository.
  3. Perform,
    cd src
    mkdir build
    cd build
    cmake ..
    make
    ./tinytorch
    
    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.

Steps to build the Python bindings from C++ (Mac OSX Big Sur)

  1. Install PyBind11,
    brew install pybind11
    
  2. Perform,
    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
    
    This should output an error such as this,
        a = 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
    
    Anything else means that the python module is not properly working. It may happen that the system can't find the appropriate .so file from the appropriate python site-packages directory. In that case, copy the .so file generated in the path which appears in the make logs to the site-packages directory which is being used by the system.

tinytorch's People

Contributors

qmrpy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.