use crate::activation::relu_vec;
use crate::error::ZkmlError;
use crate::fixed_point::FixedPoint;
use crate::models::{DecisionTree, DenseLayer, LogisticRegression, Model, TinyMLP, TreeNode};
#[cfg(feature = "std")]
use alloc::format;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub fn run_inference(model: &Model, inputs: &[FixedPoint]) -> FixedPoint {
match model {
Model::DecisionTree(tree) => infer_decision_tree(tree, inputs),
Model::LogisticRegression(lr) => infer_logistic_regression(lr, inputs),
Model::TinyMLP(mlp) => infer_tiny_mlp(mlp, inputs),
}
}
fn infer_decision_tree(tree: &DecisionTree, inputs: &[FixedPoint]) -> FixedPoint {
try_infer_decision_tree(tree, inputs).expect("decision tree inference exceeded iteration limit")
}
fn try_infer_decision_tree(
tree: &DecisionTree,
inputs: &[FixedPoint],
) -> Result<FixedPoint, ZkmlError> {
if inputs.len() != tree.num_features {
return Err(ZkmlError::FeatureCountMismatch {
expected: tree.num_features,
got: inputs.len(),
});
}
const MAX_ITERATIONS: usize = 10_000;
let mut node_idx = 0;
for _iteration in 0..MAX_ITERATIONS {
match &tree.nodes[node_idx] {
TreeNode::Split {
feature_index,
threshold,
left,
right,
} => {
if inputs[*feature_index].value <= threshold.value {
node_idx = *left;
} else {
node_idx = *right;
}
}
TreeNode::Leaf { value } => return Ok(*value),
}
}
Err(ZkmlError::InvalidModel({
#[cfg(feature = "std")]
{
format!(
"decision tree traversal exceeded maximum iterations ({MAX_ITERATIONS}) - possible cycle"
)
}
#[cfg(not(feature = "std"))]
{
"decision tree traversal exceeded maximum iterations - possible cycle".into()
}
}))
}
fn try_infer_logistic_regression(
lr: &LogisticRegression,
inputs: &[FixedPoint],
) -> Result<FixedPoint, ZkmlError> {
if inputs.is_empty() {
return Err(ZkmlError::FeatureCountMismatch {
expected: lr.weights.len(),
got: 0,
});
}
if inputs.len() != lr.weights.len() {
return Err(ZkmlError::FeatureCountMismatch {
expected: lr.weights.len(),
got: inputs.len(),
});
}
let scale = inputs[0].scale;
if inputs.iter().any(|x| x.scale != scale) {
return Err(ZkmlError::QuantizationError(
"inputs have non-uniform fixed-point scale".to_string(),
));
}
let mut acc = lr.bias;
for (w, x) in lr.weights.iter().zip(inputs.iter()) {
let product: FixedPoint = w.checked_mul(*x).ok_or(ZkmlError::ArithmeticOverflow)?;
acc = acc
.checked_add(product)
.ok_or(ZkmlError::ArithmeticOverflow)?;
}
Ok(FixedPoint::from_raw(acc.value, scale))
}
fn infer_logistic_regression(lr: &LogisticRegression, inputs: &[FixedPoint]) -> FixedPoint {
try_infer_logistic_regression(lr, inputs).expect("Logistic regression inference overflow")
}
#[allow(clippy::needless_range_loop)]
fn dense_forward(layer: &DenseLayer, inputs: &[FixedPoint]) -> Result<Vec<FixedPoint>, ZkmlError> {
let scale = inputs.first().map(|x| x.scale).unwrap_or(16);
let mut out = Vec::with_capacity(layer.output_size);
for j in 0..layer.output_size {
let mut acc = layer.biases[j];
for (i, x) in inputs.iter().enumerate().take(layer.input_size) {
let w = layer.weights[j * layer.input_size + i];
let product = w.checked_mul(*x).ok_or(ZkmlError::ArithmeticOverflow)?;
acc = acc
.checked_add(product)
.ok_or(ZkmlError::ArithmeticOverflow)?;
}
out.push(FixedPoint::from_raw(acc.value, scale));
}
Ok(out)
}
fn infer_tiny_mlp(mlp: &TinyMLP, inputs: &[FixedPoint]) -> FixedPoint {
try_infer_tiny_mlp(mlp, inputs).expect("TinyMLP inference overflow")
}
fn try_infer_tiny_mlp(mlp: &TinyMLP, inputs: &[FixedPoint]) -> Result<FixedPoint, ZkmlError> {
let mut activations: Vec<FixedPoint> = inputs.to_vec();
let last = mlp.layers.len().saturating_sub(1);
for (idx, layer) in mlp.layers.iter().enumerate() {
let mut out = dense_forward(layer, &activations)?;
if idx != last {
out = relu_vec(&out);
}
activations = out;
}
Ok(activations
.first()
.copied()
.unwrap_or(FixedPoint::from_raw(0, 16)))
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests_mlp {
use super::*;
use crate::models::{DenseLayer, Model, TinyMLP};
fn fp(x: f64) -> FixedPoint {
FixedPoint::quantize(x)
}
#[test]
fn single_layer_identity() {
let layer = DenseLayer {
weights: vec![fp(1.0)],
biases: vec![fp(0.0)],
input_size: 1,
output_size: 1,
};
let model = Model::TinyMLP(TinyMLP {
layers: vec![layer],
});
let out = run_inference(&model, &[fp(0.7)]);
assert!((out.dequantize() - 0.7).abs() < 1e-2);
}
}
pub fn argmax(values: &[FixedPoint]) -> Option<usize> {
let mut best: Option<(usize, i64)> = None;
for (i, v) in values.iter().enumerate() {
if best.is_none_or(|(_, val)| v.value > val) {
best = Some((i, v.value));
}
}
best.map(|(i, _)| i)
}
pub fn binary_decision(score: FixedPoint, threshold: FixedPoint) -> i64 {
if score.value >= threshold.value {
1
} else {
0
}
}
pub fn run_inference_with_decision(model: &Model, inputs: &[FixedPoint]) -> (FixedPoint, i64) {
match model {
Model::DecisionTree(tree) => {
let score = infer_decision_tree(tree, inputs);
(score, 0) }
Model::LogisticRegression(lr) => {
let score = infer_logistic_regression(lr, inputs);
let decision = binary_decision(score, lr.decision_threshold);
(score, decision)
}
Model::TinyMLP(mlp) => {
let logits =
try_infer_tiny_mlp_all_outputs(mlp, inputs).expect("TinyMLP inference overflow");
let score = logits
.first()
.copied()
.unwrap_or(FixedPoint::from_raw(0, 16));
let decision = argmax(&logits).map(|i| i as i64).unwrap_or(0);
(score, decision)
}
}
}
fn try_infer_tiny_mlp_all_outputs(
mlp: &TinyMLP,
inputs: &[FixedPoint],
) -> Result<Vec<FixedPoint>, ZkmlError> {
let mut activations: Vec<FixedPoint> = inputs.to_vec();
let last = mlp.layers.len().saturating_sub(1);
for (idx, layer) in mlp.layers.iter().enumerate() {
let mut out = dense_forward(layer, &activations)?;
if idx != last {
out = relu_vec(&out);
}
activations = out;
}
Ok(activations)
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests_argmax {
use super::*;
#[test]
fn argmax_picks_highest_logit() {
let logits = vec![
FixedPoint::quantize(0.1),
FixedPoint::quantize(0.9),
FixedPoint::quantize(0.4),
];
assert_eq!(argmax(&logits), Some(1));
}
#[test]
fn argmax_breaks_ties_low() {
let logits = vec![
FixedPoint::quantize(1.0),
FixedPoint::quantize(1.0),
FixedPoint::quantize(0.5),
];
assert_eq!(argmax(&logits), Some(0));
}
}
#[cfg(test)]
mod tests_binary_decision {
use super::*;
#[test]
fn binary_decision_above_threshold() {
let score = FixedPoint::quantize(0.5);
let threshold = FixedPoint::quantize(0.0);
assert_eq!(binary_decision(score, threshold), 1);
}
#[test]
fn binary_decision_below_threshold() {
let score = FixedPoint::quantize(-0.5);
let threshold = FixedPoint::quantize(0.0);
assert_eq!(binary_decision(score, threshold), 0);
}
#[test]
fn binary_decision_at_threshold() {
let score = FixedPoint::quantize(0.0);
let threshold = FixedPoint::quantize(0.0);
assert_eq!(binary_decision(score, threshold), 1); }
#[test]
fn binary_decision_just_below_threshold() {
let score = FixedPoint::quantize(-0.001);
let threshold = FixedPoint::quantize(0.0);
assert_eq!(binary_decision(score, threshold), 0);
}
#[test]
fn binary_decision_just_above_threshold() {
let score = FixedPoint::quantize(0.001);
let threshold = FixedPoint::quantize(0.0);
assert_eq!(binary_decision(score, threshold), 1);
}
}
#[cfg(test)]
mod tests_inference_with_decision {
use super::*;
use crate::models::{DenseLayer, LogisticRegression, Model, TinyMLP};
fn fp(x: f64) -> FixedPoint {
FixedPoint::quantize(x)
}
#[test]
fn logistic_regression_with_threshold_positive() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![fp(1.0)],
bias: fp(0.0),
decision_threshold: fp(0.0),
});
let inputs = vec![fp(0.5)];
let (score, decision) = run_inference_with_decision(&model, &inputs);
assert!((score.dequantize() - 0.5).abs() < 1e-3);
assert_eq!(decision, 1);
}
#[test]
fn logistic_regression_with_threshold_negative() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![fp(1.0)],
bias: fp(0.0),
decision_threshold: fp(1.0),
});
let inputs = vec![fp(0.5)];
let (score, decision) = run_inference_with_decision(&model, &inputs);
assert!((score.dequantize() - 0.5).abs() < 1e-3);
assert_eq!(decision, 0); }
#[test]
fn mlp_argmax_decision() {
let layer = DenseLayer {
weights: vec![fp(1.0), fp(0.1), fp(0.1), fp(0.1)], biases: vec![fp(0.0), fp(0.0)],
input_size: 2,
output_size: 2,
};
let model = Model::TinyMLP(TinyMLP {
layers: vec![layer],
});
let inputs = vec![fp(1.0), fp(0.0)];
let (score, decision) = run_inference_with_decision(&model, &inputs);
assert!((score.dequantize() - 1.0).abs() < 1e-3);
assert_eq!(decision, 0);
}
#[test]
fn mlp_argmax_golden_vector_3class() {
let layer = DenseLayer {
weights: vec![
fp(0.5),
fp(-0.2),
fp(0.1), fp(-0.3),
fp(0.8),
fp(0.2), fp(0.1),
fp(0.1),
fp(-0.5), ],
biases: vec![fp(0.1), fp(-0.1), fp(0.0)],
input_size: 3,
output_size: 3,
};
let model = Model::TinyMLP(TinyMLP {
layers: vec![layer],
});
let inputs1 = vec![fp(0.0), fp(1.0), fp(0.0)];
let (_score1, decision1) = run_inference_with_decision(&model, &inputs1);
assert_eq!(decision1, 1);
let inputs2 = vec![fp(1.0), fp(0.0), fp(0.0)];
let (_score2, decision2) = run_inference_with_decision(&model, &inputs2);
assert_eq!(decision2, 0);
let inputs3 = vec![fp(0.0), fp(0.0), fp(-1.0)];
let (_score3, decision3) = run_inference_with_decision(&model, &inputs3);
assert_eq!(decision3, 2);
}
#[test]
fn decision_tree_returns_zero_decision() {
use crate::models::{DecisionTree, TreeNode};
let tree = DecisionTree {
num_features: 1,
nodes: vec![TreeNode::Leaf { value: fp(42.0) }],
};
let model = Model::DecisionTree(tree);
let inputs = vec![fp(0.5)];
let (score, decision) = run_inference_with_decision(&model, &inputs);
assert!((score.dequantize() - 42.0).abs() < 1e-3);
assert_eq!(decision, 0); }
}
pub fn run_batch(model: &Model, rows: &[Vec<FixedPoint>]) -> Vec<FixedPoint> {
rows.iter().map(|row| run_inference(model, row)).collect()
}
pub fn try_run_batch(
model: &Model,
rows: &[Vec<FixedPoint>],
) -> Vec<Result<FixedPoint, ZkmlError>> {
rows.iter()
.map(|row| try_run_inference(model, row))
.collect()
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests_batch {
use super::*;
use crate::models::{LogisticRegression, Model};
fn one_feature_lr() -> Model {
Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::quantize(1.0)],
bias: FixedPoint::quantize(0.0),
decision_threshold: FixedPoint::quantize(0.0),
})
}
#[test]
fn batch_matches_single() {
let model = one_feature_lr();
let rows = vec![
vec![FixedPoint::quantize(0.5)],
vec![FixedPoint::quantize(0.9)],
];
let batched = run_batch(&model, &rows);
for (row, out) in rows.iter().zip(batched.iter()) {
assert_eq!(run_inference(&model, row).value, out.value);
}
}
#[test]
fn try_batch_mixed_feature_count_keeps_valid_rows() {
let model = one_feature_lr();
let good_a = vec![FixedPoint::quantize(0.5)];
let bad = vec![FixedPoint::quantize(0.1), FixedPoint::quantize(0.2)];
let good_b = vec![FixedPoint::quantize(0.9)];
let rows = vec![good_a.clone(), bad, good_b.clone()];
let panicked = std::panic::catch_unwind(|| try_run_batch(&model, &rows));
assert!(
panicked.is_ok(),
"try_run_batch must not panic on a malformed row"
);
let results = panicked.unwrap();
assert_eq!(results.len(), 3);
assert_eq!(
results[0].as_ref().map(|o| o.value),
Ok(run_inference(&model, &good_a).value)
);
assert_eq!(
results[1],
Err(ZkmlError::FeatureCountMismatch {
expected: 1,
got: 2,
})
);
assert_eq!(
results[2].as_ref().map(|o| o.value),
Ok(run_inference(&model, &good_b).value)
);
}
#[test]
fn try_batch_overflow_row_does_not_drop_neighbors() {
let big = FixedPoint::from_raw(i64::MAX / 2, 16);
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![big],
bias: FixedPoint::quantize(0.0),
decision_threshold: FixedPoint::quantize(0.0),
});
let good = vec![FixedPoint::quantize(0.5)];
let overflow = vec![big];
let rows = vec![good.clone(), overflow, good.clone()];
let results = try_run_batch(&model, &rows);
assert_eq!(results.len(), 3);
assert_eq!(
results[0].as_ref().map(|o| o.value),
Ok(run_inference(&model, &good).value)
);
assert_eq!(results[1], Err(ZkmlError::ArithmeticOverflow));
assert_eq!(
results[2].as_ref().map(|o| o.value),
Ok(run_inference(&model, &good).value)
);
}
#[test]
fn try_batch_empty_rows_returns_empty() {
let model = one_feature_lr();
let results = try_run_batch(&model, &[]);
assert!(results.is_empty());
}
}
pub fn try_run_inference(model: &Model, inputs: &[FixedPoint]) -> Result<FixedPoint, ZkmlError> {
if inputs.is_empty() {
return Err(ZkmlError::FeatureCountMismatch {
expected: model.num_features(),
got: 0,
});
}
let expected = model.num_features();
if expected != 0 && inputs.len() != expected {
return Err(ZkmlError::FeatureCountMismatch {
expected,
got: inputs.len(),
});
}
match model {
Model::LogisticRegression(lr) => try_infer_logistic_regression(lr, inputs),
Model::TinyMLP(mlp) => {
mlp.validate()?;
try_infer_tiny_mlp(mlp, inputs)
}
Model::DecisionTree(tree) => try_infer_decision_tree(tree, inputs),
}
}
pub fn try_run_inference_with_decision(
model: &Model,
inputs: &[FixedPoint],
) -> Result<(FixedPoint, i64), ZkmlError> {
let expected = model.num_features();
if expected != 0 && inputs.len() != expected {
return Err(ZkmlError::FeatureCountMismatch {
expected,
got: inputs.len(),
});
}
match model {
Model::DecisionTree(tree) => {
let score = try_infer_decision_tree(tree, inputs)?;
Ok((score, 0))
}
Model::LogisticRegression(lr) => {
let score = try_infer_logistic_regression(lr, inputs)?;
Ok((score, binary_decision(score, lr.decision_threshold)))
}
Model::TinyMLP(mlp) => {
mlp.validate()?;
let logits = try_infer_tiny_mlp_all_outputs(mlp, inputs)?;
let score = logits
.first()
.copied()
.unwrap_or(FixedPoint::from_raw(0, 16));
let decision = argmax(&logits).map(|i| i as i64).unwrap_or(0);
Ok((score, decision))
}
}
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests_decision_fallible {
use super::*;
use crate::models::{DecisionTree, LogisticRegression, Model, TreeNode};
fn fp(x: f64) -> FixedPoint {
FixedPoint::quantize(x)
}
#[test]
fn matches_the_panicking_version() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![fp(0.5), fp(-0.25)],
bias: fp(0.1),
decision_threshold: fp(0.0),
});
let inputs = vec![fp(1.0), fp(2.0)];
assert_eq!(
try_run_inference_with_decision(&model, &inputs).unwrap(),
run_inference_with_decision(&model, &inputs)
);
}
#[test]
fn wrong_feature_count_is_an_error() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![fp(1.0), fp(1.0)],
bias: fp(0.0),
decision_threshold: fp(0.0),
});
assert_eq!(
try_run_inference_with_decision(&model, &[fp(1.0)]),
Err(ZkmlError::FeatureCountMismatch {
expected: 2,
got: 1
})
);
}
#[test]
fn overflow_is_an_error_instead_of_a_panic() {
let big = FixedPoint::from_raw(i64::MAX / 2, 16);
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![big],
bias: fp(0.0),
decision_threshold: fp(0.0),
});
assert_eq!(
try_run_inference_with_decision(&model, &[big]),
Err(ZkmlError::ArithmeticOverflow)
);
}
#[test]
fn malformed_tree_is_an_error_instead_of_a_panic() {
let model = Model::DecisionTree(DecisionTree {
num_features: 1,
nodes: vec![TreeNode::Split {
feature_index: 0,
threshold: fp(0.5),
left: 0,
right: 0,
}],
});
assert!(try_run_inference_with_decision(&model, &[fp(0.1)]).is_err());
}
#[test]
fn decision_tree_label_is_zero() {
let model = Model::DecisionTree(DecisionTree {
num_features: 1,
nodes: vec![
TreeNode::Split {
feature_index: 0,
threshold: fp(0.5),
left: 1,
right: 2,
},
TreeNode::Leaf { value: fp(0.0) },
TreeNode::Leaf { value: fp(1.0) },
],
});
let (score, label) = try_run_inference_with_decision(&model, &[fp(0.9)]).unwrap();
assert_eq!(label, 0);
assert_eq!(score, fp(1.0));
}
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests_validated {
use super::*;
use crate::models::{LogisticRegression, Model};
#[test]
fn empty_input_is_rejected() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::quantize(1.0)],
bias: FixedPoint::quantize(0.0),
decision_threshold: FixedPoint::quantize(0.0),
});
assert!(try_run_inference(&model, &[]).is_err());
}
#[test]
fn logistic_regression_overflow_boundary_returns_error() {
let big = FixedPoint::from_raw(i64::MAX / 2, 16);
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![big],
bias: FixedPoint::quantize(0.0),
decision_threshold: FixedPoint::quantize(0.0),
});
let inputs = vec![big];
assert_eq!(
try_run_inference(&model, &inputs),
Err(ZkmlError::ArithmeticOverflow)
);
}
#[test]
fn logistic_regression_mixed_scale_returns_error() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(100, 16), FixedPoint::from_raw(100, 16)],
bias: FixedPoint::from_raw(0, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
let inputs = vec![FixedPoint::from_raw(100, 16), FixedPoint::from_raw(100, 8)];
assert!(matches!(
try_run_inference(&model, &inputs),
Err(ZkmlError::QuantizationError(_))
));
}
#[test]
fn logistic_regression_in_range_parity() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::quantize(2.5), FixedPoint::quantize(-1.5)],
bias: FixedPoint::quantize(0.5),
decision_threshold: FixedPoint::quantize(0.0),
});
let inputs = vec![FixedPoint::quantize(4.0), FixedPoint::quantize(2.0)];
let res = try_run_inference(&model, &inputs).unwrap();
assert!((res.dequantize() - 7.5).abs() < 1e-3);
assert_eq!(run_inference(&model, &inputs).value, res.value);
}
}