candle-einops 0.2.0

Compile-time einops and einsum tensor operations for Candle
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
use std::cell::RefCell;
use std::rc::Rc;

use candle_core::{Device, Result, Storage, Tensor, Var};
use candle_einops::{Backend, Operation, einops};

#[derive(Clone, Debug)]
struct RecordingBackend {
    shape: Vec<usize>,
    calls: Rc<RefCell<Vec<&'static str>>>,
}

impl RecordingBackend {
    fn new(shape: &[usize]) -> Self {
        Self {
            shape: shape.to_vec(),
            calls: Rc::new(RefCell::new(Vec::new())),
        }
    }
}

impl Backend for RecordingBackend {
    type Output = Self;

    fn shape(self) -> Vec<usize> {
        self.shape
    }

    fn reshape(mut self, shape: &[usize]) -> Result<Self::Output> {
        self.calls.borrow_mut().push("reshape");
        self.shape = shape.to_vec();
        Ok(self)
    }

    fn transpose(mut self, axes: &[usize]) -> Result<Self::Output> {
        self.calls.borrow_mut().push("transpose");
        self.shape = axes.iter().map(|&axis| self.shape[axis]).collect();
        Ok(self)
    }

    fn permute_and_compose(
        mut self,
        _permutation: &[usize],
        output_shape: &[usize],
        _group_lengths: &[usize],
    ) -> Result<<Self::Output as Backend>::Output>
    where
        Self::Output: Backend,
    {
        self.calls.borrow_mut().push("fused");
        self.shape = output_shape.to_vec();
        Ok(self)
    }

    fn reduce_axes(mut self, axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        self.calls.borrow_mut().push("reduce");
        axes.sort_by_key(|(axis, _)| *axis);
        for &(axis, _) in axes.iter().rev() {
            self.shape.remove(axis);
        }
        Ok(self)
    }

    fn add_axes(mut self, naxes: usize, positions: &[(usize, usize)]) -> Result<Self::Output> {
        self.calls.borrow_mut().push("repeat");
        let mut output = Vec::with_capacity(naxes);
        let mut input_axis = 0;
        for axis in 0..naxes {
            if let Some((_, extent)) = positions.iter().find(|(position, _)| *position == axis) {
                output.push(*extent);
            } else {
                output.push(self.shape[input_axis]);
                input_axis += 1;
            }
        }
        self.shape = output;
        Ok(self)
    }
}

impl Backend for &RecordingBackend {
    type Output = RecordingBackend;
    fn shape(self) -> Vec<usize> {
        self.shape.clone()
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.clone().reshape(shape)
    }
    fn transpose(self, axes: &[usize]) -> Result<Self::Output> {
        self.clone().transpose(axes)
    }
    fn reduce_axes(self, axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        self.clone().reduce_axes(axes)
    }
    fn add_axes(self, naxes: usize, positions: &[(usize, usize)]) -> Result<Self::Output> {
        self.clone().add_axes(naxes, positions)
    }
}

#[derive(Clone, Debug)]
struct DefaultBackend(RecordingBackend);

impl Backend for DefaultBackend {
    type Output = Self;

    fn shape(self) -> Vec<usize> {
        self.0.shape
    }

    fn reshape(mut self, shape: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("reshape");
        self.0.shape = shape.to_vec();
        Ok(self)
    }

    fn transpose(mut self, axes: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("transpose");
        self.0.shape = axes.iter().map(|&axis| self.0.shape[axis]).collect();
        Ok(self)
    }

    fn reduce_axes(self, _axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        unreachable!()
    }

    fn add_axes(self, _naxes: usize, _positions: &[(usize, usize)]) -> Result<Self::Output> {
        unreachable!()
    }
}

impl Backend for &DefaultBackend {
    type Output = DefaultBackend;
    fn shape(self) -> Vec<usize> {
        self.0.shape.clone()
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.clone().reshape(shape)
    }
    fn transpose(self, axes: &[usize]) -> Result<Self::Output> {
        self.clone().transpose(axes)
    }
    fn reduce_axes(self, axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        self.clone().reduce_axes(axes)
    }
    fn add_axes(self, naxes: usize, positions: &[(usize, usize)]) -> Result<Self::Output> {
        self.clone().add_axes(naxes, positions)
    }
}

