torchscript
v0.0.1
Published
PyTorch/libtorch compiled to WebAssembly for TypeScript
Downloads
21
Maintainers
Readme
torchscript
PyTorch/libtorch compiled to WebAssembly for TypeScript.
This package provides TypeScript bindings for PyTorch tensor operations compiled to WebAssembly using Emscripten. It works in both Node.js and browser environments.
Features
- Tensor creation (ones, zeros, from data)
- Basic tensor operations (add, mul, matmul)
- TorchScript model loading and inference
- Works in Node.js and browsers via WebAssembly
Installation
npm install torchscript
# or
pnpm add torchscriptUsage
import { loadModule, ones, zeros, tensor, DType } from 'torchscript';
// Initialize the WASM module
await loadModule();
// Create tensors
const a = await ones([2, 3]);
const b = await zeros([2, 3]);
const c = await tensor([1, 2, 3, 4, 5, 6], [2, 3]);
// Tensor operations
const sum = a.add(b);
const product = a.mul(c);
// Matrix multiplication
const x = await ones([2, 3]);
const y = await ones([3, 4]);
const result = x.matmul(y);
// Get tensor data
console.log(result.shape); // [2, 4]
console.log(result.toArray());
// Free tensors when done
a.free();
b.free();
c.free();
sum.free();
product.free();
x.free();
y.free();
result.free();Loading TorchScript Models
import { loadModule, load, tensor } from 'torchscript';
await loadModule();
// Load a TorchScript model
const model = await load('model.pt');
// Create input tensor
const input = await tensor([/* your input data */], [1, 3, 224, 224]);
// Run inference
const output = model.forward(input);
console.log(output.shape);
console.log(output.toArray());
// Clean up
input.free();
output.free();
model.free();Building from Source
Prerequisites
- Docker
- Node.js 18+
- pnpm
Build Steps
Build the Docker image with Emscripten and PyTorch:
pnpm wasm:docker-buildBuild the WASM module:
pnpm wasm:buildBuild the TypeScript wrapper:
pnpm build:js
Or run all steps at once:
pnpm buildNote: The initial Docker build is slow as it compiles PyTorch from source.
API Reference
Functions
loadModule(): Promise<void>- Initialize the WASM module (must be called before other operations)version(): Promise<string>- Get PyTorch versionones(shape: number[], dtype?: DType): Promise<Tensor>- Create tensor filled with oneszeros(shape: number[], dtype?: DType): Promise<Tensor>- Create tensor filled with zerostensor(data: number[] | Float32Array, shape: number[]): Promise<Tensor>- Create tensor from dataload(filename: string): Promise<Module>- Load a TorchScript model
Tensor Class
shape: number[]- Tensor dimensionsnumel: number- Number of elementstoArray(): Float32Array- Get tensor dataadd(other: Tensor): Tensor- Element-wise additionmul(other: Tensor): Tensor- Element-wise multiplicationmatmul(other: Tensor): Tensor- Matrix multiplicationfree(): void- Free tensor memory
Module Class
forward(input: Tensor): Tensor- Run model inferencefree(): void- Free module memory
DType Enum
DType.Float32- 32-bit floating pointDType.Float64- 64-bit floating pointDType.Int32- 32-bit integerDType.Int64- 64-bit integer
License
MIT
Credits
Based on:
- libtorch-wasm - PyTorch WASM compilation
- libpg-query-node - Package structure patterns
