torsh-nn 0.2.0

Neural network modules 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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Convolution operations for neural networks
//!
//! This module provides 1D, 2D, and 3D convolution operations with comprehensive
//! parameter support including stride, padding, dilation, and groups.

use torsh_core::error::Result;
use torsh_tensor::Tensor;

// =============================================================================
// 1D CONVOLUTION
// =============================================================================

/// 1D convolution function
pub fn conv1d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: usize,
    padding: usize,
    dilation: usize,
    groups: usize,
) -> Result<Tensor> {
    // Input shape: [batch_size, in_channels, length]
    // Weight shape: [out_channels, in_channels/groups, kernel_size]
    // Output shape: [batch_size, out_channels, out_length]

    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 3 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Input must be 3D [batch, in_channels, length], got {}D",
            input_shape.len()
        )));
    }

    if weight_shape.len() != 3 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Weight must be 3D [out_channels, in_channels/groups, kernel_size], got {}D",
            weight_shape.len()
        )));
    }

    let batch_size = input_shape[0];
    let in_channels = input_shape[1];
    let in_length = input_shape[2];

    let out_channels = weight_shape[0];
    let kernel_size = weight_shape[2];

    // Validate groups
    if in_channels % groups != 0 || out_channels % groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "in_channels ({}) and out_channels ({}) must be divisible by groups ({})",
            in_channels, out_channels, groups
        )));
    }

    // Calculate output dimensions
    let out_length = (in_length + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1;

    // Extract data once for efficiency
    let input_vec = input.to_vec()?;
    let weight_vec = weight.to_vec()?;

    // Build output data directly
    let mut output_data = vec![0.0f32; batch_size * out_channels * out_length];

    // Implement 1D convolution
    for b in 0..batch_size {
        for g in 0..groups {
            let channels_per_group = in_channels / groups;
            let out_channels_per_group = out_channels / groups;
            let group_start = g * channels_per_group;
            let out_start = g * out_channels_per_group;

            for oc in 0..out_channels_per_group {
                let global_oc = out_start + oc;

                for ol in 0..out_length {
                    let mut sum = 0.0f32;

                    // Convolve over kernel
                    for ic in 0..channels_per_group {
                        let global_ic = group_start + ic;

                        for k in 0..kernel_size {
                            // Calculate input position
                            let il = (ol * stride + k * dilation) as i32 - padding as i32;

                            // Check bounds
                            if il >= 0 && il < in_length as i32 {
                                // Compute flat indices
                                let input_idx = b * in_channels * in_length
                                    + global_ic * in_length
                                    + il as usize;

                                let weight_idx = global_oc * (in_channels / groups) * kernel_size
                                    + ic * kernel_size
                                    + k;

                                sum += input_vec[input_idx] * weight_vec[weight_idx];
                            }
                        }
                    }

                    // Store result
                    let output_idx = b * out_channels * out_length + global_oc * out_length + ol;
                    output_data[output_idx] = sum;
                }
            }
        }
    }

    // Create output tensor from data
    let mut output = Tensor::from_vec(output_data, &[batch_size, out_channels, out_length])?;

    // Apply bias if provided
    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1])?;
        output = output.add(&bias_reshaped)?;
    }

    Ok(output)
}

// =============================================================================
// 2D CONVOLUTION
// =============================================================================

