pmmlruntime 0.1.0

PMML 4.4 runtime — session-based execution, Arrow batch, hardened XML
Documentation
//! SIMD batch scoring — 4-wide `f64x4` via the optional `wide` crate.
//!
//! Provides a data-parallel fast path for single-table [`RegressionIr`] scoring.
//! When the `simd` feature is enabled and the target is not `wasm32`, the batch
//! evaluator uses `wide::f64x4` (AVX2 on x86_64, NEON on aarch64) to score 4 rows
//! per iteration. All other configurations fall back to the scalar evaluator
//! without branching in caller code.
//!
//! # What belongs here
//!
//! - [`evaluate_regression_batch_simd`] — entry point; dispatches to SIMD or scalar,
//!   handles multi-table classification fallback and categorical fixup.
//! - [`evaluate_regression_batch_scalar`] — scalar helper exposed for testing and for the
//!   fallback implementation.
//!
//! # Feature flags
//!
//! - `simd` (via `wide`): enables the `f64x4` fast path. Without it, or on `wasm32`,
//!   [`evaluate_regression_batch_simd`] is an alias for the scalar loop.
//!
//! # Concurrency
//!
//! Both functions are pure (`&RegressionIr` + `&[&[Value]] → Vec<Value>`) and `Send`.
//! No shared mutable state is accessed.
//!
//! # Performance
//!
//! SIMD processes numeric predictors for 4 rows together: loads 4 `f64` lanes,
//! applies `exponent` (`powi` for 2/0/general), multiplies by `coefficient`, and
//! accumulates into a `f64x4` sum seeded with `intercept`. Remainder `batch.len() % 4` rows
//! are handled scalar. Categorical predictors are applied per lane via scalar fixup.
//! Single-row latency remains 402 ns; batched throughput is ~4× for numeric-only tables.

use crate::base::Value;
use crate::ir::RegressionIr;

#[cfg(all(feature = "simd", not(target_arch = "wasm32")))]
use wide::f64x4;

