burn_dragon_vision 0.4.0

Foveation and vision sampling utilities for burn dragon
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
#![allow(clippy::too_many_arguments)]

use std::any::{Any, TypeId};

use burn::tensor::Tensor as BurnTensor;
use burn::tensor::backend::Backend as BackendTrait;
use burn::tensor::{DType, Shape, TensorData, TensorPrimitive};
use burn_cubecl::fusion::FusionCubeRuntime;
use burn_cubecl::kernel::into_contiguous;
use burn_cubecl::ops::numeric::empty_device;
use burn_cubecl::tensor::CubeTensor;
use burn_cubecl::{BoolElement, CubeBackend, CubeRuntime};
use burn_fusion::FusionTensor;
use burn_fusion::stream::StreamId;
use burn_wgpu::{KernelSource, SourceKernel, SourceTemplate, WgpuRuntime};
use cubecl::prelude::*;
use cubecl_runtime::server::Bindings;

use crate::FOVEATION_BUFFER_SHADER;

use crate::train::saccade::{SaccadeLaplacianImages, SaccadeMipLevel};
use burn_dragon_train::VisionFoveaWarpMode;

const MAX_LEVELS: usize = 8;
const META_HEADER_LEN: usize = 10;
const META_LEN: usize = META_HEADER_LEN + (MAX_LEVELS * 6) + 3;
const PARAM_STRIDE: usize = 5;
const WORKGROUP_SIZE: u32 = 8;

pub(crate) fn supports_backend<B: BackendTrait>() -> bool
where
    B::FloatTensorPrimitive: 'static,
{
    matches_type::<B::FloatTensorPrimitive, FusionTensor<FusionCubeRuntime<WgpuRuntime, u32>>>()
        || matches_type::<B::FloatTensorPrimitive, FusionTensor<FusionCubeRuntime<WgpuRuntime, u8>>>(
        )
        || matches_type::<B::FloatTensorPrimitive, CubeTensor<WgpuRuntime>>()
}

pub(crate) fn try_foveated_patch_wgsl<B: BackendTrait>(
    levels: &[SaccadeMipLevel<B>],
    base_grid: &BurnTensor<B, 4>,
    center_x: &BurnTensor<B, 3>,
    center_y: &BurnTensor<B, 3>,
    sigma_px: &BurnTensor<B, 3>,
    radius_px: &BurnTensor<B, 3>,
    lod_sigma: &BurnTensor<B, 3>,
    laplacian_images: Option<&SaccadeLaplacianImages<B>>,
    warp_mode: VisionFoveaWarpMode,
) -> Option<BurnTensor<B, 4>>
where
    B::FloatTensorPrimitive: 'static,
{
    if levels.is_empty() {
        return None;
    }
    if !supports_backend::<B>() {
        return None;
    }
    let [_, patch_h, patch_w, _] = base_grid.shape().dims::<4>();
    if patch_h == 0 || patch_w == 0 {
        return None;
    }

    let use_laplacian = laplacian_images.is_some();
    let level_count = levels.len();
    if level_count > MAX_LEVELS {
        return None;
    }
    if use_laplacian {
        let residual_count = laplacian_images
            .as_ref()
            .map(|laplacian| laplacian.residuals.len())
            .unwrap_or(0);
        if residual_count + 1 > MAX_LEVELS {
            return None;
        }
    }

    if let Some(result) = try_foveated_patch_wgsl_fusion::<B, u32>(
        levels,
        center_x,
        center_y,
        sigma_px,
        radius_px,
        lod_sigma,
        laplacian_images,
        patch_h,
        patch_w,
        warp_mode,
    ) {
        return Some(result);
    }
    if let Some(result) = try_foveated_patch_wgsl_fusion::<B, u8>(
        levels,
        center_x,
        center_y,
        sigma_px,
        radius_px,
        lod_sigma,
        laplacian_images,
        patch_h,
        patch_w,
        warp_mode,
    ) {
        return Some(result);
    }
    try_foveated_patch_wgsl_direct::<B>(
        levels,
        center_x,
        center_y,
        sigma_px,
        radius_px,
        lod_sigma,
        laplacian_images,
        patch_h,
        patch_w,
        warp_mode,
    )
}