/// 2D convolution function
pub fn conv2d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: (usize, usize),
    padding: (usize, usize),
    dilation: (usize, usize),
    groups: usize,
) -> Result<Tensor> {
    // Input shape: [batch_size, in_channels, height, width]
    // Weight shape: [out_channels, in_channels/groups, kernel_height, kernel_width]
    // Output shape: [batch_size, out_channels, out_height, out_width]

    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 4 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Input must be 4D [batch, in_channels, height, width], got {}D",
            input_shape.len()
        )));
    }

    if weight_shape.len() != 4 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Weight must be 4D [out_channels, in_channels/groups, kernel_h, kernel_w], got {}D",
            weight_shape.len()
        )));
    }

    let batch_size = input_shape[0];
    let in_channels = input_shape[1];
    let in_height = input_shape[2];
    let in_width = input_shape[3];

    let out_channels = weight_shape[0];
    let kernel_height = weight_shape[2];
    let kernel_width = weight_shape[3];

    // Validate groups
    if in_channels % groups != 0 || out_channels % groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "in_channels ({}) and out_channels ({}) must be divisible by groups ({})",
            in_channels, out_channels, groups
        )));
    }

    // Calculate output dimensions
    let out_height =
        (in_height + 2 * padding.0 - dilation.0 * (kernel_height - 1) - 1) / stride.0 + 1;
    let out_width = (in_width + 2 * padding.1 - dilation.1 * (kernel_width - 1) - 1) / stride.1 + 1;

    // Extract data once for efficiency
    let input_vec = input.to_vec()?;
    let weight_vec = weight.to_vec()?;

    // Build output data directly
    let mut output_data = vec![0.0f32; batch_size * out_channels * out_height * out_width];

    // Implement 2D convolution using direct approach
    for b in 0..batch_size {
        for g in 0..groups {
            let channels_per_group = in_channels / groups;
            let out_channels_per_group = out_channels / groups;
            let group_start = g * channels_per_group;
            let out_start = g * out_channels_per_group;

            for oc in 0..out_channels_per_group {
                let global_oc = out_start + oc;

                for oh in 0..out_height {
                    for ow in 0..out_width {
                        let mut sum = 0.0f32;

                        // Convolve over kernel
                        for ic in 0..channels_per_group {
                            let global_ic = group_start + ic;

                            for kh in 0..kernel_height {
                                for kw in 0..kernel_width {
                                    // Calculate input position with stride, padding, and dilation
                                    let ih =
                                        (oh * stride.0 + kh * dilation.0) as i32 - padding.0 as i32;
                                    let iw =
                                        (ow * stride.1 + kw * dilation.1) as i32 - padding.1 as i32;

                                    // Check bounds
                                    if ih >= 0
                                        && ih < in_height as i32
                                        && iw >= 0
                                        && iw < in_width as i32
                                    {
                                        // Compute flat indices
                                        let input_idx = b * in_channels * in_height * in_width
                                            + global_ic * in_height * in_width
                                            + ih as usize * in_width
                                            + iw as usize;

                                        let weight_idx = global_oc
                                            * (in_channels / groups)
                                            * kernel_height
                                            * kernel_width
                                            + ic * kernel_height * kernel_width
                                            + kh * kernel_width
                                            + kw;

                                        sum += input_vec[input_idx] * weight_vec[weight_idx];
                                    }
                                }
                            }
                        }

                        // Store result
                        let output_idx = b * out_channels * out_height * out_width
                            + global_oc * out_height * out_width
                            + oh * out_width
                            + ow;
                        output_data[output_idx] = sum;
                    }
                }
            }
        }
    }

    // Create output tensor from data
    let mut output = Tensor::from_vec(
        output_data,
        &[batch_size, out_channels, out_height, out_width],
    )?;

    // Apply bias if provided
    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1, 1])?;
        output = output.add(&bias_reshaped)?;
    }

    Ok(output)
}

// =============================================================================
// 3D CONVOLUTION
// =============================================================================

