burn_tripo 0.1.0

TripoSG(-scribble) implemented in burn
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
use burn::{module::Ignored, nn, prelude::*, tensor::Int};

use super::{
    components::{CrossAttention, FeedForward, record_tensor},
    hooks::HookRecorder,
};

#[derive(Config, Debug)]
pub struct TripoSGDiTConfig {
    pub in_channels: usize,
    pub width: usize,
    pub num_layers: usize,
    pub num_attention_heads: usize,
    pub cross_attention_dim: usize,
    pub cross_attention_2_dim: Option<usize>,
}

impl TripoSGDiTConfig {
    pub fn midi_3d() -> Self {
        Self {
            in_channels: 64,
            width: 2048,
            num_layers: 21,
            num_attention_heads: 16,
            cross_attention_dim: 768,
            cross_attention_2_dim: Some(1024),
        }
    }

    pub fn triposg_pretrained() -> Self {
        Self {
            in_channels: 64,
            width: 2048,
            num_layers: 21,
            num_attention_heads: 16,
            cross_attention_dim: 1024,
            cross_attention_2_dim: None,
        }
    }

    #[cfg(feature = "import")]
    pub fn from_config_bytes(bytes: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
        let config: TripoSGDiTConfigFile = serde_json::from_slice(bytes)?;
        Ok(Self {
            in_channels: config.in_channels.unwrap_or(64),
            width: config.width.unwrap_or(2048),
            num_layers: config.num_layers.unwrap_or(21),
            num_attention_heads: config.num_attention_heads.unwrap_or(16),
            cross_attention_dim: config.cross_attention_dim.unwrap_or(768),
            cross_attention_2_dim: config.cross_attention_2_dim,
        })
    }

    #[cfg(feature = "import")]
    pub fn from_config_file(
        path: impl AsRef<std::path::Path>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let bytes = std::fs::read(path)?;
        Self::from_config_bytes(&bytes)
    }

    pub fn init<B: Backend>(&self, device: &B::Device) -> TripoSGDiT<B> {
        TripoSGDiT::new(device, self.clone())
    }
}

#[cfg(feature = "import")]
#[derive(serde::Deserialize)]
struct TripoSGDiTConfigFile {
    cross_attention_dim: Option<usize>,
    cross_attention_2_dim: Option<usize>,
    in_channels: Option<usize>,
    num_attention_heads: Option<usize>,
    num_layers: Option<usize>,
    width: Option<usize>,
}

#[derive(Module, Debug)]
pub struct TimestepEmbedding<B: Backend> {
    pub linear_1: nn::Linear<B>,
    pub linear_2: nn::Linear<B>,
    pub activation: nn::Gelu,
}

impl<B: Backend> TimestepEmbedding<B> {
    pub fn new(device: &B::Device, in_dim: usize, hidden_dim: usize, out_dim: usize) -> Self {
        let linear_1 = nn::LinearConfig::new(in_dim, hidden_dim)
            .with_bias(true)
            .init(device);
        let linear_2 = nn::LinearConfig::new(hidden_dim, out_dim)
            .with_bias(true)
            .init(device);
        let activation = nn::Gelu::new();

        Self {
            linear_1,
            linear_2,
            activation,
        }
    }

    pub fn forward(&self, x: Tensor<B, 2>) -> Tensor<B, 2> {
        let x = self.linear_1.forward(x);
        let x = self.activation.forward(x);
        self.linear_2.forward(x)
    }
}

