maidenx_tensor 0.1.5

maidenx tensor
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
use crate::{
    utils::indexing::{add_at_index, set_index},
    Tensor, TensorNode,
};
use maidenx_core::{
    error::{Error, Result},
    scalar::Scalar,
};

impl Tensor {
    pub fn index(&self, indices: &Tensor) -> Result<Self> {
        self.index_select(0, indices)
    }

    pub fn index_add_(&mut self, dim: impl Into<Scalar>, indices: &Tensor, src: &Tensor) -> Result<()> {
        let dim_i32 = dim.into().as_i32();
        let dim_usize: usize = if dim_i32 < 0 {
            (self.ndim() as i32 + dim_i32) as usize
        } else {
            dim_i32 as usize
        };

        if dim_usize >= self.ndim() {
            return Err(Error::DimensionOutOfBounds {
                dim: dim_i32,
                ndim: self.ndim(),
            });
        }

        if !indices.dtype().is_int() {
            return Err(Error::InvalidArgument(format!(
                "Expected indices tensor with integer dtype, got {}",
                indices.dtype()
            )));
        }

        if src.ndim() != self.ndim() {
            return Err(Error::InvalidArgument(format!(
                "Source tensor must have same number of dimensions as self, got {} and {}",
                src.ndim(),
                self.ndim()
            )));
        }

        for d in 0..self.ndim() {
            if d != dim_usize && self.shape()[d] != src.shape()[d] {
                return Err(Error::InvalidArgument(format!(
                    "Source tensor dimension {} must match self dimension {}, got {} and {}",
                    d,
                    d,
                    src.shape()[d],
                    self.shape()[d]
                )));
            }
        }

        if src.shape()[dim_usize] != indices.size() {
            return Err(Error::InvalidArgument(format!(
                "Source tensor dimension {} should match indices size, got {} and {}",
                dim_usize,
                src.shape()[dim_usize],
                indices.size()
            )));
        }

        for (i, idx_pos) in (0..indices.size()).enumerate() {
            let idx = indices.get(&[idx_pos])?.as_i32();

            if idx < 0 || idx >= self.shape()[dim_usize] as i32 {
                return Err(Error::IndexOutOfBounds {
                    index: idx as usize,
                    size: self.shape()[dim_usize],
                });
            }

            let src_slice = src.slice(dim_usize, i, Some(i + 1), 1)?;

            let target_slice = self.slice(dim_usize, idx as usize, Some((idx as usize) + 1), 1)?;

            for idx_tuple in src_slice.index_iter()? {
                let value = src_slice.get(&idx_tuple)?;

                let curr_value = target_slice.get(&idx_tuple)?;

                let mut target_slice = self.slice(dim_usize, idx as usize, Some((idx as usize) + 1), 1)?;
                target_slice.set(&idx_tuple, curr_value + value)?;
            }
        }

        Ok(())
    }