/// 3D convolution function
pub fn conv3d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: (usize, usize, usize),
    padding: (usize, usize, usize),
    dilation: (usize, usize, usize),
    groups: usize,
) -> Result<Tensor> {
    // Input shape: [batch_size, in_channels, depth, height, width]
    // Weight shape: [out_channels, in_channels/groups, kernel_depth, kernel_height, kernel_width]
    // Output shape: [batch_size, out_channels, out_depth, out_height, out_width]

    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 5 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Input must be 5D [batch, in_channels, depth, height, width], got {}D",
            input_shape.len()
        )));
    }

    if weight_shape.len() != 5 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "Weight must be 5D [out_channels, in_channels/groups, kernel_d, kernel_h, kernel_w], got {}D", weight_shape.len()
        )));
    }

    let batch_size = input_shape[0];
    let in_channels = input_shape[1];
    let in_depth = input_shape[2];
    let in_height = input_shape[3];
    let in_width = input_shape[4];

    let out_channels = weight_shape[0];
    let kernel_depth = weight_shape[2];
    let kernel_height = weight_shape[3];
    let kernel_width = weight_shape[4];

    // Validate groups
    if in_channels % groups != 0 || out_channels % groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "in_channels ({}) and out_channels ({}) must be divisible by groups ({})",
            in_channels, out_channels, groups
        )));
    }

    // Calculate output dimensions
    let out_depth = (in_depth + 2 * padding.0 - dilation.0 * (kernel_depth - 1) - 1) / stride.0 + 1;
    let out_height =
        (in_height + 2 * padding.1 - dilation.1 * (kernel_height - 1) - 1) / stride.1 + 1;
    let out_width = (in_width + 2 * padding.2 - dilation.2 * (kernel_width - 1) - 1) / stride.2 + 1;

    // Extract data once for efficiency
    let input_vec = input.to_vec()?;
    let weight_vec = weight.to_vec()?;

    // Build output data directly
    let mut output_data =
        vec![0.0f32; batch_size * out_channels * out_depth * out_height * out_width];

    // Implement 3D convolution
    for b in 0..batch_size {
        for g in 0..groups {
            let channels_per_group = in_channels / groups;
            let out_channels_per_group = out_channels / groups;
            let group_start = g * channels_per_group;
            let out_start = g * out_channels_per_group;

            for oc in 0..out_channels_per_group {
                let global_oc = out_start + oc;

                for od in 0..out_depth {
                    for oh in 0..out_height {
                        for ow in 0..out_width {
                            let mut sum = 0.0f32;

                            // Convolve over kernel
                            for ic in 0..channels_per_group {
                                let global_ic = group_start + ic;

                                for kd in 0..kernel_depth {
                                    for kh in 0..kernel_height {
                                        for kw in 0..kernel_width {
                                            // Calculate input position
                                            let id = (od * stride.0 + kd * dilation.0) as i32
                                                - padding.0 as i32;
                                            let ih = (oh * stride.1 + kh * dilation.1) as i32
                                                - padding.1 as i32;
                                            let iw = (ow * stride.2 + kw * dilation.2) as i32
                                                - padding.2 as i32;

                                            // Check bounds
                                            if id >= 0
                                                && id < in_depth as i32
                                                && ih >= 0
                                                && ih < in_height as i32
                                                && iw >= 0
                                                && iw < in_width as i32
                                            {
                                                // Compute flat indices
                                                let input_idx = b
                                                    * in_channels
                                                    * in_depth
                                                    * in_height
                                                    * in_width
                                                    + global_ic * in_depth * in_height * in_width
                                                    + id as usize * in_height * in_width
                                                    + ih as usize * in_width
                                                    + iw as usize;

                                                let weight_idx = global_oc
                                                    * (in_channels / groups)
                                                    * kernel_depth
                                                    * kernel_height
                                                    * kernel_width
                                                    + ic * kernel_depth
                                                        * kernel_height
                                                        * kernel_width
                                                    + kd * kernel_height * kernel_width
                                                    + kh * kernel_width
                                                    + kw;

                                                sum +=
                                                    input_vec[input_idx] * weight_vec[weight_idx];
                                            }
                                        }
                                    }
                                }
                            }

                            // Store result
                            let output_idx = b * out_channels * out_depth * out_height * out_width
                                + global_oc * out_depth * out_height * out_width
                                + od * out_height * out_width
                                + oh * out_width
                                + ow;
                            output_data[output_idx] = sum;
                        }
                    }
                }
            }
        }
    }

    // Create output tensor from data
    let mut output = Tensor::from_vec(
        output_data,
        &[batch_size, out_channels, out_depth, out_height, out_width],
    )?;

    // Apply bias if provided
    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1, 1, 1])?;
        output = output.add(&bias_reshaped)?;
    }

    Ok(output)
}

// =============================================================================
// TRANSPOSED (DECONVOLUTION) OPERATIONS
// =============================================================================