fn timestep_embedding<B: Backend>(
    timesteps: Tensor<B, 1>,
    embedding_dim: usize,
    flip_sin_to_cos: bool,
    downscale_freq_shift: f32,
    scale: f32,
) -> Tensor<B, 2> {
    let [batch] = timesteps.shape().dims();
    let half = embedding_dim / 2;
    let device = timesteps.device();

    let exponent = Tensor::<B, 1, Int>::arange(0..half as i64, &device).float();
    let exponent = exponent
        .mul_scalar(-(10000.0_f32).ln())
        .div_scalar(half as f32 - downscale_freq_shift);
    let emb = exponent.exp();
    let emb = timesteps.clone().unsqueeze_dim(1).mul(emb.unsqueeze_dim(0));
    let emb = emb.mul_scalar(scale);

    let sin = emb.clone().sin();
    let cos = emb.cos();
    let mut out = if flip_sin_to_cos {
        Tensor::cat(vec![cos, sin], 1)
    } else {
        Tensor::cat(vec![sin, cos], 1)
    };

    if embedding_dim % 2 == 1 {
        let pad = Tensor::<B, 2>::zeros([batch, 1], &device);
        out = Tensor::cat(vec![out, pad], 1);
    }

    out
}

#[derive(Module, Debug)]
pub struct TripoSGDiTBlock<B: Backend> {
    pub norm1: nn::LayerNorm<B>,
    pub attn1: CrossAttention<B>,
    pub norm2: nn::LayerNorm<B>,
    pub attn2: CrossAttention<B>,
    pub norm2_2: Option<nn::LayerNorm<B>>,
    pub attn2_2: Option<CrossAttention<B>>,
    pub norm3: nn::LayerNorm<B>,
    pub ff: FeedForward<B>,
    pub skip_norm: Option<nn::LayerNorm<B>>,
    pub skip_linear: Option<nn::Linear<B>>,
    use_self_attention: bool,
    use_cross_attention: bool,
    use_cross_attention_2: bool,
    use_skip: bool,
    skip_concat_front: bool,
    skip_norm_last: bool,
}