fn try_foveated_patch_wgsl_fusion<B, BT>(
    levels: &[SaccadeMipLevel<B>],
    center_x: &BurnTensor<B, 3>,
    center_y: &BurnTensor<B, 3>,
    sigma_px: &BurnTensor<B, 3>,
    radius_px: &BurnTensor<B, 3>,
    lod_sigma: &BurnTensor<B, 3>,
    laplacian_images: Option<&SaccadeLaplacianImages<B>>,
    patch_h: usize,
    patch_w: usize,
    warp_mode: VisionFoveaWarpMode,
) -> Option<BurnTensor<B, 4>>
where
    B: BackendTrait,
    B::FloatTensorPrimitive: 'static,
    BT: BoolElement + 'static,
{
    if !matches_type::<B::FloatTensorPrimitive, FusionTensor<FusionCubeRuntime<WgpuRuntime, BT>>>()
    {
        return None;
    }

    let use_laplacian = laplacian_images.is_some();
    let gaussian_buffer = build_gaussian_buffer(levels, use_laplacian)?;
    let (residual_buffer, residual_meta) =
        build_residual_buffer(laplacian_images, &gaussian_buffer.device())?;
    let params = build_params(center_x, center_y, sigma_px, radius_px, lod_sigma);
    let meta = build_meta(
        levels,
        laplacian_images,
        &gaussian_buffer,
        &residual_meta,
        patch_h,
        patch_w,
        warp_mode,
    );

    let prim_gaussian = gaussian_buffer.clone().into_primitive().tensor();
    let fusion_gaussian: FusionTensor<FusionCubeRuntime<WgpuRuntime, BT>> =
        try_cast_primitive::<B, _>(prim_gaussian)?;
    let fusion_client = fusion_gaussian.client.clone();
    let gaussian = fusion_client
        .resolve_tensor_float::<CubeBackend<WgpuRuntime, f32, i32, BT>>(fusion_gaussian);
    if gaussian.dtype != DType::F32 {
        return None;
    }

    let residual = resolve_fusion_tensor::<B, BT, 1>(&residual_buffer)?;
    let params = resolve_fusion_tensor::<B, BT, 1>(&params)?;
    let meta = resolve_fusion_tensor::<B, BT, 1>(&meta)?;

    let [batch, channels, _, _] = levels.first()?.image.shape().dims::<4>();
    let output = foveated_patch_wgsl_runtime::<WgpuRuntime>(
        gaussian, residual, params, meta, patch_h, patch_w, batch, channels,
    );
    let shape = output.shape.clone();
    let dtype = output.dtype;
    let handle = output.into();
    let fusion_out = fusion_client.register_tensor(handle, shape, StreamId::current(), dtype);
    let out_prim = try_cast_backend::<B, _>(fusion_out)?;
    Some(BurnTensor::<B, 4>::from_primitive(TensorPrimitive::Float(
        out_prim,
    )))
}

fn try_foveated_patch_wgsl_direct<B: BackendTrait>(
    levels: &[SaccadeMipLevel<B>],
    center_x: &BurnTensor<B, 3>,
    center_y: &BurnTensor<B, 3>,
    sigma_px: &BurnTensor<B, 3>,
    radius_px: &BurnTensor<B, 3>,
    lod_sigma: &BurnTensor<B, 3>,
    laplacian_images: Option<&SaccadeLaplacianImages<B>>,
    patch_h: usize,
    patch_w: usize,
    warp_mode: VisionFoveaWarpMode,
) -> Option<BurnTensor<B, 4>>
where
    B::FloatTensorPrimitive: 'static,
{
    if !matches_type::<B::FloatTensorPrimitive, CubeTensor<WgpuRuntime>>() {
        return None;
    }

    let use_laplacian = laplacian_images.is_some();
    let gaussian = build_gaussian_buffer(levels, use_laplacian)?;
    let (residual, residual_meta) = build_residual_buffer(laplacian_images, &gaussian.device())?;
    let params = build_params(center_x, center_y, sigma_px, radius_px, lod_sigma);
    let meta = build_meta(
        levels,
        laplacian_images,
        &gaussian,
        &residual_meta,
        patch_h,
        patch_w,
        warp_mode,
    );

    let gaussian = resolve_direct_tensor::<B, 1>(&gaussian)?;
    let residual = resolve_direct_tensor::<B, 1>(&residual)?;
    let params = resolve_direct_tensor::<B, 1>(&params)?;
    let meta = resolve_direct_tensor::<B, 1>(&meta)?;

    let [batch, channels, _, _] = levels.first()?.image.shape().dims::<4>();
    let output = foveated_patch_wgsl_runtime::<WgpuRuntime>(
        gaussian, residual, params, meta, patch_h, patch_w, batch, channels,
    );
    let out_prim = try_cast_backend::<B, _>(output)?;
    Some(BurnTensor::<B, 4>::from_primitive(TensorPrimitive::Float(
        out_prim,
    )))
}

