torsh-functional 0.1.2

Functional programming utilities for ToRSh tensors
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Tensor splitting operations
//!
//! This module provides comprehensive tensor splitting functionality for dividing tensors
//! into multiple sub-tensors along specified dimensions. These operations are essential for
//! data partitioning, parallel processing, and implementing neural network architectures
//! that require tensor decomposition.

use torsh_core::Result as TorshResult;
use torsh_tensor::Tensor;

/// Split tensor into multiple sub-tensors
///
/// ## Mathematical Background
///
/// Tensor splitting partitions a tensor A ∈ ℝ^(d₁×d₂×...×dₙ) along dimension k into
/// multiple sub-tensors {B₁, B₂, ..., Bₘ} such that:
///
/// ```text
/// concatenate([B₁, B₂, ..., Bₘ], dim=k) = A
/// ```text
///
/// ## Splitting Modes
///
/// ### Fixed Size Splitting
/// For split size s, creates ⌈dₖ/s⌉ sub-tensors where:
/// - First ⌊dₖ/s⌋ tensors have size s along dimension k
/// - Last tensor has size dₖ mod s (if non-zero)
///
/// ### Section-Based Splitting
/// For n sections, creates n sub-tensors with sizes:
/// - Base size: b = ⌊dₖ/n⌋
/// - First r = dₖ mod n tensors: size b+1
/// - Remaining n-r tensors: size b
///
/// ### Index-Based Splitting
/// For indices [i₁, i₂, ..., iₘ], creates m+1 sub-tensors:
/// - A[:i₁], A[i₁:i₂], ..., A[iₘ:]
///
/// ## Parameters
/// * `tensor` - Input tensor to split
/// * `split_size_or_sections` - Splitting specification (size, sections, or indices)
/// * `dim` - Dimension along which to split (negative indexing supported)
///
/// ## Returns
/// * Vector of sub-tensors resulting from the split operation
///
/// ## Applications
/// - **Data batching**: Split large datasets into smaller batches
/// - **Parallel processing**: Distribute tensor chunks across workers
/// - **Memory management**: Process large tensors in smaller chunks
/// - **Model parallelism**: Split layers across multiple devices
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::split;
/// # use torsh_functional::manipulation::SplitArg;
/// # use torsh_tensor::creation::ones;
/// let tensor = ones(&[12, 4])?;
///
/// // Split into chunks of size 3
/// let chunks = split(&tensor, SplitArg::Size(3), 0)?; // 4 chunks of [3,4]
///
/// // Split into 4 equal sections
/// let sections = split(&tensor, SplitArg::Sections(4), 0)?; // 4 chunks of [3,4]
///
/// // Split at specific indices
/// let splits = split(&tensor, SplitArg::Indices(vec![3, 8]), 0)?; // [3,4], [5,4], [4,4]
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn split(
    tensor: &Tensor,
    split_size_or_sections: SplitArg,
    dim: isize,
) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    let ndim = shape.ndim() as isize;

    // Normalize dimension
    let dim = if dim < 0 { ndim + dim } else { dim } as usize;

    if dim >= shape.ndim() {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            &format!(
                "Dimension {} out of range for tensor with {} dimensions",
                dim,
                shape.ndim()
            ),
            "split",
        ));
    }

    match split_size_or_sections {
        SplitArg::Size(size) => {
            // Split into chunks of given size
            let dim_size = shape.dims()[dim];
            let num_splits = dim_size.div_ceil(size);

            let mut splits = Vec::new();
            for i in 0..num_splits {
                let start = i * size;
                let end = ((i + 1) * size).min(dim_size);

                // Create slice for this split
                let split = tensor.slice(dim as usize, start, end)?.to_tensor()?;
                splits.push(split);
            }

            Ok(splits)
        }
        SplitArg::Sections(sections) => {
            // Split into given number of sections
            let dim_size = shape.dims()[dim];
            let base_size = dim_size / sections;
            let remainder = dim_size % sections;

            let mut splits = Vec::new();
            let mut offset = 0;

            for i in 0..sections {
                let size = if i < remainder {
                    base_size + 1
                } else {
                    base_size
                };

                // Create slice for this split
                let split = tensor
                    .slice(dim as usize, offset, offset + size)?
                    .to_tensor()?;
                splits.push(split);

                offset += size;
            }

            Ok(splits)
        }
        SplitArg::Indices(indices) => {
            // Split at the specified indices
            let mut splits = Vec::new();
            let mut start = 0;

            for &index in &indices {
                let split = tensor.slice(dim as usize, start, index)?.to_tensor()?;
                splits.push(split);
                start = index;
            }

            // Add final split from last index to end
            let dim_size = shape.dims()[dim];
            if start < dim_size {
                let split = tensor.slice(dim as usize, start, dim_size)?.to_tensor()?;
                splits.push(split);
            }

            Ok(splits)
        }
    }
}