/// Scatter-add kernel shared by the 1-D/2-D/3-D transposed convolutions.
///
/// Transposed convolution is the gradient of a convolution with respect to its
/// input: every input element is multiplied by the whole kernel and accumulated
/// into the output at `pos * stride + k * dilation - padding`. Positions that
/// fall outside the (cropped) output are dropped, which is exactly what the
/// `padding` argument means here.
///
/// Shapes use the generic N-D layout `[batch, channels, spatial...]` with the
/// weight laid out as `[in_channels, out_channels / groups, kernel...]`.
struct TransposedConvSpec<'a> {
    input_spatial: &'a [usize],
    kernel: &'a [usize],
    stride: &'a [usize],
    padding: &'a [usize],
    output_padding: &'a [usize],
    dilation: &'a [usize],
    groups: usize,
}

impl TransposedConvSpec<'_> {
    /// Output extent of each spatial axis.
    ///
    /// Over-cropping (a `padding` larger than the uncropped extent) is reported
    /// as an error instead of wrapping around in unsigned arithmetic.
    fn output_spatial(&self) -> Result<Vec<usize>> {
        let mut extents = Vec::with_capacity(self.input_spatial.len());
        for (axis, &size) in self.input_spatial.iter().enumerate() {
            if size == 0 {
                return Err(torsh_core::error::TorshError::InvalidShape(
                    "conv_transpose does not accept an empty spatial dimension".to_string(),
                ));
            }
            let uncropped = (size - 1) * self.stride[axis]
                + self.dilation[axis] * (self.kernel[axis] - 1)
                + self.output_padding[axis]
                + 1;
            let cropped = uncropped
                .checked_sub(2 * self.padding[axis])
                .filter(|&extent| extent > 0)
                .ok_or_else(|| {
                    torsh_core::error::TorshError::InvalidArgument(format!(
                        "padding {} crops the whole output of axis {axis} (extent {uncropped})",
                        self.padding[axis]
                    ))
                })?;
            extents.push(cropped);
        }
        Ok(extents)
    }
}

/// Validate the transposed-convolution arguments and report the output extents.
fn transposed_conv_dims(
    input_shape: &[usize],
    weight_shape: &[usize],
    spec: &TransposedConvSpec<'_>,
) -> Result<(usize, usize, Vec<usize>)> {
    let batch_size = input_shape[0];
    let in_channels = input_shape[1];

    if spec.groups == 0 {
        return Err(torsh_core::error::TorshError::InvalidArgument(
            "groups must be positive".to_string(),
        ));
    }
    if in_channels != weight_shape[0] {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "weight has {} input channels but the input has {}",
            weight_shape[0], in_channels
        )));
    }
    if in_channels % spec.groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidShape(format!(
            "in_channels ({}) must be divisible by groups ({})",
            in_channels, spec.groups
        )));
    }
    for (axis, &size) in spec.kernel.iter().enumerate() {
        if size == 0 || spec.stride[axis] == 0 || spec.dilation[axis] == 0 {
            return Err(torsh_core::error::TorshError::InvalidArgument(
                "kernel size, stride and dilation must all be positive".to_string(),
            ));
        }
        if spec.output_padding[axis] >= spec.stride[axis].max(spec.dilation[axis]) {
            return Err(torsh_core::error::TorshError::InvalidArgument(format!(
                "output_padding ({}) must be smaller than stride ({}) or dilation ({})",
                spec.output_padding[axis], spec.stride[axis], spec.dilation[axis]
            )));
        }
    }

    let out_channels = weight_shape[1] * spec.groups;
    let output_spatial = spec.output_spatial()?;
    Ok((batch_size, out_channels, output_spatial))
}