fn build_params<B: BackendTrait>(
    center_x: &BurnTensor<B, 3>,
    center_y: &BurnTensor<B, 3>,
    sigma_px: &BurnTensor<B, 3>,
    radius_px: &BurnTensor<B, 3>,
    lod_sigma: &BurnTensor<B, 3>,
) -> BurnTensor<B, 1> {
    let [batch, _, _] = center_x.shape().dims::<3>();
    BurnTensor::cat(
        vec![
            center_x.clone(),
            center_y.clone(),
            sigma_px.clone(),
            radius_px.clone(),
            lod_sigma.clone(),
        ],
        2,
    )
    .reshape([batch * PARAM_STRIDE])
}

fn build_gaussian_buffer<B: BackendTrait>(
    levels: &[SaccadeMipLevel<B>],
    use_laplacian: bool,
) -> Option<BurnTensor<B, 1>>
where
    B::FloatTensorPrimitive: 'static,
{
    if use_laplacian {
        let device = levels.first()?.image.device();
        return Some(BurnTensor::<B, 1>::zeros([1], &device));
    }
    let (buffer, _) = pack_levels(levels.iter().map(|level| &level.image).collect())?;
    Some(buffer)
}

#[derive(Clone, Copy)]
struct ResidualMeta {
    offsets: [u32; MAX_LEVELS],
    widths: [u32; MAX_LEVELS],
    heights: [u32; MAX_LEVELS],
    coarse_offset: u32,
    coarse_width: u32,
    coarse_height: u32,
    residual_count: usize,
}

fn build_residual_buffer<B: BackendTrait>(
    laplacian: Option<&SaccadeLaplacianImages<B>>,
    device: &B::Device,
) -> Option<(BurnTensor<B, 1>, ResidualMeta)>
where
    B::FloatTensorPrimitive: 'static,
{
    if let Some(laplacian) = laplacian {
        let residuals: Vec<&BurnTensor<B, 4>> = laplacian.residuals.iter().collect();
        let (buffer, meta) = pack_residuals(residuals, &laplacian.coarse)?;
        return Some((buffer, meta));
    }

    let buffer = BurnTensor::<B, 1>::zeros([1], device);
    let meta = ResidualMeta {
        offsets: [0u32; MAX_LEVELS],
        widths: [0u32; MAX_LEVELS],
        heights: [0u32; MAX_LEVELS],
        coarse_offset: 0,
        coarse_width: 1,
        coarse_height: 1,
        residual_count: 0,
    };
    Some((buffer, meta))
}

fn build_meta<B: BackendTrait>(
    levels: &[SaccadeMipLevel<B>],
    laplacian: Option<&SaccadeLaplacianImages<B>>,
    gaussian: &BurnTensor<B, 1>,
    residual_meta: &ResidualMeta,
    patch_h: usize,
    patch_w: usize,
    warp_mode: VisionFoveaWarpMode,
) -> BurnTensor<B, 1> {
    let device = gaussian.device();
    let [batch, channels, _, _] = levels
        .first()
        .map(|level| level.image.shape().dims::<4>())
        .unwrap_or([0, 0, 0, 0]);
    let level_count = levels.len().min(MAX_LEVELS);
    let mode = if laplacian.is_some() { 1u32 } else { 0u32 };

    let base_source = if let Some(laplacian) = laplacian {
        laplacian.residuals.first().unwrap_or(&laplacian.coarse)
    } else {
        &levels[0].image
    };
    let [_, _, base_h, base_w] = base_source.shape().dims::<4>();

    let mut meta = vec![0.0f32; META_LEN];
    meta[0] = patch_w as f32;
    meta[1] = patch_h as f32;
    meta[2] = channels as f32;
    meta[3] = level_count as f32;
    meta[4] = residual_meta.residual_count as f32;
    meta[5] = mode as f32;
    meta[6] = match warp_mode {
        VisionFoveaWarpMode::Warped => 0.0,
        VisionFoveaWarpMode::Patched => 1.0,
    };
    meta[7] = base_w as f32;
    meta[8] = base_h as f32;
    meta[9] = batch as f32;

    let (gauss_offsets, gauss_widths, gauss_heights) = pack_level_meta(
        levels.iter().map(|level| &level.image).collect(),
        level_count,
    );
    write_meta_array(&mut meta, 10, &gauss_offsets);
    write_meta_array(&mut meta, 10 + MAX_LEVELS, &gauss_widths);
    write_meta_array(&mut meta, 10 + MAX_LEVELS * 2, &gauss_heights);

    write_meta_array(&mut meta, 10 + MAX_LEVELS * 3, &residual_meta.offsets);
    write_meta_array(&mut meta, 10 + MAX_LEVELS * 4, &residual_meta.widths);
    write_meta_array(&mut meta, 10 + MAX_LEVELS * 5, &residual_meta.heights);

    let coarse_base = 10 + MAX_LEVELS * 6;
    meta[coarse_base] = residual_meta.coarse_offset as f32;
    meta[coarse_base + 1] = residual_meta.coarse_width as f32;
    meta[coarse_base + 2] = residual_meta.coarse_height as f32;

    BurnTensor::<B, 1>::from_data(TensorData::new(meta, [META_LEN]), &device)
}