#[derive(Clone, Debug)]
struct ChainInput(RecordingBackend);
#[derive(Clone, Debug)]
struct ChainIntermediate(RecordingBackend);
#[derive(Clone, Debug)]
struct ChainFinal {
    shape: Vec<usize>,
}

impl Backend for ChainInput {
    type Output = ChainIntermediate;
    fn shape(self) -> Vec<usize> {
        self.0.shape
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("input-reshape");
        Ok(ChainIntermediate(RecordingBackend {
            shape: shape.to_vec(),
            calls: self.0.calls,
        }))
    }
    fn transpose(self, axes: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("transpose");
        Ok(ChainIntermediate(RecordingBackend {
            shape: axes.iter().map(|&axis| self.0.shape[axis]).collect(),
            calls: self.0.calls,
        }))
    }
    fn reduce_axes(self, _axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        unreachable!()
    }
    fn add_axes(self, _naxes: usize, _positions: &[(usize, usize)]) -> Result<Self::Output> {
        unreachable!()
    }
}

impl Backend for &ChainInput {
    type Output = ChainIntermediate;
    fn shape(self) -> Vec<usize> {
        self.0.shape.clone()
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.clone().reshape(shape)
    }
    fn transpose(self, axes: &[usize]) -> Result<Self::Output> {
        self.clone().transpose(axes)
    }
    fn reduce_axes(self, axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        self.clone().reduce_axes(axes)
    }
    fn add_axes(self, naxes: usize, positions: &[(usize, usize)]) -> Result<Self::Output> {
        self.clone().add_axes(naxes, positions)
    }
}

impl Backend for ChainIntermediate {
    type Output = ChainFinal;
    fn shape(self) -> Vec<usize> {
        self.0.shape
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("reshape");
        Ok(ChainFinal {
            shape: shape.to_vec(),
        })
    }
    fn transpose(self, _axes: &[usize]) -> Result<Self::Output> {
        unreachable!()
    }
    fn reduce_axes(self, _axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        unreachable!()
    }
    fn add_axes(self, _naxes: usize, _positions: &[(usize, usize)]) -> Result<Self::Output> {
        unreachable!()
    }
}

#[derive(Clone, Debug)]
struct FailingFusedBackend(RecordingBackend);

impl Backend for FailingFusedBackend {
    type Output = Self;
    fn shape(self) -> Vec<usize> {
        self.0.shape
    }
    fn reshape(self, _shape: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("reshape");
        Ok(self)
    }
    fn transpose(self, _axes: &[usize]) -> Result<Self::Output> {
        self.0.calls.borrow_mut().push("transpose");
        Ok(self)
    }
    fn permute_and_compose(
        self,
        _permutation: &[usize],
        _output_shape: &[usize],
        _group_lengths: &[usize],
    ) -> Result<<Self::Output as Backend>::Output>
    where
        Self::Output: Backend,
    {
        self.0.calls.borrow_mut().push("fused");
        candle_core::bail!("selected fused failure")
    }
    fn reduce_axes(self, _axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        unreachable!()
    }
    fn add_axes(self, _naxes: usize, _positions: &[(usize, usize)]) -> Result<Self::Output> {
        unreachable!()
    }
}

impl Backend for &FailingFusedBackend {
    type Output = FailingFusedBackend;
    fn shape(self) -> Vec<usize> {
        self.0.shape.clone()
    }
    fn reshape(self, shape: &[usize]) -> Result<Self::Output> {
        self.clone().reshape(shape)
    }
    fn transpose(self, axes: &[usize]) -> Result<Self::Output> {
        self.clone().transpose(axes)
    }
    fn reduce_axes(self, axes: &mut [(usize, Operation)]) -> Result<Self::Output> {
        self.clone().reduce_axes(axes)
    }
    fn add_axes(self, naxes: usize, positions: &[(usize, usize)]) -> Result<Self::Output> {
        self.clone().add_axes(naxes, positions)
    }
}

