{"id":13431393,"url":"https://github.com/cedrickchee/tch-js","last_synced_at":"2025-05-07T20:38:46.919Z","repository":{"id":44798912,"uuid":"332385091","full_name":"cedrickchee/tch-js","owner":"cedrickchee","description":"A JavaScript and TypeScript port of PyTorch C++ library (libtorch) - Node.js N-API bindings for libtorch.","archived":false,"fork":false,"pushed_at":"2023-01-15T11:57:48.000Z","size":575,"stargazers_count":16,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-07T20:38:39.797Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cedrickchee.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}},"created_at":"2021-01-24T06:45:21.000Z","updated_at":"2025-02-21T15:53:27.000Z","dependencies_parsed_at":"2023-02-09T22:01:30.807Z","dependency_job_id":null,"html_url":"https://github.com/cedrickchee/tch-js","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedrickchee%2Ftch-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedrickchee%2Ftch-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedrickchee%2Ftch-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedrickchee%2Ftch-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cedrickchee","download_url":"https://codeload.github.com/cedrickchee/tch-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252954125,"owners_count":21830892,"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":[],"created_at":"2024-07-31T02:01:02.780Z","updated_at":"2025-05-07T20:38:46.873Z","avatar_url":"https://github.com/cedrickchee.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# tch-js\n\nAn unofficial JavaScript and TypeScript port of PyTorch C++ library (libtorch).\n\n## Install\n\nPackage publishing to NPM is still work-in-progress. In the meantime, you can\ninstall the package directly from GitHub.\n\n```sh\n$ npm i git+https://github.com/cedrickchee/tch-js.git\n```\n\nThe package will download the pre-built binary during installation. You don't\nhave to download PyTorch/libtorch or install tools for compiling from source.\n\n## Versions Supported\n\n- Node.js:\n  - 10 (tested)\n  - 12 (tested)\n  - 14 (WIP)\n- PyTorch (CPU):\n  - 1.4.X (tested)\n  - 1.5.X\n  - 1.6.X\n  - 1.7.X (tested)\n\n## Code Examples\n\n```javascript\n// This is a real example from an audio source separation model.\nconst { tch, load, Tensor } = require('tch-js');\nconst fs = require('fs');\nconst wav = require('node-wav');\n\n// WAV samples of length 269973\nconst monoAudioChan = new Float32Array([\n  1.100000023841858, 1.590000033378601,\n  2.049999952316284, 0.18000000715255737\n]);\n// Flat tensor\nlet audio = tch.tensor(monoAudioChan); // tensor of size [269973]\n\n// Reshape to 1xSample Length to match model input\naudio = audio.view([1, monoAudioChan.length]); // tensor of size [1, 269973]\n\n// Load PyTorch traced model async from file and return resulting ScripModule.\nconst model = await load('sound-model.pt');\n// Forward tensor async and return resulting Tensor.\nmodel.forward(audio, getResult);\nconst getResult = (err: Error, result: Tensor) =\u003e {\n  if (err) return;\n\n  // result is a tensor of size [1, 1, 269973]\n  const out = result.toFloat32Array(); // convert Tensor to JS TypedArray\n\n  // Encode output to 16-bit float WAV and write to file.\n  const buf = wav.encode([out], { sampleRate: 44100, float: false, bitDepth: 16});\n  fs.writeFileSync(\"out.wav\", Buffer.from(buf));\n};\n```\n\n## Build It Yourself\n\nCurrently, only Linux builds are available.\n\nIf you want to build the package youself, below are the steps to reproduce the\nbuild.\n\nInstalling on Linux:\n- Ubuntu 18.04 (tested)\n- Ubuntu 20.04 (tested)\n\n1. Install build tools\n\n```sh\n$ apt install -y cmake make gcc-c++ unzip\n```\n\n2. Install Node.js 10\n3. Download libtorch\nDownload [libtorch pre-cxx11 ABI and CPU version](https://pytorch.org/get-started/locally/#start-locally).\n\n```sh\n$ curl -o libtorch.zip https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.7.1%2Bcpu.zip\n$ unzip libtorch.zip\n```\n\n4. Install Node.js packages\n\n```sh\n$ npm i --ignore-scripts\n```\n\n5. Build\n\n```sh\n$ npm run pre-build\n```\n\n6. Test\n\n```sh\n$ npm run test\n```\n\n## The Plan\n\nThe library should:\n- expose more libtorch types and APIs for inference\n- supports Windows\n- auto build binaries using CI\n- TypeScript types\n\n## Research\n\n- [PyTorch 1.0 tracing JIT and LibTorch C++ API to integrate PyTorch into NodeJS](https://blog.christianperone.com/2018/10/pytorch-1-0-tracing-jit-and-libtorch-c-api-to-integrate-pytorch-into-nodejs/)\n- [tch-rs](https://github.com/LaurentMazare/tch-rs) - Rust bindings for the C++ API of PyTorch\n- [torch-js](https://github.com/arition/torch-js) - Node.js binding for PyTorch\n- [pytorchjs](https://github.com/raghavmecheri/pytorchjs) - Torch and TorchVision, but for NodeJS\n- [Using the PyTorch C++ Frontend tutorial](https://pytorch.org/tutorials/advanced/cpp_frontend.html)\n- [Loading a TorchScript Model in C++](https://pytorch.org/tutorials/advanced/cpp_export.html)\n- [Torchaudio.load() in C++](https://discuss.pytorch.org/t/torchaudio-load-in-c/62400)\n- [Minimal PyTorch 1.0 -\u003e C++ full example demo](https://gist.github.com/zeryx/526dbc05479e166ca7d512a670e6b82d)\n- [Convert libtorch tensor into an array](https://discuss.pytorch.org/t/convert-tensor-into-an-array/56721)\n- [JavaScript TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)\n- [Napi::TypedArray](https://github.com/nodejs/node-addon-api/blob/master/doc/typed_array_of.md) - Node.js N-API doc\n- [How to use WebAssembly from node.js?](https://stackoverflow.com/questions/51403326/how-to-use-webassembly-from-node-js) - C++ to WebAssembly to Node.js\n- [node-pre-gyp](https://github.com/mapbox/node-pre-gyp) - Node.js tool for easy binary deployment of C++ addons\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedrickchee%2Ftch-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcedrickchee%2Ftch-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedrickchee%2Ftch-js/lists"}