tetration 0.1.9

Chunked tensor .tet format, tet CLI, and optional C ABI (tetration-ffi)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Dataset directory and chunk index (layout v1 extension).
//!
//! See `docs/layout_v1.md` for byte layout after the 32-byte superblock.
//!
//! Multi-dataset writers share geometry and file assembly via [`file_layout`] (chunk grid planning,
//! index/payload layout, encoded chunk push). [`write`] and [`stream_write`] create new files;
//! [`append`] extends an existing catalog in place.

mod append;
mod dataset;
pub mod execution;
mod file_layout;
mod history;
mod index;
pub mod metadata;
pub mod session;
mod stream_write;
pub mod tile;
mod write;

use std::borrow::Cow;
use std::io;

use serde::Serialize;
use thiserror::Error;

use crate::layout::{self, SuperblockV1};
use crate::utils::{dtype::ElementDtype, wire};

pub use append::{
    append_multi_mixed, append_multi_raw_array_file, append_multi_raw_array_streaming,
};
pub use dataset::{DatasetRecordV1, RawArrayWrite};
pub use execution::{DEFAULT_MEMORY_BUDGET_PERCENT_BPS, FileExecutionSettingsV1};
#[cfg(test)]
pub(crate) use history::rewrite_footer_bytes_for_test;
pub use history::{
    FooterBlobV1, HistoryEvent, HistoryEventV1, HistoryFooterWireV1, MetadataBlobRefV1,
    append_convert_history, append_history_events, payload_file_len, read_footer_blob,
    read_metadata, unix_timestamp_now, write_footer_blob,
};
pub use index::{CHUNK_INDEX_HEADER_V1, ChunkIndexEntryV1, ChunkIndexHeaderV1};
pub use metadata::{
    CoordAxisV1, DatasetMetadataV1, FileMetadataV1, MetadataLimitsV1, TetMetadataV1,
};
pub use session::{
    FileMetadataDraft, TetDatasetStreamSpec, TetDatasetWrite, TetFile, TetWriterSession,
};
pub use stream_write::{
    ArrayWriteMeta, StreamTileJob, StreamWriteProgress, total_chunk_count_for_meta,
    validate_array_write_meta, write_multi_raw_array_streaming,
};
pub use tile::{chunk_coords_intersecting_global_box, chunk_coords_intersecting_strided};
pub use write::{write_multi_raw_array_file, write_one_chunk_raw_file, write_raw_array_file};

/// Verify APIs moved to [`crate::verify`]; re-exported for compatibility.
pub use crate::verify::{
    TetVerifyReport, VerifyFinding, VerifyFixHint, VerifyRecommendation, VerifySeverity,
    VerifySummary, format_verify_json, format_verify_quiet, format_verify_text, verify_tet_bytes,
    verify_tet_file,
};

/// v1 chunk payload codec wire tags (`u32` values written per chunk in the index).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChunkPayloadCodecV1 {
    /// Raw little-endian tensor bytes (`stored_byte_len == raw_byte_len`).
    pub raw: u32,
    /// **zstd**–compressed bytes at `payload_offset`; decompressed size is `raw_byte_len`.
    pub zstd: u32,
}

/// Defined chunk payload codecs for layout v1 (see `docs/layout_v1.md`).
pub const CHUNK_PAYLOAD_CODEC_V1: ChunkPayloadCodecV1 = ChunkPayloadCodecV1 { raw: 0, zstd: 1 };

impl ChunkPayloadCodecV1 {
    #[must_use]
    pub const fn is_raw(self, codec: u32) -> bool {
        codec == self.raw
    }

    #[must_use]
    pub const fn is_zstd(self, codec: u32) -> bool {
        codec == self.zstd
    }

    #[must_use]
    pub const fn is_supported(self, codec: u32) -> bool {
        self.is_raw(codec) || self.is_zstd(codec)
    }