/// Evaluate a batch of rows for a single-table [`RegressionIr`] using `f64x4` SIMD when available.
///
/// Scores `batch_values.len()` rows where each `&[Value]` is a dense field array
/// indexed by [`FieldId`](crate::base::FieldId). For a single [`RegressionTableIr`](crate::ir::RegressionTableIr)
/// with no categorical predictors the numeric loop is fully vectorized; with categorical
/// predictors a scalar per-lane fixup is applied after the SIMD accumulation.
///
/// Multi-table regression (classification with multiple `targetCategory` tables) is not
/// vectorized and falls back to per-row scalar scoring (including `softmax` / `logit` normalization).
///
/// # Parameters
///
/// - `reg`: The regression model. Only `reg.regression_tables.len() == 1` takes the SIMD fast path.
/// - `batch_values`: Slice of row slices, each `&[Value]` length at least `max_field_id + 1`. Missing
///   numeric fields contribute `0.0` in the SIMD lane (matching scalar's skip behavior for missing).
///
/// # Returns
///
/// `Vec<Value>` of length `batch_values.len()`, each `Value::Continuous(normalized_score)`. `Missing` rows
/// produce a value derived from `intercept` plus `missing → 0` contributions, matching the scalar evaluator.
///
/// # Errors
///
/// Never returns `Err`; per-row missing categorical values are treated as non-matching (no contribution).
///
/// # Panics
///
/// Never panics. All `FieldId` indexing is bounds-checked on the remainder path; SIMD loads use
/// guarded matches to `0.0`.
///
/// # Concurrency
///
/// Pure and `Send`; no interior mutability. `Sync` is not required because `&RegressionIr` is shared immutably.
///
/// # Feature flags
///
/// - `simd`: when enabled and `not(target_arch = "wasm32")`, the `f64x4` path is compiled.
///   Without it, this function is a scalar alias (no code-size / portability cost). The `wide` crate
///   is optional.
///
/// # Performance
///
/// `O(batch_len * numeric_predictors)` with a 4× lane factor for numeric loops. Remainder handling is scalar and
/// branchless. Mirrors `evaluate_regression` per-row semantics.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, Value};
/// use pmmlruntime::ir::*;
///
/// let f0 = FieldId(0);
/// let reg = RegressionIr {
///     function_name: "regression".into(),
///     mining_schema: MiningSchemaIr { active_fields: vec![f0], target_field: None, field_metas: vec![], missing_value_replacement: None },
///     regression_tables: vec![RegressionTableIr { intercept: 1.0, target_category: None, numeric_predictors: vec![NumericPredictorIr { field: f0, coefficient: 2.0, exponent: 1 }], categorical_predictors: vec![] }],
///     normalization_method: RegressionNormalizationMethod::None,
///     targets: vec![], output: vec![],
/// };
/// let rows: Vec<Vec<Value>> = (0..4).map(|i| vec![Value::Continuous(i as f64)]).collect();
/// let refs: Vec<&[Value]> = rows.iter().map(|r| r.as_slice()).collect();
/// let out = pmmlruntime::engine::simd::evaluate_regression_batch_simd(&reg, &refs);
/// assert_eq!(out, vec![Value::Continuous(1.0), Value::Continuous(3.0), Value::Continuous(5.0), Value::Continuous(7.0)]);
/// ```
#[cfg(all(feature = "simd", not(target_arch = "wasm32")))]
pub fn evaluate_regression_batch_simd(reg: &RegressionIr, batch_values: &[&[Value]]) -> Vec<Value> {
    if reg.regression_tables.len() != 1 {
        // Multi-table classification: fall back to scalar per row (softmax etc. not vectorized yet)
        return batch_values
            .iter()
            .map(|vals| crate::engine::models::regression::evaluate_regression(reg, vals))
            .collect();
    }
    let tbl = &reg.regression_tables[0];
    // Fast path: only numeric predictors, no categorical — pure SIMD
    let has_categorical = !tbl.categorical_predictors.is_empty();
    let mut out = Vec::with_capacity(batch_values.len());
    let mut i = 0;
    let n = batch_values.len();
    // Process 4-wide
    while i + 4 <= n {
        let mut sums = f64x4::splat(tbl.intercept);
        for np in &tbl.numeric_predictors {
            let fid = np.field.as_usize();
            // Load 4 rows' field values; missing -> 0.0 (contributes 0)
            let v0 = match batch_values[i][fid] {
                Value::Continuous(f) => f,
                _ => 0.0,
            };
            let v1 = match batch_values[i + 1][fid] {
                Value::Continuous(f) => f,
                _ => 0.0,
            };
            let v2 = match batch_values[i + 2][fid] {
                Value::Continuous(f) => f,
                _ => 0.0,
            };
            let v3 = match batch_values[i + 3][fid] {
                Value::Continuous(f) => f,
                _ => 0.0,
            };
            let mut vals = f64x4::new([v0, v1, v2, v3]);
            // Handle exponent
            if np.exponent != 1 {
                if np.exponent == 2 {
                    vals = vals * vals;
                } else if np.exponent == 0 {
                    vals = f64x4::splat(1.0);
                } else {
                    // General powi per lane (scalar fallback inside SIMD)
                    let arr = vals.to_array();
                    let powered = [
                        arr[0].powi(np.exponent),
                        arr[1].powi(np.exponent),
                        arr[2].powi(np.exponent),
                        arr[3].powi(np.exponent),
                    ];
                    vals = f64x4::new(powered);
                }
            }
            let coeff = f64x4::splat(np.coefficient);
            sums += coeff * vals;
        }
        // For categorical, scalar fixup per lane (rare for simd batch)
        if has_categorical {
            // Fall back to scalar per row for categorical part, add to sums
            let sums_arr = sums.to_array();
            for lane in 0..4 {
                let mut lane_sum = sums_arr[lane];
                let vals = batch_values[i + lane];
                for cp in &tbl.categorical_predictors {
                    let idx = cp.field.as_usize();
                    if let Value::Discrete(sid) = vals[idx] {
                        if sid == cp.value {
                            lane_sum += cp.coefficient;
                        }
                    }
                }
                let final_val = crate::engine::models::regression::apply_normalization(
                    lane_sum,
                    reg.normalization_method,
                );
                out.push(Value::Continuous(final_val));
            }
        } else {
            let sums_arr = sums.to_array();
            for lane in 0..4 {
                let lane_sum = sums_arr[lane];
                let final_val = crate::engine::models::regression::apply_normalization(
                    lane_sum,
                    reg.normalization_method,
                );
                out.push(Value::Continuous(final_val));
            }
        }
        i += 4;
    }
    // Remainder scalar
    while i < n {
        out.push(crate::engine::models::regression::evaluate_regression(
            reg,
            batch_values[i],
        ));
        i += 1;
    }
    out
}

/// Scalar fallback for [`evaluate_regression_batch_simd`] when `simd` is disabled or on `wasm32`.
///
/// Scoring is per-row via [`crate::engine::models::regression::evaluate_regression`] with identical
/// probability / normalization handling. Provided explicitly so callers can force scalar
/// execution for testing or for `wasm32` where SIMD is unavailable.
///
/// See [`evaluate_regression_batch_simd`] for parameters, return, and examples.
#[cfg(any(not(feature = "simd"), target_arch = "wasm32"))]
pub fn evaluate_regression_batch_simd(reg: &RegressionIr, batch_values: &[&[Value]]) -> Vec<Value> {
    // Fallback scalar
    batch_values
        .iter()
        .map(|vals| crate::engine::models::regression::evaluate_regression(reg, vals))
        .collect()
}