#[test]
fn fused_codegen_is_narrow_and_default_backend_remains_compatible() -> Result<()> {
    let recording = RecordingBackend::new(&[2, 3, 4]);
    let calls = recording.calls.clone();
    let output = einops!("a b c -> c (a b)", recording)?;
    assert_eq!(output.shape, [4, 6]);
    assert_eq!(&*calls.borrow(), &["fused"]);

    let default = DefaultBackend(RecordingBackend::new(&[2, 3, 4]));
    let calls = default.0.calls.clone();
    let output =
        <DefaultBackend as Backend>::permute_and_compose(default, &[2, 0, 1], &[4, 6], &[1, 2])?;
    assert_eq!(output.0.shape, [4, 6]);
    assert_eq!(&*calls.borrow(), &["transpose", "reshape"]);

    let chain = ChainInput(RecordingBackend::new(&[2, 3, 4]));
    let calls = chain.0.calls.clone();
    let output = einops!("a b c -> c (a b)", chain)?;
    assert_eq!(output.shape, [4, 6]);
    assert_eq!(&*calls.borrow(), &["transpose", "reshape"]);

    let dynamic_default = DefaultBackend(RecordingBackend::new(&[2, 3, 4, 5]));
    let calls = dynamic_default.0.calls.clone();
    let output = einops!("a b .. -> b (..) a", dynamic_default)?;
    assert_eq!(output.0.shape, [3, 20, 2]);
    assert_eq!(&*calls.borrow(), &["transpose", "reshape"]);

    let reduction = RecordingBackend::new(&[2, 3, 4, 5]);
    let calls = reduction.calls.clone();
    let _ = einops!("a b sum(reduced) c -> c (a b)", reduction)?;
    assert_eq!(&*calls.borrow(), &["reduce", "fused"]);

    let repeat = RecordingBackend::new(&[2, 3]);
    let calls = repeat.calls.clone();
    let _ = einops!("a b -> b (a repeated:2)", repeat)?;
    assert_eq!(&*calls.borrow(), &["transpose", "repeat", "reshape"]);

    let failing = FailingFusedBackend(RecordingBackend::new(&[2, 3, 4]));
    let calls = failing.0.calls.clone();
    let error = einops!("a b c -> c (a b)", failing).expect_err("selected failure propagates");
    assert!(error.to_string().contains("selected fused failure"));
    assert_eq!(&*calls.borrow(), &["fused"]);
    Ok(())
}

fn storage_address(tensor: &Tensor) -> *const Storage {
    let (storage, _) = tensor.storage_and_layout();
    std::ptr::from_ref(&*storage)
}

fn assert_values_equal(left: &Tensor, right: &Tensor) -> Result<()> {
    assert_eq!(left.dims(), right.dims());
    assert_eq!(
        left.flatten_all()?.to_vec1::<f32>()?,
        right.flatten_all()?.to_vec1::<f32>()?
    );
    Ok(())
}

