instmodel-rust-inference
A high-performance neural network inference library for Rust that executes optimized computation sequences through a unified buffer architecture.
Installation
Or add to your Cargo.toml:
[]
= "<version>"
Overview
This library provides a lightweight, zero-dependency neural network inference engine. Models are defined as a sequence of instructions that operate on computation buffers, enabling efficient memory reuse and predictable performance.
Key Features:
- Instruction-based execution model for neural network inference
- Support for common neural network operations (dot product, activations, attention, etc.)
- JSON serialization/deserialization for model configuration
- Built-in model validation
- Memory-efficient unified buffer architecture
Quick Start
Simple Neural Network
use ;
// Define a simple single-layer neural network
// Input: 2 features -> Output: 1 value
let model_info = InstructionModelInfo ;
let model = new?;
// Run inference
let input = vec!;
let output = model.predict?;
println!;
// Or get a single output value directly
let result = model.predict_single?;
Multi-Layer Neural Network
use ;
// 2 inputs -> 2 hidden (ReLU) -> 1 output (Sigmoid)
let model_info = InstructionModelInfo ;
let model = new?;
let result = model.predict_single?;
Loading from JSON
Models can be defined in JSON format and loaded at runtime:
use ;
let json_config = r#"
{
"features": ["feature1", "feature2"],
"buffer_sizes": [2, 2, 1],
"instructions": [
{
"type": "DOT",
"input": 0,
"output": 1,
"weights": 0,
"activation": "RELU"
},
{
"type": "DOT",
"input": 1,
"output": 2,
"weights": 1,
"activation": "SIGMOID"
}
],
"weights": [
[[2.0, 0.5], [-2.0, -0.5]],
[[0.5, -1.0]]
],
"bias": [
[0.25, -0.25],
[2.0]
]
}
"#;
let model_info: InstructionModelInfo = from_str?;
let model = new?;
Logistic Regression
Create a logistic regression model directly from coefficients:
use ;
use HashMap;
let mut coefficients = new;
coefficients.insert;
coefficients.insert;
coefficients.insert; // bias term
let model_info = from_logistic_regression_model?;
let model = new?;
let probability = model.predict_single?;
Using the Builder Pattern
use ;
let model_info = builder
.feature_size
.computation_buffer_sizes
.instructions
.weights
.bias
.build?;
let model = new?;
Supported Operations
Activation Functions
| Activation | Description |
|---|---|
Relu |
f(x) = max(0, x) |
Sigmoid |
f(x) = 1 / (1 + exp(-x)) |
Softmax |
Numerically stable softmax over a buffer |
Tanh |
f(x) = tanh(x) |
Sqrt |
f(x) = sqrt(x) for x > 0, else 0 |
Log |
f(x) = ln(x + 1) for x > 0, else 0 |
Log10 |
f(x) = log10(x + 1) for x > 0, else 0 |
Inverse |
f(x) = 1 - x |
Instruction Types
| Instruction | JSON Type | Description |
|---|---|---|
| Dot Product | DOT |
Matrix multiplication with optional activation |
| Copy | COPY |
Copy buffer contents to another location |
| Copy Masked | COPY_MASKED |
Copy specific indices from a buffer |
| Activation | ACTIVATION |
Apply activation function in-place |
| Element-wise Add | ADD_ELEMENTWISE |
Add parameters element-wise |
| Element-wise Multiply | MUL_ELEMENTWISE |
Multiply by parameters element-wise |
| Buffers Add | ADD_ELEMENTWISE_BUFFERS |
Sum multiple buffers |
| Buffers Multiply | MULTIPLY_ELEMENTWISE_BUFFERS |
Multiply multiple buffers element-wise |
| Reduce Sum | REDUCE_SUM |
Sum all values in a buffer to a single value |
| Attention | ATTENTION |
Attention mechanism (linear + softmax + element-wise) |
| Map Transform | MAP_TRANSFORM |
Lookup and transform using a map |
Advanced Usage
External Buffer Management
For high-performance scenarios, you can manage the computation buffer yourself:
let model = new?;
// Allocate buffer once
let mut buffer = vec!;
// Reuse buffer for multiple predictions
for input in inputs
Model Validation
Include validation data to verify model correctness on creation:
use ValidationData;
let model_info = InstructionModelInfo ;
// Model creation will fail if outputs don't match expected values
let model = new?;
Array Features
Features can specify array sizes using bracket notation:
let model_info = InstructionModelInfo ;
Architecture
The library uses a unified buffer architecture where all computation buffers are laid out contiguously in memory. Instructions read from and write to specific regions of this buffer:
┌─────────────┬─────────────┬─────────────┬─────────────┐
│ Buffer 0 │ Buffer 1 │ Buffer 2 │ Buffer 3 │
│ (Input) │ (Hidden) │ (Hidden) │ (Output) │
└─────────────┴─────────────┴─────────────┴─────────────┘
▲ │ │
└─────────────┴─────────────┘
Instructions operate on
buffer regions by index
License
MIT