llm-multimodal 1.7.0

Multimodal processing for vision and other modalities
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
//! Shared base implementation for Qwen VL family image processors.
//!
//! This module provides a generic processor that handles the common logic
//! for Qwen2-VL, Qwen2.5-VL, and Qwen3-VL models. The specific variants
//! differ only in their default parameters (patch_size, normalization values).
//!
//! # Processing Pipeline
//!
//! 1. Validate aspect ratio (must be < 200:1)
//! 2. Smart resize to fit within min/max pixel bounds
//! 3. Align dimensions to (patch_size * merge_size) boundary
//! 4. Convert to tensor and normalize
//! 5. Reshape into patches for the vision encoder
//!
//! # Token Calculation
//!
//! ```text
//! grid_t = 1  (for images, temporal dimension is 1)
//! grid_h = resized_height / patch_size
//! grid_w = resized_width / patch_size
//! num_tokens = (grid_t * grid_h * grid_w) / merge_size²
//! ```

use std::borrow::Cow;

use image::{DynamicImage, GenericImageView};
use ndarray::{Array2, Array3};

use crate::{
    types::RgbFrameRef,
    vision::{
        preprocessor_config::PreProcessorConfig,
        processor::{ModelSpecificValue, PreprocessedEncoderInputs, VisionPreProcessor},
        transforms::{
            pil_to_filter, resize, resize_rgb_bytes, rgb_bytes, to_tensor, to_tensor_and_normalize,
            TransformError,
        },
    },
};

/// Python-compatible rounding (banker's rounding / round half to even).
///
/// This matches Python's `round()` behavior where 0.5 is rounded to the nearest
/// even number (e.g. `12.5 -> 12`, `13.5 -> 14`), unlike Rust's `f64::round()`
/// which rounds half away from zero.
#[inline]
fn round_half_to_even(x: f64) -> f64 {
    let rounded = x.round();
    // Check if we're exactly at a .5 case
    if (x - x.floor() - 0.5).abs() < 1e-9 {
        // Round to nearest even
        if rounded as i64 % 2 != 0 {
            return rounded - 1.0;
        }
    }
    rounded
}

/// Configuration for a Qwen VL processor variant.
#[derive(Debug, Clone)]
pub struct QwenVLConfig {
    /// Vision encoder patch size
    pub patch_size: usize,
    /// Merge size for token reduction
    pub merge_size: usize,
    /// Minimum total pixels allowed
    pub min_pixels: usize,
    /// Maximum total pixels allowed
    pub max_pixels: usize,
    /// Temporal patch size for video
    pub temporal_patch_size: usize,
    /// Normalization mean values
    pub mean: [f64; 3],
    /// Normalization std values
    pub std: [f64; 3],
    /// Model name for identification
    pub model_name: &'static str,
}

struct VideoFrameRgb<'a> {
    width: usize,
    height: usize,
    data: Cow<'a, [u8]>,
}

/// Generic Qwen VL image processor.
///
/// This struct implements the shared preprocessing logic for all Qwen VL
/// model variants. Each variant (Qwen2-VL, Qwen3-VL, etc.) uses this with
/// different configuration values.
#[derive(Debug, Clone)]
pub struct QwenVLProcessorBase {
    config: QwenVLConfig,
}

impl QwenVLProcessorBase {
    /// Create a new processor with the given configuration.
    pub fn new(config: QwenVLConfig) -> Self {
        Self { config }
    }

    /// Get the patch size.
    pub fn patch_size(&self) -> usize {
        self.config.patch_size
    }

    /// Get the merge size.
    pub fn merge_size(&self) -> usize {
        self.config.merge_size
    }

    /// Get the minimum pixels.
    pub fn min_pixels(&self) -> usize {
        self.config.min_pixels
    }

    /// Get the maximum pixels.
    pub fn max_pixels(&self) -> usize {
        self.config.max_pixels
    }

    /// Get the temporal patch size.
    pub fn temporal_patch_size(&self) -> usize {
        self.config.temporal_patch_size
    }

    /// Get the factor for dimension alignment.
    ///
    /// Dimensions must be divisible by (patch_size * merge_size).
    #[inline]
    pub fn get_factor(&self) -> usize {
        self.config.patch_size * self.config.merge_size
    }