/// Generic N-D transposed convolution over flat row-major buffers.
fn transposed_conv_nd(
    input: &Tensor,
    weight: &Tensor,
    spec: &TransposedConvSpec<'_>,
) -> Result<(Vec<f32>, usize, usize, Vec<usize>)> {
    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    let (batch_size, out_channels, out_spatial) =
        transposed_conv_dims(input_shape, weight_shape, spec)?;

    let in_channels = input_shape[1];
    let axes = spec.input_spatial.len();
    let in_spatial_size: usize = spec.input_spatial.iter().product();
    let out_spatial_size: usize = out_spatial.iter().product();
    let kernel_size: usize = spec.kernel.iter().product();
    let out_per_group = weight_shape[1];
    let in_per_group = in_channels / spec.groups;

    let input_data = input.to_vec()?;
    let weight_data = weight.to_vec()?;
    let mut output_data = vec![0.0f32; batch_size * out_channels * out_spatial_size];

    // Row-major coordinate decoding helpers.
    let mut in_coords = vec![0usize; axes];
    let mut kernel_coords = vec![0usize; axes];

    for batch in 0..batch_size {
        for in_channel in 0..in_channels {
            let group = in_channel / in_per_group;
            let input_base = (batch * in_channels + in_channel) * in_spatial_size;
            let weight_base = in_channel * out_per_group * kernel_size;

            for flat_in in 0..in_spatial_size {
                let value = input_data[input_base + flat_in];
                if value == 0.0 {
                    continue;
                }

                let mut remaining = flat_in;
                for axis in (0..axes).rev() {
                    in_coords[axis] = remaining % spec.input_spatial[axis];
                    remaining /= spec.input_spatial[axis];
                }

                for flat_k in 0..kernel_size {
                    let mut remaining = flat_k;
                    for axis in (0..axes).rev() {
                        kernel_coords[axis] = remaining % spec.kernel[axis];
                        remaining /= spec.kernel[axis];
                    }

                    // Map the (input position, kernel tap) pair to an output cell.
                    let mut flat_out = 0usize;
                    let mut inside = true;
                    for axis in 0..axes {
                        let position = in_coords[axis] * spec.stride[axis]
                            + kernel_coords[axis] * spec.dilation[axis];
                        if position < spec.padding[axis] {
                            inside = false;
                            break;
                        }
                        let position = position - spec.padding[axis];
                        if position >= out_spatial[axis] {
                            inside = false;
                            break;
                        }
                        flat_out = flat_out * out_spatial[axis] + position;
                    }
                    if !inside {
                        continue;
                    }

                    for out_channel in 0..out_per_group {
                        let weight_value =
                            weight_data[weight_base + out_channel * kernel_size + flat_k];
                        let global_out = group * out_per_group + out_channel;
                        let output_index =
                            (batch * out_channels + global_out) * out_spatial_size + flat_out;
                        output_data[output_index] += value * weight_value;
                    }
                }
            }
        }
    }

    Ok((output_data, batch_size, out_channels, out_spatial))
}

/// 1D transposed convolution (deconvolution)
///
/// Input `[batch, in_channels, length]`, weight
/// `[in_channels, out_channels / groups, kernel]`, output
/// `[batch, out_channels, (length - 1) * stride - 2 * padding + dilation *
/// (kernel - 1) + output_padding + 1]`.
#[allow(clippy::too_many_arguments)]
pub fn conv_transpose1d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: usize,
    padding: usize,
    output_padding: usize,
    groups: usize,
    dilation: usize,
) -> Result<Tensor> {
    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 3 || weight_shape.len() != 3 {
        return Err(torsh_core::error::TorshError::InvalidArgument(
            "Input and weight must be 3D tensors for conv_transpose1d".to_string(),
        ));
    }

    let spec = TransposedConvSpec {
        input_spatial: &input_shape[2..],
        kernel: &weight_shape[2..],
        stride: &[stride],
        padding: &[padding],
        output_padding: &[output_padding],
        dilation: &[dilation],
        groups,
    };

    let (data, batch_size, out_channels, out_spatial) = transposed_conv_nd(input, weight, &spec)?;
    let mut result = Tensor::from_vec(data, &[batch_size, out_channels, out_spatial[0]])?;

    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1])?;
        result = result.add_op(&bias_reshaped)?;
    }

    Ok(result)
}