    pub fn index_select(&self, dim: impl Into<Scalar>, indices: &Tensor) -> Result<Self> {
        let dim_i32 = dim.into().as_i32();
        let dim_usize: usize = if dim_i32 < 0 {
            (self.ndim() as i32 + dim_i32) as usize
        } else {
            dim_i32 as usize
        };

        if dim_usize >= self.ndim() {
            return Err(Error::DimensionOutOfBounds {
                dim: dim_i32,
                ndim: self.ndim(),
            });
        }

        if !indices.dtype().is_int() {
            return Err(Error::InvalidArgument(format!(
                "Expected indices tensor with integer dtype, got {}",
                indices.dtype()
            )));
        }

        let mut output_shape = self.shape().to_vec();
        output_shape[dim_usize] = indices.size();

        let mut result = Self::zeros_with_spec(&output_shape, self.device(), self.dtype())?;

        for (i, idx_pos) in (0..indices.size()).enumerate() {
            let idx = indices.get(&[idx_pos])?.as_i32();

            if idx < 0 || idx >= self.shape()[dim_usize] as i32 {
                return Err(Error::IndexOutOfBounds {
                    index: idx as usize,
                    size: self.shape()[dim_usize],
                });
            }

            let src_slice = self.slice(dim_usize, idx as usize, Some((idx as usize) + 1), 1)?;

            let mut dst_slice = result.slice(dim_usize, i, Some(i + 1), 1)?;

            for idx_tuple in src_slice.index_iter()? {
                let value = src_slice.get(&idx_tuple)?;
                dst_slice.set(&idx_tuple, value)?;
            }
        }

        if self.requires_grad() {
            result.with_grad()?;

            let orig_shape = self.shape().to_vec();
            let orig_dim = dim_usize;
            let indices_clone = indices.clone();

            let backward_fn = Box::new(move |_inputs: &[Tensor], grad_out: &Tensor| -> Result<Vec<Tensor>> {
                let mut grad_self = Tensor::zeros_with_spec(&orig_shape, grad_out.device(), grad_out.dtype())?;

                for (i, idx_pos) in (0..indices_clone.size()).enumerate() {
                    let idx = indices_clone.get(&[idx_pos])?.as_i32() as usize;

                    let grad_out_slice = grad_out.slice(orig_dim, i, Some(i + 1), 1)?;

                    for pos in grad_out_slice.index_iter()? {
                        let mut self_pos = pos.clone();
                        self_pos[orig_dim] = idx;

                        let value = grad_out_slice.get(&pos)?;
                        add_at_index(&mut grad_self, &self_pos, value)?;
                    }
                }

                let grad_indices = Tensor::zeros_like(&indices_clone)?;

                Ok(vec![grad_self, grad_indices])
            });

            let node = TensorNode::new("index_select".to_string(), vec![self.clone(), indices.clone()], Some(backward_fn));

            result.node = Some(node);
        }

        Ok(result)
    }

    #[allow(clippy::needless_range_loop)]
    pub fn index_put_(&mut self, indices: &[usize], src: &Tensor) -> Result<()> {
        // Validate that the indices specify a valid position in the selfination tensor
        if indices.len() > self.ndim() {
            return Err(Error::InvalidArgument(format!(
                "Indices length ({}) exceeds selfination tensor dimensions ({})",
                indices.len(),
                self.ndim()
            )));
        }

        // Check if we are indexing a subset of dimensions
        let remaining_dims = self.ndim() - indices.len();

        // If we're not indexing all dimensions, the source tensor should match
        // the remaining dimensions of the selfination
        if remaining_dims > 0 {
            // Get the shape of the remaining dimensions in the selfination tensor
            let mut self_remaining_shape = Vec::with_capacity(remaining_dims);
            for i in indices.len()..self.ndim() {
                self_remaining_shape.push(self.dim_size(i).unwrap_or(0));
            }

            // Check if source tensor shape matches the remaining dimensions
            if src.ndim() != remaining_dims {
                return Err(Error::InvalidArgument(format!(
                    "Source tensor dimensions ({}) don't match selfination's remaining dimensions ({})",
                    src.ndim(),
                    remaining_dims
                )));
            }

            for i in 0..remaining_dims {
                if src.dim_size(i).unwrap_or(0) != self_remaining_shape[i] {
                    return Err(Error::InvalidArgument(format!(
                        "Dimension mismatch at index {}: source has size {}, selfination expects {}",
                        i,
                        src.dim_size(i).unwrap_or(0),
                        self_remaining_shape[i]
                    )));
                }
            }

            // Calculate the base offset in the selfination tensor
            let mut base_offset = 0;
            for (dim, &idx) in indices.iter().enumerate() {
                let dim_size = self.dim_size(dim).unwrap_or(0);
                if idx >= dim_size {
                    return Err(Error::IndexOutOfBounds { index: idx, size: dim_size });
                }
                base_offset += idx * self.strides()[dim];
            }

            fn copy_elements(
                src: &Tensor,
                dest: &mut Tensor,
                base_offset: usize,
                base_dims: usize,
                curr_indices: &mut Vec<usize>,
                curr_dim: usize,
            ) -> Result<()> {
                if curr_dim == src.ndim() {
                    let mut dest_flat_idx = base_offset;
                    for dim in 0..curr_indices.len() {
                        dest_flat_idx += curr_indices[dim] * dest.strides()[base_dims + dim];
                    }

                    // Calculate the source flat index
                    let mut src_flat_idx = src.offset();
                    for dim in 0..curr_indices.len() {
                        src_flat_idx += curr_indices[dim] * src.strides()[dim];
                    }

                    // Read from source and write to destination
                    let value = src.buffer().read_scalar(src_flat_idx)?;
                    dest.with_buffer_mut(|buffer| buffer.write_scalar(dest_flat_idx, value))?;

                    return Ok(());
                }

                // Recursively iterate through all possible values for the current dimension
                let dim_size = src.dim_size(curr_dim).unwrap_or(0);
                for i in 0..dim_size {
                    curr_indices.push(i);
                    copy_elements(src, dest, base_offset, base_dims, curr_indices, curr_dim + 1)?;
                    curr_indices.pop();
                }

                Ok(())
            }

            let mut curr_indices = Vec::new();
            copy_elements(src, self, base_offset + self.offset(), indices.len(), &mut curr_indices, 0)?;
        } else {
            // If we're indexing all dimensions (or the selfination is 1D)
            // We can simply set the scalar value from the source
            if src.ndim() == 0 {
                // Source is a scalar
                let scalar = src.buffer().read_scalar(src.offset())?;
                set_index(self, indices, scalar)?;
            } else if src.size() > 0 {
                // Source is a 1D tensor or has flattened elements that we need to copy over
                let self_dim = indices[0];
                if self_dim + src.size() > self.size() {
                    return Err(Error::InvalidArgument(format!(
                        "Source tensor size ({}) exceeds available space in selfination starting at index {} (available: {})",
                        src.size(),
                        self_dim,
                        self.size() - self_dim
                    )));
                }

                // Copy each element from source to the selfination
                for i in 0..src.size() {
                    let value = src.buffer().read_scalar(src.offset() + i * src.strides()[0])?;
                    let self_idx = vec![self_dim + i]; // For a 1D tensor
                    set_index(self, &self_idx, value)?;
                }
            }
        }

        Ok(())
    }

