usls 0.1.11

A Rust library integrated with ONNXRuntime, providing a collection of ML models.
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
use aksr::Builder;
use anyhow::Result;
use ndarray::{s, Array};
use rayon::prelude::*;
#[cfg(feature = "slsl")]
use slsl::{Tensor, UninitVec};
use std::sync::Mutex;

use crate::{
    Hub, Image, ImageTensorLayout, ImageTransformInfo, LogitsSampler, ProcessorConfig, ResizeMode,
    X,
};

/// Image and text processing pipeline with tokenization and transformation capabilities.
#[derive(Builder, Debug, Clone)]
pub struct Processor {
    pub image_width: u32,
    pub image_height: u32,
    pub images_transform_info: Vec<ImageTransformInfo>,
    pub resize_mode: ResizeMode,
    pub resize_filter: &'static str,
    pub padding_value: u8,
    pub do_normalize: bool,
    pub image_mean: Vec<f32>,
    pub image_std: Vec<f32>,
    pub nchw: bool, // TODO: remove this field
    pub image_tensor_layout: ImageTensorLayout,
    #[cfg(feature = "tokenizers")]
    pub tokenizer: Option<tokenizers::Tokenizer>,
    pub vocab: Vec<String>,
    pub unsigned: bool,
    pub logits_sampler: Option<LogitsSampler>,
    pub pad_image: bool,
    pub pad_size: usize,
    pub up_scale: f32,
    pub do_resize: bool,
}

impl Default for Processor {
    fn default() -> Self {
        Self {
            images_transform_info: vec![],
            image_width: 0,
            image_height: 0,
            resize_mode: ResizeMode::FitAdaptive,
            resize_filter: "Bilinear",
            padding_value: 114,
            do_normalize: true,
            image_mean: vec![],
            image_std: vec![],
            nchw: true,
            image_tensor_layout: ImageTensorLayout::NCHW,
            #[cfg(feature = "tokenizers")]
            tokenizer: None,
            vocab: vec![],
            unsigned: false,
            logits_sampler: None,
            pad_image: false,
            pad_size: 8,
            up_scale: 2.,
            do_resize: true,
        }
    }
}

impl Processor {
    pub fn try_from_config(config: &ProcessorConfig) -> Result<Self> {
        let logits_sampler = LogitsSampler::new()
            .with_temperature(config.temperature)
            .with_topp(config.topp);

        // try to build tokenizer
        #[cfg(feature = "tokenizers")]
        let tokenizer = config.try_build_tokenizer()?;

        // try to build vocab from `vocab.txt`
        let vocab: Vec<String> = match &config.vocab_txt {
            Some(x) => {
                let file = if !std::path::PathBuf::from(&x).exists() {
                    Hub::default().try_fetch(x)?
                } else {
                    x.to_string()
                };
                std::fs::read_to_string(file)?
                    .lines()
                    .map(|line| line.to_string())
                    .collect()
            }
            None => vec![],
        };

        Ok(Processor {
            image_width: config.image_width.unwrap_or_default(),
            image_height: config.image_height.unwrap_or_default(),
            resize_mode: config.resize_mode.clone(),
            resize_filter: config.resize_filter.unwrap_or("Bilinear"),
            do_resize: config.do_resize,
            padding_value: config.padding_value,
            do_normalize: config.normalize,
            image_mean: config.image_mean.clone(),
            image_std: config.image_std.clone(),
            nchw: config.nchw,
            image_tensor_layout: config.image_tensor_layout,
            unsigned: config.unsigned,
            pad_image: config.pad_image,
            pad_size: config.pad_size,
            up_scale: config.up_scale,
            #[cfg(feature = "tokenizers")]
            tokenizer,
            vocab,
            logits_sampler: Some(logits_sampler),
            ..Default::default()
        })
    }

