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
use crate::model::load::*;

use std::error::Error;

use burn::{module::Module, tensor::backend::Backend};

use super::*;
use crate::model::groupnorm::load::load_group_norm;

fn load_conv_self_attention_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ConvSelfAttentionBlock<B>, Box<dyn Error>> {
    let norm = load_group_norm(&format!("{}/{}", path, "norm"), device)?;
    let q = load_conv2d(&format!("{}/{}", path, "q"), device)?;
    let k = load_conv2d(&format!("{}/{}", path, "k"), device)?;
    let v = load_conv2d(&format!("{}/{}", path, "v"), device)?;
    let proj_out = load_conv2d(&format!("{}/{}", path, "proj_out"), device)?;

    Ok(ConvSelfAttentionBlock {
        norm,
        q,
        k,
        v,
        proj_out,
    })
}

fn load_resnet_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResnetBlock<B>, Box<dyn Error>> {
    let norm1 = load_group_norm(&format!("{}/{}", path, "norm1"), device)?;
    let silu1 = SILU {};
    let conv1 = load_conv2d(&format!("{}/{}", path, "conv1"), device)?;
    let norm2 = load_group_norm(&format!("{}/{}", path, "norm2"), device)?;
    let silu2 = SILU {};
    let conv2 = load_conv2d(&format!("{}/{}", path, "conv2"), device)?;
    let nin_shortcut = load_conv2d(&format!("{}/{}", path, "nin_shortcut"), device).ok();

    Ok(ResnetBlock {
        norm1,
        silu1,
        conv1,
        norm2,
        silu2,
        conv2,
        nin_shortcut,
    })
}

fn load_mid<B: Backend>(path: &str, device: &B::Device) -> Result<Mid<B>, Box<dyn Error>> {
    let block_1 = load_resnet_block(&format!("{}/{}", path, "block_1"), device)?;
    let attn = load_conv_self_attention_block(&format!("{}/{}", path, "attn"), device)?;
    let block_2 = load_resnet_block(&format!("{}/{}", path, "block_2"), device)?;

    Ok(Mid {
        block_1,
        attn,
        block_2,
    })
}

fn load_padded_conv2d<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<PaddedConv2d<B>, Box<dyn Error>> {
    let conv = load_conv2d(&format!("{}/{}", path, "conv"), device)?;

    let channels = load_tensor::<B, 1>("channels", path, device)?;
    let channels = tensor_to_array_2(channels);

    let kernel_size = load_usize::<B>("kernel_size", path, device)?;
    let stride = load_usize::<B>("stride", path, device)?;

    let padding = load_tensor::<B, 1>("padding", path, device)?;
    let padding: [usize; 4] = tensor_to_array(padding);
    let padding = Padding::new(padding[0], padding[1], padding[2], padding[3]);

    let mut record = conv.into_record();

    let mut padded_conv: PaddedConv2d<B> = PaddedConv2dConfig::new(channels, kernel_size, padding)
        .with_stride(stride)
        .init();
    let padding_actual =
        PaddingConfig2d::Explicit(padded_conv.padding_actual[0], padded_conv.padding_actual[1]);

    record.padding = <PaddingConfig2d as Module<B>>::into_record(padding_actual);
    padded_conv.conv = padded_conv.conv.load_record(record);

    Ok(padded_conv)
}

fn load_decoder_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<DecoderBlock<B>, Box<dyn Error>> {
    let res1 = load_resnet_block(&format!("{}/{}", path, "res1"), device)?;
    let res2 = load_resnet_block(&format!("{}/{}", path, "res2"), device)?;
    let res3 = load_resnet_block(&format!("{}/{}", path, "res3"), device)?;
    let upsampler = load_conv2d(&format!("{}/{}", path, "upsampler"), device).ok();

    Ok(DecoderBlock {
        res1,
        res2,
        res3,
        upsampler,
    })
}

fn load_encoder_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<EncoderBlock<B>, Box<dyn Error>> {
    let res1 = load_resnet_block(&format!("{}/{}", path, "res1"), device)?;
    let res2 = load_resnet_block(&format!("{}/{}", path, "res2"), device)?;
    let downsampler = load_padded_conv2d(&format!("{}/{}", path, "downsampler"), device).ok();

    Ok(EncoderBlock {
        res1,
        res2,
        downsampler,
    })
}

fn load_decoder<B: Backend>(path: &str, device: &B::Device) -> Result<Decoder<B>, Box<dyn Error>> {
    let conv_in = load_conv2d(&format!("{}/{}", path, "conv_in"), device)?;
    let mid = load_mid(&format!("{}/{}", path, "mid"), device)?;

    let n_block = load_usize::<B>("n_block", path, device)?;
    let blocks = (0..n_block)
        .into_iter()
        .map(|i| load_decoder_block::<B>(&format!("{}/blocks/{}", path, i), device))
        .collect::<Result<Vec<_>, _>>()?;

    let norm_out = load_group_norm(&format!("{}/{}", path, "norm_out"), device)?;
    let silu = SILU {};
    let conv_out = load_conv2d(&format!("{}/{}", path, "conv_out"), device)?;

    Ok(Decoder {
        conv_in,
        mid,
        blocks,
        norm_out,
        silu,
        conv_out,
    })
}

fn load_encoder<B: Backend>(path: &str, device: &B::Device) -> Result<Encoder<B>, Box<dyn Error>> {
    let conv_in = load_conv2d(&format!("{}/{}", path, "conv_in"), device)?;
    let mid = load_mid(&format!("{}/{}", path, "mid"), device)?;

    let n_block = load_usize::<B>("n_block", path, device)?;
    let blocks = (0..n_block)
        .into_iter()
        .map(|i| load_encoder_block::<B>(&format!("{}/blocks/{}", path, i), device))
        .collect::<Result<Vec<_>, _>>()?;

    let norm_out = load_group_norm(&format!("{}/{}", path, "norm_out"), device)?;
    let silu = SILU {};
    let conv_out = load_conv2d(&format!("{}/{}", path, "conv_out"), device)?;

    Ok(Encoder {
        conv_in,
        mid,
        blocks,
        norm_out,
        silu,
        conv_out,
    })
}

pub fn load_autoencoder<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<Autoencoder<B>, Box<dyn Error>> {
    let encoder = load_encoder(&format!("{}/{}", path, "encoder"), device)?;
    let decoder = load_decoder(&format!("{}/{}", path, "decoder"), device)?;
    let quant_conv = load_conv2d(&format!("{}/{}", path, "quant_conv"), device)?;
    let post_quant_conv = load_conv2d(&format!("{}/{}", path, "post_quant_conv"), device)?;

    Ok(Autoencoder {
        encoder,
        decoder,
        quant_conv,
        post_quant_conv,
    })
}