Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions crates/fast-mlsirm-py/src/ata_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
//! Rust-owned target-information ATA gain arithmetic exposed through PyO3.
//!
//! Python owns validation, content/exposure orchestration, and deterministic tie
//! breaking. This module owns the result-affecting capped-shortfall gain used to
//! rank eligible candidates. The implementation is allocation-bounded: one
//! output scalar per candidate and no candidate-by-point temporary matrix.

use numpy::{PyReadonlyArray1, PyReadonlyArray2, PyUntypedArrayMethods};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyModule};
use pyo3::wrap_pyfunction;

/// Compute capped-shortfall target-information gains for validated ATA inputs.
fn target_information_gains_core(
information_matrix: &[f64],
n_points: usize,
n_items: usize,
candidates: &[usize],
target_info: &[f64],
accumulated: &[f64],
) -> Result<Vec<f64>, String> {
if n_points == 0 || n_items == 0 {
return Err("information matrix must be non-empty".into());
}
let expected_len = n_points
.checked_mul(n_items)
.ok_or_else(|| "information matrix shape exceeds addressable memory".to_owned())?;
if information_matrix.len() != expected_len {
return Err("information matrix storage does not match its shape".into());
}
if candidates.len() > n_items {
return Err("candidate count cannot exceed the information matrix item count".into());
}
if target_info.len() != n_points || accumulated.len() != n_points {
return Err("target_info and accumulated must match the matrix row count".into());
}
if information_matrix
.iter()
.any(|value| !value.is_finite() || *value < 0.0)
{
return Err("information matrix values must be finite and non-negative".into());
}
if target_info
.iter()
.any(|value| !value.is_finite() || *value < 0.0)
{
return Err("target_info values must be finite and non-negative".into());
}
if accumulated
.iter()
.any(|value| !value.is_finite() || *value < 0.0)
{
return Err("accumulated information must be finite and non-negative".into());
}

let mut gains = Vec::new();
gains
.try_reserve_exact(candidates.len())
.map_err(|_| "candidate output allocation exceeds available memory".to_owned())?;
for &candidate in candidates {
if candidate >= n_items {
return Err("candidate item index is out of range".into());
}
let mut gain = 0.0;
for point in 0..n_points {
let remaining = (target_info[point] - accumulated[point]).max(0.0);
let item_information = information_matrix[point * n_items + candidate];
gain += item_information.min(remaining);
}
gains.push(gain);
}
Ok(gains)
}

#[pyfunction(name = "target_information_gains")]
/// Validate Python inputs and delegate target-information arithmetic to Rust.
fn py_target_information_gains(
information_matrix: &Bound<'_, PyAny>,
candidates: &Bound<'_, PyAny>,
target_info: &Bound<'_, PyAny>,
accumulated: &Bound<'_, PyAny>,
) -> PyResult<Vec<f64>> {
let information_matrix: PyReadonlyArray2<'_, f64> =
information_matrix.extract().map_err(|_| {
PyValueError::new_err(
"information_matrix must be a 2-D float64 NumPy array",
)
})?;
let shape = information_matrix.shape();
let n_points = shape[0];
let n_items = shape[1];
if n_points == 0 || n_items == 0 {
return Err(PyValueError::new_err(
"information matrix must be non-empty",
));
}
let expected_len = n_points.checked_mul(n_items).ok_or_else(|| {
PyValueError::new_err("information matrix shape exceeds addressable memory")
})?;
if information_matrix.len() != expected_len {
return Err(PyValueError::new_err(
"information matrix storage does not match its shape",
));
}

let candidates: PyReadonlyArray1<'_, i64> = candidates.extract().map_err(|_| {
PyValueError::new_err("candidates must be a 1-D int64 NumPy array")
})?;
let candidate_count = candidates.shape()[0];
if candidate_count > n_items {
return Err(PyValueError::new_err(
"candidate count cannot exceed the information matrix item count",
));
}

