shrew-core 0.1.0

Core tensor, dtype, shape, layout, backend traits, and autograd for Shrew
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Op — Computational graph node for automatic differentiation
//
// Every tensor that results from a computation records HOW it was created
// via the Op enum. This forms a directed acyclic graph (DAG) that backward()
// traverses to compute gradients.
//
// Example: c = a + b
//   a.op = Op::None (leaf variable)
//   b.op = Op::None (leaf variable)
//   c.op = Op::Binary { lhs: a, rhs: b, op: Add }
//
// When we call c.backward():
//   1. Start with grad_c = 1.0 (by convention, dL/dL = 1)
//   2. Look at c.op → Binary { lhs: a, rhs: b, Add }
//   3. grad_a += grad_c * d(a+b)/da = grad_c * 1 = grad_c
//   4. grad_b += grad_c * d(a+b)/db = grad_c * 1 = grad_c
//
// WHY STORE Tensor<B> INSTEAD OF TensorId?
//
// In Phase 1, Op stored only TensorIds. Now in Phase 2, each Op variant stores
// the actual Tensor<B> references to its inputs. Since Tensor<B> is Arc-wrapped,
// cloning is cheap (just increment refcount). This means:
//
//   1. backward() can directly access input values for gradient computation
//      (e.g., d(a*b)/da = b — we need the actual value of b)
//   2. The computation graph keeps input tensors alive as long as the output
//      tensor exists (correct: we need them for backward)
//   3. No separate tensor registry needed — the graph IS the references
//
// MEMORY: The graph forms a DAG (no cycles), so Arc handles cleanup correctly.
// When the loss tensor is dropped, all intermediate tensors' refcounts decrease.

use crate::backend::{Backend, BinaryOp, ReduceOp, UnaryOp};

/// Unique identifier for a tensor. Used as keys in GradStore.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TensorId(pub(crate) u64);

impl Default for TensorId {
    fn default() -> Self {
        Self::new()
    }
}

impl TensorId {
    /// Generate a new unique tensor ID (uses a global atomic counter).
    pub fn new() -> Self {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        TensorId(COUNTER.fetch_add(1, Ordering::Relaxed))
    }
}

/// Records the operation that produced a tensor, storing references to inputs.
///
/// Each variant holds the actual input Tensor(s) (Arc-wrapped, cheap to clone)
/// plus the operation parameters. backward() uses these to compute gradients
/// via the chain rule.
///
/// Op<B> is generic over the Backend because it stores Tensor<B>.
pub enum Op<B: Backend> {
    /// No operation — this is a leaf tensor (input data or trainable parameter).
    None,

    /// Element-wise binary: result = op(lhs, rhs)
    Binary {
        lhs: crate::Tensor<B>,
        rhs: crate::Tensor<B>,
        op: BinaryOp,
    },

    /// Element-wise unary: result = op(input)
    Unary {
        input: crate::Tensor<B>,
        op: UnaryOp,
    },

    /// Reduction: result = reduce(input, dims)
    Reduce {
        input: crate::Tensor<B>,
        op: ReduceOp,
        dims: Vec<usize>,
        keep_dim: bool,
    },

    /// Matrix multiplication: result = lhs @ rhs
    Matmul {
        lhs: crate::Tensor<B>,
        rhs: crate::Tensor<B>,
    },

    /// Reshape (includes squeeze/unsqueeze): same data, different shape.
    /// src_shape records the original shape so backward can reshape gradients back.
    Reshape {
        input: crate::Tensor<B>,
        src_shape: crate::Shape,
    },

    /// Transpose: swap two dimensions
    Transpose {
        input: crate::Tensor<B>,
        dim0: usize,
        dim1: usize,
    },

    /// Narrow/slice along a dimension
    Narrow {
        input: crate::Tensor<B>,
        dim: usize,
        start: usize,
        len: usize,
    },

    /// Affine transform: result = input * mul + add
    Affine {
        input: crate::Tensor<B>,
        mul: f64,
        add: f64,
    },

    /// Contiguous copy: same logical values, but data is now contiguous in memory.
    /// Gradient passes through unchanged.
    Contiguous { input: crate::Tensor<B> },