impl<B: Backend> TripoSGDiTBlock<B> {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        device: &B::Device,
        dim: usize,
        num_heads: usize,
        cross_attention_dim: usize,
        cross_attention_2_dim: Option<usize>,
        use_self_attention: bool,
        use_cross_attention: bool,
        use_cross_attention_2: bool,
        use_skip: bool,
        skip_concat_front: bool,
        skip_norm_last: bool,
    ) -> Self {
        let norm1 = nn::LayerNormConfig::new(dim).init(device);
        let attn1 =
            CrossAttention::new(device, dim, dim, num_heads, false, true, false, true, false);
        let norm2 = nn::LayerNormConfig::new(dim).init(device);
        let attn2 = CrossAttention::new(
            device,
            dim,
            cross_attention_dim,
            num_heads,
            false,
            true,
            false,
            true,
            true,
        );
        let (norm2_2, attn2_2) = if use_cross_attention_2 {
            let dim2 = cross_attention_2_dim.expect("cross_attention_2_dim required");
            let norm2_2 = nn::LayerNormConfig::new(dim).init(device);
            let attn2_2 =
                CrossAttention::new(device, dim, dim2, num_heads, false, true, false, true, true);
            (Some(norm2_2), Some(attn2_2))
        } else {
            (None, None)
        };
        let norm3 = nn::LayerNormConfig::new(dim).init(device);
        let ff = FeedForward::new(device, dim, dim * 4);

        let (skip_norm, skip_linear) = if use_skip {
            let skip_norm = nn::LayerNormConfig::new(dim).init(device);
            let skip_linear = nn::LinearConfig::new(dim * 2, dim)
                .with_bias(true)
                .init(device);
            (Some(skip_norm), Some(skip_linear))
        } else {
            (None, None)
        };

        Self {
            norm1,
            attn1,
            norm2,
            attn2,
            norm2_2,
            attn2_2,
            norm3,
            ff,
            skip_norm,
            skip_linear,
            use_self_attention,
            use_cross_attention,
            use_cross_attention_2,
            use_skip,
            skip_concat_front,
            skip_norm_last,
        }
    }

    pub fn forward(
        &self,
        hidden_states: Tensor<B, 3>,
        encoder_hidden_states: Tensor<B, 3>,
        encoder_hidden_states_2: Option<Tensor<B, 3>>,
        skip: Option<Tensor<B, 3>>,
        mut hook: Option<&mut HookRecorder>,
        idx: usize,
    ) -> Tensor<B, 3> {
        let prefix = format!("dit.blocks.{idx}");
        let mut hidden = hidden_states;

        if self.use_skip {
            let skip = skip.expect("skip tensor required for this block");
            let skip_norm = self
                .skip_norm
                .as_ref()
                .expect("skip_norm missing for skip block");
            let skip_linear = self
                .skip_linear
                .as_ref()
                .expect("skip_linear missing for skip block");
            let cat = if self.skip_concat_front {
                Tensor::cat(vec![skip, hidden], 2)
            } else {
                Tensor::cat(vec![hidden, skip], 2)
            };

            if self.skip_norm_last {
                let out = skip_linear.forward(cat);
                hidden = skip_norm.forward(out);
            } else {
                let out = skip_norm.forward(cat);
                hidden = skip_linear.forward(out);
            }

            record_tensor(&mut hook, &format!("{prefix}.skip"), &hidden);
        }

        if self.use_self_attention {
            let norm_hidden = self.norm1.forward(hidden.clone());
            record_tensor(&mut hook, &format!("{prefix}.norm1"), &norm_hidden);
            let attn = self.attn1.forward(
                norm_hidden.clone(),
                norm_hidden,
                hook.as_deref_mut(),
                &format!("{prefix}.attn1"),
            );
            hidden = hidden + attn;
            record_tensor(&mut hook, &format!("{prefix}.attn1_out"), &hidden);
        }

        if self.use_cross_attention {
            if self.use_cross_attention_2 {
                let norm_hidden = self.norm2.forward(hidden.clone());
                record_tensor(&mut hook, &format!("{prefix}.norm2"), &norm_hidden);
                let attn2 = self.attn2.forward(
                    norm_hidden,
                    encoder_hidden_states.clone(),
                    hook.as_deref_mut(),
                    &format!("{prefix}.attn2"),
                );

                let enc2 = encoder_hidden_states_2.expect("encoder_hidden_states_2 required");
                let norm2_2 = self.norm2_2.as_ref().expect("norm2_2 required");
                let attn2_2 = self.attn2_2.as_ref().expect("attn2_2 required");
                let norm_hidden = norm2_2.forward(hidden.clone());
                record_tensor(&mut hook, &format!("{prefix}.norm2_2"), &norm_hidden);
                let attn2_2 = attn2_2.forward(
                    norm_hidden,
                    enc2,
                    hook.as_deref_mut(),
                    &format!("{prefix}.attn2_2"),
                );

                hidden = hidden + attn2 + attn2_2;
                record_tensor(&mut hook, &format!("{prefix}.attn2_out"), &hidden);
            } else {
                let norm_hidden = self.norm2.forward(hidden.clone());
                record_tensor(&mut hook, &format!("{prefix}.norm2"), &norm_hidden);
                let attn = self.attn2.forward(
                    norm_hidden,
                    encoder_hidden_states,
                    hook.as_deref_mut(),
                    &format!("{prefix}.attn2"),
                );
                hidden = hidden + attn;
                record_tensor(&mut hook, &format!("{prefix}.attn2_out"), &hidden);
            }
        }

        let norm_hidden = self.norm3.forward(hidden.clone());
        record_tensor(&mut hook, &format!("{prefix}.norm3"), &norm_hidden);
        let ff = self
            .ff
            .forward(norm_hidden, hook.as_deref_mut(), &format!("{prefix}.ff"));
        let hidden = hidden + ff;
        record_tensor(&mut hook, &format!("{prefix}.out"), &hidden);
        hidden
    }
}

#[derive(Module, Debug)]
pub struct TripoSGDiT<B: Backend> {
    config: Ignored<TripoSGDiTConfig>,
    pub time_proj: TimestepEmbedding<B>,
    pub proj_in: nn::Linear<B>,
    pub blocks: Vec<TripoSGDiTBlock<B>>,
    pub norm_out: nn::LayerNorm<B>,
    pub proj_out: nn::Linear<B>,
    inner_dim: usize,
}

