We’re excited to announce the release of TyTorch - TypeScript bindings for PyTorch that bring the full power of deep learning to Node.js with complete type safety.
TyTorch is a native Node.js addon that provides direct bindings to PyTorch’s C++ API (libtorch). It enables you to use PyTorch’s powerful tensor operations, automatic differentiation, and GPU acceleration directly from TypeScript and JavaScript.
Unlike other approaches that require Python bridges or ONNX conversions, TyTorch gives you direct access to PyTorch’s optimized C++ kernels with minimal overhead.
Machine learning is increasingly important in web applications, but JavaScript has lacked a robust, production-ready ML framework with the power of PyTorch. TyTorch bridges this gap:
Here’s a simple example showing how to create tensors, perform operations, and use automatic differentiation:
import { torch } from 'tytorch';
// Create tensors
const x = torch.tensor([1.0, 2.0, 3.0], { requires_grad: true });
const weights = torch.tensor([0.5, 0.3, 0.2]);
// Forward pass
const output = x.mul(weights).sum();
// Backward pass (automatic differentiation)
output.backward();
// Access gradients
console.log(x.grad.toArray()); // [0.5, 0.3, 0.2]
TyTorch currently includes 46 tensor operations covering the essentials for machine learning:
All operations are thoroughly tested with 500+ tests across unit, CPU, and MPS test suites.
Currently, TyTorch is a prototype tested primarily on macOS. Linux and Windows support is in progress.
Installing TyTorch requires two steps:
First, install PyTorch via pip:
# macOS/Linux
pip3 install torch torchvision torchaudio
# Set environment variables (add to your shell config)
export LIBTORCH="$(python3 -c 'import torch; print(torch.__path__[0])')"
export DYLD_LIBRARY_PATH="$LIBTORCH/lib:$DYLD_LIBRARY_PATH" # macOS
export LD_LIBRARY_PATH="$LIBTORCH/lib:$LD_LIBRARY_PATH" # Linux
npm install tytorch
For detailed installation instructions including Windows support, see the README.
TyTorch is under active development. Here’s what’s coming next:
See the full development roadmap for details.
Here’s a more complete example showing a training loop:
import { torch } from 'tytorch';
// Create training data
const X = torch.randn([100, 10]); // 100 samples, 10 features
const y = torch.randn([100, 1]); // 100 labels
// Initialize model parameters
const weights = torch.randn([10, 1], { requires_grad: true });
const bias = torch.zeros([1], { requires_grad: true });
// Training loop
for (let epoch = 0; epoch < 100; epoch++) {
// Forward pass
const predictions = X.matmul(weights).add(bias);
const loss = predictions.sub(y).pow(2).mean();
// Backward pass
loss.backward();
// Update weights (gradient descent)
weights.data = weights.sub(weights.grad.mul(0.01));
bias.data = bias.sub(bias.grad.mul(0.01));
// Zero gradients
weights.zero_grad();
bias.zero_grad();
if (epoch % 10 === 0) {
console.log(`Epoch ${epoch}, Loss: ${loss.toArray()[0]}`);
}
}
TyTorch uses a clean, modular architecture:
All new operations include proper error handling with try-catch blocks that convert C++ exceptions to catchable JavaScript errors.
TyTorch is open source and we welcome contributions!
Whether you want to:
We’d love to hear from you! Open an issue or submit a pull request on GitHub.
TyTorch is still in early development (v0.1.0), but it’s already capable of training simple models. Our goal is to make it a production-ready ML framework for Node.js.
Try it out and let us know what you think! Install with npm install tytorch
and check out the documentation to get
started.
TyTorch is developed by Identellica and released under the Apache 2.0 license.