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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// Copyright (C) 2025 zk4x
// SPDX-License-Identifier: LGPL-3.0-only
//! View handles movement operations.
use nanoserde::{DeBin, SerBin};
use crate::shape::{Dim, UAxis};
use std::{cmp::Ordering, fmt::Display, ops::Range};
/// .0[0] is original shape, further shapes are additional reshapes
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, SerBin, DeBin)]
pub struct View(pub Vec<Vec<RDim>>);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RDim {
pub d: Dim, // dim
pub st: Dim, // stride
pub lp: i64, // left pad
pub rp: i64, // right pad
}
impl SerBin for RDim {
fn ser_bin(&self, output: &mut Vec<u8>) {
self.d.ser_bin(output);
self.st.ser_bin(output);
output.extend(self.lp.to_le_bytes());
output.extend(self.rp.to_le_bytes());
}
}
impl DeBin for RDim {
fn de_bin(offset: &mut usize, bytes: &[u8]) -> Result<Self, nanoserde::DeBinErr> {
let d = Dim::de_bin(offset, bytes)?;
let st = Dim::de_bin(offset, bytes)?;
let i64_bytes = std::mem::size_of::<i64>();
// Read lp: isize (convert from bytes)
if *offset + i64_bytes > bytes.len() {
return Err(nanoserde::DeBinErr::new(*offset, i64_bytes, bytes.len()));
}
let lp_bytes = &bytes[*offset..*offset + i64_bytes];
let lp = i64::from_le_bytes(lp_bytes.try_into().unwrap());
*offset += i64_bytes;
// Read rp: isize
if *offset + i64_bytes > bytes.len() {
return Err(nanoserde::DeBinErr::new(*offset, i64_bytes, bytes.len()));
}
let rp_bytes = &bytes[*offset..*offset + i64_bytes];
let rp = i64::from_le_bytes(rp_bytes.try_into().unwrap());
*offset += i64_bytes;
Ok(Self { d, st, lp, rp })
}
}
fn to_contiguous_rdims(shape: &[Dim]) -> Vec<RDim> {
let mut st = 1;
let mut res = vec![RDim { st: 0, d: 0, lp: 0, rp: 0 }; shape.len()];
for (i, &d) in shape.iter().enumerate().rev() {
res[i] = RDim { st, d, lp: 0, rp: 0 };
st *= d;
}
res
}
impl View {
pub(crate) fn contiguous(shape: &[Dim]) -> View {
View(vec![to_contiguous_rdims(shape)])
}
/*pub(crate) fn binded(shape: &[usize], axes: &[usize], rank: usize) -> View {
let mut stride = 1;
debug_assert!(axes.iter().all(|&a| a < rank));
let mut a: usize = rank;
let mut res = Vec::with_capacity(a);
while a > 0 {
a -= 1;
if let Some(i) = axes.iter().position(|axis| *axis == a) {
let st = stride;
let d = shape[i];
stride *= d;
res.push(RDim { d, st, lp: 0, rp: 0 });
} else {
res.push(RDim { d: 1, st: 0, lp: 0, rp: 0 });
}
}
res.reverse();
View(vec![res])
}*/
pub(crate) fn rank(&self) -> usize {
self.0.last().map_or(0, Vec::len)
}
pub(crate) fn shape(&self) -> Vec<Dim> {
self.0
.last()
.map_or_else(|| vec![1], |inner| inner.iter().map(|dim| dim.d).collect())
}
#[cfg(test)]
fn strides(&self) -> Vec<Dim> {
self.0
.last()
.map_or_else(|| vec![1], |inner| inner.iter().map(|dim| dim.st).collect())
}
pub(crate) fn original_numel(&self) -> Dim {
let mut res = 1;
for dim in &self.0[0] {
if dim.st != 0 {
res *= Dim::try_from(i64::try_from(dim.d).unwrap() - dim.lp - dim.rp).unwrap();
}
}
res
}
/*pub(crate) fn numel(&self) -> usize {
self.0.last().map_or(1, |inner| inner.iter().map(|dim| dim.d).product())
}*/
/*#[cfg(debug_assertions)]
pub(crate) fn is_contiguous(&self) -> bool {
self.0.last().map_or(true, |inner| {
let stride = 1;
inner.iter().all(|dim| dim.lp == 0 && dim.rp == 0 && dim.st == stride)
})
}*/
/*pub(crate) fn used_axes(&self) -> Vec<usize> {
self.0.last().map_or_else(Vec::new, |inner| {
(0..inner.len()).filter(|&a| inner[a].st != 0).collect()
})
}*/
// Inserts new loop, shifts all axes greater than axis up by one
/*pub(crate) fn insert_loop(&mut self, axis: usize) {
//println!("Inserting loop at axis {axis}");
let inner = self.0.last_mut().unwrap();
debug_assert!(axis < inner.len());
let st = inner[axis].st;
inner.insert(axis, RDim { d: 1, st, lp: 0, rp: 0 });
//println!("After insert loop {self:?}");
}*/
pub fn is_reshape_contiguous(&self, axes: Range<UAxis>, new_shape: &[Dim]) -> bool {
//println!("{:?} reshape to {:?}", self, new_shape);
if let Some(last_block) = self.0.last() {
// Try to reshape last block in place
let new_block = try_reshape(&last_block[axes], new_shape);
if new_block.is_empty() {
return false;
}
}
true
}
pub fn reshape(&mut self, mut axes: Range<UAxis>, new_shape: &[Dim]) {
/*println!(
"Reshape {:?}, axes {:?} into shape {new_shape:?}, {self}",
self.shape(),
axes.clone()
);*/
debug_assert!(
axes.end as Dim <= self.0.last().map_or(1, Vec::len) as Dim,
"Reshape axes range {axes:?} is greater than view's rank {}",
self.0.last().map_or(1, Vec::len)
);
debug_assert_eq!(
self.0.last().unwrap()[axes.start as usize..axes.end as usize]
.iter()
.map(|dim| dim.d)
.product::<Dim>(),
new_shape.iter().product::<Dim>(),
"Reshape failed, products are different: {:?} axes {axes:?} -> {:?}",
self.shape(),
new_shape
);
let mut new_shape: Vec<Dim> = new_shape.into();
// Means we are inserting new dims in front
if axes.end == 0 {
axes.end = 1;
new_shape.push(self.0.last().unwrap()[0].d);
}
if let Some(last_block) = self.0.last_mut() {
// Try to reshape last block in place
let new_block = try_reshape(&last_block[axes.clone()], &new_shape);
if !new_block.is_empty() {
// Reshape succeeded in place, done
_ = last_block.splice(axes, new_block);
return;
}
}
//println!("Reshape non-contiguous");
// Reshape failed, so append a new block with contiguous strides and zero padding
let mut shape = self.shape();
shape.splice(axes, new_shape.iter().copied());
self.0.push(to_contiguous_rdims(&shape));
}
// This is used for reshape, merge and split
/*pub(crate) fn reshape_direct(&mut self, axes: Range<Axis>, shape: &[Dim]) {
//println!("Reshape {self} axes {axes:?} into shape {shape:?}");
debug_assert!(
axes.end <= self.0.last().map_or(1, Vec::len) as Dim,
"Reshape axes range {axes:?} is greater than view's rank {}",
self.0.last().map_or(1, Vec::len)
);
debug_assert_eq!(
self.0.last().unwrap()[axes.start as usize..axes.end as usize].iter().map(|dim| dim.d).product::<Dim>(),
shape.iter().product::<Dim>(),
"Reshape failed, products are different: {:?} -> {:?}",
self.shape(),
shape
);
let inner = self.0.last_mut().unwrap();
let mut contiguous = true;
let mut a = inner.len() as Dim;
let mut stride = 1;
let mut ost = 1;
while a > axes.start {
a -= 1;
let dim = &inner[a as usize];
if a >= axes.end - 1 {
if dim.st != 0 {
stride = dim.st * dim.d;
ost = dim.st;
}
} else {
let st = stride;
stride *= dim.d;
//println!("a = {a} stride = {stride} dim = {dim:?}");
if dim.st != st || dim.lp != 0 || dim.rp != 0 {
contiguous = false;
break;
}
}
}
//println!("contiguous={contiguous}");
if axes.clone().any(|a| inner[a as usize].st == 0) {
contiguous = false;
}
// If all reshaped axes are expanded
let expanded_reshape = if axes.clone().all(|a| inner[a as usize].st == 0) {
contiguous = true;
true
} else {
false
};
if axes.clone().any(|a| inner[a as usize].lp != 0 || inner[a as usize].rp != 0) {
contiguous = false;
}
if contiguous {
//println!("Reshape contiguous");
for a in axes.clone().rev() {
let dim = inner.remove(a);
debug_assert_eq!(dim.lp, 0);
debug_assert_eq!(dim.rp, 0);
}
for &d in shape.iter().rev() {
let st = if expanded_reshape { 0 } else { ost };
ost *= d;
inner.insert(axes.start, RDim { d, st, lp: 0, rp: 0 });
}
} else {
//println!("Reshape non-contiguous");
let mut old_shape = self.shape();
old_shape.splice(axes, shape.iter().copied());
self.0.push(to_contiguous_rdims(&old_shape));
}
//println!("After reshape: {self}\n");
}*/
/// If axes are shorter than inner, we just permute the first dimensions
pub(crate) fn permute(&mut self, axes: &[usize]) {
// Move around strides, dim, rp and lp
let inner = self.0.last_mut().unwrap();
debug_assert!(
inner.len() >= axes.len(),
"Failed to permute {:?} by axes={axes:?}",
self.shape()
);
debug_assert_eq!(
*axes.iter().max().unwrap(),
axes.len() - 1,
"Failed to permute {:?} by axes={axes:?}",
self.shape()
);
let mut temp_data = inner.clone();
for i in 0..axes.len() {
temp_data[i] = inner[axes[i]].clone();
}
*inner = temp_data;
}
pub(crate) fn expand(&mut self, shape: &[Dim]) {
// Expands first shape.len() dims
debug_assert!(self.rank() >= shape.len());
let inner = self.0.last_mut().unwrap();
for (dim, &d) in inner.iter_mut().zip(shape) {
if d != dim.d {
debug_assert_eq!(dim.d, 1);
debug_assert_eq!(dim.lp, 0);
debug_assert_eq!(dim.rp, 0);
dim.d = d;
dim.st = 0;
}
}
}
/*pub fn expand_axis(&mut self, axis: Axis, ndim: Dim) {
//println!("View expand {self} axis = {axis} to ndim {ndim}");
let inner = self.0.last_mut().unwrap();
let dim = &mut inner[axis];
debug_assert!(dim.d == ndim || dim.d == 1);
debug_assert_eq!(dim.lp, 0);
debug_assert_eq!(dim.rp, 0);
dim.d = ndim;
dim.st = 0;
}*/
pub fn pad(&mut self, rank: UAxis, padding: &[(i64, i64)]) {
//println!("view: {:?} padding: {padding:?}", self.shape());
for (axis, &(lp, rp)) in (0..rank).zip(padding) {
if lp != 0 || rp != 0 {
self.pad_axis(axis, lp, rp);
}
}
//println!("Shape after padding: {:?}", self.shape());
}
pub fn pad_axis(&mut self, axis: UAxis, left_pad: i64, right_pad: i64) {
let mut old_shape = self.shape();
let inner = self.0.last_mut().unwrap();
let mut dim = &mut inner[axis];
dim.d = Dim::try_from(i64::try_from(dim.d).unwrap() + left_pad + right_pad).unwrap();
if dim.lp < 0 && left_pad > 0 {
dim.d = Dim::try_from(i64::try_from(dim.d).unwrap() - left_pad).unwrap();
let mut stride = 1;
let mut res: Vec<RDim> = old_shape
.iter()
.enumerate()
.rev()
.map(|(a, &d)| {
let st = stride;
stride *= d;
RDim {
st,
d: if a == axis {
Dim::try_from(i64::try_from(d).unwrap() + left_pad).unwrap()
} else {
d
},
lp: if a == axis { left_pad } else { 0 },
rp: 0,
}
})
.collect();
res.reverse();
self.0.push(res);
dim = &mut self.0.last_mut().unwrap()[axis];
} else {
dim.lp += left_pad;
}
if dim.rp < 0 && right_pad > 0 {
dim.d = Dim::try_from(i64::try_from(dim.d).unwrap() - right_pad).unwrap();
let old_shape = self.shape();
let mut stride = 1;
let mut res: Vec<RDim> = old_shape
.iter()
.enumerate()
.rev()
.map(|(a, &d)| {
let st = stride;
stride *= d;
RDim {
st,
d: if a == axis {
Dim::try_from(i64::try_from(d).unwrap() + right_pad).unwrap()
} else {
d
},
lp: 0,
rp: if a == axis { right_pad } else { 0 },
}
})
.collect();
res.reverse();
self.0.push(res);
} else {
dim.rp += right_pad;
}
old_shape[axis] = Dim::try_from(i64::try_from(old_shape[axis]).unwrap() + left_pad + right_pad).unwrap();
debug_assert_eq!(self.shape(), old_shape);
}
}
fn try_reshape(block: &[RDim], new_shape: &[Dim]) -> Vec<RDim> {
fn is_contiguous_block(dims: &[RDim]) -> bool {
//println!("is contiguous: {dims:?}");
let mut expected_stride = dims.last().map_or(1, |rd| rd.st);
for rd in dims.iter().rev() {
if rd.lp != 0 || rd.rp != 0 {
return false;
}
if rd.d == 1 {
continue; // Stride doesn't matter if dim == 1
}
if rd.st != expected_stride {
//println!("non-contiguous");
return false;
}
expected_stride *= rd.d;
}
//println!("contiguous");
true
}
/*let old_total: usize = block.iter().map(|rd| rd.d).product();
let new_total: usize = new_shape.iter().product();
if old_total != new_total {
return Vec::new();
}*/
if block.iter().map(|rd| rd.d).eq(new_shape.iter().copied()) {
return block.into(); // Same shape, nothing to do
}
let mut new_dims = vec![RDim { d: 0, st: 0, lp: 0, rp: 0 }; new_shape.len()];
let (mut orig_start, mut new_start) = (0, 0);
let old_len = block.len();
let new_len = new_shape.len();
while orig_start < old_len && new_start < new_len {
let (mut orig_prod, mut new_prod) = (block[orig_start].d, new_shape[new_start]);
let (mut i, mut j) = (orig_start + 1, new_start + 1);
// Expand until products match
loop {
match orig_prod.cmp(&new_prod) {
Ordering::Less => {
orig_prod *= block[i].d;
i += 1;
debug_assert!(i <= old_len);
}
Ordering::Greater => {
new_prod *= new_shape[j];
j += 1;
debug_assert!(j <= new_len);
}
Ordering::Equal => {
if i < old_len {
if block[i].d == 1 {
i += 1;
continue;
}
}
if j < new_len {
if new_shape[j] == 1 {
j += 1;
continue;
}
}
break;
}
}
}
let orig_slice = &block[orig_start..i];
let new_slice_shape = &new_shape[new_start..j];
if orig_slice.iter().map(|rd| rd.d).eq(new_slice_shape.iter().copied()) {
// Shape unchanged: copy original RDims as-is, skip contiguous check
for (k, rd) in (new_start..j).zip(orig_slice.iter()) {
new_dims[k] = rd.clone();
}
} else {
// Shape changed: check contiguity and no padding allowed
if !is_contiguous_block(orig_slice) {
return Vec::new();
}
// Recompute strides, zero padding
let mut stride = orig_slice.last().map_or(1, |rd| rd.st);
for k in (new_start..j).rev() {
let dim = new_shape[k];
new_dims[k] = RDim { d: dim, st: if dim == 1 { 0 } else { stride }, lp: 0, rp: 0 };
stride *= dim;
}
}
orig_start = i;
new_start = j;
}
if orig_start != old_len || new_start != new_len {
//println!("{new_dims:?}");
//println!("{orig_start}, {new_start}");
//return Vec::new();
unreachable!();
}
new_dims
}
impl Display for View {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for inner in &self.0 {
f.write_fmt(format_args!(
"V(sh{:?} st{:?} pd{:?})",
inner.iter().map(|d| d.d).collect::<Vec<Dim>>(),
inner.iter().map(|d| d.st).collect::<Vec<Dim>>(),
inner.iter().map(|d| (d.lp, d.rp)).collect::<Vec<(i64, i64)>>()
))?;
}
Ok(())
}
}
#[test]
fn view_permute_1() {
let mut view = View::contiguous(&[3, 1, 4, 2]);
view.permute(&[3, 1, 0, 2]);
assert_eq!(view.shape(), [2, 1, 3, 4]);
view.permute(&[2, 0, 3, 1]);
assert_eq!(view.shape(), [3, 2, 4, 1]);
}
#[test]
fn view_permute_2() {
let mut view = View::contiguous(&[3, 1, 4, 2]);
view.permute(&[1, 2, 0]);
assert_eq!(view.shape(), [1, 4, 3, 2]);
view.permute(&[2, 1, 0]);
assert_eq!(view.shape(), [3, 4, 1, 2]);
}
// Permute test, no alloc is ~25% faster
/*#[test]
fn view_permute2() {
let mut view = View::contiguous(&[3, 1, 4, 2]);
let begin = std::time::Instant::now();
for _ in 0..10000000 {
view.permute(&[3, 1, 0, 2]);
}
let micros = begin.elapsed().as_micros();
println!("Took {micros}us");
}*/
#[test]
fn view_split() {
let mut view = View::contiguous(&[3, 1, 4, 2]);
//println!("{view}");
view.reshape(2..3, &[2, 2, 1]);
assert_eq!(view.shape(), [3, 1, 2, 2, 1, 2]);
view.reshape(0..1, &[1, 3, 1]);
assert_eq!(view.shape(), [1, 3, 1, 1, 2, 2, 1, 2]);
assert_eq!(view.0.len(), 1);
}
/*#[test]
fn view_binded() {
let view = View::binded(&[4, 2, 3], &[5, 1, 2], 6);
println!("{view}");
assert_eq!(view.rank(), 6);
assert_eq!(view.used_axes(), [1, 2, 5]);
assert_eq!(view.shape(), [1, 2, 3, 1, 1, 4]);
}*/
#[test]
fn view_reshape_1() {
let mut view = View::contiguous(&[3, 1, 4, 2]);
view.reshape(1..3, &[2, 2]);
assert_eq!(view.shape(), [3, 2, 2, 2]);
assert_eq!(view.0.len(), 1);
let mut view = View::contiguous(&[3, 3]);
view.reshape(0..2, &[9]);
assert_eq!(view.shape(), [9]);
assert_eq!(view.0.len(), 1);
let mut view = View::contiguous(&[3, 3]);
view.reshape(0..1, &[1, 3]);
view.reshape(2..3, &[1, 3]);
assert_eq!(view.shape(), [1, 3, 1, 3]);
assert_eq!(view.0.len(), 1);
}
#[test]
fn view_reshape_2() {
let mut view = View::contiguous(&[512, 368]);
view.permute(&[1, 0]);
view.reshape(0..2, &[1, 368, 512]);
//println!("{view}");
debug_assert_eq!(view.0.len(), 1);
debug_assert_eq!(view.shape(), [1, 368, 512]);
debug_assert_eq!(view.strides(), [0, 1, 368]);
}
#[test]
fn view_pad2() {
// Pad view twice in with opposite sings
//todo!()
let mut view = View::contiguous(&[1, 1, 2, 6]);
view.pad_axis(3, -3, 0);
view.pad_axis(3, 2, 0);
assert_eq!(
view,
View(vec![
vec![
RDim { d: 1, st: 12, lp: 0, rp: 0 },
RDim { d: 1, st: 12, lp: 0, rp: 0 },
RDim { d: 2, st: 6, lp: 0, rp: 0 },
RDim { d: 3, st: 1, lp: -3, rp: 0 }
],
vec![
RDim { d: 1, st: 6, lp: 0, rp: 0 },
RDim { d: 1, st: 6, lp: 0, rp: 0 },
RDim { d: 2, st: 3, lp: 0, rp: 0 },
RDim { d: 5, st: 1, lp: 2, rp: 0 }
]
])
);
}
#[test]
fn view_serialization() {
let view = View::contiguous(&[3, 4, 1, 7, 1]);
let x = view.serialize_bin();
let view2: View = nanoserde::DeBin::deserialize_bin(&x).ok().unwrap();
assert_eq!(view, view2);
}