impl<B: Backend> TripoSGDiT<B> {
    pub fn new(device: &B::Device, config: TripoSGDiTConfig) -> Self {
        let inner_dim = config.width;
        let time_embed_dim = inner_dim * 4;
        let time_proj = TimestepEmbedding::new(device, inner_dim, time_embed_dim, inner_dim);
        let proj_in = nn::LinearConfig::new(config.in_channels, inner_dim)
            .with_bias(true)
            .init(device);

        let mut blocks = Vec::with_capacity(config.num_layers);
        let half = config.num_layers / 2;
        let use_cross_attention_2 = config.cross_attention_2_dim.is_some();
        for layer in 0..config.num_layers {
            let use_skip = layer > half;
            blocks.push(TripoSGDiTBlock::new(
                device,
                inner_dim,
                config.num_attention_heads,
                config.cross_attention_dim,
                config.cross_attention_2_dim,
                true,
                true,
                use_cross_attention_2,
                use_skip,
                true,
                true,
            ));
        }

        let norm_out = nn::LayerNormConfig::new(inner_dim).init(device);
        let proj_out = nn::LinearConfig::new(inner_dim, config.in_channels)
            .with_bias(true)
            .init(device);

        Self {
            config: Ignored(config),
            time_proj,
            proj_in,
            blocks,
            norm_out,
            proj_out,
            inner_dim,
        }
    }

    pub fn config(&self) -> &TripoSGDiTConfig {
        &self.config
    }

    pub fn forward(
        &self,
        hidden_states: Tensor<B, 3>,
        timestep: Tensor<B, 1>,
        encoder_hidden_states: Tensor<B, 3>,
        encoder_hidden_states_2: Option<Tensor<B, 3>>,
        mut hook: Option<&mut HookRecorder>,
    ) -> Tensor<B, 3> {
        let [batch, n, _] = hidden_states.shape().dims();
        let temb = timestep_embedding(timestep, self.inner_dim, false, 0.0, 1.0);
        record_tensor(&mut hook, "dit.temb", &temb);
        let temb = self.time_proj.forward(temb);
        record_tensor(&mut hook, "dit.temb_proj", &temb);
        let temb = temb.unsqueeze_dim(1);

        let hidden = self.proj_in.forward(hidden_states);
        record_tensor(&mut hook, "dit.proj_in", &hidden);
        let mut hidden = Tensor::cat(vec![temb.clone(), hidden], 1);
        record_tensor(&mut hook, "dit.tokens", &hidden);

        let mut skips = Vec::new();
        let half = self.blocks.len() / 2;
        for (idx, block) in self.blocks.iter().enumerate() {
            let skip = if idx > half { skips.pop() } else { None };
            hidden = block.forward(
                hidden,
                encoder_hidden_states.clone(),
                encoder_hidden_states_2.clone(),
                skip,
                hook.as_deref_mut(),
                idx,
            );

            if idx < half {
                skips.push(hidden.clone());
            }
        }

        let hidden = self.norm_out.forward(hidden);
        record_tensor(&mut hook, "dit.norm_out", &hidden);
        let hidden = hidden.slice([0..batch, 1..(n + 1), 0..self.inner_dim]);
        let hidden = self.proj_out.forward(hidden);
        record_tensor(&mut hook, "dit.proj_out", &hidden);
        hidden
    }
}

#[cfg(feature = "import")]
pub mod import {
    use std::path::{Path, PathBuf};

    use burn::module::{Module, ModuleMapper, Param};
    use burn::prelude::*;
    use burn::tensor::Bytes;
    use burn::tensor::FloatDType;
    use burn_store::{
        BurnpackStore, KeyRemapper, ModuleSnapshot, PyTorchToBurnAdapter, SafetensorsStore,
    };
    use burn_synth_import::parts::load_model_from_burnpack_parts;

    use super::super::load_policy::{BurnpackLoadPolicy, burnpack_path, candidate_burnpack_paths};
    use super::{TripoSGDiT, TripoSGDiTConfig};