fn pack_level_meta<B: BackendTrait>(
    levels: Vec<&BurnTensor<B, 4>>,
    level_count: usize,
) -> ([u32; MAX_LEVELS], [u32; MAX_LEVELS], [u32; MAX_LEVELS]) {
    let mut offsets = [0u32; MAX_LEVELS];
    let mut widths = [0u32; MAX_LEVELS];
    let mut heights = [0u32; MAX_LEVELS];
    let mut offset = 0u32;
    for (idx, level) in levels.into_iter().take(level_count).enumerate() {
        let [batch, channels, height, width] = level.shape().dims::<4>();
        offsets[idx] = offset;
        widths[idx] = width as u32;
        heights[idx] = height as u32;
        let elems = batch * channels * height * width;
        offset = offset.saturating_add(elems as u32);
    }
    (offsets, widths, heights)
}

fn pack_levels<B: BackendTrait>(levels: Vec<&BurnTensor<B, 4>>) -> Option<(BurnTensor<B, 1>, usize)>
where
    B::FloatTensorPrimitive: 'static,
{
    let first = levels.first()?;
    let [batch, channels, _, _] = first.shape().dims::<4>();
    let mut flats = Vec::with_capacity(levels.len());
    for level in levels {
        let [b, c, h, w] = level.shape().dims::<4>();
        if b != batch || c != channels {
            return None;
        }
        let elems = b * c * h * w;
        flats.push(level.clone().reshape([elems]));
    }
    let merged = if flats.len() == 1 {
        flats[0].clone()
    } else {
        BurnTensor::cat(flats, 0)
    };
    Some((merged, batch))
}

fn pack_residuals<B: BackendTrait>(
    residuals: Vec<&BurnTensor<B, 4>>,
    coarse: &BurnTensor<B, 4>,
) -> Option<(BurnTensor<B, 1>, ResidualMeta)>
where
    B::FloatTensorPrimitive: 'static,
{
    let mut offsets = [0u32; MAX_LEVELS];
    let mut widths = [0u32; MAX_LEVELS];
    let mut heights = [0u32; MAX_LEVELS];
    let mut offset = 0u32;
    let residual_count = residuals.len().min(MAX_LEVELS - 1);

    let mut flats = Vec::with_capacity(residual_count + 1);
    for (idx, level) in residuals.into_iter().take(residual_count).enumerate() {
        let [batch, channels, height, width] = level.shape().dims::<4>();
        offsets[idx] = offset;
        widths[idx] = width as u32;
        heights[idx] = height as u32;
        let elems = batch * channels * height * width;
        flats.push(level.clone().reshape([elems]));
        offset = offset.saturating_add(elems as u32);
    }

    let [batch, channels, coarse_h, coarse_w] = coarse.shape().dims::<4>();
    let coarse_elems = batch * channels * coarse_h * coarse_w;
    let coarse_offset = offset;
    flats.push(coarse.clone().reshape([coarse_elems]));
    let merged = if flats.len() == 1 {
        flats[0].clone()
    } else {
        BurnTensor::cat(flats, 0)
    };

    let meta = ResidualMeta {
        offsets,
        widths,
        heights,
        coarse_offset,
        coarse_width: coarse_w as u32,
        coarse_height: coarse_h as u32,
        residual_count,
    };
    Some((merged, meta))
}

