|
| 1 | +//! InferenceStream — forward-iterator over a borrowed `&[InferenceRow]` slice. |
| 2 | +//! Per cognitive-substrate-convergence-v1.md §5 L-20: vertical streaming |
| 3 | +//! over the inference-mantissa lane of the EdgeColumn SoA. Used by the |
| 4 | +//! integer-SIMD MUL evaluation hot path (D-CSV-8 sprint-12 SIMD vec). |
| 5 | +//! |
| 6 | +//! Pure iterator scaffold; `par_inference_stream` rayon variant is sprint-13+. |
| 7 | +
|
| 8 | +// Local mirror of CausalEdge64 shape (bit-compatible with causal_edge::CausalEdge64). |
| 9 | +// No cross-crate import: ndarray is the producer; causal-edge is the consumer. |
| 10 | + |
| 11 | +/// A single row of the EdgeColumn SoA, bit-compatible with |
| 12 | +/// `causal_edge::CausalEdge64` v2 layout. |
| 13 | +/// |
| 14 | +/// Fields of interest for the inference-mantissa lane: |
| 15 | +/// - bits 46-49: signed 4-bit inference mantissa (−8..+7) |
| 16 | +/// - bits 53-58: W-slot corpus root handle (0..=63) |
| 17 | +#[repr(C, align(8))] |
| 18 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)] |
| 19 | +pub struct InferenceRow(pub u64); |
| 20 | + |
| 21 | +impl InferenceRow { |
| 22 | + /// Read the 4-bit signed mantissa at bits 46-49 (matches causal-edge v2 |
| 23 | + /// `inference_mantissa()` exactly — see `causal-edge/src/layout.rs`). |
| 24 | + /// |
| 25 | + /// Sign-extension: extract 4-bit unsigned value, then sign-extend to i8 |
| 26 | + /// via arithmetic left-shift trick: `(raw << 4) >> 4`. |
| 27 | + #[inline] |
| 28 | + pub fn inference_mantissa(&self) -> i8 { |
| 29 | + let raw = ((self.0 >> 46) & 0xF) as i8; |
| 30 | + (raw << 4) >> 4 // sign-extend 4 → 8 bits |
| 31 | + } |
| 32 | + |
| 33 | + /// Read the W-slot at bits 53-58 (6 bits, 0..=63). |
| 34 | + /// |
| 35 | + /// The W-slot is the witness corpus root handle per CausalEdge64 v2 L-6. |
| 36 | + /// Returns 0 for zero-initialized rows. |
| 37 | + #[inline] |
| 38 | + pub fn w_slot(&self) -> u8 { |
| 39 | + ((self.0 >> 53) & 0x3F) as u8 |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Forward-iterator over a borrowed slice of [`InferenceRow`] values. |
| 44 | +/// |
| 45 | +/// Provides vertical streaming access to the inference-mantissa lane of the |
| 46 | +/// EdgeColumn SoA. Yields `(index, &InferenceRow)` tuples so callers can |
| 47 | +/// correlate back to the originating row without maintaining external counters. |
| 48 | +/// |
| 49 | +/// # Example |
| 50 | +/// ```rust |
| 51 | +/// use ndarray::hpc::stream::inference::{InferenceRow, InferenceStream}; |
| 52 | +/// |
| 53 | +/// let rows = vec![InferenceRow(0), InferenceRow(1 << 46)]; |
| 54 | +/// let mut stream = InferenceStream::new(&rows); |
| 55 | +/// assert_eq!(stream.len(), 2); |
| 56 | +/// let (idx, row) = stream.next().unwrap(); |
| 57 | +/// assert_eq!(idx, 0); |
| 58 | +/// ``` |
| 59 | +pub struct InferenceStream<'a> { |
| 60 | + rows: &'a [InferenceRow], |
| 61 | + cursor: usize, |
| 62 | +} |
| 63 | + |
| 64 | +impl<'a> InferenceStream<'a> { |
| 65 | + /// Construct a new stream over the given slice. The cursor starts at 0. |
| 66 | + pub fn new(rows: &'a [InferenceRow]) -> Self { |
| 67 | + Self { rows, cursor: 0 } |
| 68 | + } |
| 69 | + |
| 70 | + /// Total number of rows in the underlying slice (not remaining). |
| 71 | + pub fn len(&self) -> usize { |
| 72 | + self.rows.len() |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns `true` if the underlying slice is empty. |
| 76 | + pub fn is_empty(&self) -> bool { |
| 77 | + self.rows.is_empty() |
| 78 | + } |
| 79 | + |
| 80 | + /// Number of rows not yet yielded by the iterator. |
| 81 | + pub fn remaining(&self) -> usize { |
| 82 | + self.rows.len().saturating_sub(self.cursor) |
| 83 | + } |
| 84 | + |
| 85 | + /// Reset the cursor to the beginning so the stream can be iterated again. |
| 86 | + pub fn reset(&mut self) { |
| 87 | + self.cursor = 0; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<'a> Iterator for InferenceStream<'a> { |
| 92 | + type Item = (usize, &'a InferenceRow); |
| 93 | + |
| 94 | + fn next(&mut self) -> Option<Self::Item> { |
| 95 | + if self.cursor < self.rows.len() { |
| 96 | + let i = self.cursor; |
| 97 | + self.cursor += 1; |
| 98 | + Some((i, &self.rows[i])) |
| 99 | + } else { |
| 100 | + None |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 105 | + let rem = self.remaining(); |
| 106 | + (rem, Some(rem)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<'a> ExactSizeIterator for InferenceStream<'a> { |
| 111 | + fn len(&self) -> usize { |
| 112 | + self.remaining() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod tests { |
| 118 | + use super::*; |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_inference_stream_empty() { |
| 122 | + let rows: &[InferenceRow] = &[]; |
| 123 | + let mut stream = InferenceStream::new(rows); |
| 124 | + assert!(stream.is_empty()); |
| 125 | + assert_eq!(stream.len(), 0); |
| 126 | + assert_eq!(stream.remaining(), 0); |
| 127 | + assert!(stream.next().is_none()); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_inference_stream_yields_all() { |
| 132 | + let rows = vec![InferenceRow(0), InferenceRow(1), InferenceRow(2)]; |
| 133 | + let stream = InferenceStream::new(&rows); |
| 134 | + let collected: Vec<_> = stream.collect(); |
| 135 | + assert_eq!(collected.len(), 3); |
| 136 | + assert_eq!(collected[0].0, 0); |
| 137 | + assert_eq!(collected[1].0, 1); |
| 138 | + assert_eq!(collected[2].0, 2); |
| 139 | + assert_eq!(collected[0].1 as *const _, &rows[0] as *const _); |
| 140 | + assert_eq!(collected[2].1 as *const _, &rows[2] as *const _); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_mantissa_signed_extraction() { |
| 145 | + // Pack bits 46-49 = 0b1111 = 15 (raw), which is -1 in 4-bit two's complement. |
| 146 | + let raw_bits: u64 = 0b1111u64 << 46; |
| 147 | + let row = InferenceRow(raw_bits); |
| 148 | + assert_eq!(row.inference_mantissa(), -1); |
| 149 | + |
| 150 | + // Pack bits 46-49 = 0b0111 = 7 (raw), positive maximum. |
| 151 | + let row_pos = InferenceRow(0b0111u64 << 46); |
| 152 | + assert_eq!(row_pos.inference_mantissa(), 7); |
| 153 | + |
| 154 | + // Pack bits 46-49 = 0b1000 = 8 (raw), which is -8 in 4-bit two's complement. |
| 155 | + let row_min = InferenceRow(0b1000u64 << 46); |
| 156 | + assert_eq!(row_min.inference_mantissa(), -8); |
| 157 | + |
| 158 | + // Zero mantissa. |
| 159 | + let row_zero = InferenceRow(0); |
| 160 | + assert_eq!(row_zero.inference_mantissa(), 0); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn test_w_slot_extraction() { |
| 165 | + // Pack bits 53-58 = 0b111111 = 63 (maximum W-slot value). |
| 166 | + let raw_bits: u64 = 0b111111u64 << 53; |
| 167 | + let row = InferenceRow(raw_bits); |
| 168 | + assert_eq!(row.w_slot(), 63); |
| 169 | + |
| 170 | + // W-slot = 0 (zero row). |
| 171 | + let row_zero = InferenceRow(0); |
| 172 | + assert_eq!(row_zero.w_slot(), 0); |
| 173 | + |
| 174 | + // W-slot = 1. |
| 175 | + let row_one = InferenceRow(1u64 << 53); |
| 176 | + assert_eq!(row_one.w_slot(), 1); |
| 177 | + |
| 178 | + // W-slot = 32 (bit 58 set, bit 53 clear). |
| 179 | + let row_32 = InferenceRow(32u64 << 53); |
| 180 | + assert_eq!(row_32.w_slot(), 32); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn test_remaining_decrements() { |
| 185 | + let rows = vec![InferenceRow(0); 4]; |
| 186 | + let mut stream = InferenceStream::new(&rows); |
| 187 | + assert_eq!(stream.remaining(), 4); |
| 188 | + stream.next(); |
| 189 | + assert_eq!(stream.remaining(), 3); |
| 190 | + stream.next(); |
| 191 | + assert_eq!(stream.remaining(), 2); |
| 192 | + stream.next(); |
| 193 | + assert_eq!(stream.remaining(), 1); |
| 194 | + stream.next(); |
| 195 | + assert_eq!(stream.remaining(), 0); |
| 196 | + // Exhausted: remaining stays 0. |
| 197 | + stream.next(); |
| 198 | + assert_eq!(stream.remaining(), 0); |
| 199 | + } |
| 200 | + |
| 201 | + #[test] |
| 202 | + fn test_reset_restarts() { |
| 203 | + let rows = vec![InferenceRow(10), InferenceRow(20)]; |
| 204 | + let mut stream = InferenceStream::new(&rows); |
| 205 | + |
| 206 | + // Exhaust the stream. |
| 207 | + assert!(stream.next().is_some()); |
| 208 | + assert!(stream.next().is_some()); |
| 209 | + assert!(stream.next().is_none()); |
| 210 | + assert_eq!(stream.remaining(), 0); |
| 211 | + |
| 212 | + // After reset, the stream yields from the beginning again. |
| 213 | + stream.reset(); |
| 214 | + assert_eq!(stream.remaining(), 2); |
| 215 | + let first = stream.next().unwrap(); |
| 216 | + assert_eq!(first.0, 0); |
| 217 | + assert_eq!(first.1 .0, 10); |
| 218 | + } |
| 219 | +} |
0 commit comments