    /// Smart resize algorithm for Qwen VL models.
    ///
    /// Resizes image dimensions to fit within min/max pixel bounds while:
    /// - Preserving aspect ratio
    /// - Aligning to (patch_size * merge_size) boundaries
    ///
    /// # Arguments
    /// * `height` - Original image height
    /// * `width` - Original image width
    ///
    /// # Returns
    /// (new_height, new_width) or error if aspect ratio is too extreme
    ///
    /// # Errors
    /// - If height or width is zero
    /// - If aspect ratio exceeds 200:1
    pub fn smart_resize(
        &self,
        height: usize,
        width: usize,
    ) -> Result<(usize, usize), TransformError> {
        let factor = self.get_factor();

        // Validate non-zero dimensions
        if height == 0 || width == 0 {
            return Err(TransformError::InvalidShape {
                expected: "non-zero dimensions".to_string(),
                actual: vec![height, width],
            });
        }

        // Validate aspect ratio
        let max_dim = height.max(width) as f64;
        let min_dim = height.min(width) as f64;
        let aspect_ratio = max_dim / min_dim;
        if aspect_ratio > 200.0 {
            return Err(TransformError::InvalidShape {
                expected: "aspect ratio < 200:1".to_string(),
                actual: vec![height, width],
            });
        }

        // Round to nearest factor multiple using Python-compatible rounding
        // Python uses banker's rounding (round half to even), which affects
        // edge cases like 400/32 = 12.5 -> 12 (not 13)
        let mut h_bar = round_half_to_even(height as f64 / factor as f64) as usize * factor;
        let mut w_bar = round_half_to_even(width as f64 / factor as f64) as usize * factor;

        // Ensure minimum size
        h_bar = h_bar.max(factor);
        w_bar = w_bar.max(factor);

        // Scale down if exceeding max_pixels
        if h_bar * w_bar > self.config.max_pixels {
            let beta = ((height * width) as f64 / self.config.max_pixels as f64).sqrt();
            h_bar = ((height as f64 / beta / factor as f64).floor() as usize) * factor;
            w_bar = ((width as f64 / beta / factor as f64).floor() as usize) * factor;
            // Ensure minimum size after scaling down
            h_bar = h_bar.max(factor);
            w_bar = w_bar.max(factor);
        }
        // Scale up if below min_pixels
        else if h_bar * w_bar < self.config.min_pixels {
            let beta = (self.config.min_pixels as f64 / (height * width) as f64).sqrt();
            h_bar = ((height as f64 * beta / factor as f64).ceil() as usize) * factor;
            w_bar = ((width as f64 * beta / factor as f64).ceil() as usize) * factor;
        }

        Ok((h_bar, w_bar))
    }

    /// Smart resize for Qwen3-style video processors.
    ///
    /// Unlike image resize, the pixel budget is applied to the full sampled
    /// video volume (`T * H * W`), matching HuggingFace's Qwen3 video
    /// processor.
    pub fn smart_resize_video(
        &self,
        num_frames: usize,
        height: usize,
        width: usize,
    ) -> Result<(usize, usize), TransformError> {
        let factor = self.get_factor();

        if num_frames == 0 {
            return Err(TransformError::InvalidShape {
                expected: "num_frames > 0".to_string(),
                actual: vec![num_frames],
            });
        }

        if height < factor || width < factor {
            return Err(TransformError::InvalidShape {
                expected: format!("height and width >= factor ({factor})"),
                actual: vec![height, width],
            });
        }

        let max_dim = height.max(width) as f64;
        let min_dim = height.min(width) as f64;
        let aspect_ratio = max_dim / min_dim;
        if aspect_ratio > 200.0 {
            return Err(TransformError::InvalidShape {
                expected: "aspect ratio < 200:1".to_string(),
                actual: vec![height, width],
            });
        }

        let mut h_bar = round_half_to_even(height as f64 / factor as f64) as usize * factor;
        let mut w_bar = round_half_to_even(width as f64 / factor as f64) as usize * factor;
        h_bar = h_bar.max(factor);
        w_bar = w_bar.max(factor);

        let t_bar =
            num_frames.div_ceil(self.config.temporal_patch_size) * self.config.temporal_patch_size;
        let resized_pixels = (t_bar * h_bar * w_bar) as f64;
        if resized_pixels > self.config.max_pixels as f64 {
            let beta = ((t_bar * height * width) as f64 / self.config.max_pixels as f64).sqrt();
            h_bar = ((height as f64 / beta / factor as f64).floor() as usize) * factor;
            w_bar = ((width as f64 / beta / factor as f64).floor() as usize) * factor;
            h_bar = h_bar.max(factor);
            w_bar = w_bar.max(factor);
        } else if resized_pixels < self.config.min_pixels as f64 {
            let beta = (self.config.min_pixels as f64 / (t_bar * height * width) as f64).sqrt();
            h_bar = ((height as f64 * beta / factor as f64).ceil() as usize) * factor;
            w_bar = ((width as f64 * beta / factor as f64).ceil() as usize) * factor;
        }

        Ok((h_bar, w_bar))
    }

