Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
72
vendor/ruvector/crates/ruvector-sparse-inference/examples/basic_usage.rs
vendored
Normal file
72
vendor/ruvector/crates/ruvector-sparse-inference/examples/basic_usage.rs
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
//! Basic usage example for the sparse inference engine
|
||||
|
||||
use ndarray::Array2;
|
||||
use ruvector_sparse_inference::backend::get_backend;
|
||||
use ruvector_sparse_inference::sparse::ActivationType;
|
||||
|
||||
fn main() {
|
||||
// Get the best available backend for this platform
|
||||
let backend = get_backend();
|
||||
println!("Using backend: {}", backend.name());
|
||||
println!("SIMD width: {} f32s per register", backend.simd_width());
|
||||
|
||||
// Example 1: Dot product
|
||||
println!("\n=== Dot Product ===");
|
||||
let a = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
|
||||
let b = vec![2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0];
|
||||
let dot = backend.dot_product(&a, &b);
|
||||
println!("a · b = {}", dot);
|
||||
assert_eq!(dot, 72.0);
|
||||
|
||||
// Example 2: ReLU activation
|
||||
println!("\n=== ReLU Activation ===");
|
||||
let mut data = vec![-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, -4.0, 5.0];
|
||||
println!("Before: {:?}", data);
|
||||
backend.activation(&mut data, ActivationType::Relu);
|
||||
println!("After: {:?}", data);
|
||||
assert_eq!(data, vec![0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 0.0, 5.0]);
|
||||
|
||||
// Example 3: AXPY (a = a + b * scalar)
|
||||
println!("\n=== AXPY (a = a + b * 2.5) ===");
|
||||
let mut a = vec![1.0, 2.0, 3.0, 4.0];
|
||||
let b = vec![1.0, 1.0, 1.0, 1.0];
|
||||
println!("Before: a = {:?}", a);
|
||||
backend.axpy(&mut a, &b, 2.5);
|
||||
println!("After: a = {:?}", a);
|
||||
assert_eq!(a, vec![3.5, 4.5, 5.5, 6.5]);
|
||||
|
||||
// Example 4: Sparse matrix-vector multiplication
|
||||
println!("\n=== Sparse MatMul ===");
|
||||
let matrix = Array2::from_shape_vec(
|
||||
(4, 4),
|
||||
vec![
|
||||
1.0, 0.0, 2.0, 0.0, 0.0, 3.0, 0.0, 4.0, 5.0, 0.0, 6.0, 0.0, 0.0, 7.0, 0.0, 8.0,
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
let input = vec![1.0, 2.0, 3.0, 4.0];
|
||||
|
||||
// Only compute rows 0 and 2 (sparse computation)
|
||||
let active_rows = vec![0, 2];
|
||||
let output = backend.sparse_matmul(&matrix, &input, &active_rows);
|
||||
println!("Matrix (4x4):");
|
||||
println!("{:?}", matrix);
|
||||
println!("Input: {:?}", input);
|
||||
println!("Active rows: {:?}", active_rows);
|
||||
println!("Output: {:?}", output);
|
||||
assert_eq!(output, vec![7.0, 23.0]); // row 0: 1*1 + 2*3 = 7, row 2: 5*1 + 6*3 = 23
|
||||
|
||||
// Example 5: Different activation functions
|
||||
println!("\n=== Activation Functions ===");
|
||||
for activation in [
|
||||
ActivationType::Relu,
|
||||
ActivationType::Gelu,
|
||||
ActivationType::Silu,
|
||||
] {
|
||||
let mut data = vec![-1.0, 0.0, 1.0, 2.0];
|
||||
backend.activation(&mut data, activation);
|
||||
println!("{:?}: {:?}", activation, data);
|
||||
}
|
||||
|
||||
println!("\n✓ All examples completed successfully!");
|
||||
}
|
||||
77
vendor/ruvector/crates/ruvector-sparse-inference/examples/gguf_loader.rs
vendored
Normal file
77
vendor/ruvector/crates/ruvector-sparse-inference/examples/gguf_loader.rs
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
//! Example: GGUF model loading and inspection
|
||||
//!
|
||||
//! Demonstrates how to parse and inspect GGUF model files.
|
||||
|
||||
use ruvector_sparse_inference::model::{GgufParser, ModelMetadata};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("GGUF Model Loader Example");
|
||||
println!("==========================\n");
|
||||
|
||||
// Example: Parse a minimal GGUF file structure
|
||||
// In practice, you would load this from a file:
|
||||
// let data = std::fs::read("model.gguf")?;
|
||||
|
||||
// Create a minimal valid GGUF header for demonstration
|
||||
let mut data = Vec::new();
|
||||
|
||||
// Magic number "GGUF" = 0x46554747
|
||||
data.extend_from_slice(&0x46554747u32.to_le_bytes());
|
||||
|
||||
// Version 3
|
||||
data.extend_from_slice(&3u32.to_le_bytes());
|
||||
|
||||
// Tensor count: 0
|
||||
data.extend_from_slice(&0u64.to_le_bytes());
|
||||
|
||||
// Metadata KV count: 1
|
||||
data.extend_from_slice(&1u64.to_le_bytes());
|
||||
|
||||
// Add one metadata entry: general.architecture = "llama"
|
||||
// Key: "general.architecture" (22 chars)
|
||||
data.extend_from_slice(&22u64.to_le_bytes());
|
||||
data.extend_from_slice(b"general.architecture");
|
||||
data.extend_from_slice(&[0, 0]); // Padding
|
||||
|
||||
// Value type: String (8)
|
||||
data.extend_from_slice(&8u32.to_le_bytes());
|
||||
|
||||
// String value: "llama" (5 chars)
|
||||
data.extend_from_slice(&5u64.to_le_bytes());
|
||||
data.extend_from_slice(b"llama");
|
||||
data.extend_from_slice(&[0, 0, 0]); // Padding
|
||||
|
||||
// Parse the GGUF file
|
||||
match GgufParser::parse(&data) {
|
||||
Ok(model) => {
|
||||
println!("✓ Successfully parsed GGUF file");
|
||||
println!(" Magic: 0x{:08X}", model.header.magic);
|
||||
println!(" Version: {}", model.header.version);
|
||||
println!(" Tensor count: {}", model.header.tensor_count);
|
||||
println!(" Metadata entries: {}", model.header.metadata_kv_count);
|
||||
println!("\nMetadata:");
|
||||
for (key, value) in &model.metadata {
|
||||
println!(" {} = {:?}", key, value);
|
||||
}
|
||||
|
||||
// Extract model metadata
|
||||
match ModelMetadata::from_gguf(&model) {
|
||||
Ok(metadata) => {
|
||||
println!("\nModel Configuration:");
|
||||
println!(" Architecture: {:?}", metadata.architecture);
|
||||
println!(" Hidden size: {}", metadata.hidden_size);
|
||||
println!(" Num layers: {}", metadata.num_layers);
|
||||
println!(" Num heads: {}", metadata.num_heads);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("\n⚠ Could not extract full metadata (demo data): {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("✗ Failed to parse GGUF file: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user