// Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. // Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). // This is not open-source software: see the license for permitted uses. mod density_kernels; mod geometry_kernels; use std::slice; unsafe fn peak_features( n: usize, width: i64, idx: *const i64, val: *const f64, order: *const i64, context: *const f64, peak: *const f64, logs: *const f64, retained: *mut i64, out: *mut f64, ) -> usize { let ix = slice::from_raw_parts(idx, n); let v = slice::from_raw_parts(val, n); let order = slice::from_raw_parts(order, n); let ctx = slice::from_raw_parts(context, 53); let peak = slice::from_raw_parts(peak, 2); let logs = slice::from_raw_parts(logs, 5); let retained = slice::from_raw_parts_mut(retained, 4); let out = slice::from_raw_parts_mut(out, 88); let xy: Vec<(f64, f64)> = ix .iter() .map(|i| ((i % width) as f64 + 0.5, (i / width) as f64 + 0.5)) .collect(); let mut count = 0; for &j in order { let j = j as usize; if (0..count).any(|k| { let p = xy[retained[k] as usize]; (xy[j].0 - p.0).powi(2) + (xy[j].1 - p.1).powi(2) < 2.25 }) { continue; } retained[count] = j as i64; count += 1; if count == 4 { break; } } let total = v.iter().sum::(); let maximum = v.iter().copied().fold(f64::NEG_INFINITY, f64::max); for k in 0..count { let j = retained[k] as usize; let p = xy[j]; let dx = p.0 - peak[0]; let dy = p.1 - peak[1]; let nearby_mass: f64 = (0..n) .filter(|&i| (xy[i].0 - p.0).abs() <= 1.0 && (xy[i].1 - p.1).abs() <= 1.0) .map(|i| v[i]) .sum(); let r = &mut out[k * 22..(k + 1) * 22]; r[0] = k as f64; r[1] = v[j] / maximum; r[2] = nearby_mass / total; r[3] = logs[0]; r[4] = dx; r[5] = dy; for h in 0..4 { let c = 34 + h * 4; let t = 6 + h * 4; r[t] = if ctx[c + 3] != 0.0 { (ctx[c] - dx).clamp(-30.0, 30.0) } else { 0.0 }; r[t + 1] = if ctx[c + 3] != 0.0 { (ctx[c + 1] - dy).clamp(-30.0, 30.0) } else { 0.0 }; r[t + 2] = logs[h + 1]; r[t + 3] = ctx[c + 3]; } } count } // Python validates nonempty matching sparse arrays and contiguous buffers. // Offsets index flattened cells; contexts have 53 columns, outputs four x 22. // Caller owns all buffers for the duration of this synchronous call. #[no_mangle] pub unsafe extern "C" fn page_peak_features( words: usize, width: i64, offsets: *const i64, idx: *const i64, val: *const f64, order: *const i64, context: *const f64, peaks: *const f64, logs: *const f64, retained: *mut i64, out: *mut f64, counts: *mut i64, ) { let offsets = slice::from_raw_parts(offsets, words + 1); for w in 0..words { let start = offsets[w] as usize; let n = (offsets[w + 1] - offsets[w]) as usize; *counts.add(w) = peak_features( n, width, idx.add(start), val.add(start), order.add(start), context.add(w * 53), peaks.add(w * 2), logs.add(w * 5), retained.add(w * 4), out.add(w * 88), ) as i64; } } // Caller validates sparse lengths and supplies matching NumPy sort orders. // Output contains ten quantile offsets and twelve threshold edge offsets per word. #[no_mangle] pub unsafe extern "C" fn context_bounds( words: usize, width: i64, offsets: *const i64, indices: *const i64, attention: *const f64, mass: *const f64, orders_x: *const i64, orders_y: *const i64, peaks: *const f64, output: *mut f64, ) { let offsets = slice::from_raw_parts(offsets, words + 1); for w in 0..words { let start = offsets[w] as usize; let n = (offsets[w + 1] - offsets[w]) as usize; let ix = slice::from_raw_parts(indices.add(start), n); let a = slice::from_raw_parts(attention.add(start), n); let m = slice::from_raw_parts(mass.add(start), n); let p = slice::from_raw_parts(peaks.add(w * 2), 2); let out = slice::from_raw_parts_mut(output.add(w * 22), 22); for axis in 0..2 { let order = slice::from_raw_parts( if axis == 0 { orders_x.add(start) } else { orders_y.add(start) }, n, ); let mut cdf = Vec::with_capacity(n); let mut total = 0.0; for &j in order { total += m[j as usize]; cdf.push(total); } for (q, &v) in [0.05, 0.25, 0.5, 0.75, 0.95].iter().enumerate() { let k = cdf.partition_point(|&x| x < v).min(n - 1); let cell = ix[order[k] as usize]; let coordinate = if axis == 0 { cell % width } else { cell / width }; out[axis * 5 + q] = coordinate as f64 + 0.5 - p[axis]; } } let maximum = a.iter().copied().fold(f64::NEG_INFINITY, f64::max); for (t, &threshold) in [0.1, 0.3, 0.6].iter().enumerate() { let mut minx = i64::MAX; let mut miny = i64::MAX; let mut maxx = i64::MIN; let mut maxy = i64::MIN; for j in 0..n { if a[j] >= threshold * maximum { let x = ix[j] % width; let y = ix[j] / width; minx = minx.min(x); miny = miny.min(y); maxx = maxx.max(x); maxy = maxy.max(y); } } out[10 + t * 4] = minx as f64 - p[0]; out[11 + t * 4] = miny as f64 - p[1]; out[12 + t * 4] = maxx as f64 + 1.0 - p[0]; out[13 + t * 4] = maxy as f64 + 1.0 - p[1]; } } } // Python supplies nonempty sparse rows and optional anchor positions (-1 means argmax). // Each output row has 34 features and a separate two-coordinate peak center. #[no_mangle] pub unsafe extern "C" fn local_features_unsorted( words: usize, height: i64, width: i64, offsets: *const i64, indices: *const i64, values: *const f64, lengths: *const i64, anchors: *const i64, output: *mut f64, peaks: *mut f64, ) { let offsets = slice::from_raw_parts(offsets, words + 1); for w in 0..words { let start = offsets[w] as usize; let n = (offsets[w + 1] - offsets[w]) as usize; let ix = slice::from_raw_parts(indices.add(start), n); let v = slice::from_raw_parts(values.add(start), n); let out = slice::from_raw_parts_mut(output.add(w * 34), 34); out.fill(0.0); let maximum = v.iter().copied().fold(f64::NEG_INFINITY, f64::max); let total = v.iter().sum::(); let anchor = *anchors.add(w); let peak = if anchor >= 0 { anchor as usize } else { v.iter().position(|&value| value == maximum).unwrap() }; let x = ix[peak] % width; let y = ix[peak] / width; out[0] = *lengths.add(w) as f64; out[1] = height as f64; out[2] = width as f64; out[3] = maximum; out[4] = total; *peaks.add(w * 2) = x as f64 + 0.5; *peaks.add(w * 2 + 1) = y as f64 + 0.5; for j in 0..n { let dx = ix[j] % width - x; let dy = ix[j] / width - y; if dx.abs() <= 2 && dy.abs() <= 2 { out[9 + ((dy + 2) * 5 + dx + 2) as usize] = v[j] / maximum; } let mass = v[j] / total; let cx = dx.clamp(-5, 5) as f64; let cy = dy.clamp(-5, 5) as f64; out[5] += mass * cx; out[6] += mass * cy; out[7] += mass * (cx * cx); out[8] += mass * (cy * cy); } } }