Squashed 'vendor/ruvector/' content from commit b64c2172
git-subtree-dir: vendor/ruvector git-subtree-split: b64c21726f2bb37286d9ee36a7869fef60cc6900
This commit is contained in:
29
crates/ruvector-fpga-transformer-wasm/Cargo.toml
Normal file
29
crates/ruvector-fpga-transformer-wasm/Cargo.toml
Normal file
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "ruvector-fpga-transformer-wasm"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.77"
|
||||
authors = ["RuVector Team"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "WASM bindings for FPGA Transformer backend"
|
||||
repository = "https://github.com/ruvnet/ruvector"
|
||||
keywords = ["fpga", "transformer", "wasm", "inference"]
|
||||
categories = ["wasm", "algorithms"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
ruvector-fpga-transformer = { path = "../ruvector-fpga-transformer", features = ["wasm", "native_sim"] }
|
||||
wasm-bindgen = "0.2"
|
||||
js-sys = "0.3"
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
lto = true
|
||||
73
crates/ruvector-fpga-transformer-wasm/src/lib.rs
Normal file
73
crates/ruvector-fpga-transformer-wasm/src/lib.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
//! WASM bindings for FPGA Transformer
|
||||
//!
|
||||
//! This crate provides WebAssembly bindings for the FPGA Transformer backend,
|
||||
//! enabling browser and Node.js environments to run transformer inference
|
||||
//! with the same API as native Rust.
|
||||
//!
|
||||
//! ## Usage in JavaScript/TypeScript
|
||||
//!
|
||||
//! ```javascript
|
||||
//! import { WasmEngine, microShape, validateArtifact } from 'ruvector-fpga-transformer-wasm';
|
||||
//!
|
||||
//! // Create engine
|
||||
//! const engine = new WasmEngine();
|
||||
//!
|
||||
//! // Load model
|
||||
//! const artifactBytes = await fetch('/model.rva').then(r => r.arrayBuffer());
|
||||
//! const modelId = engine.loadArtifact(new Uint8Array(artifactBytes));
|
||||
//!
|
||||
//! // Run inference
|
||||
//! const tokens = new Uint16Array([1, 2, 3, 4, ...]);
|
||||
//! const mask = new Uint8Array(tokens.length).fill(1);
|
||||
//! const result = engine.infer(modelId, tokens, mask, 256, false, 2);
|
||||
//!
|
||||
//! console.log('Top prediction:', result.topk[0]);
|
||||
//! console.log('Latency:', result.witness.latency_ns / 1_000_000, 'ms');
|
||||
//! ```
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// Re-export the WASM engine from the main crate
|
||||
pub use ruvector_fpga_transformer::ffi::wasm_bindgen::{
|
||||
micro_shape as microShape, validate_artifact as validateArtifact, WasmEngine,
|
||||
};
|
||||
|
||||
/// Initialize the WASM module
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn init() {
|
||||
// Set up panic hook for better error messages
|
||||
#[cfg(feature = "console_error_panic_hook")]
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
/// Get the crate version
|
||||
#[wasm_bindgen]
|
||||
pub fn version() -> String {
|
||||
ruvector_fpga_transformer::VERSION.to_string()
|
||||
}
|
||||
|
||||
/// Check if WASM is properly initialized
|
||||
#[wasm_bindgen(js_name = isReady)]
|
||||
pub fn is_ready() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use wasm_bindgen_test::*;
|
||||
|
||||
wasm_bindgen_test_configure!(run_in_browser);
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_engine_creation() {
|
||||
let engine = WasmEngine::new();
|
||||
assert!(engine.get_loaded_models().length() == 0);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_version() {
|
||||
let v = version();
|
||||
assert!(!v.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user