/// Split argument specification for tensor splitting operations
///
/// ## Variants
///
/// ### Size
/// Split into chunks of specified size along the dimension.
/// The last chunk may be smaller if the dimension size is not evenly divisible.
///
/// ### Sections
/// Split into the specified number of approximately equal sections.
/// If not evenly divisible, earlier sections will be one element larger.
///
/// ### Indices
/// Split at the specified indices, creating len(indices)+1 sub-tensors.
/// Indices must be sorted and within bounds of the dimension.
#[derive(Debug, Clone)]
pub enum SplitArg {
    /// Split into chunks of fixed size
    Size(usize),
    /// Split into specified number of sections
    Sections(usize),
    /// Split at specified indices
    Indices(Vec<usize>),
}

/// Split tensor into approximately equal chunks
///
/// ## Mathematical Background
///
/// Chunks a tensor into approximately equal pieces along the specified dimension.
/// For tensor with dimension size d and n chunks:
///
/// ```text
/// chunk_size = ⌈d/n⌉
/// num_full_chunks = n - (n * chunk_size - d)
/// ```text
///
/// The first `num_full_chunks` will have size `chunk_size`, and remaining chunks
/// will have size `chunk_size - 1`.
///
/// ## Parameters
/// * `tensor` - Input tensor to chunk
/// * `chunks` - Number of chunks to create
/// * `dim` - Dimension along which to chunk (negative indexing supported)
///
/// ## Returns
/// * Vector of approximately equal-sized tensor chunks
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::chunk;
/// # use torsh_tensor::creation::ones;
/// let tensor = ones(&[10, 3])?; // 10 rows, 3 columns
///
/// // Split into 3 chunks along first dimension
/// let chunks = chunk(&tensor, 3, 0)?; // [4,3], [3,3], [3,3]
///
/// // Split into 4 chunks along second dimension
/// let chunks = chunk(&tensor, 4, 1)?; // [10,1], [10,1], [10,1], [10,0] (empty)
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn chunk(tensor: &Tensor, chunks: usize, dim: isize) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    let ndim = shape.ndim() as isize;

    // Normalize dimension
    let dim = if dim < 0 { ndim + dim } else { dim } as usize;

    if dim >= shape.ndim() {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            &format!(
                "Dimension {} out of range for tensor with {} dimensions",
                dim,
                shape.ndim()
            ),
            "chunk",
        ));
    }

    split(tensor, SplitArg::Sections(chunks), dim as isize)
}

