o41-training-data 0.1.0

Deterministic, resumable training batch streams over Parquet shards and data manifests, with a C API
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use crate::{
    columns,
    config::{Field, Source},
    storage::{Client, Input, Object},
};
use anyhow::{Result, ensure};
#[derive(Default)]
struct Location {
    record: usize,
    byte: u64,
    group: usize,
}
use arrow::record_batch::RecordBatch;
use rmpv::Value;
use std::{
    io::{BufRead, BufReader, Cursor, Read},
    sync::{Arc, Mutex},
};

fn validate(value: &Value) -> Result<()> {
    match value {
        Value::Map(m) => {
            for (k, v) in m {
                ensure!(k.as_str().is_some(), "map key must be UTF-8 string");
                validate(v)?;
            }
        }
        Value::Array(a) => {
            for v in a {
                validate(v)?;
            }
        }
        Value::String(s) => ensure!(s.as_str().is_some(), "invalid UTF-8"),
        _ => (),
    }
    Ok(())
}
fn msgpack(
    input: &mut impl Read,
    fields: &[Field],
    limit: usize,
    emit: &mut impl FnMut(Value) -> Result<bool>,
    location: &mut Location,
) -> Result<()> {
    loop {
        let mut header = [0u8; 8];
        if input.read(&mut header[..1])? == 0 {
            break;
        }
        input.read_exact(&mut header[1..])?;
        let size = u64::from_be_bytes(header);
        ensure!(
            size > 0 && size <= (256 * 1024 * 1024).min(limit as u64),
            "invalid frame length"
        );
        let mut compressed = vec![0; size as usize];
        input.read_exact(&mut compressed)?;
        let decompress = crate::profile::Timer::new(1);
        let frame_size = zstd::zstd_safe::find_frame_compressed_size(&compressed)
            .map_err(|e| anyhow::anyhow!("invalid zstd frame: {e}"))?;
        ensure!(
            frame_size == compressed.len(),
            "extra data after zstd frame"
        );
        let declared = zstd::zstd_safe::get_frame_content_size(&compressed)
            .map_err(|e| anyhow::anyhow!("invalid zstd content size: {e:?}"))?;
        ensure!(
            declared.is_none_or(|n| n <= limit as u64),
            "payload exceeds maxRecordBytes"
        );
        let mut payload = Vec::new();
        let mut decoder = zstd::stream::read::Decoder::new(compressed.as_slice())?;
        decoder.window_log_max(limit.max(1024).next_power_of_two().trailing_zeros())?;
        if let Err(error) = decoder.take(limit as u64 + 1).read_to_end(&mut payload) {
            location.byte += payload.len() as u64;
            return Err(error.into());
        }
        ensure!(payload.len() <= limit, "payload exceeds maxRecordBytes");
        drop(decompress);
        let parse = crate::profile::Timer::new(2);
        let mut cursor = Cursor::new(&payload);
        let row = (|| -> Result<Value> {
            let count = rmp::decode::read_map_len(&mut cursor)?;
            let mut values = Vec::new();
            for _ in 0..count {
                let key = rmpv::decode::read_value(&mut cursor)?;
                let name = key
                    .as_str()
                    .ok_or_else(|| anyhow::anyhow!("map key must be UTF-8 string"))?;
                if fields.iter().any(|f| f.from == name) {
                    values.push((key, rmpv::decode::read_value(&mut cursor)?));
                } else {
                    use serde::Deserialize;
                    serde::de::IgnoredAny::deserialize(&mut rmp_serde::Deserializer::new(
                        &mut cursor,
                    ))?;
                }
            }
            ensure!(
                cursor.position() == payload.len() as u64,
                "extra MessagePack data"
            );
            Ok(Value::Map(values))
        })();
        location.byte += cursor.position();
        let row = row?;
        ensure!(row.is_map(), "record must be a map");
        validate(&row)?;
        drop(parse);
        if !emit(row)? {
            break;
        }
        location.record += 1;
    }
    Ok(())
}
fn json_value(value: &jiter::JsonValue<'_>, strings: Option<&[Value]>) -> Result<Value> {
    use jiter::JsonValue as J;
    Ok(match value {
        J::Null => Value::Nil,
        J::Bool(b) => Value::Boolean(*b),
        J::Int(i) => Value::from(*i),
        J::BigInt(i) => {
            let digits = i.to_string();
            if let Ok(n) = digits.parse::<i64>() {
                Value::from(n)
            } else {
                Value::from(digits.parse::<u64>()?)
            }
        }
        J::Float(f) => Value::F64(*f),
        J::Str(s) => match strings {
            Some(values) => values[s.parse::<usize>()?].clone(),
            None => Value::from(s.as_ref()),
        },
        J::Array(a) => Value::Array(
            a.iter()
                .map(|v| json_value(v, strings))
                .collect::<Result<_>>()?,
        ),
        J::Object(m) => {
            let mut map = std::collections::BTreeMap::new();
            for (k, v) in m.iter() {
                let key = match strings {
                    Some(values) => values[k.parse::<usize>()?].as_str().ok_or_else(|| {
                        anyhow::anyhow!("Arrow field names require valid Unicode")
                    })?,
                    None => k.as_ref(),
                };
                map.insert(key, json_value(v, strings)?);
            }
            Value::Map(map.into_iter().map(|(k, v)| (Value::from(k), v)).collect())
        }
        #[allow(unreachable_patterns)]
        _ => Value::Nil,
    })
}
// serde_json's byte visitor preserves lone surrogates as WTF-8. Replace every
// string token with an index while jiter parses Python's non-finite numbers.
// Replacing all strings prevents placeholder collisions with user values.
struct StringBytes;
impl<'de> serde::de::Visitor<'de> for StringBytes {
    type Value = Vec<u8>;
    fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("a JSON string")
    }
    fn visit_bytes<E: serde::de::Error>(self, bytes: &[u8]) -> std::result::Result<Vec<u8>, E> {
        Ok(bytes.to_vec())
    }
}
fn json_tokens(line: &str) -> Result<(String, Vec<Value>)> {
    use serde::Deserializer;
    let mut out = String::with_capacity(line.len());
    let mut strings = Vec::new();
    let mut i = 0;
    let bytes = line.as_bytes();
    let mut start = 0;
    while i < bytes.len() {
        if bytes[i] != b'"' {
            i += 1;
            continue;
        }
        out.push_str(&line[start..i]);
        let token_start = i;
        i += 1;
        while i < bytes.len() {
            if bytes[i] == b'\\' {
                i = (i + 2).min(bytes.len());
            } else if bytes[i] == b'"' {
                i += 1;
                break;
            } else {
                i += 1;
            }
        }
        let mut decoder = serde_json::Deserializer::from_str(&line[token_start..i]);
        let decoded = decoder.deserialize_bytes(StringBytes)?;
        decoder.end()?;
        out.push_str(&format!("\"{}\"", strings.len()));
        strings.push(match String::from_utf8(decoded) {
            Ok(s) => Value::from(s),
            Err(e) => Value::Binary(e.into_bytes()),
        });
        start = i;
    }
    out.push_str(&line[start..]);
    Ok((out, strings))
}
fn selected_json(line: &str, fields: &[Field], strings: Option<&[Value]>) -> Result<Value> {
    let mut parser = jiter::Jiter::new(line.as_bytes()).with_allow_inf_nan();
    let mut key = parser.next_object()?;
    let mut values = Vec::new();
    while let Some(name) = key {
        // A key that is not valid Unicode cannot name a projected field.
        let name = match strings {
            Some(values) => values[name.parse::<usize>()?].as_str(),
            None => Some(name),
        };
        match name.filter(|name| fields.iter().any(|f| f.from == *name)) {
            Some(name) => {
                let name = name.to_owned();
                let value = {
                    let _parse = crate::profile::Timer::new(2);
                    parser.next_value()?
                };
                let _value = crate::profile::Timer::new(3);
                values.push((Value::from(name), json_value(&value, strings)?));
            }
            None => {
                let _parse = crate::profile::Timer::new(2);
                parser.next_skip()?;
            }
        }
        key = parser.next_key()?;
    }
    parser.finish()?;
    Ok(Value::Map(values))
}
fn jsonl(
    input: Input,
    fields: &[Field],
    limit: usize,
    emit: &mut impl FnMut(Value) -> Result<bool>,
    location: &mut Location,
) -> Result<()> {
    let mut decoder = flate2::bufread::GzDecoder::new(BufReader::new(input));
    {
        let mut lines = BufReader::new(&mut decoder);
        let mut bytes = Vec::new();
        loop {
            bytes.clear();
            let decompress = crate::profile::Timer::new(1);
            let read = (&mut lines)
                .take(limit as u64 + 1)
                .read_until(b'\n', &mut bytes);
            location.byte += bytes.len() as u64;
            let n = read?;
            let line = std::str::from_utf8(&bytes)?;
            ensure!(n <= limit, "JSON line exceeds maxRecordBytes");
            drop(decompress);
            if n == 0 {
                break;
            }
            {
                match selected_json(line, fields, None) {
                    Ok(row) => {
                        if !emit(row)? {
                            return Ok(());
                        }
                        location.record += 1;
                    }
                    Err(_) => {
                        // jiter rejects lone surrogates even in skipped strings.
                        // Normalize string tokens, then still skip unused arrays.
                        let Ok((normalized, strings)) = json_tokens(line) else {
                            continue;
                        };
                        match selected_json(&normalized, fields, Some(&strings)) {
                            Ok(row) => {
                                if !emit(row)? {
                                    return Ok(());
                                }
                                location.record += 1;
                            }
                            Err(error) => {
                                let mut syntax =
                                    jiter::Jiter::new(normalized.as_bytes()).with_allow_inf_nan();
                                if normalized.trim_start().starts_with('{')
                                    && syntax.next_skip().is_ok()
                                    && syntax.finish().is_ok()
                                {
                                    return Err(error);
                                }
                            }
                        }
                    }
                }
                continue;
            }
        }
    }
    ensure!(
        decoder.into_inner().fill_buf()?.is_empty(),
        "extra data after gzip member"
    );
    Ok(())
}