    /// Encode one tile’s uncompressed `f32` row-major bytes for the given wire `codec`.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError::UnsupportedCodec`] when `codec` is neither [`ChunkPayloadCodecV1::raw`]
    /// nor [`ChunkPayloadCodecV1::zstd`] for this table, and [`CatalogError::Io`] when zstd
    /// compression fails.
    pub fn encode_tile_payload(
        self,
        codec: u32,
        tile_bytes: Vec<u8>,
    ) -> Result<Vec<u8>, CatalogError> {
        if self.is_raw(codec) {
            return Ok(tile_bytes);
        }
        if self.is_zstd(codec) {
            return zstd::encode_all(tile_bytes.as_slice(), 0).map_err(CatalogError::Io);
        }
        Err(CatalogError::UnsupportedCodec { codec })
    }

    /// Decode stored chunk bytes to uncompressed tile payload (`raw_byte_len` bytes).
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError::UnsupportedCodec`], [`CatalogError::RawStoredMismatch`],
    /// [`CatalogError::ZstdDecode`], or [`CatalogError::DecodedLengthMismatch`].
    pub fn decode_tile_payload(
        self,
        stored: &[u8],
        raw_byte_len: u64,
        stored_byte_len: u64,
        codec: u32,
    ) -> Result<Cow<'_, [u8]>, CatalogError> {
        if self.is_raw(codec) {
            if stored_byte_len != raw_byte_len {
                return Err(CatalogError::RawStoredMismatch {
                    raw: raw_byte_len,
                    stored: stored_byte_len,
                });
            }
            return Ok(Cow::Borrowed(stored));
        }
        if self.is_zstd(codec) {
            let dec =
                zstd::decode_all(stored).map_err(|e| CatalogError::ZstdDecode(e.to_string()))?;
            if dec.len() as u64 != raw_byte_len {
                return Err(CatalogError::DecodedLengthMismatch {
                    decoded: dec.len(),
                    raw: raw_byte_len,
                });
            }
            return Ok(Cow::Owned(dec));
        }
        Err(CatalogError::UnsupportedCodec { codec })
    }
}

/// v1 dataset element `dtype` wire tags (`u32` in each dataset directory record).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DatasetDtypeTagV1 {
    /// IEEE754 binary32 (`f32`), row-major within each chunk.
    pub f32: u32,
    /// IEEE754 binary64 (`f64`), row-major within each chunk.
    pub f64: u32,
    /// Two's-complement `i32`, row-major within each chunk.
    pub i32: u32,
    /// Two's-complement `i64`, row-major within each chunk.
    pub i64: u32,
    /// Unsigned `u8`, row-major within each chunk.
    pub u8: u32,
    /// Unsigned `u16`, row-major within each chunk.
    pub u16: u32,
    /// Two's-complement `i16`, row-major within each chunk.
    pub i16: u32,
    /// Unsigned `u32`, row-major within each chunk.
    pub u32: u32,
    /// IEEE754 binary16 (`f16`), row-major within each chunk.
    pub f16: u32,
    /// Unsigned `u64`, row-major within each chunk.
    pub u64: u32,
}

/// Defined dataset element dtypes for layout v1 (see `docs/layout_v1.md`).
pub const DATASET_DTYPE_TAG_V1: DatasetDtypeTagV1 = DatasetDtypeTagV1 {
    f32: 1,
    f64: 2,
    i32: 3,
    i64: 4,
    u8: 5,
    u16: 6,
    i16: 7,
    u32: 8,
    f16: 9,
    u64: 10,
};

impl DatasetDtypeTagV1 {
    #[must_use]
    pub const fn is_f32(self, dtype: u32) -> bool {
        dtype == self.f32
    }

    #[must_use]
    pub const fn is_f64(self, dtype: u32) -> bool {
        dtype == self.f64
    }

    #[must_use]
    pub const fn is_i32(self, dtype: u32) -> bool {
        dtype == self.i32
    }

    #[must_use]
    pub const fn is_i64(self, dtype: u32) -> bool {
        dtype == self.i64
    }

    #[must_use]
    pub const fn is_u8(self, dtype: u32) -> bool {
        dtype == self.u8
    }

    #[must_use]
    pub const fn is_u16(self, dtype: u32) -> bool {
        dtype == self.u16
    }

    #[must_use]
    pub const fn is_i16(self, dtype: u32) -> bool {
        dtype == self.i16
    }

    #[must_use]
    pub const fn is_u32(self, dtype: u32) -> bool {
        dtype == self.u32
    }

    #[must_use]
    pub const fn is_f16(self, dtype: u32) -> bool {
        dtype == self.f16
    }

    #[must_use]
    pub const fn is_u64(self, dtype: u32) -> bool {
        dtype == self.u64
    }

    #[must_use]
    pub const fn is_supported(self, dtype: u32) -> bool {
        self.is_f32(dtype)
            || self.is_f64(dtype)
            || self.is_i32(dtype)
            || self.is_i64(dtype)
            || self.is_u8(dtype)
            || self.is_u16(dtype)
            || self.is_i16(dtype)
            || self.is_u32(dtype)
            || self.is_f16(dtype)
            || self.is_u64(dtype)
    }
}

/// Maximum tensor rank supported by the v1 catalog on disk.
pub const MAX_NDIM: usize = 8;

/// Element count × element size for a tensor with `shape` and wire `dtype`.
#[must_use]
pub fn tensor_bytes_from_shape(shape: &[u64], dtype: u32) -> Option<u64> {
    crate::utils::dtype::ElementDtype::try_from_wire_tag(dtype)?.tensor_bytes_for_shape(shape)
}

/// Element size in bytes for a supported catalog wire `dtype` tag.
///
/// # Errors
///
/// Returns [`CatalogError::InvalidWriteSpec`] when `dtype` is not a supported v1 tag.
pub fn elem_size_for_wire_tag(dtype: u32) -> Result<usize, CatalogError> {
    crate::utils::dtype::ElementDtype::try_from_wire_tag(dtype)
        .map(ElementDtype::elem_size)
        .ok_or(CatalogError::InvalidWriteSpec(
            "unsupported dataset dtype tag",
        ))
}

/// Per-dataset chunk grid geometry for tile iteration during writes.
///
/// Produced by [`chunk_grid_plan`]; consumed by [`file_layout::push_raw_tiles_from_tensor`]
/// and streaming index builders.
pub(crate) struct ChunkGridPlan {
    /// Tensor rank (`shape.len()`).
    pub ndim: usize,
    /// Chunks per axis: `ceil(shape[d] / chunk_shape[d])`.
    pub counts: Vec<u64>,
    /// `product(counts)` — linear tile index runs `0..n_chunks`.
    pub n_chunks: u64,
    /// Element size in bytes for `dtype`'s wire tag.
    pub elem_size: usize,
}

/// Chunk grid counts, total tiles, and element size for a supported wire dtype.
///
/// # Errors
///
/// Returns [`CatalogError`] when the grid overflows or `dtype` is unsupported.
pub(crate) fn chunk_grid_plan(
    shape: &[u64],
    chunk_shape: &[u64],
    dtype: u32,
) -> Result<ChunkGridPlan, CatalogError> {
    let ndim = shape.len();
    let counts = tile::chunk_grid_counts(shape, chunk_shape);
    let n_chunks = tile::total_chunk_count(&counts)?;
    let elem_size = elem_size_for_wire_tag(dtype)?;
    Ok(ChunkGridPlan {
        ndim,
        counts,
        n_chunks,
        elem_size,
    })
}

/// Typed helpers for callers that already know the element type.
#[must_use]
pub fn f32_tensor_bytes_from_shape(shape: &[u64]) -> Option<u64> {
    ElementDtype::F32.tensor_bytes_for_shape(shape)
}

/// Element count × 8 for an `f64` tensor with `shape`.
#[must_use]
pub fn f64_tensor_bytes_from_shape(shape: &[u64]) -> Option<u64> {
    ElementDtype::F64.tensor_bytes_for_shape(shape)
}

/// Element count × 4 for an `i32` tensor with `shape`.
#[must_use]
pub fn i32_tensor_bytes_from_shape(shape: &[u64]) -> Option<u64> {
    ElementDtype::I32.tensor_bytes_for_shape(shape)
}

/// Element count × 8 for an `i64` tensor with `shape`.
#[must_use]
pub fn i64_tensor_bytes_from_shape(shape: &[u64]) -> Option<u64> {
    ElementDtype::I64.tensor_bytes_for_shape(shape)
}

/// High-level view of a mapped `.tet` file (superblock + catalog).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct TetFileSummaryV1 {
    pub superblock: SuperblockV1,
    pub datasets: Vec<DatasetRecordV1>,
    pub chunks: Vec<ChunkIndexEntryV1>,
    /// Execution preferences from the chunk index header (defaults when all zero).
    pub file_execution: FileExecutionSettingsV1,
    /// Optional provenance/history footer (`[["convert","h5"|"nc","<unix_secs>"], …]`).
    pub history: Vec<HistoryEventV1>,
    /// Optional `metadata` object from the same `THST` footer JSON.
    pub metadata: TetMetadataV1,
}

/// Catalog read, index validation, codec, and writer failures.
#[derive(Debug, Error)]
pub enum CatalogError {
    #[error(transparent)]
    Layout(#[from] layout::LayoutError),
    #[error("file too short for catalog: need at least {need} bytes, got {got}")]
    TooShort { need: usize, got: usize },
    #[error("dataset_count is {count} but dataset directory is missing (file length {len})")]
    MissingDatasetDirectory { count: u32, len: usize },
    #[error("invalid UTF-8 in dataset name")]
    BadDatasetName,
    #[error("ndim {ndim} out of range (max {MAX_NDIM})")]
    BadNdim { ndim: usize },
    #[error("dataset blob length mismatch: declared {declared}, parsed {parsed}")]
    DatasetBlobMismatch { declared: u64, parsed: u64 },
    #[error("chunk index offset mismatch: superblock says {sb}, expected {expected}")]
    ChunkIndexOffsetMismatch { sb: u64, expected: u64 },
    #[error("bad chunk index magic: expected TIDX, got {0:?}")]
    BadIndexMagic([u8; 4]),
    #[error("unsupported chunk index version: {0}")]
    UnsupportedIndexVersion(u32),
    #[error("chunk index entry count {count} does not fit in region length {region}")]
    BadIndexLength { count: u64, region: u64 },
    #[error("chunk payload [{start}, {end}) out of bounds for file length {file_len}")]
    PayloadOutOfBounds { file_len: u64, start: u64, end: u64 },
    #[error("unsupported codec {codec} (supported: 0 = raw, 1 = zstd)")]
    UnsupportedCodec { codec: u32 },
    #[error("raw/stored length mismatch for codec=0: raw={raw}, stored={stored}")]
    RawStoredMismatch { raw: u64, stored: u64 },
    #[error("zstd decode failed: {0}")]
    ZstdDecode(String),
    #[error("decoded payload length {decoded} != raw_byte_len {raw}")]
    DecodedLengthMismatch { decoded: usize, raw: u64 },
    #[error("invalid one-chunk write spec: {0}")]
    InvalidWriteSpec(&'static str),
    #[error("catalog numeric value too large for this platform ({field}={value})")]
    TooLargeForPlatform { field: &'static str, value: u64 },
    #[error(transparent)]
    Io(#[from] io::Error),
}

/// Arguments for writing a single-dataset, single-chunk raw payload file (no compression).
#[derive(Debug, Clone)]
pub struct OneChunkRawWrite<'a> {
    pub name: &'a str,
    pub dtype: u32,
    pub shape: &'a [u64],
    pub chunk_shape: &'a [u64],
    pub payload: &'a [u8],
}

/// Read superblock, dataset directory, and chunk index from a mapped file.
///
/// # Errors
///
/// Returns [`CatalogError`] when layout rules are violated or regions are inconsistent.
pub fn read_tet_summary_v1(data: &[u8]) -> Result<TetFileSummaryV1, CatalogError> {
    let sb = layout::read_superblock_v1(data)?;
    if sb.dataset_count == 0 {
        let flags = sb.flags;
        return Ok(TetFileSummaryV1 {
            superblock: sb,
            datasets: Vec::new(),
            chunks: Vec::new(),
            file_execution: FileExecutionSettingsV1::default_engine(),
            history: history::read_history(data, flags)?,
            metadata: history::read_metadata(data, flags)?,
        });
    }

    if data.len() < 40 {
        return Err(CatalogError::MissingDatasetDirectory {
            count: sb.dataset_count,
            len: data.len(),
        });
    }

    let dataset_blob_len_u64 = wire::u64_le_at(data, 32);
    let dataset_blob_len =
        usize::try_from(dataset_blob_len_u64).map_err(|_| CatalogError::TooLargeForPlatform {
            field: "dataset_blob_len",
            value: dataset_blob_len_u64,
        })?;
    let blob_start = 40usize;
    let blob_end = blob_start
        .checked_add(dataset_blob_len)
        .ok_or(CatalogError::TooShort {
            need: usize::MAX,
            got: data.len(),
        })?;
    if data.len() < blob_end {
        return Err(CatalogError::TooShort {
            need: blob_end,
            got: data.len(),
        });
    }

    let mut datasets = Vec::with_capacity(sb.dataset_count as usize);
    let mut cursor = blob_start;
    for _ in 0..sb.dataset_count {
        let (rec, next) = dataset::parse_one_dataset_record(data, cursor, blob_end)?;
        datasets.push(rec);
        cursor = next;
    }
    if cursor != blob_end {
        return Err(CatalogError::DatasetBlobMismatch {
            declared: dataset_blob_len as u64,
            parsed: (cursor - blob_start) as u64,
        });
    }

    let blob_end_u64 = u64::try_from(blob_end).map_err(|_| CatalogError::TooLargeForPlatform {
        field: "dataset_blob_end",
        value: u64::MAX,
    })?;
    let expected_index_off = wire::align8_u64(blob_end_u64);
    if sb.chunk_index_offset != expected_index_off {
        return Err(CatalogError::ChunkIndexOffsetMismatch {
            sb: sb.chunk_index_offset,
            expected: expected_index_off,
        });
    }

    let idx_start = usize_from_u64("chunk_index_offset", sb.chunk_index_offset)?;
    let idx_len = usize_from_u64("chunk_index_length", sb.chunk_index_length)?;
    let idx_end = idx_start
        .checked_add(idx_len)
        .filter(|&e| e <= data.len())
        .ok_or(CatalogError::TooShort {
            need: idx_start.saturating_add(idx_len),
            got: data.len(),
        })?;
    let idx_bytes = &data[idx_start..idx_end];
    let (chunks, file_execution) = index::parse_chunk_index(idx_bytes)?;

    let payload_len = history::payload_file_len(data, sb.flags)?;
    index::validate_chunk_payloads(&chunks, payload_len)?;
    let history = history::read_history(data, sb.flags)?;
    let metadata = history::read_metadata(data, sb.flags)?;

    Ok(TetFileSummaryV1 {
        superblock: sb,
        datasets,
        chunks,
        file_execution,
        history,
        metadata,
    })
}

/// Validate chunk index payload spans against a file length (same rules as [`read_tet_summary_v1`]).
///
/// Exposed for integration tests in [`crate::tests`].
pub use index::validate_chunk_payloads;

pub(super) fn usize_from_u64(field: &'static str, v: u64) -> Result<usize, CatalogError> {
    usize::try_from(v).map_err(|_| CatalogError::TooLargeForPlatform { field, value: v })
}