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

use std::error::Error;

use burn::tensor::backend::Backend;

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

pub fn load_res_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResBlock<B>, Box<dyn Error>> {
    let norm_in = load_group_norm::<B>(&format!("{}/{}", path, "norm_in"), device)?;
    let conv_in = load_conv2d::<B>(&format!("{}/{}", path, "conv_in"), device)?;
    let lin_embed = load_linear::<B>(&format!("{}/{}", path, "lin_embed"), device)?;
    let norm_out = load_group_norm::<B>(&format!("{}/{}", path, "norm_out"), device)?;
    let conv_out = load_conv2d::<B>(&format!("{}/{}", path, "conv_out"), device)?;
    let skip_connection = load_conv2d::<B>(&format!("{}/{}", path, "skip_connection"), device).ok();

    let res_block = ResBlock {
        norm_in: norm_in,
        silu_in: SILU::new(),
        conv_in: conv_in,
        silu_embed: SILU::new(),
        lin_embed: lin_embed,
        norm_out: norm_out,
        silu_out: SILU::new(),
        conv_out: conv_out,
        skip_connection: skip_connection,
    };

    Ok(res_block)
}

pub fn load_multi_head_attention<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<MultiHeadAttention<B>, Box<dyn Error>> {
    let n_head = load_usize::<B>("n_head", path, device)?;
    let query = load_linear::<B>(&format!("{}/{}", path, "query"), device)?;
    let key = load_linear::<B>(&format!("{}/{}", path, "key"), device)?;
    let value = load_linear::<B>(&format!("{}/{}", path, "value"), device)?;
    let out = load_linear::<B>(&format!("{}/{}", path, "out"), device)?;

    let multi_head_attention = MultiHeadAttention {
        n_head: n_head,
        query: query,
        key: key,
        value: value,
        out: out,
    };

    Ok(multi_head_attention)
}

pub fn load_geglu<B: Backend>(path: &str, device: &B::Device) -> Result<GEGLU<B>, Box<dyn Error>> {
    let proj = load_linear::<B>(&format!("{}/{}", path, "proj"), device)?;

    let geglue = GEGLU {
        proj: proj,
        gelu: GELU::new(), // Assuming GELU::new() initializes a new GELU struct
    };

    Ok(geglue)
}

pub fn load_mlp<B: Backend>(path: &str, device: &B::Device) -> Result<MLP<B>, Box<dyn Error>> {
    let geglu = load_geglu::<B>(&format!("{}/{}", path, "geglu"), device)?;
    let lin = load_linear::<B>(&format!("{}/{}", path, "lin"), device)?;

    let mlp = MLP {
        geglu: geglu,
        lin: lin,
    };

    Ok(mlp)
}

pub fn load_transformer_block<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<TransformerBlock<B>, Box<dyn Error>> {
    let norm1 = load_layer_norm::<B>(&format!("{}/{}", path, "norm1"), device)?;
    let attn1 = load_multi_head_attention::<B>(&format!("{}/{}", path, "attn1"), device)?;
    let norm2 = load_layer_norm::<B>(&format!("{}/{}", path, "norm2"), device)?;
    let attn2 = load_multi_head_attention::<B>(&format!("{}/{}", path, "attn2"), device)?;
    let norm3 = load_layer_norm::<B>(&format!("{}/{}", path, "norm3"), device)?;
    let mlp = load_mlp::<B>(&format!("{}/{}", path, "mlp"), device)?;

    let transformer_block = TransformerBlock {
        norm1: norm1,
        attn1: attn1,
        norm2: norm2,
        attn2: attn2,
        norm3: norm3,
        mlp: mlp,
    };

    Ok(transformer_block)
}

pub fn load_spatial_transformer<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<SpatialTransformer<B>, Box<dyn Error>> {
    let norm = load_group_norm::<B>(&format!("{}/{}", path, "norm"), device)?;
    let proj_in = load_conv2d::<B>(&format!("{}/{}", path, "proj_in"), device)?;
    let transformer = load_transformer_block::<B>(&format!("{}/{}", path, "transformer"), device)?;
    let proj_out = load_conv2d::<B>(&format!("{}/{}", path, "proj_out"), device)?;

    let spatial_transformer = SpatialTransformer {
        norm: norm,
        proj_in: proj_in,
        transformer: transformer,
        proj_out: proj_out,
    };

    Ok(spatial_transformer)
}

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

    let upsample = Upsample { conv: conv };

    Ok(upsample)
}

pub fn load_downsample<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<Downsample<B>, Box<dyn Error>> {
    load_conv2d(path, device)
}

pub fn load_res_transformer_res<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResTransformerRes<B>, Box<dyn Error>> {
    let res1 = load_res_block::<B>(&format!("{}/{}", path, "res1"), device)?; // Assuming load_res_block function
    let transformer =
        load_spatial_transformer::<B>(&format!("{}/{}", path, "transformer"), device)?;
    let res2 = load_res_block::<B>(&format!("{}/{}", path, "res2"), device)?;

    let res_transformer_res = ResTransformerRes {
        res1: res1,
        transformer: transformer,
        res2: res2,
    };

    Ok(res_transformer_res)
}

pub fn load_res_transformer_upsample<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResTransformerUpsample<B>, Box<dyn Error>> {
    let res = load_res_block::<B>(&format!("{}/{}", path, "res"), device)?;
    let transformer =
        load_spatial_transformer::<B>(&format!("{}/{}", path, "transformer"), device)?;
    let upsample = load_upsample::<B>(&format!("{}/{}", path, "upsample"), device)?;

    let res_transformer_upsample = ResTransformerUpsample {
        res: res,
        transformer: transformer,
        upsample: upsample,
    };

    Ok(res_transformer_upsample)
}

