torsh-tensor 0.1.2

Tensor implementation for ToRSh with PyTorch-compatible 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
//! Streaming I/O with Progress Reporting for Large Tensors
//!
//! This module provides streaming serialization capabilities for large tensors
//! with progress reporting, memory management, and efficient I/O operations.

use super::common::SerializationOptions;
use crate::{Tensor, TensorElement};
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Seek, Write};
use std::sync::Arc;
use std::time::{Duration, Instant};
use torsh_core::error::{Result, TorshError};

/// Progress callback function type
///
/// Called periodically during streaming operations to report progress.
///
/// # Parameters
/// * `bytes_processed` - Number of bytes processed so far
/// * `total_bytes` - Total number of bytes to process
/// * `elapsed_time` - Time elapsed since operation started
pub type ProgressCallback = Arc<dyn Fn(u64, u64, Duration) + Send + Sync>;

/// Configuration for streaming operations
///
/// Controls various aspects of streaming I/O including chunk sizes,
/// buffering, compression, and memory limits.
#[derive(Debug, Clone)]
pub struct StreamingConfig {
    /// Size of each chunk in bytes
    pub chunk_size: usize,
    /// Progress reporting interval in bytes
    pub progress_interval: u64,
    /// Buffer size for I/O operations
    pub buffer_size: usize,
    /// Enable compression for chunks
    pub compress_chunks: bool,
    /// Maximum memory usage (bytes) before spilling to disk
    pub memory_limit: usize,
}

impl Default for StreamingConfig {
    fn default() -> Self {
        Self {
            chunk_size: 64 * 1024 * 1024,   // 64 MB chunks
            progress_interval: 1024 * 1024, // Report every 1 MB
            buffer_size: 8 * 1024,          // 8 KB buffer
            compress_chunks: false,
            memory_limit: 1024 * 1024 * 1024, // 1 GB memory limit
        }
    }
}

impl StreamingConfig {
    /// Create configuration optimized for speed
    ///
    /// # Returns
    /// * `Self` - Configuration for fastest streaming
    pub fn fast() -> Self {
        Self {
            chunk_size: 128 * 1024 * 1024,       // Larger chunks
            progress_interval: 16 * 1024 * 1024, // Less frequent reporting
            buffer_size: 64 * 1024,              // Larger buffer
            compress_chunks: false,
            memory_limit: 2 * 1024 * 1024 * 1024, // More memory
        }
    }

    /// Create configuration optimized for memory usage
    ///
    /// # Returns
    /// * `Self` - Configuration for minimal memory usage
    pub fn low_memory() -> Self {
        Self {
            chunk_size: 1024 * 1024,         // Smaller chunks
            progress_interval: 512 * 1024,   // More frequent reporting
            buffer_size: 4 * 1024,           // Smaller buffer
            compress_chunks: true,           // Enable compression
            memory_limit: 128 * 1024 * 1024, // Limited memory
        }
    }
}

/// Streaming tensor writer with progress reporting
///
/// Provides efficient streaming serialization of large tensors with
/// progress callbacks, compression, and memory management.
pub struct StreamingTensorWriter<W: Write + Seek> {
    writer: BufWriter<W>,
    config: StreamingConfig,
    bytes_written: u64,
    total_bytes: u64,
    last_progress_report: u64,
    start_time: Instant,
    progress_callback: Option<ProgressCallback>,
}

impl<W: Write + Seek> StreamingTensorWriter<W> {
    /// Create a new streaming writer
    ///
    /// # Arguments
    /// * `writer` - Output writer
    /// * `config` - Streaming configuration
    ///
    /// # Returns
    /// * `Self` - New streaming writer instance
    pub fn new(writer: W, config: StreamingConfig) -> Self {
        let buf_writer = BufWriter::with_capacity(config.buffer_size, writer);

        Self {
            writer: buf_writer,
            config,
            bytes_written: 0,
            total_bytes: 0,
            last_progress_report: 0,
            start_time: Instant::now(),
            progress_callback: None,
        }
    }