    /// 2D convolution: result = conv2d(input, weight) + bias
    /// input: [N, C_in, H, W], weight: [C_out, C_in, kH, kW]
    Conv2d {
        input: crate::Tensor<B>,
        weight: crate::Tensor<B>,
        bias: Option<crate::Tensor<B>>,
        stride: [usize; 2],
        padding: [usize; 2],
    },

    /// 2D max-pooling.
    /// input: [N, C, H, W]
    /// indices stores the argmax positions for backward.
    MaxPool2d {
        input: crate::Tensor<B>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        indices: Vec<usize>,
    },

    /// Concatenation along a dimension.
    /// `inputs` are the original tensors that were concatenated.
    /// `dim` is the concatenation dimension.
    /// `sizes` stores the size of each input along `dim` (needed by backward
    /// to slice the gradient back into per-input pieces via narrow).
    Cat {
        inputs: Vec<crate::Tensor<B>>,
        dim: usize,
        sizes: Vec<usize>,
    },

    /// Element-wise power: result = input ^ exponent.
    Powf {
        input: crate::Tensor<B>,
        exponent: f64,
    },

    /// Element-wise clamp: result = clamp(input, min, max).
    Clamp {
        input: crate::Tensor<B>,
        min: f64,
        max: f64,
    },

    /// Conditional select: result[i] = if mask[i] { on_true[i] } else { on_false[i] }.
    WhereCond {
        mask: crate::Tensor<B>,
        on_true: crate::Tensor<B>,
        on_false: crate::Tensor<B>,
    },

    /// Gather elements along a dimension using index tensor.
    Gather {
        input: crate::Tensor<B>,
        index: crate::Tensor<B>,
        dim: usize,
    },

    /// Constant padding.
    Pad {
        input: crate::Tensor<B>,
        padding: Vec<[usize; 2]>,
    },

    /// 2D average-pooling.
    /// input: [N, C, H, W]
    AvgPool2d {
        input: crate::Tensor<B>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
    },

    /// 1D convolution: result = conv1d(input, weight) + bias
    /// input: [N, C_in, L], weight: [C_out, C_in, K]
    Conv1d {
        input: crate::Tensor<B>,
        weight: crate::Tensor<B>,
        bias: Option<crate::Tensor<B>>,
        stride: usize,
        padding: usize,
    },

    /// Index select along a dimension: result = input.index_select(dim, indices)
    /// Backward = scatter-add of grad_output into grad_input at index positions.
    IndexSelect {
        input: crate::Tensor<B>,
        indices: crate::Tensor<B>,
        dim: usize,
    },

    /// Dtype conversion: result = input.to_dtype(target_dtype)
    /// Backward casts gradient back to the original dtype.
    ToDtype {
        input: crate::Tensor<B>,
        src_dtype: crate::dtype::DType,
    },
}