/// Split tensor into sections at specified indices
///
/// ## Mathematical Background
///
/// Performs tensor splitting at explicitly specified indices along a dimension.
/// For tensor A with dimension size d and indices [i₁, i₂, ..., iₘ]:
///
/// ```text
/// Result = [A[..., :i₁, ...], A[..., i₁:i₂, ...], ..., A[..., iₘ:, ...]]
/// ```text
///
/// Where the ellipsis represents all other dimensions.
///
/// ## Index Validation
/// - All indices must be within bounds: 0 ≤ iⱼ ≤ d
/// - Indices should be in ascending order for meaningful results
/// - Empty sections are allowed (consecutive identical indices)
///
/// ## Parameters
/// * `tensor` - Input tensor to split
/// * `indices_or_sections` - Either number of sections or explicit indices
/// * `dim` - Dimension along which to split (negative indexing supported)
///
/// ## Returns
/// * Vector of tensor sections
///
/// ## Applications
/// - **Sequence processing**: Split variable-length sequences at boundaries
/// - **Data preprocessing**: Extract regions of interest from images/signals
/// - **Batch processing**: Create non-uniform batches based on data characteristics
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::{tensor_split, TensorSplitArg};
/// # use torsh_tensor::creation::ones;
/// let tensor = ones(&[8, 4])?;
///
/// // Split into 3 sections
/// let sections = tensor_split(&tensor, TensorSplitArg::Sections(3), 0)?;
/// // Results: [3,4], [3,4], [2,4]
///
/// // Split at specific indices
/// let splits = tensor_split(&tensor, TensorSplitArg::Indices(vec![2, 5]), 0)?;
/// // Results: [2,4], [3,4], [3,4]
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn tensor_split(
    tensor: &Tensor,
    indices_or_sections: TensorSplitArg,
    dim: isize,
) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    let ndim = shape.ndim() as isize;

    // Normalize dimension
    let dim = if dim < 0 { ndim + dim } else { dim } as usize;

    if dim >= shape.ndim() {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            &format!(
                "Dimension {} out of range for tensor with {} dimensions",
                dim,
                shape.ndim()
            ),
            "tensor_split",
        ));
    }

    match indices_or_sections {
        TensorSplitArg::Sections(sections) => {
            split(tensor, SplitArg::Sections(sections), dim as isize)
        }
        TensorSplitArg::Indices(indices) => {
            let dim_size = shape.dims()[dim];
            let mut splits = Vec::new();
            let mut prev_idx = 0;

            for &idx in &indices {
                if idx > dim_size {
                    return Err(torsh_core::TorshError::invalid_argument_with_context(
                        &format!(
                            "Split index {} out of range for dimension size {}",
                            idx, dim_size
                        ),
                        "tensor_split",
                    ));
                }

                if idx > prev_idx {
                    let split = tensor.slice(dim as usize, prev_idx, idx)?.to_tensor()?;
                    splits.push(split);
                }
                prev_idx = idx;
            }

            // Add final split if needed
            if prev_idx < dim_size {
                let split = tensor
                    .slice(dim as usize, prev_idx, dim_size)?
                    .to_tensor()?;
                splits.push(split);
            }

            Ok(splits)
        }
    }
}

/// Split argument specification for tensor_split operations
///
/// ## Variants
///
/// ### Sections
/// Split into the specified number of approximately equal sections.
///
/// ### Indices
/// Split at the specified indices. The indices define the boundaries
/// where the tensor should be divided.
#[derive(Debug, Clone)]
pub enum TensorSplitArg {
    /// Split into specified number of sections
    Sections(usize),
    /// Split at specified indices
    Indices(Vec<usize>),
}