    /// Set progress callback
    ///
    /// # Arguments
    /// * `callback` - Progress callback function
    pub fn with_progress_callback(mut self, callback: ProgressCallback) -> Self {
        self.progress_callback = Some(callback);
        self
    }

    /// Start writing a tensor with known total size
    ///
    /// # Arguments
    /// * `total_bytes` - Total number of bytes that will be written
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn begin_tensor(&mut self, total_bytes: u64) -> Result<()> {
        self.total_bytes = total_bytes;
        self.bytes_written = 0;
        self.last_progress_report = 0;
        self.start_time = Instant::now();

        // Report initial progress
        self.report_progress();

        Ok(())
    }

    /// Write a chunk of tensor data
    ///
    /// # Arguments
    /// * `data` - Data chunk to write
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn write_chunk(&mut self, data: &[u8]) -> Result<()> {
        // Apply compression if enabled.
        //
        // When compression is on the chunk is framed as:
        //   [4 bytes: compressed_len as u32 LE] [compressed_bytes…]
        // This framing lets the reader reconstruct chunk boundaries
        // without external metadata.
        let chunk_data = if self.config.compress_chunks {
            #[cfg(feature = "serialize")]
            {
                let compressed = oxiarc_zstd::compress_with_level(data, 3).map_err(|e| {
                    TorshError::SerializationError(format!(
                        "Failed to compress streaming chunk: {}",
                        e
                    ))
                })?;
                let len = compressed.len() as u32;
                let mut framed = Vec::with_capacity(4 + compressed.len());
                framed.extend_from_slice(&len.to_le_bytes());
                framed.extend_from_slice(&compressed);
                framed
            }
            #[cfg(not(feature = "serialize"))]
            {
                data.to_vec()
            }
        } else {
            data.to_vec()
        };

        // Write chunk
        self.writer
            .write_all(&chunk_data)
            .map_err(|e| TorshError::SerializationError(format!("Failed to write chunk: {}", e)))?;

        // Update progress
        self.bytes_written += data.len() as u64;

        // Report progress if needed
        if self.bytes_written - self.last_progress_report >= self.config.progress_interval {
            self.report_progress();
            self.last_progress_report = self.bytes_written;
        }

        Ok(())
    }

    /// Finish writing and flush all data
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn finish(mut self) -> Result<()> {
        self.writer.flush().map_err(|e| {
            TorshError::SerializationError(format!("Failed to flush writer: {}", e))
        })?;

        // Final progress report
        self.report_progress();

        Ok(())
    }

    /// Report current progress
    fn report_progress(&self) {
        if let Some(ref callback) = self.progress_callback {
            let elapsed = self.start_time.elapsed();
            callback(self.bytes_written, self.total_bytes, elapsed);
        }
    }
}

/// Streaming tensor reader with progress reporting
///
/// Provides efficient streaming deserialization of large tensors with
/// progress callbacks and memory management.
pub struct StreamingTensorReader<R: Read> {
    reader: BufReader<R>,
    config: StreamingConfig,
    bytes_read: u64,
    total_bytes: u64,
    last_progress_report: u64,
    start_time: Instant,
    progress_callback: Option<ProgressCallback>,
    /// Decompressed data buffered from the most recent compressed frame.
    /// Only populated when `config.compress_chunks` is true and the
    /// `serialize` feature is enabled.
    #[cfg(feature = "serialize")]
    decomp_buf: Vec<u8>,
    /// Read cursor into `decomp_buf`.
    #[cfg(feature = "serialize")]
    decomp_cursor: usize,
}