#[test]
fn tensor_selection_materializes_cpu_rank_two_and_preserves_other_views() -> Result<()> {
    let device = Device::Cpu;
    let input = Var::from_vec((0..24).map(|v| v as f32).collect(), (2, 3, 4), &device)?;
    let old_permuted = input.permute([2, 0, 1])?;
    let old = Tensor::reshape(&old_permuted, (4, 6))?;
    let selected = einops!("a b c -> c (a b)", input.as_tensor())?;
    assert_values_equal(&selected, &old)?;
    assert_eq!(selected.layout(), old.layout());
    assert_ne!(
        storage_address(&selected),
        storage_address(input.as_tensor())
    );
    assert_ne!(storage_address(&old), storage_address(input.as_tensor()));

    let weights = Tensor::reshape(&Tensor::arange(1f32, 25., &device)?, (4, 6))?;
    let selected_gradients = selected.mul(&weights)?.sum_all()?.backward()?;
    let old_gradients = old.mul(&weights)?.sum_all()?.backward()?;
    assert_values_equal(
        selected_gradients.get(input.as_tensor()).unwrap(),
        old_gradients.get(input.as_tensor()).unwrap(),
    )?;

    let storage = Tensor::reshape(&Tensor::arange(0f32, 48., &device)?, (4, 3, 4))?;
    let offset = storage.narrow(0, 1, 2)?;
    let offset_output = einops!("a b c -> c (a b)", &offset)?;
    let offset_old = offset.permute([2, 0, 1])?.reshape(&[4, 6])?;
    assert_eq!(offset_output.layout(), offset_old.layout());
    assert_ne!(storage_address(&offset_output), storage_address(&offset));

    let nchw = Tensor::reshape(&Tensor::arange(0f32, 210., &device)?, (2, 3, 5, 7))?;
    let nhwc = einops!("n c h w -> n (h w) c", &nchw)?;
    let nhwc_permuted = nchw.permute([0, 2, 3, 1])?;
    let nhwc_old = Tensor::reshape(&nhwc_permuted, (2, 35, 3))?;
    assert_values_equal(&nhwc, &nhwc_old)?;
    assert_eq!(nhwc.stride(), [105, 1, 35]);
    assert_eq!(storage_address(&nhwc), storage_address(&nchw));

    let ellipsis = Tensor::arange(0f32, 120., &device)?.reshape(&[2, 3, 4, 5])?;
    let ellipsis_old = ellipsis.permute((1, 2, 3, 0))?.reshape(&[3, 20, 2])?;
    let ellipsis_selected = einops!("a b .. -> b (..) a", &ellipsis)?;
    assert_values_equal(&ellipsis_selected, &ellipsis_old)?;
    assert_eq!(
        storage_address(&ellipsis_selected),
        storage_address(&ellipsis)
    );
    assert_ne!(storage_address(&ellipsis_old), storage_address(&ellipsis));

    let zero_capture = Tensor::arange(0f32, 3., &device)?;
    let zero_capture_output = einops!("a .. -> (..) a", &zero_capture)?;
    assert_eq!(zero_capture_output.dims(), [1, 3]);
    assert_eq!(
        storage_address(&zero_capture_output),
        storage_address(&zero_capture)
    );

    let selected_input = Var::from_vec(
        (0..48).map(|value| value as f32 / 7.).collect(),
        (2, 2, 3, 4),
        &device,
    )?;
    let reference_input = Var::from_vec(
        (0..48).map(|value| value as f32 / 7.).collect(),
        (2, 2, 3, 4),
        &device,
    )?;
    let selected = einops!("sum(reduced) a b c -> c (a b)", selected_input.as_tensor())?;
    let reference = reference_input
        .sum(0)?
        .permute((2, 0, 1))?
        .reshape(&[4, 6])?;
    assert_values_equal(&selected, &reference)?;
    let weights = Tensor::arange(1f32, 25., &device)?.reshape(&[4, 6])?;
    let selected_gradients = selected.mul(&weights)?.sum_all()?.backward()?;
    let reference_gradients = reference.mul(&weights)?.sum_all()?.backward()?;
    assert_values_equal(
        selected_gradients.get(selected_input.as_tensor()).unwrap(),
        reference_gradients
            .get(reference_input.as_tensor())
            .unwrap(),
    )?;
    Ok(())
}

#[test]
fn tensor_noncontiguous_zero_singleton_identity_and_fallback_boundaries() -> Result<()> {
    let device = Device::Cpu;
    let base = Tensor::reshape(&Tensor::arange(0f32, 24., &device)?, (2, 3, 4))?;
    let source = base.permute([2, 0, 1])?;
    let eligible =
        <&Tensor as Backend>::permute_and_compose(&source, &[0, 1, 2], &[4, 6], &[1, 2])?;
    let eligible_old = Tensor::reshape(&source, (4, 6))?;
    assert_values_equal(&eligible, &eligible_old)?;
    assert_eq!(eligible.layout(), eligible_old.layout());
    assert_ne!(storage_address(&eligible), storage_address(&source));
    assert_ne!(storage_address(&eligible_old), storage_address(&source));

    let fallback =
        <&Tensor as Backend>::permute_and_compose(&source, &[0, 1, 2], &[8, 3], &[2, 1])?;
    let fallback_old = Tensor::reshape(&source, (8, 3))?;
    assert_values_equal(&fallback, &fallback_old)?;
    assert_ne!(storage_address(&fallback), storage_address(&source));

    let singleton = Tensor::reshape(&Tensor::arange(0f32, 6., &device)?, (2, 1, 3))?;
    let singleton_output =
        <&Tensor as Backend>::permute_and_compose(&singleton, &[1, 0, 2], &[1, 6], &[1, 2])?;
    let singleton_permuted = singleton.permute([1, 0, 2])?;
    let singleton_old = Tensor::reshape(&singleton_permuted, (1, 6))?;
    assert_eq!(singleton_output.layout(), singleton_old.layout());

    let zero = Tensor::zeros((2, 0, 3), candle_core::DType::F32, &device)?;
    let zero_output =
        <&Tensor as Backend>::permute_and_compose(&zero, &[2, 0, 1], &[3, 0], &[1, 2])?;
    assert_eq!(zero_output.dims(), [3, 0]);
    assert_eq!(storage_address(&zero_output), storage_address(&zero));

    let identity =
        <&Tensor as Backend>::permute_and_compose(&base, &[0, 1, 2], &[2, 3, 4], &[1, 1, 1])?;
    assert_eq!(identity.layout(), base.layout());
    assert_eq!(storage_address(&identity), storage_address(&base));
    Ok(())
}