    /// Calculate the grid dimensions (T, H, W) for an image.
    ///
    /// For single images, T=1. For video, T = num_frames / temporal_patch_size.
    ///
    /// # Arguments
    /// * `height` - Resized image height
    /// * `width` - Resized image width
    /// * `num_frames` - Number of frames (1 for images)
    ///
    /// # Returns
    /// (grid_t, grid_h, grid_w)
    pub fn calculate_grid_thw(
        &self,
        height: usize,
        width: usize,
        num_frames: usize,
    ) -> (usize, usize, usize) {
        let grid_t =
            num_frames.max(self.config.temporal_patch_size) / self.config.temporal_patch_size;
        let grid_h = height / self.config.patch_size;
        let grid_w = width / self.config.patch_size;
        (grid_t, grid_h, grid_w)
    }

    /// Calculate the number of image tokens after merge.
    ///
    /// tokens = (grid_t * grid_h * grid_w) / merge_size²
    pub fn calculate_tokens_from_grid(&self, grid_t: usize, grid_h: usize, grid_w: usize) -> usize {
        (grid_t * grid_h * grid_w) / (self.config.merge_size * self.config.merge_size)
    }

    /// Patchify tensor directly into an output buffer (avoids intermediate Vec allocation).
    /// Patchify a [C, H, W] tensor and append the patches to `output`.
    ///
    /// Output layout per image:
    ///   `[grid_t, patch_rows, patch_cols, merge_h, merge_w, C, temporal, patch_h, patch_w]`
    ///
    /// Each "merged patch" covers a `(merge_size * patch_size)²` spatial region.
    /// Within it, `merge_size²` sub-patches are emitted, each containing all channels.
    pub fn patchify_into(
        &self,
        tensor: &Array3<f32>,
        grid_t: usize,
        grid_h: usize,
        grid_w: usize,
        output: &mut Vec<f32>,
    ) -> Result<(), TransformError> {
        let channel = tensor.shape()[0];
        let height = tensor.shape()[1];
        let width = tensor.shape()[2];
        let patch_size = self.config.patch_size;
        let merge_size = self.config.merge_size;
        let temporal_patch_size = self.config.temporal_patch_size;

        debug_assert_eq!(
            height,
            grid_h * patch_size,
            "Height must match grid_h * patch_size"
        );
        debug_assert_eq!(
            width,
            grid_w * patch_size,
            "Width must match grid_w * patch_size"
        );

        let num_patches = grid_t * grid_h * grid_w;
        let patch_features = channel * temporal_patch_size * patch_size * patch_size;
        let base_idx = output.len();
        output.resize(base_idx + num_patches * patch_features, 0.0);

        let data = tensor.as_standard_layout();
        let flat = data.as_slice().ok_or_else(|| {
            TransformError::ShapeError("tensor not contiguous after as_standard_layout".to_string())
        })?;
        let planes: Vec<&[f32]> = (0..channel)
            .map(|c| &flat[c * height * width..(c + 1) * height * width])
            .collect();

        let merged_patch = merge_size * patch_size;
        let mut out_idx = base_idx;

        for _gt in 0..grid_t {
            for pr in 0..grid_h / merge_size {
                for pc in 0..grid_w / merge_size {
                    let y0 = pr * merged_patch;
                    let x0 = pc * merged_patch;

                    for mh in 0..merge_size {
                        for mw in 0..merge_size {
                            for plane in &planes {
                                for _tp in 0..temporal_patch_size {
                                    for py in 0..patch_size {
                                        let row = (y0 + mh * patch_size + py) * width
                                            + x0
                                            + mw * patch_size;
                                        output[out_idx..out_idx + patch_size]
                                            .copy_from_slice(&plane[row..row + patch_size]);
                                        out_idx += patch_size;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Patchify a sequence of frame tensors into Qwen's video patch layout.
    ///
    /// `tensors` must already be resized and normalized to the same spatial
    /// shape. If the frame count is not divisible by `temporal_patch_size`,
    /// the caller should pad by repeating the final frame before calling this.
    pub fn patchify_video_into(
        &self,
        tensors: &[Array3<f32>],
        grid_t: usize,
        grid_h: usize,
        grid_w: usize,
        output: &mut Vec<f32>,
    ) -> Result<(), TransformError> {
        if tensors.is_empty() {
            return Err(TransformError::EmptyBatch);
        }

        let channel = tensors[0].shape()[0];
        let height = tensors[0].shape()[1];
        let width = tensors[0].shape()[2];
        let patch_size = self.config.patch_size;
        let merge_size = self.config.merge_size;
        let temporal_patch_size = self.config.temporal_patch_size;

        debug_assert_eq!(height, grid_h * patch_size);
        debug_assert_eq!(width, grid_w * patch_size);
        debug_assert_eq!(tensors.len(), grid_t * temporal_patch_size);

        let num_patches = grid_t * grid_h * grid_w;
        let patch_features = channel * temporal_patch_size * patch_size * patch_size;
        let base_idx = output.len();
        output.resize(base_idx + num_patches * patch_features, 0.0);

        let frame_planes: Vec<Vec<&[f32]>> = tensors
            .iter()
            .map(|tensor| {
                let flat = tensor.as_slice().ok_or_else(|| {
                    TransformError::ShapeError("video frame tensor is not contiguous".to_string())
                })?;
                Ok((0..channel)
                    .map(|c| &flat[c * height * width..(c + 1) * height * width])
                    .collect::<Vec<_>>())
            })
            .collect::<Result<_, TransformError>>()?;

        let merged_patch = merge_size * patch_size;
        let mut out_idx = base_idx;

        for gt in 0..grid_t {
            let frame_start = gt * temporal_patch_size;
            for pr in 0..grid_h / merge_size {
                for pc in 0..grid_w / merge_size {
                    let y0 = pr * merged_patch;
                    let x0 = pc * merged_patch;

                    for mh in 0..merge_size {
                        for mw in 0..merge_size {
                            let frame_window =
                                &frame_planes[frame_start..frame_start + temporal_patch_size];
                            for channel_frames in (0..channel).map(|channel_idx| {
                                frame_window.iter().map(move |planes| planes[channel_idx])
                            }) {
                                for plane in channel_frames {
                                    for py in 0..patch_size {
                                        let row = (y0 + mh * patch_size + py) * width
                                            + x0
                                            + mw * patch_size;
                                        output[out_idx..out_idx + patch_size]
                                            .copy_from_slice(&plane[row..row + patch_size]);
                                        out_idx += patch_size;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn patchify_video_rgb_chunk_into(
        &self,
        frames: &[VideoFrameRgb<'_>],
        grid_h: usize,
        grid_w: usize,
        output: &mut [f32],
        out_idx: &mut usize,
        lut: &[[f32; 256]; 3],
    ) -> Result<(), TransformError> {
        let patch_size = self.config.patch_size;
        let merge_size = self.config.merge_size;
        let temporal_patch_size = self.config.temporal_patch_size;
        if frames.len() != temporal_patch_size {
            return Err(TransformError::InvalidShape {
                expected: format!("{temporal_patch_size} video frames in temporal patch"),
                actual: vec![frames.len()],
            });
        }

        let height = grid_h * patch_size;
        let width = grid_w * patch_size;
        for frame in frames {
            if frame.height != height || frame.width != width {
                return Err(TransformError::InvalidShape {
                    expected: format!("video frame size {width}x{height}"),
                    actual: vec![frame.width, frame.height],
                });
            }
        }

        let merged_patch = merge_size * patch_size;
        for pr in 0..grid_h / merge_size {
            for pc in 0..grid_w / merge_size {
                let y0 = pr * merged_patch;
                let x0 = pc * merged_patch;

                for mh in 0..merge_size {
                    for mw in 0..merge_size {
                        for (c, lut_c) in lut.iter().enumerate().take(3) {
                            for frame in frames {
                                let raw = frame.data.as_ref();
                                for py in 0..patch_size {
                                    let row =
                                        (y0 + mh * patch_size + py) * width + x0 + mw * patch_size;
                                    let mut src_idx = row * 3 + c;
                                    let dst_end = *out_idx + patch_size;
                                    for dst in &mut output[*out_idx..dst_end] {
                                        *dst = lut_c[raw[src_idx] as usize];
                                        src_idx += 3;
                                    }
                                    *out_idx = dst_end;
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }
}

impl VisionPreProcessor for QwenVLProcessorBase {
    fn default_mean(&self) -> [f64; 3] {
        self.config.mean
    }

    fn default_std(&self) -> [f64; 3] {
        self.config.std
    }

    fn preprocess(
        &self,
        images: &[DynamicImage],
        config: &PreProcessorConfig,
    ) -> Result<PreprocessedEncoderInputs, TransformError> {
        if images.is_empty() {
            return Err(TransformError::EmptyBatch);
        }

        // Store original sizes
        let item_sizes: Vec<(u32, u32)> = images.iter().map(|img| img.dimensions()).collect();

        let mean = config.get_image_mean();
        let std = config.get_image_std();
        let filter = pil_to_filter(config.resampling);

        let patch_size = self.config.patch_size;
        let temporal_patch_size = self.config.temporal_patch_size;
        let patch_features = 3 * temporal_patch_size * patch_size * patch_size;

        // Pre-allocate based on total pixel count to avoid repeated Vec growth
        let estimated_total: usize = images
            .iter()
            .map(|img| {
                let (w, h) = img.dimensions();
                (w as usize * h as usize) / (self.config.merge_size * self.config.merge_size)
                    * patch_features
                    / (patch_size * patch_size)
            })
            .sum();
        let mut all_patches: Vec<f32> = Vec::with_capacity(estimated_total);
        let mut patches_per_image: Vec<i64> = Vec::with_capacity(images.len());
        let mut grid_thw_data = Vec::with_capacity(images.len() * 3);
        let mut feature_token_counts = Vec::with_capacity(images.len());

        for image in images {
            let (w, h) = image.dimensions();
            let (target_h, target_w) = self.smart_resize(h as usize, w as usize)?;

            // Resize to the image's own target size (skip if dimensions match)
            let (tw32, th32) = (target_w as u32, target_h as u32);
            let needs_resize = config.do_resize.unwrap_or(true) && (w != tw32 || h != th32);
            let resized;
            let img_ref = if needs_resize {
                resized = resize(image, tw32, th32, filter);
                &resized
            } else {
                image
            };

            // Grid dimensions based on the target size
            let (grid_t, grid_h, grid_w) = self.calculate_grid_thw(target_h, target_w, 1);
            grid_thw_data.push(grid_t as i64);
            grid_thw_data.push(grid_h as i64);
            grid_thw_data.push(grid_w as i64);

            let num_patches = grid_t * grid_h * grid_w;
            let tokens = self.calculate_tokens_from_grid(grid_t, grid_h, grid_w);
            feature_token_counts.push(tokens);

            // Convert to tensor [C, H, W] and normalize in one fused pass
            let tensor = if config.do_normalize.unwrap_or(true) {
                to_tensor_and_normalize(img_ref, &mean, &std)
            } else {
                to_tensor(img_ref)
            };

            // Patchify directly into all_patches to avoid intermediate Vec + copy
            self.patchify_into(&tensor, grid_t, grid_h, grid_w, &mut all_patches)?;
            patches_per_image.push(num_patches as i64);
        }

        let total_patches: usize = patches_per_image.iter().map(|&n| n as usize).sum();
        let encoder_input =
            Array2::from_shape_vec((total_patches, patch_features), all_patches).map_err(|e| {
                TransformError::ShapeError(format!(
                    "Failed to create patchified encoder_input [{total_patches}, {patch_features}]: {e}"
                ))
            })?;

        let result = PreprocessedEncoderInputs::new_dynamic(
            encoder_input.into_dyn(),
            feature_token_counts,
            item_sizes,
        )
        .with_extra(
            "image_grid_thw",
            ModelSpecificValue::int_2d(grid_thw_data, images.len(), 3),
        )
        .with_extra(
            "patches_per_image",
            ModelSpecificValue::int_1d(patches_per_image),
        );

        Ok(result)
    }

    fn preprocess_video(
        &self,
        frames: &[DynamicImage],
        config: &PreProcessorConfig,
    ) -> Result<PreprocessedEncoderInputs, TransformError> {
        if frames.is_empty() {
            return Err(TransformError::EmptyBatch);
        }

        let (w, h) = frames[0].dimensions();
        let item_sizes = vec![(w, h)];
        let mean = config.get_image_mean();
        let std = config.get_image_std();
        let filter = pil_to_filter(config.resampling);

        let temporal_patch_size = self.config.temporal_patch_size;
        let padded_frames = frames.len().div_ceil(temporal_patch_size) * temporal_patch_size;
        let (target_h, target_w) = self.smart_resize_video(frames.len(), h as usize, w as usize)?;
        let (tw32, th32) = (target_w as u32, target_h as u32);
        let (grid_t, grid_h, grid_w) = self.calculate_grid_thw(target_h, target_w, padded_frames);

        let patch_size = self.config.patch_size;
        let patch_features = 3 * temporal_patch_size * patch_size * patch_size;
        let num_patches = grid_t * grid_h * grid_w;
        let tokens = self.calculate_tokens_from_grid(grid_t, grid_h, grid_w);
        let mut all_patches = vec![0.0; num_patches * patch_features];

        let do_normalize = config.do_normalize.unwrap_or(true);
        let scale: [f32; 3] = if do_normalize {
            std::array::from_fn(|c| 1.0 / (255.0 * std[c] as f32))
        } else {
            [1.0 / 255.0; 3]
        };
        let bias: [f32; 3] = if do_normalize {
            std::array::from_fn(|c| -(mean[c] as f32) / (std[c] as f32))
        } else {
            [0.0; 3]
        };
        let lut: [[f32; 256]; 3] =
            std::array::from_fn(|c| std::array::from_fn(|v| v as f32 * scale[c] + bias[c]));

        let mut out_idx = 0;
        let mut frame_rgbs = Vec::with_capacity(temporal_patch_size);
        for gt in 0..grid_t {
            frame_rgbs.clear();
            for tp in 0..temporal_patch_size {
                let idx = (gt * temporal_patch_size + tp).min(frames.len() - 1);
                let frame = &frames[idx];
                let needs_resize = config.do_resize.unwrap_or(true)
                    && (frame.width() != tw32 || frame.height() != th32);
                if needs_resize {
                    let resized = resize(frame, tw32, th32, filter);
                    let (width, height, data) = rgb_bytes(&resized);
                    frame_rgbs.push(VideoFrameRgb {
                        width,
                        height,
                        data: Cow::Owned(data.into_owned()),
                    });
                } else {
                    let (width, height, data) = rgb_bytes(frame);
                    frame_rgbs.push(VideoFrameRgb {
                        width,
                        height,
                        data,
                    });
                }
            }

            self.patchify_video_rgb_chunk_into(
                &frame_rgbs,
                grid_h,
                grid_w,
                &mut all_patches,
                &mut out_idx,
                &lut,
            )?;
        }
        debug_assert_eq!(out_idx, all_patches.len());

        let encoder_input = Array2::from_shape_vec((num_patches, patch_features), all_patches)
            .map_err(|e| {
                TransformError::ShapeError(format!(
                    "Failed to create video encoder_input [{num_patches}, {patch_features}]: {e}"
                ))
            })?;

        let result = PreprocessedEncoderInputs::new_dynamic(
            encoder_input.into_dyn(),
            vec![tokens],
            item_sizes,
        )
        .with_extra(
            "video_grid_thw",
            ModelSpecificValue::int_2d(vec![grid_t as i64, grid_h as i64, grid_w as i64], 1, 3),
        )
        .with_extra(
            "patches_per_video",
            ModelSpecificValue::int_1d(vec![num_patches as i64]),
        )
        .with_extra(
            "patches_per_image",
            ModelSpecificValue::int_1d(vec![num_patches as i64]),
        );

        Ok(result)
    }

    fn preprocess_video_rgb(
        &self,
        frames: &[RgbFrameRef<'_>],
        config: &PreProcessorConfig,
    ) -> Result<PreprocessedEncoderInputs, TransformError> {
        if frames.is_empty() {
            return Err(TransformError::EmptyBatch);
        }

        let w = frames[0].width;
        let h = frames[0].height;
        let item_sizes = vec![(w, h)];
        let mean = config.get_image_mean();
        let std = config.get_image_std();
        let filter = pil_to_filter(config.resampling);

        let temporal_patch_size = self.config.temporal_patch_size;
        let padded_frames = frames.len().div_ceil(temporal_patch_size) * temporal_patch_size;
        let (target_h, target_w) = self.smart_resize_video(frames.len(), h as usize, w as usize)?;
        let (tw32, th32) = (target_w as u32, target_h as u32);
        let (grid_t, grid_h, grid_w) = self.calculate_grid_thw(target_h, target_w, padded_frames);

        let patch_size = self.config.patch_size;
        let patch_features = 3 * temporal_patch_size * patch_size * patch_size;
        let num_patches = grid_t * grid_h * grid_w;
        let tokens = self.calculate_tokens_from_grid(grid_t, grid_h, grid_w);
        let mut all_patches = vec![0.0; num_patches * patch_features];

        let do_normalize = config.do_normalize.unwrap_or(true);
        let scale: [f32; 3] = if do_normalize {
            std::array::from_fn(|c| 1.0 / (255.0 * std[c] as f32))
        } else {
            [1.0 / 255.0; 3]
        };
        let bias: [f32; 3] = if do_normalize {
            std::array::from_fn(|c| -(mean[c] as f32) / (std[c] as f32))
        } else {
            [0.0; 3]
        };
        let lut: [[f32; 256]; 3] =
            std::array::from_fn(|c| std::array::from_fn(|v| v as f32 * scale[c] + bias[c]));

        let mut needs_resize_any = false;
        let do_resize = config.do_resize.unwrap_or(true);
        for frame in frames {
            let expected_len = (frame.width as usize)
                .checked_mul(frame.height as usize)
                .and_then(|pixels| pixels.checked_mul(3))
                .ok_or_else(|| {
                    TransformError::ShapeError(format!(
                        "video frame dimensions are too large: {}x{}",
                        frame.width, frame.height
                    ))
                })?;
            if frame.data.len() != expected_len {
                return Err(TransformError::InvalidShape {
                    expected: format!(
                        "RGB frame byte length {expected_len} for {}x{}",
                        frame.width, frame.height
                    ),
                    actual: vec![frame.data.len()],
                });
            }
            needs_resize_any |= do_resize && (frame.width != tw32 || frame.height != th32);
        }

        let mut out_idx = 0;
        if needs_resize_any {
            let mut frame_rgbs = Vec::with_capacity(temporal_patch_size);
            for gt in 0..grid_t {
                frame_rgbs.clear();
                for tp in 0..temporal_patch_size {
                    let idx = (gt * temporal_patch_size + tp).min(frames.len() - 1);
                    let frame = frames[idx];
                    let needs_resize = do_resize && (frame.width != tw32 || frame.height != th32);
                    if needs_resize {
                        let resized = resize_rgb_bytes(
                            frame.data,
                            frame.width,
                            frame.height,
                            tw32,
                            th32,
                            filter,
                        )?;
                        frame_rgbs.push(VideoFrameRgb {
                            width: tw32 as usize,
                            height: th32 as usize,
                            data: Cow::Owned(resized.into_raw()),
                        });
                    } else {
                        frame_rgbs.push(VideoFrameRgb {
                            width: frame.width as usize,
                            height: frame.height as usize,
                            data: Cow::Borrowed(frame.data),
                        });
                    }
                }

                self.patchify_video_rgb_chunk_into(
                    &frame_rgbs,
                    grid_h,
                    grid_w,
                    &mut all_patches,
                    &mut out_idx,
                    &lut,
                )?;
            }
        } else {
            let mut frame_rgbs = Vec::with_capacity(temporal_patch_size);
            for gt in 0..grid_t {
                frame_rgbs.clear();
                for tp in 0..temporal_patch_size {
                    let idx = (gt * temporal_patch_size + tp).min(frames.len() - 1);
                    let frame = frames[idx];
                    frame_rgbs.push(VideoFrameRgb {
                        width: frame.width as usize,
                        height: frame.height as usize,
                        data: Cow::Borrowed(frame.data),
                    });
                }

                self.patchify_video_rgb_chunk_into(
                    &frame_rgbs,
                    grid_h,
                    grid_w,
                    &mut all_patches,
                    &mut out_idx,
                    &lut,
                )?;
            }
        }
        debug_assert_eq!(out_idx, all_patches.len());

        let encoder_input = Array2::from_shape_vec((num_patches, patch_features), all_patches)
            .map_err(|e| {
                TransformError::ShapeError(format!(
                    "Failed to create video encoder_input [{num_patches}, {patch_features}]: {e}"
                ))
            })?;

        let result = PreprocessedEncoderInputs::new_dynamic(
            encoder_input.into_dyn(),
            vec![tokens],
            item_sizes,
        )
        .with_extra(
            "video_grid_thw",
            ModelSpecificValue::int_2d(vec![grid_t as i64, grid_h as i64, grid_w as i64], 1, 3),
        )
        .with_extra(
            "patches_per_video",
            ModelSpecificValue::int_1d(vec![num_patches as i64]),
        )
        .with_extra(
            "patches_per_image",
            ModelSpecificValue::int_1d(vec![num_patches as i64]),
        );

        Ok(result)
    }

    fn calculate_num_tokens(&self, width: u32, height: u32, _config: &PreProcessorConfig) -> usize {
        // Calculate resized dimensions
        let (new_height, new_width) = match self.smart_resize(height as usize, width as usize) {
            Ok((h, w)) => (h, w),
            Err(_) => {
                // Fallback: use minimum size
                let factor = self.get_factor();
                (factor, factor)
            }
        };

        // Calculate grid and tokens
        let (grid_t, grid_h, grid_w) = self.calculate_grid_thw(new_height, new_width, 1);
        self.calculate_tokens_from_grid(grid_t, grid_h, grid_w)
    }

    fn model_name(&self) -> &'static str {
        self.config.model_name
    }

    fn get_processed_size(&self, _config: &PreProcessorConfig) -> Option<(u32, u32)> {
        // Qwen VL models have dynamic sizing, no fixed output size
        None
    }
}

#[cfg(test)]
mod tests {
    use image::RgbImage;

    use super::*;

    fn create_test_config() -> QwenVLConfig {
        QwenVLConfig {
            patch_size: 14,
            merge_size: 2,
            min_pixels: 256 * 28 * 28,
            max_pixels: 1280 * 28 * 28,
            temporal_patch_size: 2,
            mean: [0.5, 0.5, 0.5],
            std: [0.5, 0.5, 0.5],
            model_name: "test-qwen-vl",
        }
    }

    fn create_video_test_config() -> QwenVLConfig {
        QwenVLConfig {
            patch_size: 2,
            merge_size: 1,
            min_pixels: 1,
            max_pixels: 1024 * 1024,
            temporal_patch_size: 2,
            mean: [0.5, 0.25, 0.75],
            std: [0.5, 0.25, 0.5],
            model_name: "test-qwen-vl-video",
        }
    }

    fn create_pattern_frame(seed: u8) -> DynamicImage {
        let mut image = RgbImage::new(4, 4);
        for y in 0..4 {
            for x in 0..4 {
                image.put_pixel(
                    x,
                    y,
                    image::Rgb([
                        seed.wrapping_add((x * 3 + y) as u8),
                        seed.wrapping_add((x + y * 5) as u8),
                        seed.wrapping_add((x * 7 + y * 11) as u8),
                    ]),
                );
            }
        }
        DynamicImage::ImageRgb8(image)
    }

    #[test]
    fn test_qwen_vl_base_factor() {
        let processor = QwenVLProcessorBase::new(create_test_config());
        assert_eq!(processor.get_factor(), 28); // 14 * 2
    }

    #[test]
    fn test_smart_resize_within_bounds() {
        let processor = QwenVLProcessorBase::new(create_test_config());
        let (h, w) = processor.smart_resize(500, 500).unwrap();

        assert_eq!(h % 28, 0);
        assert_eq!(w % 28, 0);
        assert!(h * w >= processor.min_pixels());
        assert!(h * w <= processor.max_pixels());
    }

    #[test]
    fn test_smart_resize_extreme_aspect_ratio_error() {
        let processor = QwenVLProcessorBase::new(create_test_config());
        let result = processor.smart_resize(100, 30000);
        assert!(result.is_err());
    }

    #[test]
    fn test_calculate_grid_thw() {
        let processor = QwenVLProcessorBase::new(create_test_config());
        let (t, h, w) = processor.calculate_grid_thw(448, 448, 1);

        assert_eq!(t, 1);
        assert_eq!(h, 448 / 14);
        assert_eq!(w, 448 / 14);
    }

    #[test]
    fn test_calculate_tokens() {
        let processor = QwenVLProcessorBase::new(create_test_config());
        let tokens = processor.calculate_tokens_from_grid(1, 32, 32);
        assert_eq!(tokens, (32 * 32) / 4);
    }

    #[test]
    fn test_preprocess_video_matches_tensor_patchify() {
        let processor = QwenVLProcessorBase::new(create_video_test_config());
        let config = PreProcessorConfig {
            image_mean: Some(processor.default_mean().to_vec()),
            image_std: Some(processor.default_std().to_vec()),
            ..Default::default()
        };
        let frames = vec![create_pattern_frame(3), create_pattern_frame(101)];

        let result = processor.preprocess_video(&frames, &config).unwrap();
        let actual = result.encoder_input.as_slice_memory_order().unwrap();

        let tensors = frames
            .iter()
            .map(|frame| {
                to_tensor_and_normalize(frame, &processor.default_mean(), &processor.default_std())
            })
            .collect::<Vec<_>>();
        let (grid_t, grid_h, grid_w) = processor.calculate_grid_thw(4, 4, frames.len());
        let mut expected = Vec::new();
        processor
            .patchify_video_into(&tensors, grid_t, grid_h, grid_w, &mut expected)
            .unwrap();

        assert_eq!(actual.len(), expected.len());
        for (idx, (&got, &want)) in actual.iter().zip(expected.iter()).enumerate() {
            assert_eq!(
                got.to_bits(),
                want.to_bits(),
                "video patch value differs at index {idx}: got {got}, want {want}"
            );
        }
    }

    #[test]
    fn test_preprocess_video_rgb_matches_dynamic_video() {
        let processor = QwenVLProcessorBase::new(create_video_test_config());
        let config = PreProcessorConfig {
            image_mean: Some(processor.default_mean().to_vec()),
            image_std: Some(processor.default_std().to_vec()),
            ..Default::default()
        };
        let frames = vec![
            create_pattern_frame(3),
            create_pattern_frame(101),
            create_pattern_frame(177),
        ];

        let rgb_frames = frames
            .iter()
            .map(|frame| {
                let DynamicImage::ImageRgb8(rgb) = frame else {
                    panic!("test frame is not RGB8");
                };
                RgbFrameRef {
                    width: rgb.width(),
                    height: rgb.height(),
                    data: rgb.as_raw(),
                }
            })
            .collect::<Vec<_>>();

        let dynamic_result = processor.preprocess_video(&frames, &config).unwrap();
        let rgb_result = processor
            .preprocess_video_rgb(&rgb_frames, &config)
            .unwrap();

        assert_eq!(
            dynamic_result.encoder_input.shape(),
            rgb_result.encoder_input.shape()
        );
        let mut dynamic_keys = dynamic_result.model_specific.keys().collect::<Vec<_>>();
        let mut rgb_keys = rgb_result.model_specific.keys().collect::<Vec<_>>();
        dynamic_keys.sort();
        rgb_keys.sort();
        assert_eq!(dynamic_keys, rgb_keys);

        let dynamic_values = dynamic_result
            .encoder_input
            .as_slice_memory_order()
            .unwrap();
        let rgb_values = rgb_result.encoder_input.as_slice_memory_order().unwrap();
        for (idx, (&got, &want)) in rgb_values.iter().zip(dynamic_values.iter()).enumerate() {
            assert_eq!(
                got.to_bits(),
                want.to_bits(),
                "RGB video patch value differs at index {idx}: got {got}, want {want}"
            );
        }
    }
}