// Manual Clone implementation because derive can't handle the generic well.
// All clones are cheap: Tensor clone is just Arc refcount increment.
impl<B: Backend> Clone for Op<B> {
    fn clone(&self) -> Self {
        match self {
            Op::None => Op::None,
            Op::Binary { lhs, rhs, op } => Op::Binary {
                lhs: lhs.clone(),
                rhs: rhs.clone(),
                op: *op,
            },
            Op::Unary { input, op } => Op::Unary {
                input: input.clone(),
                op: *op,
            },
            Op::Reduce {
                input,
                op,
                dims,
                keep_dim,
            } => Op::Reduce {
                input: input.clone(),
                op: *op,
                dims: dims.clone(),
                keep_dim: *keep_dim,
            },
            Op::Matmul { lhs, rhs } => Op::Matmul {
                lhs: lhs.clone(),
                rhs: rhs.clone(),
            },
            Op::Reshape { input, src_shape } => Op::Reshape {
                input: input.clone(),
                src_shape: src_shape.clone(),
            },
            Op::Transpose { input, dim0, dim1 } => Op::Transpose {
                input: input.clone(),
                dim0: *dim0,
                dim1: *dim1,
            },
            Op::Narrow {
                input,
                dim,
                start,
                len,
            } => Op::Narrow {
                input: input.clone(),
                dim: *dim,
                start: *start,
                len: *len,
            },
            Op::Affine { input, mul, add } => Op::Affine {
                input: input.clone(),
                mul: *mul,
                add: *add,
            },
            Op::Contiguous { input } => Op::Contiguous {
                input: input.clone(),
            },
            Op::Conv2d {
                input,
                weight,
                bias,
                stride,
                padding,
            } => Op::Conv2d {
                input: input.clone(),
                weight: weight.clone(),
                bias: bias.clone(),
                stride: *stride,
                padding: *padding,
            },
            Op::MaxPool2d {
                input,
                kernel_size,
                stride,
                padding,
                indices,
            } => Op::MaxPool2d {
                input: input.clone(),
                kernel_size: *kernel_size,
                stride: *stride,
                padding: *padding,
                indices: indices.clone(),
            },
            Op::Cat { inputs, dim, sizes } => Op::Cat {
                inputs: inputs.clone(),
                dim: *dim,
                sizes: sizes.clone(),
            },
            Op::Powf { input, exponent } => Op::Powf {
                input: input.clone(),
                exponent: *exponent,
            },
            Op::Clamp { input, min, max } => Op::Clamp {
                input: input.clone(),
                min: *min,
                max: *max,
            },
            Op::WhereCond {
                mask,
                on_true,
                on_false,
            } => Op::WhereCond {
                mask: mask.clone(),
                on_true: on_true.clone(),
                on_false: on_false.clone(),
            },
            Op::Gather { input, index, dim } => Op::Gather {
                input: input.clone(),
                index: index.clone(),
                dim: *dim,
            },
            Op::Pad { input, padding } => Op::Pad {
                input: input.clone(),
                padding: padding.clone(),
            },
            Op::AvgPool2d {
                input,
                kernel_size,
                stride,
                padding,
            } => Op::AvgPool2d {
                input: input.clone(),
                kernel_size: *kernel_size,
                stride: *stride,
                padding: *padding,
            },
            Op::Conv1d {
                input,
                weight,
                bias,
                stride,
                padding,
            } => Op::Conv1d {
                input: input.clone(),
                weight: weight.clone(),
                bias: bias.clone(),
                stride: *stride,
                padding: *padding,
            },
            Op::IndexSelect {
                input,
                indices,
                dim,
            } => Op::IndexSelect {
                input: input.clone(),
                indices: indices.clone(),
                dim: *dim,
            },
            Op::ToDtype { input, src_dtype } => Op::ToDtype {
                input: input.clone(),
                src_dtype: *src_dtype,
            },
        }
    }
}