pub fn load_res_upsample<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResUpSample<B>, Box<dyn Error>> {
    let res = load_res_block::<B>(&format!("{}/{}", path, "res"), device)?;
    let upsample = load_upsample::<B>(&format!("{}/{}", path, "upsample"), device)?;

    let res_upsample = ResUpSample {
        res: res,
        upsample: upsample,
    };

    Ok(res_upsample)
}

pub fn load_res_transformer<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<ResTransformer<B>, Box<dyn Error>> {
    let res = load_res_block::<B>(&format!("{}/{}", path, "res"), device)?;
    let transformer =
        load_spatial_transformer::<B>(&format!("{}/{}", path, "transformer"), device)?;

    let res_transformer = ResTransformer {
        res: res,
        transformer: transformer,
    };

    Ok(res_transformer)
}

pub fn load_unet_input_blocks<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<UNetInputBlocks<B>, Box<dyn Error>> {
    let conv = load_conv2d::<B>(&format!("{}/{}", path, "conv"), device)?;
    let rt1 = load_res_transformer::<B>(&format!("{}/{}", path, "rt1"), device)?;
    let rt2 = load_res_transformer::<B>(&format!("{}/{}", path, "rt2"), device)?;
    let d1 = load_downsample::<B>(&format!("{}/{}", path, "d1"), device)?;
    let rt3 = load_res_transformer::<B>(&format!("{}/{}", path, "rt3"), device)?;
    let rt4 = load_res_transformer::<B>(&format!("{}/{}", path, "rt4"), device)?;
    let d2 = load_downsample::<B>(&format!("{}/{}", path, "d2"), device)?;
    let rt5 = load_res_transformer::<B>(&format!("{}/{}", path, "rt5"), device)?;
    let rt6 = load_res_transformer::<B>(&format!("{}/{}", path, "rt6"), device)?;
    let d3 = load_downsample::<B>(&format!("{}/{}", path, "d3"), device)?;
    let r1 = load_res_block::<B>(&format!("{}/{}", path, "r1"), device)?;
    let r2 = load_res_block::<B>(&format!("{}/{}", path, "r2"), device)?;

    let unet_input_blocks = UNetInputBlocks {
        conv: conv,
        rt1: rt1,
        rt2: rt2,
        d1: d1,
        rt3: rt3,
        rt4: rt4,
        d2: d2,
        rt5: rt5,
        rt6: rt6,
        d3: d3,
        r1: r1,
        r2: r2,
    };

    Ok(unet_input_blocks)
}

pub fn load_unet_output_blocks<B: Backend>(
    path: &str,
    device: &B::Device,
) -> Result<UNetOutputBlocks<B>, Box<dyn Error>> {
    let r1 = load_res_block::<B>(&format!("{}/{}", path, "r1"), device)?;
    let r2 = load_res_block::<B>(&format!("{}/{}", path, "r2"), device)?;
    let ru = load_res_upsample::<B>(&format!("{}/{}", path, "ru"), device)?;
    let rt1 = load_res_transformer::<B>(&format!("{}/{}", path, "rt1"), device)?;
    let rt2 = load_res_transformer::<B>(&format!("{}/{}", path, "rt2"), device)?;
    let rtu1 = load_res_transformer_upsample::<B>(&format!("{}/{}", path, "rtu1"), device)?;
    let rt3 = load_res_transformer::<B>(&format!("{}/{}", path, "rt3"), device)?;
    let rt4 = load_res_transformer::<B>(&format!("{}/{}", path, "rt4"), device)?;
    let rtu2 = load_res_transformer_upsample::<B>(&format!("{}/{}", path, "rtu2"), device)?;
    let rt5 = load_res_transformer::<B>(&format!("{}/{}", path, "rt5"), device)?;
    let rt6 = load_res_transformer::<B>(&format!("{}/{}", path, "rt6"), device)?;
    let rt7 = load_res_transformer::<B>(&format!("{}/{}", path, "rt7"), device)?;

    Ok(UNetOutputBlocks {
        r1,
        r2,
        ru,
        rt1,
        rt2,
        rtu1,
        rt3,
        rt4,
        rtu2,
        rt5,
        rt6,
        rt7,
    })
}

pub fn load_unet<B: Backend>(path: &str, device: &B::Device) -> Result<UNet<B>, Box<dyn Error>> {
    let lin1_time_embed = load_linear::<B>(&format!("{}/{}", path, "lin1_time_embed"), device)?;
    let silu_time_embed = SILU::new(); // Assuming SILU::new() initializes a new SILU struct
    let lin2_time_embed = load_linear::<B>(&format!("{}/{}", path, "lin2_time_embed"), device)?;
    let input_blocks =
        load_unet_input_blocks::<B>(&format!("{}/{}", path, "input_blocks"), device)?;
    let middle_block =
        load_res_transformer_res::<B>(&format!("{}/{}", path, "middle_block"), device)?;
    let output_blocks =
        load_unet_output_blocks::<B>(&format!("{}/{}", path, "output_blocks"), device)?;
    let norm_out = load_group_norm::<B>(&format!("{}/{}", path, "norm_out"), device)?;
    let silu_out = SILU::new(); // Assuming SILU::new() initializes a new SILU struct
    let conv_out = load_conv2d::<B>(&format!("{}/{}", path, "conv_out"), device)?;

    Ok(UNet {
        lin1_time_embed,
        silu_time_embed,
        lin2_time_embed,
        input_blocks,
        middle_block,
        output_blocks,
        norm_out,
        silu_out,
        conv_out,
    })
}