    pub fn gather(&self, dim: impl Into<Scalar>, index: &Tensor) -> Result<Tensor> {
        let dim_i32 = dim.into().as_i32();
        let dim_usize: usize = if dim_i32 < 0 {
            (self.ndim() as i32 + dim_i32) as usize
        } else {
            dim_i32 as usize
        };

        if dim_usize >= self.ndim() {
            return Err(Error::DimensionOutOfBounds {
                dim: dim_usize as i32,
                ndim: self.ndim(),
            });
        }

        for d in 0..self.ndim() {
            if d != dim_usize && self.shape()[d] != index.shape()[d] {
                return Err(Error::InvalidArgument(format!(
                    "Index tensor must have the same shape as self in all dimensions except for dimension {}, but got {} and {}",
                    dim_usize,
                    self.shape()[d],
                    index.shape()[d]
                )));
            }
        }

        let mut output = Tensor::zeros_with_spec(index.shape(), self.device(), self.dtype())?;

        let mut index_vec = vec![0; self.ndim()];
        let mut output_vec = vec![0; output.ndim()];

        let index_iter = output.index_iter()?;
        for output_indices in index_iter {
            output_indices.iter().enumerate().for_each(|(d, &idx)| {
                output_vec[d] = idx;
                index_vec[d] = idx;
            });

            let index_value = index.get(&output_indices)?.as_i32();

            let dim_size = self.shape()[dim_usize] as i32;
            let actual_index = if index_value < 0 { dim_size + index_value } else { index_value };

            if actual_index < 0 || actual_index >= dim_size {
                return Err(Error::IndexOutOfBounds {
                    index: actual_index as usize,
                    size: dim_size as usize,
                });
            }

            index_vec[dim_usize] = actual_index as usize;

            let value = self.get(&index_vec)?;
            output.set(&output_vec, value)?;
        }

        if self.requires_grad() {
            output.with_grad()?;

            let self_clone = self.clone();
            let index_clone = index.clone();
            let dim_clone = dim_i32;

            let backward_fn = Box::new(move |_grads: &[Tensor], grad_output: &Tensor| -> Result<Vec<Tensor>> {
                let mut grad_self = Tensor::zeros_like(&self_clone)?;
                grad_self.scatter_add_(dim_clone, &index_clone, grad_output)?;

                let grad_index = Tensor::zeros_like(&index_clone)?;

                Ok(vec![grad_self, grad_index])
            });

            let node = crate::TensorNode::new("gather".to_string(), vec![self.clone(), index.clone()], Some(backward_fn));

            output.set_node(node);
        }

        Ok(output)
    }