impl<R: Read> StreamingTensorReader<R> {
    /// Create a new streaming reader
    ///
    /// # Arguments
    /// * `reader` - Input reader
    /// * `config` - Streaming configuration
    ///
    /// # Returns
    /// * `Self` - New streaming reader instance
    pub fn new(reader: R, config: StreamingConfig) -> Self {
        let buf_reader = BufReader::with_capacity(config.buffer_size, reader);

        Self {
            reader: buf_reader,
            config,
            bytes_read: 0,
            total_bytes: 0,
            last_progress_report: 0,
            start_time: Instant::now(),
            progress_callback: None,
            #[cfg(feature = "serialize")]
            decomp_buf: Vec::new(),
            #[cfg(feature = "serialize")]
            decomp_cursor: 0,
        }
    }

    /// Set progress callback
    ///
    /// # Arguments
    /// * `callback` - Progress callback function
    pub fn with_progress_callback(mut self, callback: ProgressCallback) -> Self {
        self.progress_callback = Some(callback);
        self
    }

    /// Start reading a tensor with known total size
    ///
    /// # Arguments
    /// * `total_bytes` - Total number of bytes that will be read
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn begin_tensor(&mut self, total_bytes: u64) -> Result<()> {
        self.total_bytes = total_bytes;
        self.bytes_read = 0;
        self.last_progress_report = 0;
        self.start_time = Instant::now();

        // Report initial progress
        self.report_progress();

        Ok(())
    }

    /// Read a chunk of tensor data
    ///
    /// When `config.compress_chunks` is true each compressed frame in the
    /// stream is length-prefixed (4 bytes LE u32).  Decompressed bytes are
    /// buffered internally so the caller's `buffer` can be fully satisfied
    /// even when it is smaller than one decompressed frame.
    ///
    /// # Arguments
    /// * `buffer` - Buffer to read data into
    ///
    /// # Returns
    /// * `Result<usize>` - Number of bytes read or error
    pub fn read_chunk(&mut self, buffer: &mut [u8]) -> Result<usize> {
        if self.config.compress_chunks {
            #[cfg(feature = "serialize")]
            {
                // Serve remaining bytes from the decompression buffer first.
                if self.decomp_cursor < self.decomp_buf.len() {
                    let available = self.decomp_buf.len() - self.decomp_cursor;
                    let to_copy = available.min(buffer.len());
                    buffer[..to_copy].copy_from_slice(
                        &self.decomp_buf[self.decomp_cursor..self.decomp_cursor + to_copy],
                    );
                    self.decomp_cursor += to_copy;
                    self.bytes_read += to_copy as u64;
                    if self.bytes_read - self.last_progress_report >= self.config.progress_interval
                    {
                        self.report_progress();
                        self.last_progress_report = self.bytes_read;
                    }
                    return Ok(to_copy);
                }

                // Read the next compressed frame: 4-byte length prefix then payload.
                let mut len_buf = [0u8; 4];
                match self.reader.read_exact(&mut len_buf) {
                    Ok(()) => {}
                    Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                        return Ok(0);
                    }
                    Err(e) => {
                        return Err(TorshError::SerializationError(format!(
                            "Failed to read compressed frame length: {}",
                            e
                        )));
                    }
                }
                let compressed_len = u32::from_le_bytes(len_buf) as usize;
                let mut compressed = vec![0u8; compressed_len];
                self.reader.read_exact(&mut compressed).map_err(|e| {
                    TorshError::SerializationError(format!(
                        "Failed to read compressed frame payload: {}",
                        e
                    ))
                })?;

                self.decomp_buf = oxiarc_zstd::decompress(&compressed).map_err(|e| {
                    TorshError::SerializationError(format!(
                        "Failed to decompress streaming chunk: {}",
                        e
                    ))
                })?;
                self.decomp_cursor = 0;

                // Copy as many bytes as fit in the caller's buffer.
                let to_copy = self.decomp_buf.len().min(buffer.len());
                buffer[..to_copy].copy_from_slice(&self.decomp_buf[..to_copy]);
                self.decomp_cursor = to_copy;
                self.bytes_read += to_copy as u64;
                if self.bytes_read - self.last_progress_report >= self.config.progress_interval {
                    self.report_progress();
                    self.last_progress_report = self.bytes_read;
                }
                return Ok(to_copy);
            }
            #[cfg(not(feature = "serialize"))]
            {
                // Compression disabled at compile time — fall through to plain read.
            }
        }

