bellande_artificial_intelligence_framework 0.1.0

A comprehensive artificial intelligence and computer vision training framework developed by Bellande AI Research. Provides tools and utilities for machine learning model development, training, and deployment.
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
// Copyright (C) 2024 Bellande Artificial Intelligence Computer Vision Research Innovation Center, Ronaldson Bellande

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::core::{error::BellandeError, tensor::Tensor};
use crate::models::sequential::NeuralLayer;

pub struct Conv2d {
    in_channels: usize,
    out_channels: usize,
    kernel_size: (usize, usize),
    stride: (usize, usize),
    padding: (usize, usize),
    pub(crate) weight: Tensor,
    pub(crate) bias: Option<Tensor>,
    pub(crate) input: Option<Tensor>, // Changed from input_cache to input
    pub(crate) weight_grad: Option<Tensor>,
    pub(crate) bias_grad: Option<Tensor>,
    pub(crate) training: bool,
}

impl Conv2d {
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        kernel_size: (usize, usize),
        stride: Option<(usize, usize)>,
        padding: Option<(usize, usize)>,
        bias: bool,
    ) -> Self {
        let stride = stride.unwrap_or(kernel_size);
        let padding = padding.unwrap_or((0, 0));
        let weight = Tensor::randn(&[out_channels, in_channels, kernel_size.0, kernel_size.1]);
        let bias = if bias {
            Some(Tensor::zeros(&[out_channels]))
        } else {
            None
        };

        Conv2d {
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
            weight,
            bias,
            input: None,
            weight_grad: None,
            bias_grad: None,
            training: true,
        }
    }

    fn forward_impl(&mut self, input: &Tensor) -> Result<Tensor, BellandeError> {
        if input.shape.len() != 4 {
            return Err(BellandeError::InvalidShape(
                "Expected 4D tensor (batch_size, channels, height, width)".into(),
            ));
        }

        let (batch_size, channels, height, width) = (
            input.shape[0],
            input.shape[1],
            input.shape[2],
            input.shape[3],
        );

        if channels != self.in_channels {
            return Err(BellandeError::DimensionMismatch);
        }

        // Safe output dimension calculation
        let output_height = ((height as i64 + 2 * self.padding.0 as i64
            - self.kernel_size.0 as i64)
            / self.stride.0 as i64
            + 1) as usize;
        let output_width = ((width as i64 + 2 * self.padding.1 as i64 - self.kernel_size.1 as i64)
            / self.stride.1 as i64
            + 1) as usize;

        // Validate output dimensions
        if output_height == 0 || output_width == 0 {
            return Err(BellandeError::InvalidShape(
                "Convolution resulted in zero output dimensions".into(),
            ));
        }

        let mut output = vec![0.0; batch_size * self.out_channels * output_height * output_width];

        // Implement convolution operation with bounds checking
        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for out_h in 0..output_height {
                    for out_w in 0..output_width {
                        let mut sum = 0.0;

                        for in_c in 0..self.in_channels {
                            for k_h in 0..self.kernel_size.0 {
                                for k_w in 0..self.kernel_size.1 {
                                    // Safe input position calculation with padding
                                    let in_h = out_h
                                        .checked_mul(self.stride.0)
                                        .and_then(|h| h.checked_add(k_h))
                                        .and_then(|h| h.checked_sub(self.padding.0));

                                    let in_w = out_w
                                        .checked_mul(self.stride.1)
                                        .and_then(|w| w.checked_add(k_w))
                                        .and_then(|w| w.checked_sub(self.padding.1));

                                    // Check if the input position is valid
                                    if let (Some(h), Some(w)) = (in_h, in_w) {
                                        if h < height && w < width {
                                            let input_idx =
                                                ((b * channels + in_c) * height + h) * width + w;
                                            let weight_idx = ((out_c * self.in_channels + in_c)
                                                * self.kernel_size.0
                                                + k_h)
                                                * self.kernel_size.1
                                                + k_w;

                                            if input_idx < input.data.len()
                                                && weight_idx < self.weight.data.len()
                                            {
                                                sum += input.data[input_idx]
                                                    * self.weight.data[weight_idx];
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if let Some(ref bias) = self.bias {
                            if out_c < bias.data.len() {
                                sum += bias.data[out_c];
                            }
                        }

                        let output_idx = ((b * self.out_channels + out_c) * output_height + out_h)
                            * output_width
                            + out_w;
                        if output_idx < output.len() {
                            output[output_idx] = sum;
                        }
                    }
                }
            }
        }

        Ok(Tensor::new(
            output,
            vec![batch_size, self.out_channels, output_height, output_width],
            input.requires_grad,
            input.device.clone(),
            input.dtype,
        ))
    }

    pub fn backward_input(
        &self,
        grad_output: &Tensor,
        input: &Tensor,
    ) -> Result<Tensor, BellandeError> {
        let (batch_size, _, height, width) = (
            input.shape[0],
            input.shape[1],
            input.shape[2],
            input.shape[3],
        );

        let mut grad_input = vec![0.0; input.data.len()];
        let (_, _, output_height, output_width) = (
            grad_output.shape[0],
            grad_output.shape[1],
            grad_output.shape[2],
            grad_output.shape[3],
        );

        // Compute input gradients
        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for out_h in 0..output_height {
                    for out_w in 0..output_width {
                        let out_idx = ((b * self.out_channels + out_c) * output_height + out_h)
                            * output_width
                            + out_w;
                        let grad = grad_output.data[out_idx];

                        for in_c in 0..self.in_channels {
                            for k_h in 0..self.kernel_size.0 {
                                for k_w in 0..self.kernel_size.1 {
                                    let in_h = out_h * self.stride.0 + k_h - self.padding.0;
                                    let in_w = out_w * self.stride.1 + k_w - self.padding.1;

                                    if in_h < height && in_w < width {
                                        let input_idx =
                                            ((b * self.in_channels + in_c) * height + in_h) * width
                                                + in_w;
                                        let weight_idx = ((out_c * self.in_channels + in_c)
                                            * self.kernel_size.0
                                            + k_h)
                                            * self.kernel_size.1
                                            + k_w;
                                        grad_input[input_idx] +=
                                            grad * self.weight.data[weight_idx];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(Tensor::new(
            grad_input,
            input.shape.clone(),
            true,
            input.device.clone(),
            input.dtype,
        ))
    }

    pub fn backward_weight(
        &self,
        grad_output: &Tensor,
        input: &Tensor,
    ) -> Result<Tensor, BellandeError> {
        let mut grad_weight = vec![0.0; self.weight.data.len()];
        let (batch_size, _, output_height, output_width) = (
            grad_output.shape[0],
            grad_output.shape[1],
            grad_output.shape[2],
            grad_output.shape[3],
        );

        let (_, _, height, width) = (
            input.shape[0],
            input.shape[1],
            input.shape[2],
            input.shape[3],
        );

        // Compute weight gradients
        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for out_h in 0..output_height {
                    for out_w in 0..output_width {
                        let out_idx = ((b * self.out_channels + out_c) * output_height + out_h)
                            * output_width
                            + out_w;
                        let grad = grad_output.data[out_idx];

                        for in_c in 0..self.in_channels {
                            for k_h in 0..self.kernel_size.0 {
                                for k_w in 0..self.kernel_size.1 {
                                    let in_h = out_h * self.stride.0 + k_h - self.padding.0;
                                    let in_w = out_w * self.stride.1 + k_w - self.padding.1;

                                    if in_h < height && in_w < width {
                                        let input_idx =
                                            ((b * self.in_channels + in_c) * height + in_h) * width
                                                + in_w;
                                        let weight_idx = ((out_c * self.in_channels + in_c)
                                            * self.kernel_size.0
                                            + k_h)
                                            * self.kernel_size.1
                                            + k_w;
                                        grad_weight[weight_idx] += grad * input.data[input_idx];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(Tensor::new(
            grad_weight,
            self.weight.shape.clone(),
            true,
            self.weight.device.clone(),
            self.weight.dtype,
        ))
    }

    fn backward_bias(&self, grad_output: &Tensor) -> Result<Tensor, BellandeError> {
        if self.bias.is_none() {
            return Err(BellandeError::InvalidParameter("No bias present".into()));
        }

        let mut grad_bias = vec![0.0; self.out_channels];
        let (batch_size, _, output_height, output_width) = (
            grad_output.shape[0],
            grad_output.shape[1],
            grad_output.shape[2],
            grad_output.shape[3],
        );

        // Compute bias gradients
        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for out_h in 0..output_height {
                    for out_w in 0..output_width {
                        let out_idx = ((b * self.out_channels + out_c) * output_height + out_h)
                            * output_width
                            + out_w;
                        grad_bias[out_c] += grad_output.data[out_idx];
                    }
                }
            }
        }

        Ok(Tensor::new(
            grad_bias,
            vec![self.out_channels],
            true,
            self.weight.device.clone(),
            self.weight.dtype,
        ))
    }
}

impl NeuralLayer for Conv2d {
    fn forward(&mut self, input: &Tensor) -> Result<Tensor, BellandeError> {
        let output = self.forward_impl(input)?;
        self.input = Some(input.clone());
        Ok(output)
    }

    fn backward(&mut self, grad_output: &Tensor) -> Result<Tensor, BellandeError> {
        let input = self.input.as_ref().ok_or(BellandeError::InvalidBackward(
            "Forward pass not called before backward".into(),
        ))?;

        let grad_input = self.backward_input(grad_output, input)?;
        let grad_weight = self.backward_weight(grad_output, input)?;
        let grad_bias = if self.bias.is_some() {
            Some(self.backward_bias(grad_output)?)
        } else {
            None
        };

        // Store gradients
        self.weight_grad = Some(grad_weight);
        self.bias_grad = grad_bias;

        Ok(grad_input)
    }

    fn parameters(&self) -> Vec<Tensor> {
        let mut params = vec![self.weight.clone()];
        if let Some(ref bias) = self.bias {
            params.push(bias.clone());
        }
        params
    }

    fn named_parameters(&self) -> Vec<(String, Tensor)> {
        let mut params = vec![("weight".to_string(), self.weight.clone())];
        if let Some(ref bias) = self.bias {
            params.push(("bias".to_string(), bias.clone()));
        }
        params
    }

    fn set_parameter(&mut self, name: &str, value: Tensor) -> Result<(), BellandeError> {
        match name {
            "weight" => {
                if value.shape == self.weight.shape {
                    self.weight = value;
                    Ok(())
                } else {
                    Err(BellandeError::ShapeMismatch(
                        "Weight shape mismatch".to_string(),
                    ))
                }
            }
            "bias" => {
                if let Some(ref bias) = self.bias {
                    if value.shape == bias.shape {
                        self.bias = Some(value);
                        Ok(())
                    } else {
                        Err(BellandeError::ShapeMismatch(
                            "Bias shape mismatch".to_string(),
                        ))
                    }
                } else {
                    Err(BellandeError::InvalidParameter(
                        "Layer does not use bias".to_string(),
                    ))
                }
            }
            _ => Err(BellandeError::InvalidParameter(format!(
                "Unknown parameter name: {}",
                name
            ))),
        }
    }

    fn train(&mut self) {
        self.training = true;
    }

    fn eval(&mut self) {
        self.training = false;
    }
}

// Implement Send and Sync for thread safety
unsafe impl Send for Conv2d {}
unsafe impl Sync for Conv2d {}