burn-import 0.20.1

Library for importing datamodels into the Burn framework
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
use super::prelude::*;

impl NodeCodegen for onnx_ir::node::resize::ResizeNode {
    fn inputs(&self) -> &[Argument] {
        &self.inputs
    }

    fn outputs(&self) -> &[Argument] {
        &self.outputs
    }

    fn field(&self) -> Option<Field> {
        use onnx_ir::node::resize::{ResizeMode, ResizeScales, ResizeSizes};

        // Only create a field for static resize (no runtime inputs)
        let has_runtime_scales = matches!(&self.config.scales, Some(ResizeScales::Runtime(_)));
        let has_runtime_sizes = matches!(&self.config.sizes, Some(ResizeSizes::Runtime(_)));

        if has_runtime_scales || has_runtime_sizes {
            return None; // Runtime resize doesn't need a field
        }

        // Check if we have static scales or sizes
        let has_static = matches!(&self.config.scales, Some(ResizeScales::Static(_)))
            || matches!(&self.config.sizes, Some(ResizeSizes::Static(_)));

        if !has_static {
            return None;
        }

        // Determine field type based on input rank
        let input_arg = self.inputs.first().unwrap();
        let input_rank = match &input_arg.ty {
            ArgType::Tensor(t) => t.rank,
            _ => panic!("Resize input must be a tensor"),
        };

        let name = syn::Ident::new(&self.name, proc_macro2::Span::call_site());
        let mode = match self.config.mode {
            ResizeMode::Nearest => quote! { burn::nn::interpolate::InterpolateMode::Nearest },
            ResizeMode::Linear => quote! { burn::nn::interpolate::InterpolateMode::Linear },
            ResizeMode::Cubic => quote! { burn::nn::interpolate::InterpolateMode::Cubic },
        };

        if input_rank == 3 {
            let size = match &self.config.sizes {
                Some(ResizeSizes::Static(sizes)) if !sizes.is_empty() => {
                    let size = sizes[0].to_tokens();
                    quote! { Some(#size) }
                }
                _ => quote! { None },
            };

            let scale_factor = match &self.config.scales {
                Some(ResizeScales::Static(scales)) if !scales.is_empty() => {
                    let scale = scales[0].to_tokens();
                    quote! { Some(#scale) }
                }
                _ => quote! { None },
            };

            Some(Field::new(
                &self.name,
                quote! { burn::nn::interpolate::Interpolate1d },
                quote! {
                    let #name = burn::nn::interpolate::Interpolate1dConfig::new()
                        .with_output_size(#size)
                        .with_scale_factor(#scale_factor)
                        .with_mode(#mode)
                        .init();
                },
            ))
        } else if input_rank == 4 {
            let size = match &self.config.sizes {
                Some(ResizeSizes::Static(sizes)) if sizes.len() >= 2 => {
                    let h = sizes[0].to_tokens();
                    let w = sizes[1].to_tokens();
                    quote! { Some([#h, #w]) }
                }
                _ => quote! { None },
            };

            let scale_factor = match &self.config.scales {
                Some(ResizeScales::Static(scales)) if scales.len() >= 2 => {
                    let h = scales[0].to_tokens();
                    let w = scales[1].to_tokens();
                    quote! { Some([#h, #w]) }
                }
                _ => quote! { None },
            };

            Some(Field::new(
                &self.name,
                quote! { burn::nn::interpolate::Interpolate2d },
                quote! {
                    let #name = burn::nn::interpolate::Interpolate2dConfig::new()
                        .with_output_size(#size)
                        .with_scale_factor(#scale_factor)
                        .with_mode(#mode)
                        .init();
                },
            ))
        } else {
            None
        }
    }

    fn forward(&self, scope: &mut ScopeAtPosition<'_>) -> TokenStream {
        use onnx_ir::node::resize::{ResizeMode, ResizeScales, ResizeSizes};

        let input_arg = self.inputs.first().unwrap();
        let input = scope.arg(input_arg);
        let output = arg_to_ident(self.outputs.first().unwrap());

        // Check if this is static (has field) or runtime resize
        let has_runtime = matches!(&self.config.scales, Some(ResizeScales::Runtime(_)))
            || matches!(&self.config.sizes, Some(ResizeSizes::Runtime(_)));
        let has_static = matches!(&self.config.scales, Some(ResizeScales::Static(_)))
            || matches!(&self.config.sizes, Some(ResizeSizes::Static(_)));

        if !has_runtime && has_static {
            // Static resize - use the field
            let field_name = syn::Ident::new(&self.name, proc_macro2::Span::call_site());
            quote! {
                let #output = self.#field_name.forward(#input);
            }
        } else {
            // Runtime resize - use tensor operations directly
            let mode = match self.config.mode {
                ResizeMode::Nearest => quote! { burn::tensor::ops::InterpolateMode::Nearest },
                ResizeMode::Linear => quote! { burn::tensor::ops::InterpolateMode::Bilinear },
                ResizeMode::Cubic => quote! { burn::tensor::ops::InterpolateMode::Bicubic },
            };

            // Handle runtime sizes input
            match &self.config.sizes {
                Some(ResizeSizes::Runtime(sizes_ref)) => {
                    let sizes_arg = &self.inputs[sizes_ref.input_index];

                    match &sizes_arg.ty {
                        ArgType::Shape(_) => {
                            let sizes_name = arg_to_ident(sizes_arg);
                            // Extract the last 2 dimensions from the shape (H, W for 2D resize)
                            quote! {
                                let #output = {
                                    let target_height = #sizes_name[2] as usize;
                                    let target_width = #sizes_name[3] as usize;
                                    burn::tensor::module::interpolate(
                                        #input,
                                        [target_height, target_width],
                                        burn::tensor::ops::InterpolateOptions::new(#mode)
                                    )
                                };
                            }
                        }
                        ArgType::Tensor(_) => {
                            let sizes_name = scope.arg(sizes_arg);
                            quote! {
                                let #output = {
                                    let sizes_data = #sizes_name.to_data().convert::<i64>();
                                    let sizes_array = sizes_data.as_slice::<i64>().unwrap();
                                    let target_height = sizes_array[2] as usize;
                                    let target_width = sizes_array[3] as usize;
                                    burn::tensor::module::interpolate(
                                        #input,
                                        [target_height, target_width],
                                        burn::tensor::ops::InterpolateOptions::new(#mode)
                                    )
                                };
                            }
                        }
                        _ => panic!("Runtime resize sizes must be Shape or Tensor"),
                    }
                }
                _ => panic!("Runtime resize requires sizes input"),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_helpers::*;
    use insta::assert_snapshot;
    use onnx_ir::ir::{DType, RuntimeInputRef};
    use onnx_ir::node::resize::{
        ResizeConfig, ResizeMode, ResizeNodeBuilder, ResizeScales, ResizeSizes,
    };

    // ==================== Static Resize - Rank 3 (1D) Tests ====================

    #[test]
    fn test_resize_1d_static_sizes_nearest() {
        let config = ResizeConfig {
            mode: ResizeMode::Nearest,
            scales: None,
            sizes: Some(ResizeSizes::Static(vec![64])),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("upsample")
            .input_tensor("signal", 3, DType::F32)
            .output_tensor("upsampled", 3, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, signal: Tensor<B, 3>) -> Tensor<B, 3> {
            let upsampled = self.upsample.forward(signal);
            upsampled
        }
        ");
    }

    #[test]
    fn test_resize_1d_static_scales_linear() {
        let config = ResizeConfig {
            mode: ResizeMode::Linear,
            scales: Some(ResizeScales::Static(vec![2.0])),
            sizes: None,
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("interpolate")
            .input_tensor("audio", 3, DType::F32)
            .output_tensor("resampled", 3, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, audio: Tensor<B, 3>) -> Tensor<B, 3> {
            let resampled = self.interpolate.forward(audio);
            resampled
        }
        ");
    }

    #[test]
    fn test_resize_1d_static_sizes_cubic() {
        let config = ResizeConfig {
            mode: ResizeMode::Cubic,
            scales: None,
            sizes: Some(ResizeSizes::Static(vec![128])),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("resize1d")
            .input_tensor("waveform", 3, DType::F32)
            .output_tensor("output", 3, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, waveform: Tensor<B, 3>) -> Tensor<B, 3> {
            let output = self.resize1d.forward(waveform);
            output
        }
        ");
    }

    // ==================== Static Resize - Rank 4 (2D) Tests ====================

    #[test]
    fn test_resize_2d_static_sizes_nearest() {
        let config = ResizeConfig {
            mode: ResizeMode::Nearest,
            scales: None,
            sizes: Some(ResizeSizes::Static(vec![224, 224])),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("resize")
            .input_tensor("image", 4, DType::F32)
            .output_tensor("resized", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, image: Tensor<B, 4>) -> Tensor<B, 4> {
            let resized = self.resize.forward(image);
            resized
        }
        ");
    }

    #[test]
    fn test_resize_2d_static_sizes_linear() {
        let config = ResizeConfig {
            mode: ResizeMode::Linear,
            scales: None,
            sizes: Some(ResizeSizes::Static(vec![512, 512])),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("upscale")
            .input_tensor("input_img", 4, DType::F32)
            .output_tensor("output_img", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, input_img: Tensor<B, 4>) -> Tensor<B, 4> {
            let output_img = self.upscale.forward(input_img);
            output_img
        }
        ");
    }

    #[test]
    fn test_resize_2d_static_sizes_cubic() {
        let config = ResizeConfig {
            mode: ResizeMode::Cubic,
            scales: None,
            sizes: Some(ResizeSizes::Static(vec![128, 256])),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("bicubic_resize")
            .input_tensor("features", 4, DType::F32)
            .output_tensor("scaled", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, features: Tensor<B, 4>) -> Tensor<B, 4> {
            let scaled = self.bicubic_resize.forward(features);
            scaled
        }
        ");
    }

    #[test]
    fn test_resize_2d_static_scales_nearest() {
        let config = ResizeConfig {
            mode: ResizeMode::Nearest,
            scales: Some(ResizeScales::Static(vec![2.0, 2.0])),
            sizes: None,
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("double_size")
            .input_tensor("tensor", 4, DType::F32)
            .output_tensor("doubled", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, tensor: Tensor<B, 4>) -> Tensor<B, 4> {
            let doubled = self.double_size.forward(tensor);
            doubled
        }
        ");
    }

    #[test]
    fn test_resize_2d_static_scales_linear() {
        let config = ResizeConfig {
            mode: ResizeMode::Linear,
            scales: Some(ResizeScales::Static(vec![0.5, 0.5])),
            sizes: None,
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("downsample")
            .input_tensor("hires", 4, DType::F32)
            .output_tensor("lores", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, hires: Tensor<B, 4>) -> Tensor<B, 4> {
            let lores = self.downsample.forward(hires);
            lores
        }
        ");
    }

    #[test]
    fn test_resize_2d_static_scales_cubic() {
        let config = ResizeConfig {
            mode: ResizeMode::Cubic,
            scales: Some(ResizeScales::Static(vec![1.5, 1.5])),
            sizes: None,
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("scale_up")
            .input_tensor("data", 4, DType::F32)
            .output_tensor("result", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, data: Tensor<B, 4>) -> Tensor<B, 4> {
            let result = self.scale_up.forward(data);
            result
        }
        ");
    }

    // ==================== Runtime Resize with Shape Input Tests ====================

    #[test]
    fn test_resize_runtime_shape_nearest() {
        let config = ResizeConfig {
            mode: ResizeMode::Nearest,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "target_size".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("dynamic_resize")
            .input_tensor("input", 4, DType::F32)
            .input_shape("target_size")
            .output_tensor("output", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, input: Tensor<B, 4>, target_size: [i64; 1]) -> Tensor<B, 4> {
            let output = {
                let target_height = target_size[2] as usize;
                let target_width = target_size[3] as usize;
                burn::tensor::module::interpolate(
                    input,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Nearest,
                    ),
                )
            };
            output
        }
        ");
    }

    #[test]
    fn test_resize_runtime_shape_linear() {
        let config = ResizeConfig {
            mode: ResizeMode::Linear,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "new_dims".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("bilinear_resize")
            .input_tensor("img", 4, DType::F32)
            .input_shape("new_dims")
            .output_tensor("resized_img", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, img: Tensor<B, 4>, new_dims: [i64; 1]) -> Tensor<B, 4> {
            let resized_img = {
                let target_height = new_dims[2] as usize;
                let target_width = new_dims[3] as usize;
                burn::tensor::module::interpolate(
                    img,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Bilinear,
                    ),
                )
            };
            resized_img
        }
        ");
    }

    #[test]
    fn test_resize_runtime_shape_cubic() {
        let config = ResizeConfig {
            mode: ResizeMode::Cubic,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "output_shape".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("cubic_interp")
            .input_tensor("source", 4, DType::F32)
            .input_shape("output_shape")
            .output_tensor("dest", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, source: Tensor<B, 4>, output_shape: [i64; 1]) -> Tensor<B, 4> {
            let dest = {
                let target_height = output_shape[2] as usize;
                let target_width = output_shape[3] as usize;
                burn::tensor::module::interpolate(
                    source,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Bicubic,
                    ),
                )
            };
            dest
        }
        ");
    }

    // ==================== Runtime Resize with Tensor Input Tests ====================

    #[test]
    fn test_resize_runtime_tensor_nearest() {
        let config = ResizeConfig {
            mode: ResizeMode::Nearest,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "size_tensor".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("resize_op")
            .input_tensor("x", 4, DType::F32)
            .input_tensor("size_tensor", 1, DType::I64)
            .output_tensor("y", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, x: Tensor<B, 4>, size_tensor: Tensor<B, 1, Int>) -> Tensor<B, 4> {
            let y = {
                let sizes_data = size_tensor.to_data().convert::<i64>();
                let sizes_array = sizes_data.as_slice::<i64>().unwrap();
                let target_height = sizes_array[2] as usize;
                let target_width = sizes_array[3] as usize;
                burn::tensor::module::interpolate(
                    x,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Nearest,
                    ),
                )
            };
            y
        }
        ");
    }

    #[test]
    fn test_resize_runtime_tensor_linear() {
        let config = ResizeConfig {
            mode: ResizeMode::Linear,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "dims_tensor".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("interp2d")
            .input_tensor("frame", 4, DType::F32)
            .input_tensor("dims_tensor", 1, DType::I64)
            .output_tensor("resampled_frame", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(
            &self,
            frame: Tensor<B, 4>,
            dims_tensor: Tensor<B, 1, Int>,
        ) -> Tensor<B, 4> {
            let resampled_frame = {
                let sizes_data = dims_tensor.to_data().convert::<i64>();
                let sizes_array = sizes_data.as_slice::<i64>().unwrap();
                let target_height = sizes_array[2] as usize;
                let target_width = sizes_array[3] as usize;
                burn::tensor::module::interpolate(
                    frame,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Bilinear,
                    ),
                )
            };
            resampled_frame
        }
        ");
    }

    #[test]
    fn test_resize_runtime_tensor_cubic() {
        let config = ResizeConfig {
            mode: ResizeMode::Cubic,
            scales: None,
            sizes: Some(ResizeSizes::Runtime(RuntimeInputRef {
                name: "target_dims".to_string(),
                input_index: 1,
            })),
            ..Default::default()
        };
        let node = ResizeNodeBuilder::new("bicubic_op")
            .input_tensor("input_data", 4, DType::F32)
            .input_tensor("target_dims", 1, DType::I64)
            .output_tensor("output_data", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(
            &self,
            input_data: Tensor<B, 4>,
            target_dims: Tensor<B, 1, Int>,
        ) -> Tensor<B, 4> {
            let output_data = {
                let sizes_data = target_dims.to_data().convert::<i64>();
                let sizes_array = sizes_data.as_slice::<i64>().unwrap();
                let target_height = sizes_array[2] as usize;
                let target_width = sizes_array[3] as usize;
                burn::tensor::module::interpolate(
                    input_data,
                    [target_height, target_width],
                    burn::tensor::ops::InterpolateOptions::new(
                        burn::tensor::ops::InterpolateMode::Bicubic,
                    ),
                )
            };
            output_data
        }
        ");
    }
}