    pub fn scatter_add_(&mut self, dim: impl Into<Scalar>, index: &Tensor, src: &Tensor) -> Result<()> {
        let dim_i32 = dim.into().as_i32();
        let dim_usize: usize = if dim_i32 < 0 {
            (self.ndim() as i32 + dim_i32) as usize
        } else {
            dim_i32 as usize
        };

        if dim_usize >= self.ndim() {
            return Err(Error::DimensionOutOfBounds {
                dim: dim_usize as i32,
                ndim: self.ndim(),
            });
        }

        for d in 0..self.ndim() {
            if d != dim_usize && index.shape()[d] != src.shape()[d] {
                return Err(Error::InvalidArgument(format!(
                    "Index and source tensors must have the same shape, but got {:?} and {:?}",
                    index.shape(),
                    src.shape()
                )));
            }
        }

        if src.shape() != index.shape() {
            return Err(Error::InvalidArgument(format!(
                "Source and index tensors must have the same shape, but got {:?} and {:?}",
                src.shape(),
                index.shape()
            )));
        }

        let src_iter = src.index_iter()?;
        for src_indices in src_iter {
            let value = src.get(&src_indices)?;
            let target_index = index.get(&src_indices)?.as_i32();
            let dim_size = self.shape()[dim_usize] as i32;
            let actual_index = if target_index < 0 { dim_size + target_index } else { target_index };

            if actual_index < 0 || actual_index >= dim_size {
                return Err(Error::IndexOutOfBounds {
                    index: actual_index as usize,
                    size: dim_size as usize,
                });
            }

            let mut target_indices = src_indices.clone();
            target_indices[dim_usize] = actual_index as usize;

            crate::utils::indexing::add_at_index(self, &target_indices, value)?;
        }

        Ok(())
    }

    pub fn bincount(&self, weights: Option<&Tensor>, minlength: Option<usize>) -> Result<Tensor> {
        if self.ndim() != 1 {
            return Err(Error::InvalidArgument(format!("Expected 1D tensor for bincount, got {}D", self.ndim())));
        }

        if !self.dtype().is_int() {
            return Err(Error::InvalidArgument(format!(
                "Expected tensor with integer dtype for bincount, got {}",
                self.dtype()
            )));
        }

        let mut max_val: i32 = 0;
        for i in 0..self.size() {
            let val = self.get(&[i])?.as_i32();
            if val < 0 {
                return Err(Error::InvalidArgument(
                    "bincount input tensor must not contain negative values".to_string(),
                ));
            }
            if val > max_val {
                max_val = val;
            }
        }

        let output_size = std::cmp::max(max_val as usize + 1, minlength.unwrap_or(0));
        let output_dtype = if let Some(w) = weights { w.dtype() } else { self.dtype() };

        let mut output = Self::zeros_with_spec(&[output_size], self.device(), output_dtype)?;

        if let Some(w) = weights {
            if w.shape() != self.shape() {
                return Err(Error::InvalidArgument(format!(
                    "weights tensor must have the same shape as input tensor, got {:?} and {:?}",
                    w.shape(),
                    self.shape()
                )));
            }

            for i in 0..self.size() {
                let bin_idx = self.get(&[i])?.as_i32() as usize;
                let weight_val = w.get(&[i])?;

                let current_val = output.get(&[bin_idx])?;
                output.set(&[bin_idx], current_val + weight_val)?;
            }
        } else {
            for i in 0..self.size() {
                let bin_idx = self.get(&[i])?.as_i32() as usize;

                let current_count = output.get(&[bin_idx])?.as_i32();
                output.set(&[bin_idx], current_count + 1)?;
            }
        }

        Ok(output)
    }
}