{"id":13677541,"url":"https://github.com/torch-js/torch-js","last_synced_at":"2025-04-29T11:31:14.236Z","repository":{"id":152905886,"uuid":"158912615","full_name":"torch-js/torch-js","owner":"torch-js","description":"Node.js binding for PyTorch.","archived":false,"fork":false,"pushed_at":"2019-04-17T17:18:28.000Z","size":34,"stargazers_count":302,"open_issues_count":6,"forks_count":39,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-08-02T13:18:23.976Z","etag":null,"topics":["nodejs","pytorch"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/torch-js.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-11-24T07:23:51.000Z","updated_at":"2024-07-31T22:54:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"4226457f-ad8b-4254-9d0e-0b1c86540769","html_url":"https://github.com/torch-js/torch-js","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torch-js%2Ftorch-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torch-js%2Ftorch-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torch-js%2Ftorch-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torch-js%2Ftorch-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/torch-js","download_url":"https://codeload.github.com/torch-js/torch-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224163627,"owners_count":17266542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","pytorch"],"created_at":"2024-08-02T13:00:43.710Z","updated_at":"2024-11-11T19:32:03.853Z","avatar_url":"https://github.com/torch-js.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# TorchJS\n\nTorchJS is a JS binding for PyTorch. Its primary objective is to allow running [Torch Script](https://pytorch.org/docs/master/jit.html) inside Node.js program. Complete binding of libtorch is possible but is out-of-scope at the moment.\n\n## Example\n\nIn `test/torch_module.py`, you will find the defination of our test module and the code to generate the trace file.\n\n```python\nclass TestModule(torch.nn.Module):\n    def __init__(self):\n        super(TestModule, self).__init__()\n\n    def forward(self, input1, input2):\n        return input1 + input2\n```\n\nOnce you have the trace file, you can load it into Node.js like this\n\n```javascript\nconst torch = require(\"torch-js\");\n\nvar test_model_path = \"test/test_model.pt\";\n\nvar script_module = new torch.ScriptModule(test_model_path);\nconsole.log(script_module.toString());\n\nvar a = torch.rand(1, 5);\nconsole.log(a.toObject());\nvar b = torch.rand([1, 5]);\nconsole.log(b.toObject());\n\nvar c = script_module.forward(a, b);\nconsole.log(c.toObject());\n\nvar d = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5]]);\nconsole.log(d.toObject());\n\nvar e = script_module.forward(c, d);\nconsole.log(e.toObject());\n```\n\nThe program above will print something like this on console. Your result will be different from this since `a` and `b` are random variables.\n\n```javascript\nScriptModule(\"/Users/kittipat/torchjs/tests/test_model.pt\")\n{ data:\n   Float32Array [\n     0.5436246991157532,\n     0.30234378576278687,\n     0.4031236171722412,\n     0.8123507499694824,\n     0.3121740221977234 ],\n  shape: [ 1, 5 ] }\n{ data:\n   Float32Array [\n     0.20072013139724731,\n     0.09114563465118408,\n     0.588677167892456,\n     0.14665216207504272,\n     0.8567551374435425 ],\n  shape: [ 1, 5 ] }\n{ data:\n   Float32Array [\n     0.7443448305130005,\n     0.39348942041397095,\n     0.9918007850646973,\n     0.9590029120445251,\n     1.168929100036621 ],\n  shape: [ 1, 5 ] }\n{ data:\n   Float32Array [\n     0.10000000149011612,\n     0.20000000298023224,\n     0.30000001192092896,\n     0.4000000059604645,\n     0.5 ],\n  shape: [ 1, 5 ] }\n{ data:\n   Float32Array [\n     0.8443448543548584,\n     0.593489408493042,\n     1.2918007373809814,\n     1.359002947807312,\n     1.668929100036621 ],\n  shape: [ 1, 5 ] }\n```\n\n### Tensor creation\n\nThere are several ways to create tensors\n\n```javascript\n// With TypedArray and shape array\nvar a = torch.tensor(\n  new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5]), {\n    shape: [1, 5],\n  });\n\n// With array, will create tensor with float32 data type\nvar b = torch.tensor([\n  [0.1, 0.2, 0.3],\n  [0.4, 0.5, 0.6],\n]);\n\n// With array and option object\nvar c = torch.tensor([\n  [0.1, 0.2, 0.3],\n  [0.4, 0.5, 0.6],\n], {\n  dtype: torch.float64\n});\n\n// With torch.Tensor.fromObject()\nvar d = torch.Tensor.fromObject({\n  data: new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]),\n  shape: [2, 3],\n});\n```\n\n## Installation\n\nThis project uses `cmake-js` to build Node extension. You will need to download [the preview build of libtorch](https://pytorch.org/get-started/locally/) and extract it to an accessible location. The build script assumes you put `libtorch` in the same directory as this library. E.g., if you checked out this library to `~/torch-js`, then `libtorch` should be at `~/libtorch`. Once you have that, you can run\n\n```bash\nyarn install\n```\n\nAnd, to test, run:\n\n```bash\nnode tests/runTorch.js\n```\n\nIf it failed to run because `libmklml` is missing, you can download it from conda.\n\n```bash\nconda install libmklml\n```\n\nIf conda's `lib` directory is in your path, then you should be able to run the command above. Otherwise, you can set environment variable to point to the directory.\n\nOn macOS, it would be:\n\n```bash\nDYLD_LIBRARY_PATH=$CONDA_PREFIX/lib/ node tests/runTorch.js\n```\n\nOn Linux, it should be:\n\n```bash\nLD_LIBRARY_PATH=$CONDA_PREFIX/lib/ node tests/runTorch.js\n```\n\nIf there is any error loading the trace file, you might have to resolve it by installing the matching versions of PyTorch and libtorch and regenerate the file.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorch-js%2Ftorch-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftorch-js%2Ftorch-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorch-js%2Ftorch-js/lists"}