/// Split tensor horizontally (along second dimension)
///
/// ## Mathematical Background
///
/// Horizontal splitting divides a tensor along its second dimension (columns for 2D matrices).
/// For tensor A ∈ ℝ^(m×n×...), hsplit creates sub-tensors along dimension 1:
///
/// ```text
/// A = [A₁ | A₂ | ... | Aₖ]  (column-wise concatenation)
/// ```text
///
/// ## Requirements
/// - Input tensor must have at least 2 dimensions
/// - Equivalent to `tensor_split(tensor, indices_or_sections, 1)`
///
/// ## Parameters
/// * `tensor` - Input tensor (≥2D required)
/// * `indices_or_sections` - Split specification (sections or indices)
///
/// ## Returns
/// * Vector of horizontally split tensors
///
/// ## Applications
/// - **Image processing**: Split images into vertical strips
/// - **Feature extraction**: Separate different feature groups in matrices
/// - **Data analysis**: Split datasets by column groups
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::{hsplit, TensorSplitArg};
/// # use torsh_tensor::creation::ones;
/// let image = ones(&[100, 200, 3])?; // Height × Width × Channels
///
/// // Split into 4 vertical strips
/// let strips = hsplit(&image, TensorSplitArg::Sections(4))?;
/// // Each strip: [100, 50, 3]
///
/// // Split at specific column positions
/// let splits = hsplit(&image, TensorSplitArg::Indices(vec![50, 150]))?;
/// // Results: [100,50,3], [100,100,3], [100,50,3]
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn hsplit(tensor: &Tensor, indices_or_sections: TensorSplitArg) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    if shape.ndim() < 2 {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            "Input tensor must have at least 2 dimensions for hsplit",
            "hsplit",
        ));
    }

    tensor_split(tensor, indices_or_sections, 1)
}

/// Split tensor vertically (along first dimension)
///
/// ## Mathematical Background
///
/// Vertical splitting divides a tensor along its first dimension (rows for 2D matrices).
/// For tensor A ∈ ℝ^(m×n×...), vsplit creates sub-tensors along dimension 0:
///
/// ```text
/// A = [A₁; A₂; ...; Aₖ]  (row-wise concatenation)
/// ```text
///
/// ## Requirements
/// - Input tensor must have at least 2 dimensions
/// - Equivalent to `tensor_split(tensor, indices_or_sections, 0)`
///
/// ## Parameters
/// * `tensor` - Input tensor (≥2D required)
/// * `indices_or_sections` - Split specification (sections or indices)
///
/// ## Returns
/// * Vector of vertically split tensors
///
/// ## Applications
/// - **Image processing**: Split images into horizontal strips
/// - **Batch processing**: Divide batches into smaller sub-batches
/// - **Time series**: Split sequences into temporal segments
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::{vsplit, TensorSplitArg};
/// # use torsh_tensor::creation::ones;
/// let batch = ones(&[64, 784])?; // Batch size × Features
///
/// // Split into 4 mini-batches
/// let mini_batches = vsplit(&batch, TensorSplitArg::Sections(4))?;
/// // Each mini-batch: [16, 784]
///
/// // Split at specific row positions
/// let splits = vsplit(&batch, TensorSplitArg::Indices(vec![16, 48]))?;
/// // Results: [16,784], [32,784], [16,784]
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn vsplit(tensor: &Tensor, indices_or_sections: TensorSplitArg) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    if shape.ndim() < 2 {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            "Input tensor must have at least 2 dimensions for vsplit",
            "vsplit",
        ));
    }

    tensor_split(tensor, indices_or_sections, 0)
}