/// 2D transposed convolution (deconvolution)
///
/// Input `[batch, in_channels, height, width]`, weight
/// `[in_channels, out_channels / groups, kernel_h, kernel_w]`.
#[allow(clippy::too_many_arguments)]
pub fn conv_transpose2d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: (usize, usize),
    padding: (usize, usize),
    output_padding: (usize, usize),
    groups: usize,
    dilation: (usize, usize),
) -> Result<Tensor> {
    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 4 || weight_shape.len() != 4 {
        return Err(torsh_core::error::TorshError::InvalidArgument(
            "Input and weight must be 4D tensors for conv_transpose2d".to_string(),
        ));
    }

    let spec = TransposedConvSpec {
        input_spatial: &input_shape[2..],
        kernel: &weight_shape[2..],
        stride: &[stride.0, stride.1],
        padding: &[padding.0, padding.1],
        output_padding: &[output_padding.0, output_padding.1],
        dilation: &[dilation.0, dilation.1],
        groups,
    };

    let (data, batch_size, out_channels, out_spatial) = transposed_conv_nd(input, weight, &spec)?;
    let mut result = Tensor::from_vec(
        data,
        &[batch_size, out_channels, out_spatial[0], out_spatial[1]],
    )?;

    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1, 1])?;
        result = result.add_op(&bias_reshaped)?;
    }

    Ok(result)
}

/// 3D transposed convolution (deconvolution)
///
/// Input `[batch, in_channels, depth, height, width]`, weight
/// `[in_channels, out_channels / groups, kernel_d, kernel_h, kernel_w]`.
#[allow(clippy::too_many_arguments)]
pub fn conv_transpose3d(
    input: &Tensor,
    weight: &Tensor,
    bias: Option<&Tensor>,
    stride: (usize, usize, usize),
    padding: (usize, usize, usize),
    output_padding: (usize, usize, usize),
    groups: usize,
    dilation: (usize, usize, usize),
) -> Result<Tensor> {
    let input_shape_obj = input.shape();
    let input_shape = input_shape_obj.dims();
    let weight_shape_obj = weight.shape();
    let weight_shape = weight_shape_obj.dims();

    if input_shape.len() != 5 || weight_shape.len() != 5 {
        return Err(torsh_core::error::TorshError::InvalidArgument(
            "Input and weight must be 5D tensors for conv_transpose3d".to_string(),
        ));
    }

    let spec = TransposedConvSpec {
        input_spatial: &input_shape[2..],
        kernel: &weight_shape[2..],
        stride: &[stride.0, stride.1, stride.2],
        padding: &[padding.0, padding.1, padding.2],
        output_padding: &[output_padding.0, output_padding.1, output_padding.2],
        dilation: &[dilation.0, dilation.1, dilation.2],
        groups,
    };

    let (data, batch_size, out_channels, out_spatial) = transposed_conv_nd(input, weight, &spec)?;
    let mut result = Tensor::from_vec(
        data,
        &[
            batch_size,
            out_channels,
            out_spatial[0],
            out_spatial[1],
            out_spatial[2],
        ],
    )?;

    if let Some(bias_tensor) = bias {
        let bias_reshaped = bias_tensor.reshape(&[1, out_channels as i32, 1, 1, 1])?;
        result = result.add_op(&bias_reshaped)?;
    }

    Ok(result)
}

// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================

/// Calculate output size for convolution operation
pub fn conv_output_size(
    input_size: usize,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    dilation: usize,
) -> usize {
    (input_size + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1
}

/// Calculate output size for transposed convolution operation
pub fn conv_transpose_output_size(
    input_size: usize,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    output_padding: usize,
    dilation: usize,
) -> usize {
    (input_size - 1) * stride - 2 * padding + dilation * (kernel_size - 1) + output_padding + 1
}

/// Validate convolution parameters
pub fn validate_conv_params(
    in_channels: usize,
    out_channels: usize,
    groups: usize,
    kernel_size: &[usize],
) -> Result<()> {
    if in_channels % groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidArgument(format!(
            "in_channels ({}) must be divisible by groups ({})",
            in_channels, groups
        )));
    }

    if out_channels % groups != 0 {
        return Err(torsh_core::error::TorshError::InvalidArgument(format!(
            "out_channels ({}) must be divisible by groups ({})",
            out_channels, groups
        )));
    }

    for &size in kernel_size {
        if size == 0 {
            return Err(torsh_core::error::TorshError::InvalidArgument(
                "Kernel size must be positive".to_string(),
            ));
        }
    }

    Ok(())
}