use super::family::*;
use super::hessian_paths::*;
use super::*;
use gam_linalg::matrix::DesignMatrix;
use gam_problem::{InverseLink, StandardLink};
use ndarray::Array1;
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::{Arc, Mutex};
struct ThreadCountingAllocator;
thread_local! {
static TRACK_THIS_THREAD: Cell<bool> = const { Cell::new(false) };
static THREAD_ALLOCATION_CALLS: Cell<u64> = const { Cell::new(0) };
static THREAD_ALLOCATED_BYTES: Cell<u64> = const { Cell::new(0) };
}
fn note_allocation(size: usize) {
if !TRACK_THIS_THREAD.try_with(Cell::get).unwrap_or(false) {
return;
}
THREAD_ALLOCATION_CALLS
.try_with(|c| c.set(c.get() + 1))
.unwrap_or(());
THREAD_ALLOCATED_BYTES
.try_with(|b| b.set(b.get() + size as u64))
.unwrap_or(());
}
unsafe impl GlobalAlloc for ThreadCountingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc(layout) };
if !ptr.is_null() {
note_allocation(layout.size());
}
ptr
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc_zeroed(layout) };
if !ptr.is_null() {
note_allocation(layout.size());
}
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let new_ptr = unsafe { System.realloc(ptr, layout, new_size) };
if !new_ptr.is_null() {
note_allocation(new_size);
}
new_ptr
}
}
#[global_allocator]
static GLOBAL_ALLOCATOR: ThreadCountingAllocator = ThreadCountingAllocator;
fn begin_thread_allocation_measurement() {
TRACK_THIS_THREAD.with(|t| t.set(false));
THREAD_ALLOCATION_CALLS.with(|c| c.set(0));
THREAD_ALLOCATED_BYTES.with(|b| b.set(0));
TRACK_THIS_THREAD.with(|t| t.set(true));
}
fn end_thread_allocation_measurement() -> (u64, u64) {
TRACK_THIS_THREAD.with(|t| t.set(false));
(
THREAD_ALLOCATION_CALLS.with(|c| c.get()),
THREAD_ALLOCATED_BYTES.with(|b| b.get()),
)
}
const GRID_NODES: usize = 65;
struct MFixture {
family: BernoulliMarginalSlopeFamily,
primary: PrimarySlices,
}
fn mgrid() -> EmpiricalZGrid {
let nodes: Vec<f64> = (0..GRID_NODES)
.map(|i| -2.6 + 5.2 * (i as f64) / ((GRID_NODES - 1) as f64))
.collect();
let raw: Vec<f64> = nodes.iter().map(|z| (-0.5 * z * z).exp()).collect();
let total: f64 = raw.iter().sum();
let weights: Vec<f64> = raw.iter().map(|w| w / total).collect();
EmpiricalZGrid::new(nodes, weights, "flex_measure_932 grid").expect("valid 65-node grid")
}
fn mruntime() -> DeviationRuntime {
let n_knots = 11usize;
let knots = Array1::from_iter(
(0..n_knots).map(|i| -2.45_f64 + 5.0_f64 * (i as f64) / ((n_knots - 1) as f64)),
);
DeviationRuntime::try_new(knots, 0.0, 3).expect("deviation runtime")
}
fn mfixture(is_score_warp: bool) -> MFixture {
let grid = mgrid();
let runtime = mruntime();
let basis_dim = runtime.basis_dim();
let policy = gam_runtime::resource::ResourcePolicy::default_library();
let dummy = || {
DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(
ndarray::Array2::zeros((1, 1)),
))
};
let family = BernoulliMarginalSlopeFamily {
y: Arc::new(Array1::from_vec(vec![1.0])),
weights: Arc::new(Array1::from_vec(vec![1.0])),
z: Arc::new(Array1::from_vec(vec![0.45])),
latent_measure: LatentMeasureKind::GlobalEmpirical { grid },
gaussian_frailty_sd: None,
base_link: InverseLink::Standard(StandardLink::Probit),
marginal_design: dummy(),
logslope_design: dummy(),
score_warp: if is_score_warp {
Some(runtime.clone())
} else {
None
},
link_dev: if is_score_warp { None } else { Some(runtime) },
policy: policy.clone(),
cell_moment_lru: new_cell_moment_lru_cache(&policy),
cell_moment_cache_stats: new_cell_moment_cache_stats(),
intercept_warm_starts: None,
auto_subsample_phase_counter: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
auto_subsample_last_rho: Arc::new(Mutex::new(None)),
};
let primary = PrimarySlices {
q: 0,
logslope: 1,
h: if is_score_warp {
Some(2..2 + basis_dim)
} else {
None
},
w: if is_score_warp {
None
} else {
Some(2..2 + basis_dim)
},
total: 2 + basis_dim,
};
MFixture { family, primary }
}
struct MPoint {
q: f64,
b: f64,
beta: Array1<f64>,
}
fn mpoint(fx: &MFixture) -> MPoint {
let basis_dim = fx.primary.total - 2;
let beta = Array1::from_shape_fn(basis_dim, |i| {
let center = 0.5 * (basis_dim.saturating_sub(1) as f64);
let radius = center.max(1.0);
0.06 * ((i as f64) - center) / radius
});
MPoint {
q: 0.2,
b: 0.35,
beta,
}
}
fn assert_empirical_branch(fx: &MFixture) {
let grid = fx
.family
.latent_measure
.empirical_grid_for_training_row(0)
.expect("latent measure query")
.expect("GlobalEmpirical fixture must select the empirical-grid branch");
assert_eq!(
grid.pairs().count(),
GRID_NODES,
"fixture grid must be the forced 65-node empirical grid"
);
}
fn measure_branch(is_score_warp: bool) {
let label = if is_score_warp {
"score-warp"
} else {
"link-dev"
};
let fx = mfixture(is_score_warp);
assert_empirical_branch(&fx);
let r = fx.primary.total;
let pt = mpoint(&fx);
let (beta_h, beta_w) = if is_score_warp {
(Some(&pt.beta), None)
} else {
(None, Some(&pt.beta))
};
let (intercept, m_a, _) = fx
.family
.solve_row_intercept_base(0, pt.q, pt.b, beta_h, beta_w, None)
.expect("intercept solve");
let row_ctx = BernoulliMarginalSlopeRowExactContext {
intercept,
m_a,
intercept_fast_path: false,
degree9_cells: None,
};
let grid = fx
.family
.latent_measure
.empirical_grid_for_training_row(0)
.expect("measured grid query")
.expect("forced empirical grid");
let cells = fx
.family
.denested_partition_cells(intercept, pt.b, beta_h, beta_w)
.expect("measured denested cells");
let occupied_cells = cells
.iter()
.filter(|partition_cell| {
grid.nodes
.iter()
.any(|&node| node >= partition_cell.cell.left && node < partition_cell.cell.right)
})
.count();
let mut scratch = BernoulliMarginalSlopeFlexRowScratch::new(r);
let call = |scratch: &mut BernoulliMarginalSlopeFlexRowScratch| -> f64 {
fx.family
.lower_bms_flex_row_order2_from_parts(
0,
&fx.primary,
pt.q,
pt.b,
beta_h,
beta_w,
&row_ctx,
None,
None,
true,
scratch,
)
.expect("production empirical-flex row call")
};
let warm_value = call(&mut scratch);
let warm_value2 = call(&mut scratch);
assert_eq!(
warm_value.to_bits(),
warm_value2.to_bits(),
"{label}: warmed row op must be deterministic"
);
const WARMED_ROWS: u64 = 512;
begin_thread_allocation_measurement();
let mut acc = 0.0f64;
for _ in 0..WARMED_ROWS {
acc += call(&mut scratch);
}
let (warm_calls, warm_bytes) = end_thread_allocation_measurement();
assert!(acc.is_finite());
let calls_per_row = (warm_calls as f64) / (WARMED_ROWS as f64);
let bytes_per_row = (warm_bytes as f64) / (WARMED_ROWS as f64);
let timer = std::time::Instant::now();
let mut tacc = 0.0f64;
for _ in 0..WARMED_ROWS {
tacc += call(&mut scratch);
}
let warm_ns_per_row = (timer.elapsed().as_nanos() as f64) / (WARMED_ROWS as f64);
assert_eq!(
acc.to_bits(),
tacc.to_bits(),
"{label}: timing loop must repeat identical work"
);
begin_thread_allocation_measurement();
let cold_timer = std::time::Instant::now();
let (cold_intercept, cold_m_a, _) = fx
.family
.solve_row_intercept_base(0, pt.q, pt.b, beta_h, beta_w, None)
.expect("cold intercept solve");
let cold_ctx = BernoulliMarginalSlopeRowExactContext {
intercept: cold_intercept,
m_a: cold_m_a,
intercept_fast_path: false,
degree9_cells: None,
};
let mut cold_scratch = BernoulliMarginalSlopeFlexRowScratch::new(r);
let cold_value = fx
.family
.lower_bms_flex_row_order2_from_parts(
0,
&fx.primary,
pt.q,
pt.b,
beta_h,
beta_w,
&cold_ctx,
None,
None,
true,
&mut cold_scratch,
)
.expect("cold production row call");
let cold_ns = cold_timer.elapsed().as_nanos() as f64;
let (cold_calls, cold_bytes) = end_thread_allocation_measurement();
assert_eq!(
cold_value.to_bits(),
warm_value.to_bits(),
"{label}: cold and warmed row ops must agree exactly"
);
eprintln!(
"#932 measure {label}: r={r} grid={GRID_NODES} cells={} occupied={} \
warmed[allocs/row={calls_per_row:.3} bytes/row={bytes_per_row:.1} ns/row={warm_ns_per_row:.0}] \
cold[allocs={cold_calls} bytes={cold_bytes} ns={cold_ns:.0}]",
cells.len(),
occupied_cells,
);
let floor = if is_score_warp { 5.0 } else { 7.0 };
assert!(
calls_per_row <= floor,
"{label}: warmed empirical-flex row op regressed to {calls_per_row:.3} \
allocation calls/row ({bytes_per_row:.1} bytes/row) above the measured \
post-d1a7a0bc6 floor of {floor} — the per-row coefficient buffers \
deleted by d1a7a0bc6 (4 calls, 128·r bytes/row) or new per-row heap \
traffic came back"
);
}
#[test]
fn empirical_flex_warmed_row_allocation_gate_score_warp_932() {
measure_branch(true);
}
#[test]
fn empirical_flex_warmed_row_allocation_gate_link_dev_932() {
measure_branch(false);
}
use super::flex_row_program::BmsFlexRowProgram;
fn tier_knots(n_knots: usize) -> Array1<f64> {
Array1::from_iter(
(0..n_knots).map(|i| -2.45_f64 + 5.0_f64 * (i as f64) / ((n_knots - 1) as f64)),
)
}
fn tier_runtime() -> (DeviationRuntime, usize) {
let mut fallback: Option<(DeviationRuntime, usize)> = None;
for n_knots in 6..=64usize {
let Ok(runtime) = DeviationRuntime::try_new(tier_knots(n_knots), 0.0, 3) else {
continue;
};
let width = 2 + runtime.basis_dim();
if width == 12 {
return (runtime, width);
}
if matches!(width, 8 | 18) && fallback.is_none() {
fallback = Some((runtime, width));
}
}
fallback.expect("some knot count must land the BMS primary width on a fixed-K tier")
}
fn build_fixture_with_runtime(is_score_warp: bool, runtime: DeviationRuntime) -> MFixture {
let grid = mgrid();
let basis_dim = runtime.basis_dim();
let policy = gam_runtime::resource::ResourcePolicy::default_library();
let dummy = || {
DesignMatrix::Dense(gam_linalg::matrix::DenseDesignMatrix::from(
ndarray::Array2::zeros((1, 1)),
))
};
let family = BernoulliMarginalSlopeFamily {
y: Arc::new(Array1::from_vec(vec![1.0])),
weights: Arc::new(Array1::from_vec(vec![1.0])),
z: Arc::new(Array1::from_vec(vec![0.45])),
latent_measure: LatentMeasureKind::GlobalEmpirical { grid },
gaussian_frailty_sd: None,
base_link: InverseLink::Standard(StandardLink::Probit),
marginal_design: dummy(),
logslope_design: dummy(),
score_warp: if is_score_warp {
Some(runtime.clone())
} else {
None
},
link_dev: if is_score_warp { None } else { Some(runtime) },
policy: policy.clone(),
cell_moment_lru: new_cell_moment_lru_cache(&policy),
cell_moment_cache_stats: new_cell_moment_cache_stats(),
intercept_warm_starts: None,
auto_subsample_phase_counter: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
auto_subsample_last_rho: Arc::new(Mutex::new(None)),
};
let primary = PrimarySlices {
q: 0,
logslope: 1,
h: if is_score_warp {
Some(2..2 + basis_dim)
} else {
None
},
w: if is_score_warp {
None
} else {
Some(2..2 + basis_dim)
},
total: 2 + basis_dim,
};
MFixture { family, primary }
}
fn fixed_third_contracted<const K: usize>(
plan: &BmsFlexRowProgram,
point: &[f64; K],
dir: &Array1<f64>,
) -> ndarray::Array2<f64> {
use gam_math::jet_scalar::{FixedRuntimeJet, OneSeed};
let vars: [FixedRuntimeJet<OneSeed<K>, K>; K] = std::array::from_fn(|axis| {
FixedRuntimeJet::from_inner(OneSeed::seed_direction(point[axis], axis, dir[axis]))
});
let contracted = plan
.evaluate(&vars, 3, &())
.expect("fixed-K third contraction of canonical plan")
.into_inner()
.contracted_third();
ndarray::Array2::from_shape_fn((K, K), |(a, b)| contracted[a][b])
}
fn dynamic_third_contracted(
plan: &BmsFlexRowProgram,
point: &[f64],
dir: &Array1<f64>,
r: usize,
) -> ndarray::Array2<f64> {
use gam_math::jet_scalar::{DynamicJetBatchWorkspace, DynamicOneSeedBatch};
let mut workspace = DynamicJetBatchWorkspace::new(1);
workspace.reset(1);
let vars = workspace.alloc_slice_fill_with(r, |axis| {
DynamicOneSeedBatch::seed_directions(point[axis], axis, r, &workspace, |_| dir[axis])
});
let jet = plan
.evaluate(vars, 3, &workspace)
.expect("dynamic third contraction of canonical plan");
ndarray::Array2::from_shape_vec((r, r), jet.contracted_third(0).to_vec())
.expect("dynamic third contraction shape")
}
fn best_measure_ns<F: FnMut(f64) -> f64>(iterations: usize, base: f64, mut evaluate: F) -> f64 {
let mut best = f64::INFINITY;
for _ in 0..5 {
let mut checksum = 0.0_f64;
let started = std::time::Instant::now();
for _ in 0..iterations {
checksum += evaluate(base + checksum * 1e-18);
}
assert!(
checksum.is_finite(),
"BMS-FLEX-CONTRACTED-932 release-measure checksum must stay finite"
);
best = best.min(started.elapsed().as_secs_f64());
}
best * 1e9 / iterations as f64
}
fn measure_third_fourth_branch<const K: usize>(is_score_warp: bool, runtime: DeviationRuntime) {
use super::cell_moment_assembly::{
EmpiricalBmsFourthJetSchedule, EmpiricalBmsThirdJetSchedule,
empirical_bms_fourth_jet_schedule, empirical_bms_third_jet_schedule,
};
let label = if is_score_warp {
"score-warp"
} else {
"link-dev"
};
let fx = build_fixture_with_runtime(is_score_warp, runtime);
assert_empirical_branch(&fx);
let r = fx.primary.total;
assert_eq!(
r, K,
"{label}: tier fixture width {r} != specialization width K={K}"
);
assert_eq!(
empirical_bms_third_jet_schedule(r),
EmpiricalBmsThirdJetSchedule::FixedWidthFromPlan,
"{label}: production must pick the fixed-K third jet at width {r}"
);
assert_eq!(
empirical_bms_fourth_jet_schedule(r),
EmpiricalBmsFourthJetSchedule::RepeatedFixedWidth,
"{label}: production must pick the fixed-K fourth jet at width {r}"
);
let pt = mpoint(&fx);
let (beta_h, beta_w) = if is_score_warp {
(Some(&pt.beta), None)
} else {
(None, Some(&pt.beta))
};
let intercept = fx
.family
.solve_row_intercept_base(0, pt.q, pt.b, beta_h, beta_w, None)
.expect("intercept solve")
.0;
let grid = fx
.family
.latent_measure
.empirical_grid_for_training_row(0)
.expect("latent measure query")
.expect("forced empirical grid");
let plan = fx
.family
.compile_empirical_bms_row_program(
0,
&fx.primary,
pt.q,
pt.b,
beta_h,
beta_w,
intercept,
&grid,
)
.expect("canonical empirical-flex row plan");
let point_vec =
BernoulliMarginalSlopeFamily::intercept_primary_point(pt.q, pt.b, beta_h, beta_w);
let point: [f64; K] = point_vec
.as_slice()
.try_into()
.expect("primary point width matches specialization K");
let dir_u = Array1::from_shape_fn(r, |i| 0.5 + 0.3 * ((i % 3) as f64) - 0.2 * ((i % 2) as f64));
let dir_v = Array1::from_shape_fn(r, |i| {
-0.4 + 0.3 * (((i + 1) % 4) as f64) - 0.1 * ((i % 2) as f64)
});
let pairs: [(&Array1<f64>, &Array1<f64>); 1] = [(&dir_u, &dir_v)];
let fixed_third = fixed_third_contracted::<K>(&plan, &point, &dir_u);
let dynamic_third = dynamic_third_contracted(&plan, &point, &dir_u, r);
let fixed_fourth = BernoulliMarginalSlopeFamily::empirical_fixed_fourth_many_from_plan::<K>(
&plan, &point, &pairs,
)
.expect("fixed-K fourth contraction of canonical plan");
let dynamic_fourth = BernoulliMarginalSlopeFamily::empirical_dynamic_fourth_batch_from_plan(
&plan,
&point,
&pairs,
&fx.primary,
1,
)
.expect("dynamic fourth contraction of canonical plan");
for a in 0..r {
for b in 0..r {
let (f3, d3) = (fixed_third[(a, b)], dynamic_third[(a, b)]);
let band3 = 1e-11 * f3.abs().max(d3.abs()).max(1.0);
assert!(
(f3 - d3).abs() <= band3,
"{label}: third[{a}][{b}] fixed {f3:+.15e} vs dynamic {d3:+.15e}"
);
let (f4, d4) = (fixed_fourth[0][(a, b)], dynamic_fourth[0][(a, b)]);
let band4 = 1e-11 * f4.abs().max(d4.abs()).max(1.0);
assert!(
(f4 - d4).abs() <= band4,
"{label}: fourth[{a}][{b}] fixed {f4:+.15e} vs dynamic {d4:+.15e}"
);
}
}
let iters_third = 800usize;
let iters_fourth = 500usize;
let third_fixed_ns = best_measure_ns(iters_third, point[0], |p0| {
let mut perturbed = point;
perturbed[0] = p0;
let m = fixed_third_contracted::<K>(&plan, &perturbed, &dir_u);
m[(0, 0)] + m[(r - 1, r - 1)]
});
let third_dynamic_ns = best_measure_ns(iters_third, point[0], |p0| {
let mut perturbed = point;
perturbed[0] = p0;
let m = dynamic_third_contracted(&plan, &perturbed, &dir_u, r);
m[(0, 0)] + m[(r - 1, r - 1)]
});
eprintln!(
"BMS-FLEX-CONTRACTED-932 branch={label} width={K} grid={GRID_NODES} order=3 \
production_fixed={third_fixed_ns:.2} ns/row dynamic={third_dynamic_ns:.2} ns/row \
hand_over_production={:.6}",
third_dynamic_ns / third_fixed_ns,
);
let fourth_fixed_ns = best_measure_ns(iters_fourth, point[0], |p0| {
let mut perturbed = point;
perturbed[0] = p0;
let out = BernoulliMarginalSlopeFamily::empirical_fixed_fourth_many_from_plan::<K>(
&plan, &perturbed, &pairs,
)
.expect("fixed-K fourth contraction");
out[0][(0, 0)] + out[0][(r - 1, r - 1)]
});
let fourth_dynamic_ns = best_measure_ns(iters_fourth, point[0], |p0| {
let mut perturbed = point;
perturbed[0] = p0;
let out = BernoulliMarginalSlopeFamily::empirical_dynamic_fourth_batch_from_plan(
&plan,
&perturbed,
&pairs,
&fx.primary,
1,
)
.expect("dynamic fourth contraction");
out[0][(0, 0)] + out[0][(r - 1, r - 1)]
});
eprintln!(
"BMS-FLEX-CONTRACTED-932 branch={label} width={K} grid={GRID_NODES} order=4 \
production_fixed={fourth_fixed_ns:.2} ns/row dynamic={fourth_dynamic_ns:.2} ns/row \
hand_over_production={:.6}",
fourth_dynamic_ns / fourth_fixed_ns,
);
}
#[test]
fn release_measure_bms_empirical_third_fourth_fixed_vs_dynamic_932() {
let (runtime, width) = tier_runtime();
match width {
8 => {
measure_third_fourth_branch::<8>(true, runtime.clone());
measure_third_fourth_branch::<8>(false, runtime);
}
12 => {
measure_third_fourth_branch::<12>(true, runtime.clone());
measure_third_fourth_branch::<12>(false, runtime);
}
18 => {
measure_third_fourth_branch::<18>(true, runtime.clone());
measure_third_fourth_branch::<18>(false, runtime);
}
other => panic!("tier_runtime returned non-specialized width {other}"),
}
}