/// Evaluate a batch via the scalar path (always scalar, even when `simd` is enabled).
///
/// Identical to the scalar fallback of [`evaluate_regression_batch_simd`] but unconditional.
/// Exposed for testing and for benchmarks that compare SIMD against scalar.
///
/// # Parameters
///
/// Same as [`evaluate_regression_batch_simd`]: `reg` and `batch_values`.
///
/// # Returns
///
/// `Vec<Value>` length `batch_values.len()`; each entry is the regression score for that row.
///
/// # Panics
///
/// Never panics.
///
/// # Performance
///
/// `O(batch_len * predictors)` scalar; no SIMD.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, Value};
/// use pmmlruntime::ir::*;
///
/// let f0 = FieldId(0);
/// let reg = RegressionIr {
///     function_name: "regression".into(),
///     mining_schema: MiningSchemaIr { active_fields: vec![f0], target_field: None, field_metas: vec![], missing_value_replacement: None },
///     regression_tables: vec![RegressionTableIr { intercept: 0.0, target_category: None, numeric_predictors: vec![NumericPredictorIr { field: f0, coefficient: 1.0, exponent: 1 }], categorical_predictors: vec![] }],
///     normalization_method: RegressionNormalizationMethod::None, targets: vec![], output: vec![],
/// };
/// let rows = vec![vec![Value::Continuous(2.0)], vec![Value::Continuous(3.0)]];
/// let refs: Vec<&[Value]> = rows.iter().map(|r| r.as_slice()).collect();
/// let out = pmmlruntime::engine::simd::evaluate_regression_batch_scalar(&reg, &refs);
/// assert_eq!(out, vec![Value::Continuous(2.0), Value::Continuous(3.0)]);
/// ```
pub fn evaluate_regression_batch_scalar(
    reg: &RegressionIr,
    batch_values: &[&[Value]],
) -> Vec<Value> {
    batch_values
        .iter()
        .map(|vals| crate::engine::models::regression::evaluate_regression(reg, vals))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base::{FieldId, Value};
    use crate::ir::*;

    fn make_reg() -> RegressionIr {
        let f0 = FieldId(0);
        RegressionIr {
            function_name: "regression".into(),
            mining_schema: MiningSchemaIr {
                active_fields: vec![f0],
                target_field: None,
                field_metas: vec![],
                missing_value_replacement: None,
            },
            regression_tables: vec![RegressionTableIr {
                intercept: 1.0,
                target_category: None,
                numeric_predictors: vec![NumericPredictorIr {
                    field: f0,
                    coefficient: 2.0,
                    exponent: 1,
                }],
                categorical_predictors: vec![],
            }],
            normalization_method: RegressionNormalizationMethod::None,
            targets: vec![],
            output: vec![],
        }
    }

    #[test]
    fn simd_regression_batch() {
        let reg = make_reg();
        let rows: Vec<Vec<Value>> = (0..8).map(|i| vec![Value::Continuous(i as f64)]).collect();
        let refs: Vec<&[Value]> = rows.iter().map(|r| r.as_slice()).collect();
        let simd_out = evaluate_regression_batch_simd(&reg, &refs);
        let scalar_out = evaluate_regression_batch_scalar(&reg, &refs);
        assert_eq!(simd_out.len(), 8);
        for (a, b) in simd_out.iter().zip(scalar_out.iter()) {
            assert_eq!(a, b);
        }
        // Check values: 1 + 2*x
        assert_eq!(simd_out[0], Value::Continuous(1.0));
        assert_eq!(simd_out[1], Value::Continuous(3.0));
        assert_eq!(simd_out[7], Value::Continuous(15.0));
    }

    #[test]
    fn simd_vs_scalar_with_missing() {
        let reg = make_reg();
        let rows = vec![
            vec![Value::Continuous(2.0)],
            vec![Value::Missing],
            vec![Value::Continuous(3.0)],
            vec![Value::Continuous(4.0)],
        ];
        let refs: Vec<&[Value]> = rows.iter().map(|r| r.as_slice()).collect();
        let simd_out = evaluate_regression_batch_simd(&reg, &refs);
        let scalar_out = evaluate_regression_batch_scalar(&reg, &refs);
        assert_eq!(simd_out, scalar_out);
    }
}