#[test]
fn fused_metadata_errors_are_deterministic_and_selected_errors_do_not_retry() -> Result<()> {
    let input = Tensor::reshape(&Tensor::arange(0f32, 24., &Device::Cpu)?, (2, 3, 4))?;
    assert!(
        <&Tensor as Backend>::permute_and_compose(&input, &[0, 0, 2], &[2, 12], &[1, 2]).is_err()
    );
    assert!(<&Tensor as Backend>::permute_and_compose(&input, &[0, 1, 2], &[24], &[2]).is_err());
    assert!(<&Tensor as Backend>::permute_and_compose(&input, &[0, 1, 2], &[24], &[4]).is_err());
    Ok(())
}

fn permutations(count: usize) -> Vec<Vec<usize>> {
    fn visit(prefix: &mut Vec<usize>, remaining: &mut Vec<usize>, output: &mut Vec<Vec<usize>>) {
        if remaining.is_empty() {
            output.push(prefix.clone());
            return;
        }
        for index in 0..remaining.len() {
            let axis = remaining.remove(index);
            prefix.push(axis);
            visit(prefix, remaining, output);
            prefix.pop();
            remaining.insert(index, axis);
        }
    }
    let mut output = Vec::new();
    visit(&mut Vec::new(), &mut (0..count).collect(), &mut output);
    output
}

#[test]
fn exhaustive_bounded_permutations_partitions_and_edge_extents_match_old_order() -> Result<()> {
    let device = Device::Cpu;
    for rank in 1..=4 {
        let shape_count = 3usize.pow(rank as u32);
        for encoded_shape in 0..shape_count {
            let mut encoded = encoded_shape;
            let mut dims = Vec::with_capacity(rank);
            for _ in 0..rank {
                dims.push(encoded % 3);
                encoded /= 3;
            }
            let elements = dims.iter().product::<usize>();
            let input = if elements == 0 {
                Tensor::zeros(dims.as_slice(), candle_core::DType::F32, &device)?
            } else {
                Tensor::from_vec(
                    (0..elements).map(|value| value as f32).collect::<Vec<_>>(),
                    dims.as_slice(),
                    &device,
                )?
            };
            for permutation in permutations(rank) {
                for boundaries in 0..(1usize << rank.saturating_sub(1)) {
                    let mut group_lengths = Vec::new();
                    let mut length = 1;
                    for boundary in 0..rank.saturating_sub(1) {
                        if boundaries & (1 << boundary) != 0 {
                            group_lengths.push(length);
                            length = 1;
                        } else {
                            length += 1;
                        }
                    }
                    group_lengths.push(length);
                    let mut cursor = 0;
                    let output_shape = group_lengths
                        .iter()
                        .map(|&length| {
                            let product = permutation[cursor..cursor + length]
                                .iter()
                                .map(|&axis| dims[axis])
                                .product();
                            cursor += length;
                            product
                        })
                        .collect::<Vec<_>>();
                    let selected = <&Tensor as Backend>::permute_and_compose(
                        &input,
                        &permutation,
                        &output_shape,
                        &group_lengths,
                    )?;
                    let old_permuted = input.permute(permutation.as_slice())?;
                    let old = Tensor::reshape(&old_permuted, output_shape.as_slice())?;
                    assert_values_equal(&selected, &old)?;
                }
            }
        }
    }
    Ok(())
}