    pub fn load_triposg_dit<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        path: impl AsRef<Path>,
    ) -> Result<TripoSGDiT<B>, Box<dyn std::error::Error>> {
        load_triposg_dit_with_policy(config, device, path, default_burnpack_policy())
    }

    pub fn load_triposg_dit_with_policy<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        path: impl AsRef<Path>,
        policy: BurnpackLoadPolicy,
    ) -> Result<TripoSGDiT<B>, Box<dyn std::error::Error>> {
        let path = path.as_ref();
        let burnpack_candidates = candidate_burnpack_paths(path, policy);
        if let Some(model) = load_model_from_burnpack_parts(
            &burnpack_candidates,
            "TripoSG DiT",
            should_validate_burnpack(),
            || TripoSGDiT::new(device, config.clone()),
            |model, part_bytes| {
                apply_triposg_dit_burnpack_part_bytes(model, part_bytes).map_err(|err| {
                    format!("failed to apply TripoSG DiT burnpack part bytes: {err}")
                })
            },
        )? {
            return Ok(model);
        }
        let burnpack_path = burnpack_candidates
            .iter()
            .find(|candidate| candidate.exists())
            .cloned();
        let Some(burnpack_path) = burnpack_path else {
            let checked = burnpack_candidates
                .iter()
                .map(|candidate| candidate.display().to_string())
                .collect::<Vec<_>>()
                .join(", ");
            return Err(format!(
                "Burnpack weights missing. Checked: {checked}. Run `triposg_import` to generate .bpk files."
            )
            .into());
        };

        let mut model = TripoSGDiT::new(device, config.clone());
        let mut store =
            BurnpackStore::from_file(&burnpack_path).validate(should_validate_burnpack());
        model
            .load_from(&mut store)
            .map_err(|err| format!("failed to load TripoSG DiT burnpack: {err}"))?;
        Ok(model)
    }

    pub fn load_triposg_dit_from_burnpack_bytes<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        burnpack_bytes: Vec<u8>,
    ) -> Result<TripoSGDiT<B>, Box<dyn std::error::Error>> {
        let mut model = TripoSGDiT::new(device, config.clone());
        let mut store = BurnpackStore::from_bytes(Some(Bytes::from_bytes_vec(burnpack_bytes)))
            .validate(should_validate_burnpack());
        model
            .load_from(&mut store)
            .map_err(|err| format!("failed to load TripoSG DiT burnpack bytes: {err}"))?;
        Ok(model)
    }

    pub fn apply_triposg_dit_burnpack_part_bytes<B: Backend>(
        model: &mut TripoSGDiT<B>,
        burnpack_bytes: Vec<u8>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let mut store = BurnpackStore::from_bytes(Some(Bytes::from_bytes_vec(burnpack_bytes)))
            .allow_partial(true)
            .validate(should_validate_burnpack());
        model
            .load_from(&mut store)
            .map_err(|err| format!("failed to load TripoSG DiT burnpack part bytes: {err}"))?;
        Ok(())
    }

    pub fn load_triposg_dit_from_burnpack_file<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        burnpack_path: impl AsRef<Path>,
    ) -> Result<TripoSGDiT<B>, Box<dyn std::error::Error>> {
        let mut model = TripoSGDiT::new(device, config.clone());
        let mut store =
            BurnpackStore::from_file(burnpack_path.as_ref()).validate(should_validate_burnpack());
        model
            .load_from(&mut store)
            .map_err(|err| format!("failed to load TripoSG DiT burnpack file: {err}"))?;
        Ok(model)
    }

    fn default_burnpack_policy() -> BurnpackLoadPolicy {
        BurnpackLoadPolicy::default()
    }

    fn should_validate_burnpack() -> bool {
        cfg!(all(not(target_arch = "wasm32"), debug_assertions))
    }

    pub fn load_triposg_dit_from_safetensors<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        path: impl AsRef<Path>,
    ) -> Result<TripoSGDiT<B>, Box<dyn std::error::Error>> {
        let mut model = TripoSGDiT::new(device, config.clone());
        let mut store = build_store(path.as_ref())?;
        model
            .load_from(&mut store)
            .map_err(|err| format!("failed to apply TripoSG DiT weights: {err}"))?;
        Ok(model)
    }

    pub fn import_triposg_dit_burnpack<B: Backend>(
        config: &TripoSGDiTConfig,
        device: &B::Device,
        path: impl AsRef<Path>,
        use_f16: bool,
    ) -> Result<PathBuf, Box<dyn std::error::Error>> {
        let path = path.as_ref();
        let burnpack_path = burnpack_path(path, use_f16, BurnpackLoadPolicy::default().f16_suffix);
        let model = load_triposg_dit_from_safetensors::<B>(config, device, path)?;
        let model = if use_f16 {
            cast_module_float_dtype(model, FloatDType::F16)
        } else {
            model
        };
        save_burnpack(&model, &burnpack_path)?;
        Ok(burnpack_path)
    }

    struct FloatDTypeMapper {
        dtype: FloatDType,
    }

    impl<B: Backend> ModuleMapper<B> for FloatDTypeMapper {
        fn map_float<const D: usize>(&mut self, param: Param<Tensor<B, D>>) -> Param<Tensor<B, D>> {
            let (id, tensor, mapper) = param.consume();
            let tensor = tensor.cast(self.dtype);
            Param::from_mapped_value(id, tensor, mapper)
        }
    }

    fn cast_module_float_dtype<B: Backend, M: Module<B>>(module: M, dtype: FloatDType) -> M {
        let mut mapper = FloatDTypeMapper { dtype };
        module.map(&mut mapper)
    }

    fn save_burnpack<B: Backend>(
        model: &TripoSGDiT<B>,
        path: &Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let mut store = BurnpackStore::from_file(path).overwrite(true);
        model
            .save_into(&mut store)
            .map_err(|err| format!("failed to save TripoSG DiT burnpack: {err}"))?;
        Ok(())
    }

    fn build_store(path: &Path) -> Result<SafetensorsStore, Box<dyn std::error::Error>> {
        let mut remapper = KeyRemapper::new();
        for &(from, to) in key_remap_rules() {
            remapper = remapper
                .add_pattern(from, to)
                .map_err(|err| format!("invalid remap rule {from}->{to}: {err}"))?;
        }

        let store = SafetensorsStore::from_file(path)
            .with_from_adapter(PyTorchToBurnAdapter)
            .allow_partial(false)
            .remap(remapper)
            .validate(true);

        Ok(store)
    }

    fn key_remap_rules() -> &'static [(&'static str, &'static str)] {
        &[
            (r"^(blocks\.\d+\.attn1\.to_out)\.0\.(weight|bias)$", "$1.$2"),
            (r"^(blocks\.\d+\.attn2\.to_out)\.0\.(weight|bias)$", "$1.$2"),
            (
                r"^(blocks\.\d+\.attn2_2\.to_out)\.0\.(weight|bias)$",
                "$1.$2",
            ),
            (
                r"^(blocks\.\d+\.ff)\.net\.0\.proj\.(weight|bias)$",
                "$1.proj.$2",
            ),
            (r"^(blocks\.\d+\.ff)\.net\.2\.(weight|bias)$", "$1.out.$2"),
            (r"^(blocks\.\d+\.norm1)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.norm1)\.bias$", "$1.beta"),
            (r"^(blocks\.\d+\.norm2)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.norm2)\.bias$", "$1.beta"),
            (r"^(blocks\.\d+\.norm2_2)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.norm2_2)\.bias$", "$1.beta"),
            (r"^(blocks\.\d+\.norm3)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.norm3)\.bias$", "$1.beta"),
            (r"^(blocks\.\d+\.skip_norm)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.skip_norm)\.bias$", "$1.beta"),
            (r"^(blocks\.\d+\.attn1\.norm_q)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.attn1\.norm_k)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.attn2\.norm_q)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.attn2\.norm_k)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.attn2_2\.norm_q)\.weight$", "$1.gamma"),
            (r"^(blocks\.\d+\.attn2_2\.norm_k)\.weight$", "$1.gamma"),
            (r"^(norm_out)\.weight$", "$1.gamma"),
            (r"^(norm_out)\.bias$", "$1.beta"),
        ]
    }
}