// Concise Debug: show op type and tensor IDs only (not full tensor data).
impl<B: Backend> std::fmt::Debug for Op<B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Op::None => write!(f, "None"),
            Op::Binary { lhs, rhs, op } => {
                write!(f, "Binary({:?}, id={:?}, id={:?})", op, lhs.id(), rhs.id())
            }
            Op::Unary { input, op } => {
                write!(f, "Unary({:?}, id={:?})", op, input.id())
            }
            Op::Reduce {
                input, op, dims, ..
            } => {
                write!(f, "Reduce({:?}, dims={:?}, id={:?})", op, dims, input.id())
            }
            Op::Matmul { lhs, rhs } => {
                write!(f, "Matmul(id={:?}, id={:?})", lhs.id(), rhs.id())
            }
            Op::Reshape { input, src_shape } => {
                write!(f, "Reshape({} → ?, id={:?})", src_shape, input.id())
            }
            Op::Transpose { input, dim0, dim1 } => {
                write!(f, "Transpose({}, {}, id={:?})", dim0, dim1, input.id())
            }
            Op::Narrow {
                input,
                dim,
                start,
                len,
            } => {
                write!(
                    f,
                    "Narrow(dim={}, {}..{}, id={:?})",
                    dim,
                    start,
                    start + len,
                    input.id()
                )
            }
            Op::Affine { input, mul, add } => {
                write!(f, "Affine(*{} +{}, id={:?})", mul, add, input.id())
            }
            Op::Contiguous { input } => {
                write!(f, "Contiguous(id={:?})", input.id())
            }
            Op::Conv2d {
                input,
                weight,
                bias,
                stride,
                padding,
            } => {
                write!(
                    f,
                    "Conv2d(in={:?}, w={:?}, bias={}, s={:?}, p={:?})",
                    input.id(),
                    weight.id(),
                    bias.is_some(),
                    stride,
                    padding
                )
            }
            Op::MaxPool2d {
                input,
                kernel_size,
                stride,
                padding,
                ..
            } => {
                write!(
                    f,
                    "MaxPool2d(in={:?}, k={:?}, s={:?}, p={:?})",
                    input.id(),
                    kernel_size,
                    stride,
                    padding
                )
            }
            Op::Cat { inputs, dim, .. } => {
                let ids: Vec<_> = inputs.iter().map(|t| t.id()).collect();
                write!(f, "Cat(dim={}, ids={:?})", dim, ids)
            }
            Op::Powf { input, exponent } => {
                write!(f, "Powf(exp={}, id={:?})", exponent, input.id())
            }
            Op::Clamp { input, min, max } => {
                write!(f, "Clamp(min={}, max={}, id={:?})", min, max, input.id())
            }
            Op::WhereCond {
                mask,
                on_true,
                on_false,
            } => {
                write!(
                    f,
                    "WhereCond(mask={:?}, true={:?}, false={:?})",
                    mask.id(),
                    on_true.id(),
                    on_false.id()
                )
            }
            Op::Gather { input, index, dim } => {
                write!(
                    f,
                    "Gather(dim={}, input={:?}, index={:?})",
                    dim,
                    input.id(),
                    index.id()
                )
            }
            Op::Pad { input, padding } => {
                write!(f, "Pad(pad={:?}, id={:?})", padding, input.id())
            }
            Op::AvgPool2d {
                input,
                kernel_size,
                stride,
                padding,
                ..
            } => {
                write!(
                    f,
                    "AvgPool2d(in={:?}, k={:?}, s={:?}, p={:?})",
                    input.id(),
                    kernel_size,
                    stride,
                    padding
                )
            }
            Op::Conv1d {
                input,
                weight,
                bias,
                stride,
                padding,
            } => {
                write!(
                    f,
                    "Conv1d(in={:?}, w={:?}, bias={}, s={}, p={})",
                    input.id(),
                    weight.id(),
                    bias.is_some(),
                    stride,
                    padding
                )
            }
            Op::IndexSelect {
                input,
                indices,
                dim,
            } => {
                write!(
                    f,
                    "IndexSelect(dim={}, input={:?}, indices={:?})",
                    dim,
                    input.id(),
                    indices.id()
                )
            }
            Op::ToDtype { input, src_dtype } => {
                write!(f, "ToDtype(from={:?}, id={:?})", src_dtype, input.id())
            }
        }
    }
}

impl<B: Backend> Op<B> {
    /// Return references to all input tensors of this operation.
    /// Used by topological sort in backward() to traverse the graph.
    pub fn inputs(&self) -> Vec<&crate::Tensor<B>> {
        match self {
            Op::None => vec![],
            Op::Binary { lhs, rhs, .. } | Op::Matmul { lhs, rhs } => vec![lhs, rhs],
            Op::Unary { input, .. }
            | Op::Reduce { input, .. }
            | Op::Reshape { input, .. }
            | Op::Transpose { input, .. }
            | Op::Narrow { input, .. }
            | Op::Affine { input, .. }
            | Op::Contiguous { input }
            | Op::MaxPool2d { input, .. }
            | Op::AvgPool2d { input, .. }
            | Op::Powf { input, .. }
            | Op::Clamp { input, .. } => vec![input],
            Op::Conv2d {
                input,
                weight,
                bias,
                ..
            } => {
                let mut v = vec![input, weight];
                if let Some(b) = bias {
                    v.push(b);
                }
                v
            }
            Op::Conv1d {
                input,
                weight,
                bias,
                ..
            } => {
                let mut v = vec![input, weight];
                if let Some(b) = bias {
                    v.push(b);
                }
                v
            }
            Op::Cat { inputs, .. } => inputs.iter().collect(),
            Op::WhereCond {
                mask,
                on_true,
                on_false,
            } => {
                vec![mask, on_true, on_false]
            }
            Op::Gather { input, index, .. } => vec![input, index],
            Op::IndexSelect { input, indices, .. } => vec![input, indices],
            Op::ToDtype { input, .. } => vec![input],
            Op::Pad { input, .. } => vec![input],
        }
    }
}