sim-lib-numbers-signal 0.1.0

Deterministic transforms, autoregression, spectral estimation, and guarded signal operations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Bounded transforms over caller-supplied Table or Dir block storage.

use sim_kernel::{Cx, Symbol, Table};

use crate::{
    PlacementPolicy, SignalBuffer, SignalError, SignalView, TensorView, TransformKind,
    TransformPlan,
    block_io::{
        BlockCache, decode_complex_block, decode_real_block, encode_complex, encode_real,
        load_block, preflight_blocks, store_error,
    },
    io_plan::{
        TransformPrecision, TransformReport, TransformResources, scratch_bytes,
        transform_plan_digest,
    },
    multidimensional::{axis_plan, for_each_line, validate_nd_plan},
    tensor_view::transform_cell_count,
    transform,
};

/// Public descriptor for a row-major tensor stored as Table/Dir byte blocks.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlockedTensor {
    namespace: Symbol,
    shape: Vec<usize>,
    precision: TransformPrecision,
    block_len: usize,
}

impl BlockedTensor {
    /// Describes an existing row-major tensor in caller-managed blocks.
    ///
    /// Construction does not read the store. [`transform_nd_blocked`] performs
    /// a complete bounded preflight before its first mutation.
    pub fn new(
        namespace: Symbol,
        shape: Vec<usize>,
        precision: TransformPrecision,
        block_len: usize,
    ) -> Result<Self, SignalError> {
        let _ = transform_cell_count(&shape)?;
        if block_len == 0 {
            return Err(SignalError::InvalidPolicy {
                policy: "block_len",
                reason: "external block length must be nonzero",
            });
        }
        Ok(Self {
            namespace,
            shape,
            precision,
            block_len,
        })
    }

    /// Stable caller-selected namespace used to derive Table block keys.
    pub fn namespace(&self) -> &Symbol {
        &self.namespace
    }

    /// Logical row-major tensor extents.
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }

    /// Stored cell representation.
    pub const fn precision(&self) -> TransformPrecision {
        self.precision
    }

    /// Maximum number of cells encoded in each block.
    pub const fn block_len(&self) -> usize {
        self.block_len
    }

    /// Logical cell count.
    pub fn len(&self) -> usize {
        transform_cell_count(&self.shape).expect("blocked tensor descriptor was validated")
    }

    /// Whether the descriptor contains no cells.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Number of external blocks in the descriptor.
    pub fn block_count(&self) -> usize {
        self.len().div_ceil(self.block_len)
    }

    /// Stable Table key for `block`, or `None` when it is out of range.
    pub fn block_key(&self, block: usize) -> Option<Symbol> {
        (block < self.block_count()).then(|| self.key(block))
    }

    /// Number of cells expected in `block`, or `None` when it is out of range.
    pub fn cells_in_block(&self, block: usize) -> Option<usize> {
        (block < self.block_count()).then(|| self.block_cell_count(block))
    }

    pub(crate) fn key(&self, block: usize) -> Symbol {
        Symbol::qualified(
            "numbers-signal-block-v1",
            format!("{}:{block:016x}", self.namespace.as_qualified_str()),
        )
    }
}

/// Writes one real-f64 block into an existing external tensor descriptor.
///
/// This incremental surface lets callers populate a tensor larger than memory
/// without ever constructing a complete [`TensorView`].
pub fn write_f64_block(
    cx: &mut Cx,
    store: &dyn Table,
    tensor: &BlockedTensor,
    block: usize,
    values: &[f64],
) -> Result<(), SignalError> {
    if tensor.precision != TransformPrecision::F64 {
        return Err(SignalError::InputKind {
            expected: "complex",
            actual: "real",
        });
    }
    validate_block_cells(tensor, block, values.len())?;
    let mut bytes = Vec::with_capacity(std::mem::size_of_val(values));
    for (index, &value) in values.iter().enumerate() {
        if !value.is_finite() {
            return Err(SignalError::NonFinite {
                index,
                component: "value",
            });
        }
        encode_real(&mut bytes, value);
    }
    set_block(cx, store, tensor, block, bytes)
}

/// Writes one complex-f64 block into an existing external tensor descriptor.
///
/// Components are encoded as portable little-endian real/imaginary f64 pairs.
pub fn write_complex_f64_block(
    cx: &mut Cx,
    store: &dyn Table,
    tensor: &BlockedTensor,
    block: usize,
    values: &[(f64, f64)],
) -> Result<(), SignalError> {
    if tensor.precision != TransformPrecision::ComplexF64 {
        return Err(SignalError::InputKind {
            expected: "real",
            actual: "complex",
        });
    }
    validate_block_cells(tensor, block, values.len())?;
    let mut bytes = Vec::with_capacity(values.len() * 2 * size_of::<f64>());
    for (index, &value) in values.iter().enumerate() {
        if !value.0.is_finite() {
            return Err(SignalError::NonFinite {
                index,
                component: "real",
            });
        }
        if !value.1.is_finite() {
            return Err(SignalError::NonFinite {
                index,
                component: "imag",
            });
        }
        encode_complex(&mut bytes, value);
    }
    set_block(cx, store, tensor, block, bytes)
}