fn write_meta_array(meta: &mut [f32], start: usize, data: &[u32; MAX_LEVELS]) {
    for (idx, value) in data.iter().enumerate() {
        meta[start + idx] = *value as f32;
    }
}

fn resolve_fusion_tensor<B, BT, const D: usize>(
    tensor: &BurnTensor<B, D>,
) -> Option<CubeTensor<WgpuRuntime>>
where
    B: BackendTrait,
    B::FloatTensorPrimitive: 'static,
    BT: BoolElement + 'static,
{
    let prim = tensor.clone().into_primitive().tensor();
    let fusion: FusionTensor<FusionCubeRuntime<WgpuRuntime, BT>> =
        try_cast_primitive::<B, _>(prim)?;
    let client = fusion.client.clone();
    let cube = client.resolve_tensor_float::<CubeBackend<WgpuRuntime, f32, i32, BT>>(fusion);
    if cube.dtype != DType::F32 {
        return None;
    }
    Some(cube)
}

fn resolve_direct_tensor<B: BackendTrait, const D: usize>(
    tensor: &BurnTensor<B, D>,
) -> Option<CubeTensor<WgpuRuntime>>
where
    B::FloatTensorPrimitive: 'static,
{
    let prim = tensor.clone().into_primitive().tensor();
    let cube: CubeTensor<WgpuRuntime> = try_cast_primitive::<B, _>(prim)?;
    if cube.dtype != DType::F32 {
        return None;
    }
    Some(cube)
}

fn foveated_patch_wgsl_runtime<R: CubeRuntime>(
    gaussian: CubeTensor<R>,
    residual: CubeTensor<R>,
    params: CubeTensor<R>,
    meta: CubeTensor<R>,
    patch_h: usize,
    patch_w: usize,
    batch: usize,
    channels: usize,
) -> CubeTensor<R> {
    let gaussian = into_contiguous(gaussian);
    let residual = into_contiguous(residual);
    let params = into_contiguous(params);
    let meta = into_contiguous(meta);

    let client = gaussian.client.clone();
    let device = gaussian.device.clone();
    let shape = Shape::new([batch, channels, patch_h, patch_w]);
    let output = empty_device::<R, f32>(client.clone(), device, shape);

    let workgroups_x = div_ceil_u32(patch_w as u32, WORKGROUP_SIZE);
    let workgroups_y = div_ceil_u32(patch_h as u32, WORKGROUP_SIZE);
    let count = CubeCount::Static(workgroups_x, workgroups_y, batch as u32);

    let kernel = SourceKernel::new(
        FoveationBufferKernel,
        CubeDim::new(WORKGROUP_SIZE, WORKGROUP_SIZE, 1),
    );
    let bindings = Bindings::new().with_buffers(vec![
        gaussian.handle.clone().binding(),
        residual.handle.clone().binding(),
        output.handle.clone().binding(),
        params.handle.clone().binding(),
        meta.handle.clone().binding(),
    ]);
    client.execute(Box::new(kernel), count, bindings);
    output
}

fn div_ceil_u32(value: u32, divisor: u32) -> u32 {
    value.div_ceil(divisor)
}

#[derive(Clone)]
struct FoveationBufferKernel;

impl KernelSource for FoveationBufferKernel {
    fn source(&self) -> SourceTemplate {
        SourceTemplate::new(FOVEATION_BUFFER_SHADER)
    }

    fn id(&self) -> KernelId {
        KernelId::new::<Self>()
    }
}

fn matches_type<A: 'static, B: 'static>() -> bool {
    TypeId::of::<A>() == TypeId::of::<B>()
}

fn try_cast_primitive<B: BackendTrait, T: 'static>(value: B::FloatTensorPrimitive) -> Option<T>
where
    B::FloatTensorPrimitive: 'static,
{
    let boxed: Box<dyn Any> = Box::new(value);
    boxed.downcast::<T>().ok().map(|boxed| *boxed)
}

fn try_cast_backend<B: BackendTrait, T: 'static>(value: T) -> Option<B::FloatTensorPrimitive>
where
    B::FloatTensorPrimitive: 'static,
{
    let boxed: Box<dyn Any> = Box::new(value);
    boxed
        .downcast::<B::FloatTensorPrimitive>()
        .ok()
        .map(|boxed| *boxed)
}