#[derive(Clone)]
struct ParquetObject(Arc<Mutex<Object>>);
impl parquet::file::reader::Length for ParquetObject {
    fn len(&self) -> u64 {
        self.0.lock().unwrap().size
    }
}
impl parquet::file::reader::ChunkReader for ParquetObject {
    type T = Input;
    fn get_read(&self, start: u64) -> parquet::errors::Result<Input> {
        Ok(Input::at(self.0.clone(), start))
    }
    fn get_bytes(&self, start: u64, length: usize) -> parquet::errors::Result<bytes::Bytes> {
        self.0
            .lock()
            .unwrap()
            .range(start, start + length as u64)
            .map(bytes::Bytes::from)
            .map_err(|e| parquet::errors::ParquetError::General(e.to_string()))
    }
}
fn trim(value: &mut Value, widths: &[usize]) {
    if let Some((width, inner)) = widths.split_first()
        && let Value::Array(values) = value
    {
        values.truncate(*width);
        values.shrink_to_fit();
        for value in values {
            trim(value, inner);
        }
    }
}
pub struct Shard {
    pub offset: usize,
    pub columns: RecordBatch,
}
impl Shard {
    pub fn num_rows(&self) -> usize {
        self.offset + self.columns.num_rows()
    }
}
pub fn read(
    source: &Source,
    encoding: &str,
    client: &Client,
    fields: &[Field],
    start: usize,
    sample: bool,
    config: &crate::config::Config,
) -> Result<Shard> {
    let mut location = Location::default();
    let result = (|| -> Result<Shard> {
        let object = Object::open(source, client)?;
        if encoding == "parquet" {
            return parquet_read(
                object,
                fields,
                start,
                sample,
                config,
                source.rows,
                &mut location,
            );
        }
        let mut chunks = Vec::new();
        let mut rows = Vec::new();
        let mut count = 0;
        let mut emit = |mut row: Value| -> Result<bool> {
            count += 1;
            ensure!(
                count <= source.rows.unwrap_or(config.max_shard_rows),
                "shard exceeds declared rows or maxShardRows"
            );
            if count <= start {
                return Ok(true);
            }
            // Retain only the declared prefix on every axis while accumulating
            // one builder chunk, so retained rows stay within the memory bound.
            let Value::Map(ref mut map) = row else {
                anyhow::bail!("record must be a map");
            };
            for (key, value) in map {
                let mut widths: Vec<usize> = Vec::new();
                for field in fields
                    .iter()
                    .filter(|f| Some(f.from.as_str()) == key.as_str())
                {
                    for (axis, width) in field.dimensions().into_iter().enumerate() {
                        match widths.get_mut(axis) {
                            Some(current) => *current = (*current).max(width),
                            None => widths.push(width),
                        }
                    }
                }
                trim(value, &widths);
            }
            rows.push(row);
            if rows.len() == 128 || sample {
                chunks.push(columns::project_values(
                    &rows.iter().collect::<Vec<_>>(),
                    fields,
                )?);
                rows.clear();
            }
            Ok(!sample)
        };
        let mut input = Input::new(object);
        if encoding == "msgpack.zstf" {
            msgpack(
                &mut input,
                fields,
                config.max_record_bytes,
                &mut emit,
                &mut location,
            )?;
        } else {
            jsonl(
                input,
                fields,
                config.max_record_bytes,
                &mut emit,
                &mut location,
            )?;
        }
        if !sample
            && encoding == "msgpack.zstf"
            && let Some(n) = source.rows
        {
            ensure!(count == n, "MessagePack row count mismatch");
        }
        if !rows.is_empty() {
            chunks.push(columns::project_values(
                &rows.iter().collect::<Vec<_>>(),
                fields,
            )?);
        }
        let schema = columns::project_values(&[], fields)?.schema();
        Ok(Shard {
            offset: start,
            columns: arrow::compute::concat_batches(&schema, chunks.iter())?,
        })
    })();
    crate::profile::shard(
        &source.uri,
        sample,
        location.byte,
        result
            .as_ref()
            .map_or(0, |s| s.columns.get_array_memory_size() as u64),
    );
    result.map_err(|error| {
        let position = if encoding == "parquet" {
            format!("row_group={} row={}", location.group, location.record)
        } else {
            format!("record={} byte_offset={}", location.record, location.byte)
        };
        anyhow::anyhow!(
            "shard={} {position}: {error:#}",
            serde_json::to_string(&source.uri).unwrap()
        )
    })
}