    pub fn reset_image0_status(&mut self) {
        self.images_transform_info.clear();
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw(input: &[f32], h: usize, w: usize) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    *out = input[(i * w + j) * 3 + c];
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_normalize_and_unsigned(input: &[f32], h: usize, w: usize) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let val = input[(i * w + j) * 3 + c];
                    *out = val.max(0.0f32) / 255.0f32;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_normalize(input: &[f32], h: usize, w: usize) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let val = input[(i * w + j) * 3 + c];
                    *out = val / 255.0f32;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_unsigned(input: &[f32], h: usize, w: usize) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let val = input[(i * w + j) * 3 + c];
                    *out = val.max(0.0f32);
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_all_transforms(
        input: &[f32],
        h: usize,
        w: usize,
        do_normalize: bool,
        unsigned: bool,
        mean: &[f32],
        std: &[f32],
    ) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                let mean_c = mean[c];
                let std_c = std[c];
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let mut val = input[(i * w + j) * 3 + c];

                    // Apply all transformations in a single pass
                    if unsigned {
                        val = val.max(0.0f32);
                    }
                    if do_normalize {
                        val /= 255.0f32;
                    }
                    // Standardize: (x - mean) / std
                    val = (val - mean_c) / std_c;

                    *out = val;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_normalize_and_standardize(
        input: &[f32],
        h: usize,
        w: usize,
        mean: &[f32],
        std: &[f32],
    ) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                let mean_c = mean[c];
                let std_c = std[c];
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let mut val = input[(i * w + j) * 3 + c];
                    val = (val / 255.0f32 - mean_c) / std_c;
                    *out = val;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_unsigned_and_standardize(
        input: &[f32],
        h: usize,
        w: usize,
        mean: &[f32],
        std: &[f32],
    ) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                let mean_c = mean[c];
                let std_c = std[c];
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let mut val = input[(i * w + j) * 3 + c];
                    val = (val.max(0.0f32) - mean_c) / std_c;
                    *out = val;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn hwc_to_chw_with_standardize(
        input: &[f32],
        h: usize,
        w: usize,
        mean: &[f32],
        std: &[f32],
    ) -> Vec<f32> {
        let hw = h * w;
        UninitVec::new(input.len()).init_with(|dst| {
            dst.par_chunks_mut(hw).enumerate().for_each(|(c, plane)| {
                let mean_c = mean[c];
                let std_c = std[c];
                plane.par_iter_mut().enumerate().for_each(|(idx, out)| {
                    let i = idx / w;
                    let j = idx % w;
                    let val = input[(i * w + j) * 3 + c];
                    *out = (val - mean_c) / std_c;
                });
            });
        })
    }

    #[cfg(feature = "slsl")]
    pub fn process_images_f32(&mut self, xs: &[Image]) -> Result<Tensor> {
        if xs.is_empty() {
            anyhow::bail!("Found no input images.");
        }

        let process_one = |image: &Image| -> Result<(Tensor, ImageTransformInfo)> {
            let (image_processed, trans_info) = if self.pad_image {
                image.pad(self.pad_size)?
            } else if self.do_resize {
                image.resize_with_info(
                    self.image_width,
                    self.image_height,
                    self.resize_filter,
                    &self.resize_mode,
                    self.padding_value,
                )?
            } else {
                let (w0, h0) = image.dimensions();
                (
                    image.clone(),
                    ImageTransformInfo::default()
                        .with_width_src(w0)
                        .with_height_src(h0),
                )
            };

            // Convert image to Vec<f32>
            let vec = image_processed.to_f32s();
            let do_standardize = !self.image_std.is_empty() && !self.image_mean.is_empty();

            // Transformation
            let tensor = match self.image_tensor_layout {
                ImageTensorLayout::NCHW => {
                    let vec = match (self.do_normalize, self.unsigned, do_standardize) {
                        (true, true, true) => Self::hwc_to_chw_with_all_transforms(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            true,
                            true,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (true, false, true) => Self::hwc_to_chw_with_normalize_and_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (false, true, true) => Self::hwc_to_chw_with_unsigned_and_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (false, false, true) => Self::hwc_to_chw_with_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (true, true, false) => Self::hwc_to_chw_with_normalize_and_unsigned(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (true, false, false) => Self::hwc_to_chw_with_normalize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (false, true, false) => Self::hwc_to_chw_with_unsigned(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (false, false, false) => Self::hwc_to_chw(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                    };
                    Tensor::from_vec(
                        vec,
                        (1, 3, self.image_height as usize, self.image_width as usize),
                    )?
                }
                ImageTensorLayout::NHWC => {
                    let mut vec = vec;
                    match (self.do_normalize, self.unsigned, do_standardize) {
                        (true, true, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val).max(0.0f32) / 255.0f32;
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (true, false, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val /= 255.0f32;
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (false, true, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val).max(0.0f32);
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (false, false, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (true, true, false) => {
                            vec.par_iter_mut().for_each(|x| {
                                *x = (*x).max(0.0f32);
                                *x /= 255.0f32;
                            });
                        }
                        (true, false, false) => {
                            vec.par_iter_mut().for_each(|x| *x /= 255.0f32);
                        }
                        (false, true, false) => {
                            vec.par_iter_mut().for_each(|x| {
                                *x = (*x).max(0.0f32);
                            });
                        }
                        (false, false, false) => {
                            // No transformations needed
                        }
                    }
                    Tensor::from_vec(
                        vec,
                        (1, self.image_height as usize, self.image_width as usize, 3),
                    )?
                }
                ImageTensorLayout::CHW => {
                    let vec = match (self.do_normalize, self.unsigned, do_standardize) {
                        (true, true, true) => Self::hwc_to_chw_with_all_transforms(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            true,
                            true,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (true, false, true) => Self::hwc_to_chw_with_normalize_and_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (false, true, true) => Self::hwc_to_chw_with_unsigned_and_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (false, false, true) => Self::hwc_to_chw_with_standardize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                            &self.image_mean,
                            &self.image_std,
                        ),
                        (true, true, false) => Self::hwc_to_chw_with_normalize_and_unsigned(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (true, false, false) => Self::hwc_to_chw_with_normalize(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (false, true, false) => Self::hwc_to_chw_with_unsigned(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                        (false, false, false) => Self::hwc_to_chw(
                            &vec,
                            self.image_height as usize,
                            self.image_width as usize,
                        ),
                    };
                    Tensor::from_vec(
                        vec,
                        (3, self.image_height as usize, self.image_width as usize),
                    )?
                }
                ImageTensorLayout::HWC => {
                    let mut vec = vec;
                    match (self.do_normalize, self.unsigned, do_standardize) {
                        (true, true, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val).max(0.0f32) / 255.0f32;
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (true, false, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val /= 255.0f32;
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (false, true, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val).max(0.0f32);
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (false, false, true) => {
                            vec.par_chunks_mut(3).for_each(|pixel| {
                                pixel.par_iter_mut().enumerate().for_each(|(c, val)| {
                                    *val = (*val - self.image_mean[c]) / self.image_std[c];
                                });
                            });
                        }
                        (true, true, false) => {
                            vec.par_iter_mut().for_each(|x| {
                                *x = (*x).max(0.0f32);
                                *x /= 255.0f32;
                            });
                        }
                        (true, false, false) => {
                            vec.par_iter_mut().for_each(|x| *x /= 255.0f32);
                        }
                        (false, true, false) => {
                            vec.par_iter_mut().for_each(|x| {
                                *x = (*x).max(0.0f32);
                            });
                        }
                        (false, false, false) => {
                            // No transformations needed
                        }
                    }
                    Tensor::from_vec(
                        vec,
                        (self.image_height as usize, self.image_width as usize, 3),
                    )?
                }
            };

            Ok((tensor, trans_info))
        };

        // Process images
        match xs.len() {
            1 => {
                let (tensor, trans_info) = process_one(&xs[0])?;
                self.images_transform_info = vec![trans_info];
                Ok(tensor)
            }
            _ => {
                if self.pad_image {
                    anyhow::bail!("When pad_image is true, only one image is allowed.");
                }

                let results: Result<Vec<(Tensor, ImageTransformInfo)>> =
                    xs.par_iter().map(process_one).collect();
                let results = results?;
                let (tensors, trans_infos): (Vec<_>, Vec<_>) = results.into_iter().unzip();
                let combined_tensor = Tensor::cat(&tensors, 0)?;
                self.images_transform_info = trans_infos;

                Ok(combined_tensor)
            }
        }
    }

    pub fn process_images(&mut self, xs: &[Image]) -> Result<X> {
        let mut x = if self.pad_image {
            if xs.len() != 1 {
                anyhow::bail!("When pad_image is true, only one image is allowed.");
            }
            let (image, images_transform_info) = xs[0].pad(self.pad_size)?;
            self.images_transform_info = vec![images_transform_info];
            image.to_ndarray()?.insert_axis(0)?
        } else if self.do_resize {
            let (x, images_transform_info) = self.par_resize(xs)?;
            self.images_transform_info = images_transform_info;
            x
        } else {
            anyhow::bail!(
                "When pad_image and do_resize are both false, at least one image is required."
            );
        };

        if self.do_normalize {
            x = x.normalize(0., 255.)?;
        }

        if !self.image_std.is_empty() && !self.image_mean.is_empty() {
            x = x.standardize(&self.image_mean, &self.image_std, 3)?;
        }

        if self.nchw {
            x = x.nhwc2nchw()?;
        }

        if self.unsigned {
            x = x.unsigned();
        }

        Ok(x)
    }

    pub fn par_resize(&self, xs: &[Image]) -> Result<(X, Vec<ImageTransformInfo>)> {
        match xs.len() {
            0 => anyhow::bail!("Found no input images."),
            1 => {
                let (image, trans_info) = xs[0].resize_with_info(
                    self.image_width,
                    self.image_height,
                    self.resize_filter,
                    &self.resize_mode,
                    self.padding_value,
                )?;

                let y = image.to_ndarray()?.insert_axis(0)?;
                Ok((y, vec![trans_info]))
            }
            _ => {
                let ys = Mutex::new(
                    Array::zeros((
                        xs.len(),
                        self.image_height as usize,
                        self.image_width as usize,
                        3,
                    ))
                    .into_dyn(),
                );

                let results: Result<Vec<ImageTransformInfo>> = xs
                    .par_iter()
                    .enumerate()
                    .map(|(idx, x)| {
                        let (image, trans_info) = x.resize_with_info(
                            self.image_width,
                            self.image_height,
                            self.resize_filter,
                            &self.resize_mode,
                            self.padding_value,
                        )?;

                        let y = image.to_ndarray()?;
                        {
                            let mut ys_guard = ys
                                .lock()
                                .map_err(|e| anyhow::anyhow!("Mutex lock error: {e}"))?;
                            ys_guard.slice_mut(s![idx, .., .., ..]).assign(&y);
                        }

                        Ok(trans_info)
                    })
                    .collect();

                let ys_inner = ys
                    .into_inner()
                    .map_err(|e| anyhow::anyhow!("Mutex into_inner error: {e}"))?;

                Ok((ys_inner.into(), results?))
            }
        }
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_text(&self, x: &str, skip_special_tokens: bool) -> Result<tokenizers::Encoding> {
        let tokenizer = self.tokenizer.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "No tokenizer configured in Processor. Please initialize with a tokenizer."
            )
        })?;

        tokenizer.encode(x, skip_special_tokens).map_err(|err| {
            anyhow::anyhow!(
                "Failed to encode text '{}': {}",
                x.chars().take(50).collect::<String>(),
                err
            )
        })
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_texts(
        &self,
        xs: &[&str],
        skip_special_tokens: bool,
    ) -> Result<Vec<tokenizers::Encoding>> {
        let tokenizer = self.tokenizer.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "No tokenizer configured in Processor. Please initialize with a tokenizer."
            )
        })?;

        tokenizer
            .encode_batch(xs.to_vec(), skip_special_tokens)
            .map_err(|err| anyhow::anyhow!("Failed to encode batch of {} texts: {}", xs.len(), err))
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_text_ids(&self, x: &str, skip_special_tokens: bool) -> Result<Vec<f32>> {
        let ids: Vec<f32> = if x.is_empty() {
            vec![0.0f32]
        } else {
            self.encode_text(x, skip_special_tokens)?
                .get_ids()
                .iter()
                .map(|x| *x as f32)
                .collect()
        };

        Ok(ids)
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_texts_ids(
        &self,
        xs: &[&str],
        skip_special_tokens: bool,
    ) -> Result<Vec<Vec<f32>>> {
        let ids: Vec<Vec<f32>> = if xs.is_empty() {
            vec![vec![0.0f32]]
        } else {
            self.encode_texts(xs, skip_special_tokens)?
                .into_iter()
                .map(|encoding| encoding.get_ids().iter().map(|x| *x as f32).collect())
                .collect()
        };

        Ok(ids)
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_text_tokens(&self, x: &str, skip_special_tokens: bool) -> Result<Vec<String>> {
        Ok(self
            .encode_text(x, skip_special_tokens)?
            .get_tokens()
            .to_vec())
    }

    #[cfg(feature = "tokenizers")]
    pub fn encode_texts_tokens(
        &self,
        xs: &[&str],
        skip_special_tokens: bool,
    ) -> Result<Vec<Vec<String>>> {
        Ok(self
            .encode_texts(xs, skip_special_tokens)?
            .into_iter()
            .map(|encoding| encoding.get_tokens().to_vec())
            .collect())
    }

    #[cfg(feature = "tokenizers")]
    pub fn decode_tokens(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String> {
        let tokenizer = self.tokenizer.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "No tokenizer configured in Processor. Please initialize with a tokenizer."
            )
        })?;

        tokenizer
            .decode(ids, skip_special_tokens)
            .map_err(|err| anyhow::anyhow!("Failed to decode {} token IDs: {}", ids.len(), err))
    }

    #[cfg(feature = "tokenizers")]
    pub fn decode_tokens_batch2(
        &self,
        ids: &[&[u32]],
        skip_special_tokens: bool,
    ) -> Result<Vec<String>> {
        let tokenizer = self.tokenizer.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "No tokenizer configured in Processor. Please initialize with a tokenizer."
            )
        })?;

        tokenizer
            .decode_batch(ids, skip_special_tokens)
            .map_err(|err| {
                anyhow::anyhow!(
                    "Failed to decode batch of {} token sequences: {}",
                    ids.len(),
                    err
                )
            })
    }

    #[cfg(feature = "tokenizers")]
    pub fn decode_tokens_batch(
        &self,
        ids: &[Vec<u32>],
        skip_special_tokens: bool,
    ) -> Result<Vec<String>> {
        let tokenizer = self.tokenizer.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "No tokenizer configured in Processor. Please initialize with a tokenizer."
            )
        })?;

        tokenizer
            .decode_batch(
                &ids.iter().map(|x| x.as_slice()).collect::<Vec<_>>(),
                skip_special_tokens,
            )
            .map_err(|err| {
                anyhow::anyhow!(
                    "Failed to decode batch of {} token vectors: {}",
                    ids.len(),
                    err
                )
            })
    }
}