torsh-vision 0.1.2

Computer vision utilities for ToRSh deep learning 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
use std::collections::HashMap;
use torsh_core::{device::DeviceType, error::Result};
use torsh_nn::prelude::*;
use torsh_nn::{Module, Parameter};
use torsh_tensor::{creation, Tensor};

use crate::{ModelConfig, VisionModel};

/// Multi-Head Self-Attention
pub struct MultiHeadAttention {
    num_heads: usize,
    head_dim: usize,
    scale: f32,
    qkv: Linear,
    proj: Linear,
    dropout: Dropout,
}

impl MultiHeadAttention {
    pub fn new(embed_dim: usize, num_heads: usize, dropout: f32) -> Self {
        let head_dim = embed_dim / num_heads;
        let scale = 1.0 / (head_dim as f32).sqrt();

        Self {
            num_heads,
            head_dim,
            scale,
            qkv: Linear::new(embed_dim, embed_dim * 3, true),
            proj: Linear::new(embed_dim, embed_dim, true),
            dropout: Dropout::new(dropout),
        }
    }
}

impl Module for MultiHeadAttention {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let batch_size = input.shape().dims()[0];
        let seq_len = input.shape().dims()[1];
        let _embed_dim = input.shape().dims()[2];

        // Generate Q, K, V
        let qkv = self.qkv.forward(input)?;
        let qkv = qkv.view(&[
            batch_size as i32,
            seq_len as i32,
            3,
            self.num_heads as i32,
            self.head_dim as i32,
        ])?;
        let _qkv = qkv.permute(&[2, 0, 3, 1, 4])?; // [3, batch, num_heads, seq_len, head_dim]

        // Split into Q, K, V - simplified implementation
        // In a real implementation, we'd properly split the tensor
        let _q = input.clone(); // Placeholder
        let _k = input.clone(); // Placeholder
        let v = input.clone(); // Placeholder

        // Simplified attention - just pass through for now
        // In a full implementation, we'd compute: softmax(QK^T/sqrt(d_k))V
        let out = self.proj.forward(&v)?;
        self.dropout.forward(&out)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();
        params.extend(self.qkv.parameters());
        params.extend(self.proj.parameters());
        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn training(&self) -> bool {
        self.qkv.training()
    }

    fn train(&mut self) {
        self.qkv.train();
        self.proj.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.qkv.eval();
        self.proj.eval();
        self.dropout.eval();
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.qkv.to_device(device)?;
        self.proj.to_device(device)?;
        self.dropout.to_device(device)?;
        Ok(())
    }

    fn set_training(&mut self, training: bool) {
        if training {
            self.train();
        } else {
            self.eval();
        }
    }
}

/// Transformer Encoder Block
pub struct TransformerBlock {
    norm1: LayerNorm,
    attn: MultiHeadAttention,
    norm2: LayerNorm,
    mlp: Sequential,
    dropout: Dropout,
}

impl TransformerBlock {
    pub fn new(embed_dim: usize, num_heads: usize, mlp_ratio: usize, dropout: f32) -> Result<Self> {
        let mlp_hidden_dim = embed_dim * mlp_ratio;

        let mlp = Sequential::new()
            .add(Linear::new(embed_dim, mlp_hidden_dim, true))
            .add(GELU::new(false))
            .add(Linear::new(mlp_hidden_dim, embed_dim, true))
            .add(Dropout::new(dropout));

        Ok(Self {
            norm1: LayerNorm::new(vec![embed_dim], 1e-5, true, torsh_core::DeviceType::Cpu)?,
            attn: MultiHeadAttention::new(embed_dim, num_heads, dropout),
            norm2: LayerNorm::new(vec![embed_dim], 1e-5, true, torsh_core::DeviceType::Cpu)?,
            mlp,
            dropout: Dropout::new(dropout),
        })
    }
}

impl Module for TransformerBlock {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Self-attention with residual connection
        let normed = self.norm1.forward(input)?;
        let attn_out = self.attn.forward(&normed)?;
        let x = input.add(&attn_out)?;

        // MLP with residual connection
        let normed = self.norm2.forward(&x)?;
        let mlp_out = self.mlp.forward(&normed)?;
        x.add(&mlp_out)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();
        params.extend(self.norm1.parameters());
        params.extend(self.attn.parameters());
        params.extend(self.norm2.parameters());
        params.extend(self.mlp.parameters());
        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn training(&self) -> bool {
        self.attn.training()
    }