/// Writes a borrowed tensor view into caller-supplied Table/Dir blocks.
///
/// Blocks are portable little-endian f64 byte strings. The function retains no
/// backend handle and accepts no filesystem path.
pub fn write_blocked_tensor(
    cx: &mut Cx,
    store: &dyn Table,
    namespace: Symbol,
    input: &TensorView<'_>,
    resources: TransformResources,
) -> Result<BlockedTensor, SignalError> {
    resources.validate()?;
    let precision = input_precision(input);
    let descriptor = BlockedTensor::new(
        namespace,
        input.shape().to_vec(),
        precision,
        resources.block_len,
    )?;
    let block_bytes = resources
        .block_len
        .checked_mul(precision.cell_bytes())
        .ok_or(SignalError::InvalidTensorView {
            reason: "block byte length overflowed",
        })?;
    if block_bytes > resources.max_scratch_bytes {
        return Err(SignalError::ScratchLimit {
            required: block_bytes,
            maximum: resources.max_scratch_bytes,
        });
    }
    validate_input_finite(input)?;
    for block in 0..descriptor.len().div_ceil(descriptor.block_len) {
        let start = block * descriptor.block_len;
        let end = descriptor.len().min(start + descriptor.block_len);
        let mut bytes = Vec::with_capacity((end - start) * precision.cell_bytes());
        for logical in start..end {
            let physical = input.physical_index(logical)?;
            match input {
                TensorView::Complex { values, .. } => encode_complex(&mut bytes, values[physical]),
                TensorView::Real { values, .. } => encode_real(&mut bytes, values[physical]),
            }
        }
        let value = cx
            .factory()
            .bytes(bytes)
            .map_err(|error| store_error("encode", error))?;
        store
            .set(cx, descriptor.key(block), value)
            .map_err(|error| store_error("set", error))?;
    }
    Ok(descriptor)
}

/// Materializes Table/Dir blocks back into a canonical tensor buffer.
pub fn read_blocked_tensor(
    cx: &mut Cx,
    store: &dyn Table,
    tensor: &BlockedTensor,
) -> Result<SignalBuffer, SignalError> {
    match tensor.precision {
        TransformPrecision::ComplexF64 => {
            let mut values = Vec::with_capacity(tensor.len());
            for block in 0..tensor.len().div_ceil(tensor.block_len) {
                let bytes = load_block(cx, store, tensor, block)?;
                values.extend(decode_complex_block(
                    block,
                    &bytes,
                    tensor.block_cell_count(block),
                )?);
            }
            sim_lib_numbers_tensor_cmplxf::ComplexFTensor::new(tensor.shape.clone(), values)
                .map(SignalBuffer::Complex)
                .ok_or(SignalError::InvalidTensorView {
                    reason: "blocked complex tensor shape overflowed",
                })
        }
        TransformPrecision::F64 => {
            let mut values = Vec::with_capacity(tensor.len());
            for block in 0..tensor.len().div_ceil(tensor.block_len) {
                let bytes = load_block(cx, store, tensor, block)?;
                values.extend(decode_real_block(
                    block,
                    &bytes,
                    tensor.block_cell_count(block),
                )?);
            }
            sim_lib_numbers_tensor_f64::F64Tensor::new(tensor.shape.clone(), values)
                .map(SignalBuffer::Real)
                .ok_or(SignalError::InvalidTensorView {
                    reason: "blocked real tensor shape overflowed",
                })
        }
    }
}