/// Split tensor along depth dimension (third dimension)
///
/// ## Mathematical Background
///
/// Depth splitting divides a tensor along its third dimension (depth for 3D tensors).
/// For tensor A ∈ ℝ^(m×n×d×...), dsplit creates sub-tensors along dimension 2:
///
/// ```text
/// A[:,:,k₁:k₂,:] for each split k₁:k₂
/// ```text
///
/// ## Requirements
/// - Input tensor must have at least 3 dimensions
/// - Equivalent to `tensor_split(tensor, indices_or_sections, 2)`
///
/// ## Parameters
/// * `tensor` - Input tensor (≥3D required)
/// * `indices_or_sections` - Split specification (sections or indices)
///
/// ## Returns
/// * Vector of depth-wise split tensors
///
/// ## Applications
/// - **3D data processing**: Split volumetric data along depth
/// - **Video analysis**: Split video frames into temporal chunks
/// - **Multi-channel data**: Separate different channels or modalities
/// - **Neural networks**: Split feature maps along channel dimension
///
/// ## Examples
/// ```rust
/// # use torsh_functional::manipulation::{dsplit, TensorSplitArg};
/// # use torsh_tensor::creation::ones;
/// let volume = ones(&[64, 64, 32])?; // Height × Width × Depth
///
/// // Split into 4 depth sections
/// let sections = dsplit(&volume, TensorSplitArg::Sections(4))?;
/// // Each section: [64, 64, 8]
///
/// // Split at specific depth positions
/// let splits = dsplit(&volume, TensorSplitArg::Indices(vec![8, 24]))?;
/// // Results: [64,64,8], [64,64,16], [64,64,8]
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
pub fn dsplit(tensor: &Tensor, indices_or_sections: TensorSplitArg) -> TorshResult<Vec<Tensor>> {
    let shape = tensor.shape();
    if shape.ndim() < 3 {
        return Err(torsh_core::TorshError::invalid_argument_with_context(
            "Input tensor must have at least 3 dimensions for dsplit",
            "dsplit",
        ));
    }

    tensor_split(tensor, indices_or_sections, 2)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::random_ops::randn;

    #[test]
    fn test_split() -> TorshResult<()> {
        // Test equal splits
        let tensor = randn(&[6, 4], None, None, None)?;
        let result = split(&tensor, SplitArg::Sections(3), 0)?;
        assert_eq!(result.len(), 3);
        for chunk in &result {
            assert_eq!(chunk.shape().dims(), &[2, 4]);
        }

        // Test split with indices
        let result = split(&tensor, SplitArg::Indices(vec![2, 4]), 0)?;
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[2, 4]);
        assert_eq!(result[1].shape().dims(), &[2, 4]);
        assert_eq!(result[2].shape().dims(), &[2, 4]);

        Ok(())
    }

    #[test]
    fn test_chunk() -> TorshResult<()> {
        // Test chunking along dimension 0
        let tensor = randn(&[8, 3], None, None, None)?;
        let result = chunk(&tensor, 3, 0)?;

        // Should create 3 chunks of sizes [3, 3, 2]
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[3, 3]);
        assert_eq!(result[1].shape().dims(), &[3, 3]);
        assert_eq!(result[2].shape().dims(), &[2, 3]);

        // Test chunking along dimension 1
        let result = chunk(&tensor, 2, 1)?;
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].shape().dims(), &[8, 2]); // ceil(3/2) = 2
        assert_eq!(result[1].shape().dims(), &[8, 1]); // remaining 1

        Ok(())
    }

    #[test]
    fn test_tensor_split() -> TorshResult<()> {
        // Test with sections
        let tensor = randn(&[6, 4], None, None, None)?;
        let result = tensor_split(&tensor, TensorSplitArg::Sections(3), 0)?;
        assert_eq!(result.len(), 3);
        for chunk in &result {
            assert_eq!(chunk.shape().dims(), &[2, 4]);
        }

        // Test with indices
        let result = tensor_split(&tensor, TensorSplitArg::Indices(vec![2, 4]), 0)?;
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[2, 4]);
        assert_eq!(result[1].shape().dims(), &[2, 4]);
        assert_eq!(result[2].shape().dims(), &[2, 4]);

        Ok(())
    }

    #[test]
    fn test_hsplit() -> TorshResult<()> {
        // Test horizontal split with sections
        let tensor = randn(&[4, 6], None, None, None)?;
        let result = hsplit(&tensor, TensorSplitArg::Sections(3))?;
        assert_eq!(result.len(), 3);
        for chunk in &result {
            assert_eq!(chunk.shape().dims(), &[4, 2]);
        }

        // Test horizontal split with indices
        let result = hsplit(&tensor, TensorSplitArg::Indices(vec![2, 4]))?;
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[4, 2]);
        assert_eq!(result[1].shape().dims(), &[4, 2]);
        assert_eq!(result[2].shape().dims(), &[4, 2]);

        Ok(())
    }

    #[test]
    fn test_vsplit() -> TorshResult<()> {
        // Test vertical split with sections
        let tensor = randn(&[6, 4], None, None, None)?;
        let result = vsplit(&tensor, TensorSplitArg::Sections(3))?;
        assert_eq!(result.len(), 3);
        for chunk in &result {
            assert_eq!(chunk.shape().dims(), &[2, 4]);
        }

        // Test vertical split with indices
        let result = vsplit(&tensor, TensorSplitArg::Indices(vec![2, 4]))?;
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[2, 4]);
        assert_eq!(result[1].shape().dims(), &[2, 4]);
        assert_eq!(result[2].shape().dims(), &[2, 4]);

        Ok(())
    }

    #[test]
    fn test_dsplit() -> TorshResult<()> {
        // Test depth split with 3D tensor
        let tensor = randn(&[2, 3, 6], None, None, None)?;
        let result = dsplit(&tensor, TensorSplitArg::Sections(3))?;
        assert_eq!(result.len(), 3);
        for chunk in &result {
            assert_eq!(chunk.shape().dims(), &[2, 3, 2]);
        }

        // Test depth split with indices
        let result = dsplit(&tensor, TensorSplitArg::Indices(vec![2, 4]))?;
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].shape().dims(), &[2, 3, 2]);
        assert_eq!(result[1].shape().dims(), &[2, 3, 2]);
        assert_eq!(result[2].shape().dims(), &[2, 3, 2]);

        Ok(())
    }

    #[test]
    #[should_panic(expected = "Input tensor must have at least 2 dimensions for hsplit")]
    fn test_hsplit_invalid_dimensions() {
        let tensor = randn(&[5], None, None, None).expect("randn should succeed"); // 1D tensor
        hsplit(&tensor, TensorSplitArg::Sections(2)).expect("operation should succeed");
    }

    #[test]
    #[should_panic(expected = "Input tensor must have at least 2 dimensions for vsplit")]
    fn test_vsplit_invalid_dimensions() {
        let tensor = randn(&[5], None, None, None).expect("randn should succeed"); // 1D tensor
        vsplit(&tensor, TensorSplitArg::Sections(2)).expect("operation should succeed");
    }

    #[test]
    #[should_panic(expected = "Input tensor must have at least 3 dimensions for dsplit")]
    fn test_dsplit_invalid_dimensions() {
        let tensor = randn(&[3, 4], None, None, None).expect("randn should succeed"); // 2D tensor
        dsplit(&tensor, TensorSplitArg::Sections(2)).expect("operation should succeed");
    }

    #[test]
    fn test_split_size_mode() -> TorshResult<()> {
        let tensor = randn(&[10, 4], None, None, None)?;
        let result = split(&tensor, SplitArg::Size(3), 0)?;

        // Should create 4 chunks: [3,4], [3,4], [3,4], [1,4]
        assert_eq!(result.len(), 4);
        assert_eq!(result[0].shape().dims(), &[3, 4]);
        assert_eq!(result[1].shape().dims(), &[3, 4]);
        assert_eq!(result[2].shape().dims(), &[3, 4]);
        assert_eq!(result[3].shape().dims(), &[1, 4]);

        Ok(())
    }

    #[test]
    fn test_negative_dimension_indexing() -> TorshResult<()> {
        let tensor = randn(&[4, 6, 8], None, None, None)?;

        // Test negative dimension indexing
        let result1 = split(&tensor, SplitArg::Sections(2), -1)?; // Last dimension
        let result2 = split(&tensor, SplitArg::Sections(2), 2)?; // Explicit last dimension

        assert_eq!(result1.len(), result2.len());
        assert_eq!(result1[0].shape().dims(), result2[0].shape().dims());

        Ok(())
    }
}