fn parquet_read(
    object: Object,
    fields: &[Field],
    start: usize,
    sample: bool,
    config: &crate::config::Config,
    declared_rows: Option<usize>,
    location: &mut Location,
) -> Result<Shard> {
    use parquet::arrow::{
        ProjectionMask,
        arrow_reader::{
            ArrowReaderMetadata, ArrowReaderOptions, ParquetRecordBatchReaderBuilder, RowSelection,
            RowSelector,
        },
    };
    let input = ParquetObject(Arc::new(Mutex::new(object)));
    let metadata = ArrowReaderMetadata::load(&input, ArrowReaderOptions::default())?;
    let total = metadata.metadata().file_metadata().num_rows() as usize;
    ensure!(
        declared_rows.is_none_or(|n| n == total),
        "Parquet row count mismatch"
    );
    ensure!(
        total <= config.max_shard_rows && start <= total,
        "Parquet exceeds maxShardRows or cursor bounds"
    );
    let indices: Vec<_> = metadata
        .schema()
        .fields()
        .iter()
        .enumerate()
        .filter(|(_, f)| fields.iter().any(|p| p.from == *f.name()))
        .map(|(i, _)| i)
        .collect();
    let mask = ProjectionMask::roots(metadata.metadata().file_metadata().schema_descr(), indices);
    let mut skipped = 0;
    let mut chunks = Vec::new();
    for (i, group) in metadata.metadata().row_groups().iter().enumerate() {
        let rows = group.num_rows() as usize;
        if skipped + rows <= start {
            skipped += rows;
            continue;
        }
        location.group = i;
        location.record = start.saturating_sub(skipped);
        let bytes: i64 = group
            .columns()
            .iter()
            .enumerate()
            .filter(|(i, _)| mask.leaf_included(*i))
            .map(|(_, c)| c.uncompressed_size())
            .sum();
        ensure!(
            bytes >= 0 && bytes as usize <= config.max_row_group_bytes,
            "Parquet row group exceeds maxRowGroupBytes"
        );
        let selection = RowSelection::from(vec![
            RowSelector::skip(location.record),
            RowSelector::select(if sample { 1 } else { rows - location.record }),
        ]);
        let reader =
            ParquetRecordBatchReaderBuilder::new_with_metadata(input.clone(), metadata.clone())
                .with_projection(mask.clone())
                .with_row_groups(vec![i])
                .with_row_selection(selection)
                .with_batch_size(if sample { 1 } else { 128 })
                .build()?;
        for batch in reader {
            let batch = batch?;
            chunks.push(columns::project(&batch, fields)?);
            location.record += batch.num_rows();
            if sample {
                break;
            }
        }
        if sample {
            break;
        }
        skipped += rows;
    }
    let schema = columns::project_values(&[], fields)?.schema();
    Ok(Shard {
        offset: start,
        columns: arrow::compute::concat_batches(&schema, chunks.iter())?,
    })
}