        // Plain (uncompressed) read path.
        let bytes_read = self
            .reader
            .read(buffer)
            .map_err(|e| TorshError::SerializationError(format!("Failed to read chunk: {}", e)))?;

        self.bytes_read += bytes_read as u64;

        if self.bytes_read - self.last_progress_report >= self.config.progress_interval {
            self.report_progress();
            self.last_progress_report = self.bytes_read;
        }

        Ok(bytes_read)
    }

    /// Read exact number of bytes
    ///
    /// # Arguments
    /// * `buffer` - Buffer to read data into
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn read_exact(&mut self, buffer: &mut [u8]) -> Result<()> {
        self.reader.read_exact(buffer).map_err(|e| {
            TorshError::SerializationError(format!("Failed to read exact bytes: {}", e))
        })?;

        // Update progress
        self.bytes_read += buffer.len() as u64;

        // Report progress if needed
        if self.bytes_read - self.last_progress_report >= self.config.progress_interval {
            self.report_progress();
            self.last_progress_report = self.bytes_read;
        }

        Ok(())
    }

    /// Finish reading and report final progress
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn finish(self) -> Result<()> {
        // Final progress report
        self.report_progress();
        Ok(())
    }

    /// Report current progress
    fn report_progress(&self) {
        if let Some(ref callback) = self.progress_callback {
            let elapsed = self.start_time.elapsed();
            callback(self.bytes_read, self.total_bytes, elapsed);
        }
    }
}

/// Utility functions for streaming operations
pub mod utils {
    use super::*;

    /// Stream serialize a tensor to file with progress reporting
    ///
    /// # Arguments
    /// * `tensor` - Tensor to serialize
    /// * `path` - Output file path
    /// * `options` - Serialization options
    /// * `config` - Streaming configuration
    /// * `progress_callback` - Optional progress callback
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, error otherwise
    pub fn stream_serialize_to_file<T: TensorElement>(
        tensor: &Tensor<T>,
        path: &std::path::Path,
        _options: &SerializationOptions,
        config: StreamingConfig,
        progress_callback: Option<ProgressCallback>,
    ) -> Result<()> {
        let file = File::create(path)
            .map_err(|e| TorshError::SerializationError(format!("Failed to create file: {}", e)))?;

        let mut writer = StreamingTensorWriter::new(file, config);

        if let Some(callback) = progress_callback {
            writer = writer.with_progress_callback(callback);
        }

        // Calculate total size
        let data = tensor.data()?;
        let total_bytes = data.len() * std::mem::size_of::<T>();

        writer.begin_tensor(total_bytes as u64)?;

        // Write data in chunks
        let data_bytes =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, total_bytes) };

        let chunk_size = writer.config.chunk_size;
        for chunk in data_bytes.chunks(chunk_size) {
            writer.write_chunk(chunk)?;
        }

        writer.finish()?;

        Ok(())
    }

    /// Create a simple progress callback that prints to console
    ///
    /// # Returns
    /// * `ProgressCallback` - Progress callback function
    pub fn console_progress_callback() -> ProgressCallback {
        Arc::new(|bytes_processed, total_bytes, elapsed| {
            let percentage = if total_bytes > 0 {
                (bytes_processed as f64 / total_bytes as f64) * 100.0
            } else {
                0.0
            };

            let rate = if elapsed.as_secs_f64() > 0.0 {
                bytes_processed as f64 / elapsed.as_secs_f64() / 1024.0 / 1024.0
            } else {
                0.0
            };

            println!(
                "Progress: {:.1}% ({}/{} bytes) - {:.1} MB/s - {:?}",
                percentage, bytes_processed, total_bytes, rate, elapsed
            );
        })
    }
}