let target_info: PyReadonlyArray1<'_, f64> = target_info.extract().map_err(|_| {
PyValueError::new_err("target_info must be a 1-D float64 NumPy array")
})?;
let accumulated: PyReadonlyArray1<'_, f64> = accumulated.extract().map_err(|_| {
PyValueError::new_err("accumulated must be a 1-D float64 NumPy array")
})?;
if target_info.shape()[0] != n_points || accumulated.shape()[0] != n_points {
return Err(PyValueError::new_err(
"target_info and accumulated must match the matrix row count",
));
}

let candidate_values = candidates.as_slice().map_err(|_| {
PyValueError::new_err("candidates must be a C-contiguous int64 NumPy array")
})?;
let mut candidate_indices = Vec::new();
candidate_indices
.try_reserve_exact(candidate_count)
.map_err(|_| PyValueError::new_err("candidate count exceeds available memory"))?;
for &raw in candidate_values {
if raw < 0 {
return Err(PyValueError::new_err(
"candidate item indices must be non-negative",
));
}
candidate_indices.push(
usize::try_from(raw)
.map_err(|_| PyValueError::new_err("candidate item index is too large"))?,
);
}

let information_values = information_matrix.as_slice().map_err(|_| {
PyValueError::new_err(
"information_matrix must be a C-contiguous float64 NumPy array",
)
})?;
let target_values = target_info.as_slice().map_err(|_| {
PyValueError::new_err("target_info must be a C-contiguous float64 NumPy array")
})?;
let accumulated_values = accumulated.as_slice().map_err(|_| {
PyValueError::new_err("accumulated must be a C-contiguous float64 NumPy array")
})?;

target_information_gains_core(
information_values,
n_points,
n_items,
&candidate_indices,
target_values,
accumulated_values,
)
.map_err(PyValueError::new_err)
}

#[pymodule]
#[pyo3(name = "_ata_core")]
/// Register the secondary ATA module in the package's shared extension binary.
fn fast_mlsirm_ata_core(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(py_target_information_gains, m)?)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn target_gain_matches_hand_calculation_without_pointwise_temporaries() {
let matrix = [1.0, 2.0, 4.0, 0.5, 2.0, 3.0];
let gains = target_information_gains_core(
&matrix,
2,
3,
&[0, 2],
&[4.0, 3.0],
&[1.0, 2.0],
)
.expect("valid inputs");
assert_eq!(gains, vec![1.5, 4.0]);
}

#[test]
fn target_gain_is_zero_after_target_is_already_met() {
let gains = target_information_gains_core(
&[10.0, 20.0],
1,
2,
&[0, 1],
&[2.0],
&[3.0],
)
.expect("valid inputs");
assert_eq!(gains, vec![0.0, 0.0]);
}

#[test]
fn target_gain_rejects_invalid_shapes_values_and_candidates() {
assert!(target_information_gains_core(&[], 0, 1, &[], &[], &[])
.expect_err("zero points")
.contains("non-empty"));
assert!(target_information_gains_core(&[1.0], 1, 2, &[], &[1.0], &[0.0])
.expect_err("storage mismatch")
.contains("storage"));
assert!(target_information_gains_core(&[1.0], 1, 1, &[0, 0], &[1.0], &[0.0])
.expect_err("candidate count")
.contains("candidate count"));
assert!(target_information_gains_core(&[1.0, 2.0], 1, 2, &[], &[], &[0.0])
.expect_err("target mismatch")
.contains("row count"));
assert!(target_information_gains_core(&[f64::NAN], 1, 1, &[], &[1.0], &[0.0])
.expect_err("non-finite matrix")
.contains("information matrix values"));
assert!(target_information_gains_core(&[1.0], 1, 1, &[], &[-1.0], &[0.0])
.expect_err("negative target")
.contains("target_info"));
assert!(target_information_gains_core(&[1.0], 1, 1, &[], &[1.0], &[f64::INFINITY])
.expect_err("non-finite accumulated")
.contains("accumulated"));
assert!(target_information_gains_core(&[1.0], 1, 1, &[1], &[1.0], &[0.0])
.expect_err("candidate out of range")
.contains("out of range"));
}
}
1 change: 1 addition & 0 deletions crates/fast-mlsirm-py/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

