Skip to content

Techainer/torch-js

This branch is 34 commits ahead of, 16 commits behind arition/torch-js:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

83ccf56 · Jun 27, 2021
Apr 8, 2021
Aug 26, 2020
Mar 3, 2021
Apr 8, 2021
Oct 15, 2020
Mar 10, 2021
Oct 9, 2020
Mar 10, 2021
Aug 26, 2020
Nov 6, 2020
Nov 6, 2020
Mar 25, 2021
Oct 5, 2020
Oct 8, 2020
Oct 8, 2020
Jun 27, 2021
Mar 10, 2021
Apr 7, 2021
Apr 8, 2021
Oct 8, 2020
Oct 8, 2020
Apr 9, 2021
Nov 25, 2018
Mar 10, 2021

Repository files navigation

TorchJS

npm version CI publish-prebuild

TorchJS is a JS binding for PyTorch. Its primary objective is to allow running TorchScript inside Node.js program. Complete binding of libtorch is possible but is out-of-scope at the moment.

This fork is maintained by Techainer

Changes after fork

  • Add support for List (Javascript Array), Dict (Javascript Object), String, float (Javascript number) as inputs and outputs.

  • Add CUDA support.

  • Add ops from torchvision.

  • Add async support for forward function.

  • Add async support for call_scripted_function(function_name, **inputs) to call any method of the scripted module.

  • Provide prebuild binary for cross NVIDIA GPU from Pascal to Ampere

  • Fixed some weird conversion arround List[Tensor] input.

  • Updated libtorch to 1.8.1 and torchvision to 0.9.1

Install

To install the forked version, you can install it from npm:

yarn add torch-js@npm:@techainer1t/torch-js

Example

In tests/resources/torch_module.py, you will find the defination of our test module and the code to generate the trace file.

class TestModule(torch.nn.Module):
    def __init__(self):
        super(TestModule, self).__init__()

    def forward(self, input1, input2):
        return input1 + input2

Once you have the trace file, it may be loaded into NodeJS like this

const torch = require("torch-js");
const modelPath = `test_model.pt`;
const model = new torch.ScriptModule(testModelPath);
const inputA = torch.rand([1, 5]);
const inputB = torch.rand([1, 5]);
const res = await model.forward(inputA, inputB);

More examples regarding tensor creation, ScriptModule operations, and loading models can be found in our examples folder.

Languages

  • C++ 44.0%
  • CMake 17.1%
  • JavaScript 13.4%
  • PowerShell 11.2%
  • Shell 8.8%
  • TypeScript 2.8%
  • Other 2.7%