/// Applies a separable transform in place over caller-owned Table/Dir blocks.
///
/// One transform line and one external block are resident at a time. The
/// tensor may therefore exceed memory as long as its longest selected axis and
/// one block fit [`TransformResources::max_scratch_bytes`].
pub fn transform_nd_blocked(
    cx: &mut Cx,
    store: &dyn Table,
    tensor: &BlockedTensor,
    axes: &[usize],
    plan: &TransformPlan,
    resources: TransformResources,
) -> Result<TransformReport, SignalError> {
    resources.validate()?;
    if tensor.block_len != resources.block_len {
        return Err(SignalError::InvalidPolicy {
            policy: "block_len",
            reason: "resource block length differs from stored tensor descriptor",
        });
    }
    validate_nd_plan(&tensor.shape, axes, plan, PlacementPolicy::InPlace)?;
    validate_kind_precision(plan.kind, tensor.precision)?;
    let required = scratch_bytes(&tensor.shape, axes, tensor.precision, tensor.block_len)?;
    if required > resources.max_scratch_bytes {
        return Err(SignalError::ScratchLimit {
            required,
            maximum: resources.max_scratch_bytes,
        });
    }

    let preflight_io = preflight_blocks(cx, store, tensor)?;
    let mut cache = BlockCache::new(cx, store, tensor, preflight_io);
    for &axis in axes {
        for_each_line(&tensor.shape, axis, |start, stride, len| {
            match tensor.precision {
                TransformPrecision::ComplexF64 => {
                    let mut line = Vec::with_capacity(len);
                    for index in 0..len {
                        line.push(cache.read_complex(start + index * stride)?);
                    }
                    let axis_plan = axis_plan(plan, len, PlacementPolicy::OutOfPlace)?;
                    let SignalBuffer::Complex(output) =
                        transform(&axis_plan, SignalView::Complex(&line))?
                    else {
                        return Err(SignalError::InputKind {
                            expected: "complex",
                            actual: "real",
                        });
                    };
                    for (index, value) in output.as_slice().iter().copied().enumerate() {
                        cache.write_complex(start + index * stride, value)?;
                    }
                }
                TransformPrecision::F64 => {
                    let mut line = Vec::with_capacity(len);
                    for index in 0..len {
                        line.push(cache.read_real(start + index * stride)?);
                    }
                    let axis_plan = axis_plan(plan, len, PlacementPolicy::OutOfPlace)?;
                    let SignalBuffer::Real(output) =
                        transform(&axis_plan, SignalView::Real(&line))?
                    else {
                        return Err(SignalError::InputKind {
                            expected: "real",
                            actual: "complex",
                        });
                    };
                    for (index, value) in output.as_slice().iter().copied().enumerate() {
                        cache.write_real(start + index * stride, value)?;
                    }
                }
            }
            Ok(())
        })?;
    }
    cache.finish()?;
    Ok(TransformReport {
        scratch_bytes: required,
        passes: axes.len(),
        io_blocks: cache.io_blocks(),
        precision: tensor.precision,
        plan_digest: transform_plan_digest(
            &tensor.shape,
            axes,
            plan,
            tensor.precision,
            Some(resources),
        ),
    })
}

impl BlockedTensor {
    pub(crate) fn block_cell_count(&self, block: usize) -> usize {
        let start = block * self.block_len;
        self.len().saturating_sub(start).min(self.block_len)
    }
}

fn validate_block_cells(
    tensor: &BlockedTensor,
    block: usize,
    actual: usize,
) -> Result<(), SignalError> {
    let Some(expected) = tensor.cells_in_block(block) else {
        return Err(SignalError::BlockStore {
            operation: "set",
            message: format!("block {block} is outside descriptor"),
        });
    };
    if actual != expected {
        return Err(SignalError::BlockStore {
            operation: "set",
            message: format!("block {block} has {actual} cells, expected {expected}"),
        });
    }
    Ok(())
}

fn set_block(
    cx: &mut Cx,
    store: &dyn Table,
    tensor: &BlockedTensor,
    block: usize,
    bytes: Vec<u8>,
) -> Result<(), SignalError> {
    let value = cx
        .factory()
        .bytes(bytes)
        .map_err(|error| store_error("encode", error))?;
    store
        .set(cx, tensor.key(block), value)
        .map_err(|error| store_error("set", error))
}

fn input_precision(input: &TensorView<'_>) -> TransformPrecision {
    match input {
        TensorView::Complex { .. } => TransformPrecision::ComplexF64,
        TensorView::Real { .. } => TransformPrecision::F64,
    }
}

fn validate_input_finite(input: &TensorView<'_>) -> Result<(), SignalError> {
    for logical in 0..input.len() {
        let physical = input.physical_index(logical)?;
        match input {
            TensorView::Complex { values, .. } => {
                let (real, imag) = values[physical];
                if !real.is_finite() {
                    return Err(SignalError::NonFinite {
                        index: logical,
                        component: "real",
                    });
                }
                if !imag.is_finite() {
                    return Err(SignalError::NonFinite {
                        index: logical,
                        component: "imag",
                    });
                }
            }
            TensorView::Real { values, .. } if !values[physical].is_finite() => {
                return Err(SignalError::NonFinite {
                    index: logical,
                    component: "value",
                });
            }
            TensorView::Real { .. } => {}
        }
    }
    Ok(())
}

fn validate_kind_precision(
    kind: TransformKind,
    precision: TransformPrecision,
) -> Result<(), SignalError> {
    match (kind, precision) {
        (TransformKind::Dft | TransformKind::Fft, TransformPrecision::ComplexF64)
        | (TransformKind::Dct(_) | TransformKind::Dst(_), TransformPrecision::F64) => Ok(()),
        (TransformKind::RealFft, _) => Err(SignalError::InvalidPolicy {
            policy: "kind",
            reason: "blocked real FFT requires an explicit shape-changing packing plan",
        }),
        (TransformKind::Dft | TransformKind::Fft, TransformPrecision::F64) => {
            Err(SignalError::InputKind {
                expected: "complex",
                actual: "real",
            })
        }
        (TransformKind::Dct(_) | TransformKind::Dst(_), TransformPrecision::ComplexF64) => {
            Err(SignalError::InputKind {
                expected: "real",
                actual: "complex",
            })
        }
    }
}