    fn train(&mut self) {
        self.norm1.train();
        self.attn.train();
        self.norm2.train();
        self.mlp.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.norm1.eval();
        self.attn.eval();
        self.norm2.eval();
        self.mlp.eval();
        self.dropout.eval();
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.norm1.to_device(device)?;
        self.attn.to_device(device)?;
        self.norm2.to_device(device)?;
        self.mlp.to_device(device)?;
        self.dropout.to_device(device)?;
        Ok(())
    }

    fn set_training(&mut self, training: bool) {
        if training {
            self.train();
        } else {
            self.eval();
        }
    }
}

/// Vision Transformer (ViT) Configuration
#[derive(Debug, Clone)]
pub struct ViTConfig {
    pub image_size: usize,
    pub patch_size: usize,
    pub embed_dim: usize,
    pub num_layers: usize,
    pub num_heads: usize,
    pub mlp_ratio: usize,
    pub num_classes: usize,
    pub dropout: f32,
}

impl ViTConfig {
    pub fn vit_tiny_patch16_224() -> Self {
        Self {
            image_size: 224,
            patch_size: 16,
            embed_dim: 192,
            num_layers: 12,
            num_heads: 3,
            mlp_ratio: 4,
            num_classes: 1000,
            dropout: 0.0,
        }
    }

    pub fn vit_small_patch16_224() -> Self {
        Self {
            image_size: 224,
            patch_size: 16,
            embed_dim: 384,
            num_layers: 12,
            num_heads: 6,
            mlp_ratio: 4,
            num_classes: 1000,
            dropout: 0.0,
        }
    }

    pub fn vit_base_patch16_224() -> Self {
        Self {
            image_size: 224,
            patch_size: 16,
            embed_dim: 768,
            num_layers: 12,
            num_heads: 12,
            mlp_ratio: 4,
            num_classes: 1000,
            dropout: 0.1,
        }
    }

    pub fn vit_large_patch16_224() -> Self {
        Self {
            image_size: 224,
            patch_size: 16,
            embed_dim: 1024,
            num_layers: 24,
            num_heads: 16,
            mlp_ratio: 4,
            num_classes: 1000,
            dropout: 0.1,
        }
    }

    pub fn vit_huge_patch14_224() -> Self {
        Self {
            image_size: 224,
            patch_size: 14,
            embed_dim: 1280,
            num_layers: 32,
            num_heads: 16,
            mlp_ratio: 4,
            num_classes: 1000,
            dropout: 0.1,
        }
    }
}

/// Vision Transformer (ViT) model
pub struct VisionTransformer {
    patch_embed: Conv2d,
    cls_token: Parameter,
    pos_embed: Parameter,
    pos_dropout: Dropout,
    blocks: Vec<TransformerBlock>,
    norm: LayerNorm,
    head: Linear,
    config: ViTConfig,
    is_training: bool,
}

impl VisionTransformer {
    pub fn new(vit_config: ViTConfig) -> Result<Self> {
        let num_patches = (vit_config.image_size / vit_config.patch_size).pow(2);
        let seq_len = num_patches + 1; // +1 for class token

        // Patch embedding: convolution that extracts patches
        let patch_embed = Conv2d::new(
            3,
            vit_config.embed_dim,
            (vit_config.patch_size, vit_config.patch_size),
            (vit_config.patch_size, vit_config.patch_size),
            (0, 0),
            (1, 1),
            false,
            1,
        );

        // Learnable parameters (simplified - would need proper initialization)
        let cls_token = Parameter::new(
            torsh_tensor::creation::zeros(&[1, 1, vit_config.embed_dim])
                .expect("tensor creation should succeed"),
        );
        let pos_embed = Parameter::new(
            torsh_tensor::creation::zeros(&[1, seq_len, vit_config.embed_dim])
                .expect("tensor creation should succeed"),
        );

        // Transformer blocks
        let mut blocks = Vec::new();
        for _ in 0..vit_config.num_layers {
            blocks.push(TransformerBlock::new(
                vit_config.embed_dim,
                vit_config.num_heads,
                vit_config.mlp_ratio,
                vit_config.dropout,
            )?);
        }

        Ok(Self {
            patch_embed,
            cls_token,
            pos_embed,
            pos_dropout: Dropout::new(vit_config.dropout),
            blocks,
            norm: LayerNorm::new(
                vec![vit_config.embed_dim],
                1e-5,
                true,
                torsh_core::DeviceType::Cpu,
            )?,
            head: Linear::new(vit_config.embed_dim, vit_config.num_classes, true),
            config: vit_config,
            is_training: true,
        })
    }