include!("lib.rs");

mod ata_bindings;
mod bifactor_bindings;
mod rating_range_bindings;
mod rotation_bindings;
15 changes: 15 additions & 0 deletions docs/changelog.d/805-ata-target-gain-rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ATA target-gain Rust ownership

## Changed

- Moved result-affecting capped-shortfall target-information gains for automated
test assembly from Python/NumPy into a bounded Rust PyO3 kernel.
- Kept Python responsible for validated candidate/content/exposure orchestration
and deterministic tie breaking while the compiled path owns candidate gain
arithmetic without candidate-by-point broadcast temporaries.
- Made the public PyO3 boundary reject wrong-dtype, non-array, non-contiguous,
empty-matrix and overlong candidate-set inputs with stable package-owned
`ValueError` messages before candidate/output allocation.
- Bounded candidate inputs to the item count represented by the information
matrix and converted both candidate and result vectors with fallible reserve.
- Added direct Rust and installed-extension parity/ownership regression evidence.
50 changes: 50 additions & 0 deletions python/fast_mlsirm/_ata_core_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Load the ATA PyO3 module from the package's existing shared library.

The Rust extension exports both ``PyInit__core`` and ``PyInit__ata_core``.
Python normally discovers one module per filename, so this loader initializes
the second symbol from the already installed binary. No source rewriting,
dynamic compilation, network access, or filesystem mutation occurs at runtime.
"""

from __future__ import annotations

from importlib.machinery import ExtensionFileLoader
from importlib.util import module_from_spec, spec_from_loader
from types import ModuleType
import sys


_MODULE_NAME = "fast_mlsirm._ata_core"


def ata_core() -> ModuleType:
"""Return the cached secondary Rust ATA extension module.

Raises
------
ImportError
If the installed wheel predates the ATA entrypoint or the extension
loader cannot initialize ``PyInit__ata_core``.
"""

cached = sys.modules.get(_MODULE_NAME)
if cached is not None:
return cached

from . import _core

binary_path = getattr(_core, "__file__", None)
if not binary_path:
raise ImportError("fast_mlsirm._core does not expose an extension path")
loader = ExtensionFileLoader(_MODULE_NAME, binary_path)
spec = spec_from_loader(_MODULE_NAME, loader)
if spec is None:
raise ImportError("could not create the fast_mlsirm ATA extension spec")
module = module_from_spec(spec)
sys.modules[_MODULE_NAME] = module
try:
loader.exec_module(module)
except BaseException:
sys.modules.pop(_MODULE_NAME, None)
raise
return module
7 changes: 5 additions & 2 deletions python/fast_mlsirm/ata.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import numpy as np

from ._ata_core_loader import ata_core
from .test_design import item_information
from .types import MLSIRMParams

Expand Down Expand Up @@ -340,6 +341,7 @@ def assemble_to_target(
selected: list[int] = []
counts: dict[str, int] = {}
accum = np.zeros(n_points, dtype=np.float64)
core = ata_core()
Comment thread
seonghobae marked this conversation as resolved.
Resolved

for _ in range(length):
# Eligibility mask independent of the current partial selection.
Expand Down Expand Up @@ -373,8 +375,9 @@ def assemble_to_target(
raise ValueError("could not assemble a form that satisfies the constraints")

cand = np.asarray(candidates, dtype=np.int64)
gain = np.array(
[float(np.sum(np.minimum(target, accum + matrix[:, i]) - np.minimum(target, accum))) for i in cand]
gain = np.asarray(
core.target_information_gains(matrix, cand, target, accum),
dtype=np.float64,
)
best_gain = float(np.max(gain))
tied = cand[gain >= best_gain - _TIE_EPS]
Expand Down
Loading
Loading