| Crates.io | trustformers-wasm |
| lib.rs | trustformers-wasm |
| version | 0.1.0-alpha.1 |
| created_at | 2025-11-09 10:19:33.638815+00 |
| updated_at | 2025-11-09 10:19:33.638815+00 |
| description | WebAssembly bindings for TrustformeRS transformer library |
| homepage | |
| repository | https://github.com/cool-japan/trustformers |
| max_upload_size | |
| id | 1923942 |
| size | 2,513,981 |
WebAssembly bindings for the TrustformeRS transformer library, enabling transformer models to run directly in web browsers and Node.js environments.
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh)# Build for all targets
./build.sh
# Or build individually:
wasm-pack build --target web --out-dir pkg-web
wasm-pack build --target bundler --out-dir pkg-bundler
wasm-pack build --target nodejs --out-dir pkg-node
<script type="module">
import init, { TrustformersWasm, WasmTensor } from './pkg-web/trustformers_wasm.js';
async function run() {
await init();
const tf = new TrustformersWasm();
console.log('Version:', tf.version);
// Create and manipulate tensors
const tensor = WasmTensor.new([1, 2, 3, 4], [2, 2]);
const result = tensor.add(tensor);
console.log('Result:', result.data);
}
run();
</script>
const { TrustformersWasm, WasmTensor } = require('./pkg-node/trustformers_wasm.js');
const tf = new TrustformersWasm();
const tensor = WasmTensor.new([1, 2, 3, 4], [2, 2]);
console.log(tensor.toString());
import * as wasm from './pkg-bundler/trustformers_wasm';
async function run() {
await wasm.default();
const tf = new wasm.TrustformersWasm();
// Use the library...
}
TrustformersWasmMain entry point for the library.
const tf = new TrustformersWasm();
console.log(tf.version); // "0.1.0-alpha.1"
console.log(tf.initialized); // true
WasmTensorCore tensor operations.
// Creation
const a = WasmTensor.new([1, 2, 3, 4], [2, 2]);
const b = WasmTensor.zeros([3, 3]);
const c = WasmTensor.ones([2, 4]);
const d = WasmTensor.randn([5, 5]);
// Operations
const sum = a.add(b);
const prod = a.matmul(b);
const transposed = a.transpose();
// Activations
const relu_out = a.relu();
const gelu_out = a.gelu();
const softmax_out = a.softmax(-1);
LinearFully connected layer.
const linear = new Linear(input_size, output_size, use_bias);
const output = linear.forward(input_tensor);
BertModelWasmTiny BERT model for demonstrations.
const config = BertConfig.tiny();
const model = new BertModelWasm(config);
const output = model.forward(input_ids, attention_mask);
// Performance measurement
const timer = new Timer("My Operation");
// ... do work ...
console.log(`Elapsed: ${timer.elapsed()}ms`);
// Memory statistics
const stats = get_memory_stats();
console.log(`Memory used: ${stats.used_mb} MB`);
// Feature detection
console.log(`SIMD enabled: ${enable_simd()}`);
console.log(`Features: ${features()}`);
See the examples/ directory for complete examples:
index.html - Interactive browser demonode-example.js - Node.js usage exampleMIT OR Apache-2.0