    pub fn from_config(model_config: ModelConfig, vit_config: ViTConfig) -> Result<Self> {
        let mut config = vit_config;
        config.num_classes = model_config.num_classes;
        config.dropout = model_config.dropout;
        Self::new(config)
    }

    pub fn vit_tiny_patch16_224(config: ModelConfig) -> Result<Self> {
        Self::from_config(config, ViTConfig::vit_tiny_patch16_224())
    }

    pub fn vit_small_patch16_224(config: ModelConfig) -> Result<Self> {
        Self::from_config(config, ViTConfig::vit_small_patch16_224())
    }

    pub fn vit_base_patch16_224(config: ModelConfig) -> Result<Self> {
        Self::from_config(config, ViTConfig::vit_base_patch16_224())
    }

    pub fn vit_large_patch16_224(config: ModelConfig) -> Result<Self> {
        Self::from_config(config, ViTConfig::vit_large_patch16_224())
    }

    pub fn vit_huge_patch14_224(config: ModelConfig) -> Result<Self> {
        Self::from_config(config, ViTConfig::vit_huge_patch14_224())
    }
}

impl Module for VisionTransformer {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let batch_size = input.shape().dims()[0];

        // Patch embedding: [B, C, H, W] -> [B, embed_dim, H/P, W/P]
        let mut x = self.patch_embed.forward(input)?;

        // Flatten patches: [B, embed_dim, H/P, W/P] -> [B, num_patches, embed_dim]
        let num_patches = x.shape().dims()[2] * x.shape().dims()[3];
        x = x.view(&[
            batch_size as i32,
            self.config.embed_dim as i32,
            num_patches as i32,
        ])?;
        x = x.transpose(1, 2)?; // [B, num_patches, embed_dim]

        // Add class token
        let cls_tokens =
            self.cls_token
                .tensor()
                .read()
                .expand(&[batch_size, 1, self.config.embed_dim])?;
        x = Tensor::cat(&[&cls_tokens, &x], 1)?; // [B, num_patches+1, embed_dim]

        // Add positional embeddings
        x = x.add(&*self.pos_embed.tensor().read())?;
        x = self.pos_dropout.forward(&x)?;

        // Apply transformer blocks
        for block in &self.blocks {
            x = block.forward(&x)?;
        }

        // Layer norm and classification head
        x = self.norm.forward(&x)?;

        // Take class token (first token) for classification
        let cls_token = x.slice(1, 0, 1)?; // [B, 1, embed_dim]
        let cls_token = cls_token
            .to_tensor()?
            .view(&[batch_size as i32, self.config.embed_dim as i32])?; // [B, embed_dim]

        self.head.forward(&cls_token)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();
        params.extend(self.patch_embed.parameters());
        params.insert("cls_token".to_string(), self.cls_token.clone());
        params.insert("pos_embed".to_string(), self.pos_embed.clone());

        for (i, block) in self.blocks.iter().enumerate() {
            let block_params = block.parameters();
            for (key, param) in block_params {
                params.insert(format!("block{}.{}", i, key), param);
            }
        }

        params.extend(self.norm.parameters());
        params.extend(self.head.parameters());
        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn train(&mut self) {
        self.is_training = true;
        self.patch_embed.train();
        self.pos_dropout.train();
        for block in &mut self.blocks {
            block.train();
        }
        self.norm.train();
        self.head.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.patch_embed.eval();
        self.pos_dropout.eval();
        for block in &mut self.blocks {
            block.eval();
        }
        self.norm.eval();
        self.head.eval();
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.patch_embed.to_device(device)?;
        self.pos_dropout.to_device(device)?;
        for block in &mut self.blocks {
            block.to_device(device)?;
        }
        self.norm.to_device(device)?;
        self.head.to_device(device)?;
        Ok(())
    }

    fn set_training(&mut self, training: bool) {
        if training {
            self.train();
        } else {
            self.eval();
        }
    }
}

impl VisionModel for VisionTransformer {
    fn num_classes(&self) -> usize {
        self.config.num_classes
    }

    fn input_size(&self) -> (usize, usize) {
        (self.config.image_size, self.config.image_size)
    }

    fn name(&self) -> &str {
        "VisionTransformer"
    }
}