1use crate::DType;
23use smallvec::SmallVec;
24
25#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum Dim {
29 Static(usize),
31 Dynamic(u32),
34}
35
36impl Dim {
37 pub fn unwrap_static(self) -> usize {
38 match self {
39 Self::Static(n) => n,
40 Self::Dynamic(s) => panic!("expected static dim, got dynamic symbol {s}"),
41 }
42 }
43
44 pub fn is_static(self) -> bool {
45 matches!(self, Self::Static(_))
46 }
47}
48
49impl From<usize> for Dim {
50 fn from(n: usize) -> Self {
51 Self::Static(n)
52 }
53}
54
55impl std::fmt::Display for Dim {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 match self {
58 Self::Static(n) => write!(f, "{n}"),
59 Self::Dynamic(s) => write!(f, "?{s}"),
60 }
61 }
62}
63
64#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
68#[derive(Debug, Clone, PartialEq, Eq, Hash)]
69pub struct Shape {
70 dims: SmallVec<[Dim; 4]>,
71 dtype: DType,
72}
73
74impl Shape {
75 pub fn new(dims: &[usize], dtype: DType) -> Self {
77 Self {
78 dims: dims.iter().map(|&d| Dim::Static(d)).collect(),
79 dtype,
80 }
81 }
82
83 pub fn from_dims(dims: &[Dim], dtype: DType) -> Self {
85 Self {
86 dims: dims.into(),
87 dtype,
88 }
89 }
90
91 pub fn scalar(dtype: DType) -> Self {
93 Self {
94 dims: SmallVec::new(),
95 dtype,
96 }
97 }
98
99 pub fn rank(&self) -> usize {
100 self.dims.len()
101 }
102 pub fn dtype(&self) -> DType {
103 self.dtype
104 }
105 pub fn dims(&self) -> &[Dim] {
106 &self.dims
107 }
108 pub fn dim(&self, i: usize) -> Dim {
109 self.dims.get(i).copied().unwrap_or_else(|| {
110 let dims: Vec<_> = self.dims.iter().map(|d| d.unwrap_static()).collect();
111 panic!(
112 "Shape::dim({i}) out of bounds for rank {} dims={dims:?}",
113 self.rank()
114 );
115 })
116 }
117
118 pub fn dynamic_symbols(&self) -> Vec<u32> {
121 let mut syms: Vec<u32> = self
122 .dims
123 .iter()
124 .filter_map(|d| match d {
125 Dim::Dynamic(s) => Some(*s),
126 _ => None,
127 })
128 .collect();
129 syms.sort();
130 syms.dedup();
131 syms
132 }
133
134 pub fn bind(&self, bindings: &DimBinding) -> Self {
139 let dims = self
140 .dims
141 .iter()
142 .map(|d| match d {
143 Dim::Dynamic(s) => match bindings.get(*s) {
144 Some(n) => Dim::Static(n),
145 None => *d,
146 },
147 _ => *d,
148 })
149 .collect();
150 Self {
151 dims,
152 dtype: self.dtype,
153 }
154 }
155
156 pub fn num_elements(&self) -> Option<usize> {
158 let mut total = 1usize;
159 for d in &self.dims {
160 match d {
161 Dim::Static(n) => total = total.checked_mul(*n)?,
162 Dim::Dynamic(_) => return None,
163 }
164 }
165 Some(total)
166 }
167
168 pub fn size_bytes(&self) -> Option<usize> {
170 self.num_elements().map(|n| n * self.dtype.size_bytes())
171 }
172
173 pub fn is_static(&self) -> bool {
175 self.dims.iter().all(|d| d.is_static())
176 }
177
178 pub fn with_dim(mut self, axis: usize, dim: Dim) -> Self {
180 self.dims[axis] = dim;
181 self
182 }
183
184 pub fn with_dtype(mut self, dtype: DType) -> Self {
186 self.dtype = dtype;
187 self
188 }
189
190 pub fn broadcast_with(&self, other: &Shape) -> Result<Shape, String> {
192 broadcast(self, other)
193 }
194}
195
196pub fn broadcast(a: &Shape, b: &Shape) -> Result<Shape, String> {
200 let max_rank = a.rank().max(b.rank());
201 let mut dims = SmallVec::new();
202 for i in 0..max_rank {
203 let ad = if i < max_rank - a.rank() {
204 Dim::Static(1)
205 } else {
206 a.dims[i - (max_rank - a.rank())]
207 };
208 let bd = if i < max_rank - b.rank() {
209 Dim::Static(1)
210 } else {
211 b.dims[i - (max_rank - b.rank())]
212 };
213 let d = broadcast_dim(ad, bd)?;
214 dims.push(d);
215 }
216 Ok(Shape {
217 dims,
218 dtype: a.dtype,
219 })
220}
221
222fn broadcast_dim(a: Dim, b: Dim) -> Result<Dim, String> {
223 match (a, b) {
224 (Dim::Static(1), d) | (d, Dim::Static(1)) => Ok(d),
225 (Dim::Static(x), Dim::Static(y)) if x == y => Ok(Dim::Static(x)),
226 (Dim::Static(x), Dim::Static(y)) => Err(format!("cannot broadcast {x} with {y}")),
227 (Dim::Dynamic(s), Dim::Dynamic(t)) if s == t => Ok(Dim::Dynamic(s)),
228 (Dim::Dynamic(_), _) | (_, Dim::Dynamic(_)) => Ok(a), }
230}
231
232pub fn matmul_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, String> {
234 if lhs.rank() < 2 || rhs.rank() < 2 {
235 return Err(format!(
236 "matmul requires rank >= 2, got {} and {}",
237 lhs.rank(),
238 rhs.rank()
239 ));
240 }
241 let m = lhs.dims[lhs.rank() - 2];
242 let k1 = lhs.dims[lhs.rank() - 1];
243 let k2 = rhs.dims[rhs.rank() - 2];
244 let n = rhs.dims[rhs.rank() - 1];
245
246 match (k1, k2) {
248 (Dim::Static(a), Dim::Static(b)) if a != b => {
249 return Err(format!("matmul K mismatch: {a} vs {b}"));
250 }
251 (Dim::Dynamic(s), Dim::Dynamic(t)) if s != t => {
252 return Err(format!("matmul K mismatch: ?{s} vs ?{t}"));
253 }
254 _ => {}
255 }
256
257 let lhs_batch = &lhs.dims[..lhs.rank() - 2];
259 let rhs_batch = &rhs.dims[..rhs.rank() - 2];
260 let batch_a = Shape::from_dims(lhs_batch, lhs.dtype);
261 let batch_b = Shape::from_dims(rhs_batch, rhs.dtype);
262 let batch = if lhs_batch.is_empty() && rhs_batch.is_empty() {
263 SmallVec::new()
264 } else if lhs_batch.is_empty() {
265 rhs_batch.into()
266 } else if rhs_batch.is_empty() {
267 lhs_batch.into()
268 } else {
269 broadcast(&batch_a, &batch_b)?.dims.clone()
270 };
271
272 let mut dims = batch;
273 dims.push(m);
274 dims.push(n);
275 Ok(Shape {
276 dims,
277 dtype: lhs.dtype,
278 })
279}
280
281pub fn expand_shape(input: &Shape, target: &[i64]) -> Result<Shape, String> {
283 if target.iter().any(|&d| d < 0) {
284 return Err("expand target has negative dim".into());
285 }
286 let target_s = Shape::new(
287 &target.iter().map(|&d| d as usize).collect::<Vec<_>>(),
288 input.dtype(),
289 );
290 broadcast(input, &target_s)
291}
292
293pub fn binary_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, String> {
295 broadcast(lhs, rhs)
296}
297
298pub fn unary_shape(input: &Shape) -> Shape {
300 input.clone()
301}
302
303pub fn cast_shape(input: &Shape, to: DType) -> Shape {
305 input.clone().with_dtype(to)
306}
307
308pub fn compare_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, String> {
310 Ok(broadcast(lhs, rhs)?.with_dtype(DType::Bool))
311}
312
313pub fn reduce_shape(input: &Shape, axes: &[usize], keep_dim: bool) -> Result<Shape, String> {
315 let mut dims = SmallVec::new();
316 for (i, &d) in input.dims.iter().enumerate() {
317 if axes.contains(&i) {
318 if keep_dim {
319 dims.push(Dim::Static(1));
320 }
321 } else {
322 dims.push(d);
323 }
324 }
325 Ok(Shape {
326 dims,
327 dtype: input.dtype,
328 })
329}
330
331pub fn softmax_shape(input: &Shape) -> Shape {
333 input.clone()
334}
335
336pub fn transpose_shape(input: &Shape, perm: &[usize]) -> Result<Shape, String> {
338 if perm.len() != input.rank() {
339 return Err(format!("perm len {} != rank {}", perm.len(), input.rank()));
340 }
341 let dims: SmallVec<[Dim; 4]> = perm.iter().map(|&i| input.dims[i]).collect();
342 Ok(Shape {
343 dims,
344 dtype: input.dtype,
345 })
346}
347
348pub fn narrow_shape(input: &Shape, axis: usize, len: usize) -> Result<Shape, String> {
350 if axis >= input.rank() {
351 return Err(format!("axis {axis} >= rank {}", input.rank()));
352 }
353 Ok(input.clone().with_dim(axis, Dim::Static(len)))
354}
355
356pub fn concat_shape(inputs: &[&Shape], axis: usize) -> Result<Shape, String> {
358 if inputs.is_empty() {
359 return Err("concat: no inputs".into());
360 }
361 let base = inputs[0];
362 let mut static_sum = 0usize;
363 let mut dyn_sym: Option<u32> = None;
364 for s in inputs {
365 if s.rank() != base.rank() {
366 return Err(format!(
367 "concat: rank mismatch {} vs {}",
368 s.rank(),
369 base.rank()
370 ));
371 }
372 let ax = axis.min(s.rank().saturating_sub(1));
373 match s.dims[ax] {
374 Dim::Static(n) => static_sum += n,
375 Dim::Dynamic(sym) => {
376 if let Some(prev) = dyn_sym {
377 if prev != sym {
378 return Err(format!(
379 "concat: mismatched dynamic symbols {prev} vs {sym} on axis {axis}"
380 ));
381 }
382 }
383 dyn_sym = Some(sym);
384 }
385 }
386 }
387 let out_dim = match dyn_sym {
388 None => Dim::Static(static_sum),
389 Some(sym) if static_sum == 0 => Dim::Dynamic(sym),
390 Some(sym) => {
391 let _ = static_sum;
394 Dim::Dynamic(sym)
395 }
396 };
397 let out_axis = axis.min(base.rank().saturating_sub(1));
398 Ok(base.clone().with_dim(out_axis, out_dim))
399}
400
401pub fn gather_shape(table: &Shape, indices: &Shape, axis: usize) -> Result<Shape, String> {
403 if axis >= table.rank() {
404 return Err(format!("gather: axis {axis} >= rank {}", table.rank()));
405 }
406 let mut dims: SmallVec<[Dim; 4]> = indices.dims.clone();
407 for i in (axis + 1)..table.rank() {
408 dims.push(table.dims[i]);
409 }
410 Ok(Shape {
411 dims,
412 dtype: table.dtype,
413 })
414}
415
416pub fn reshape_shape(input: &Shape, new_shape: &[i64]) -> Result<Shape, String> {
418 let neg_count = new_shape.iter().filter(|&&d| d == -1).count();
419 if neg_count > 1 {
420 return Err("reshape: at most one -1".into());
421 }
422
423 if input.is_static() {
424 let total = input
425 .num_elements()
426 .ok_or_else(|| "reshape: input has dynamic dims".to_string())?;
427 let known_product: i64 = new_shape.iter().filter(|&&d| d != -1).product();
428 let mut dims = SmallVec::new();
429 for &d in new_shape {
430 if d == -1 {
431 let inferred = total as i64 / known_product;
432 dims.push(Dim::Static(inferred as usize));
433 } else if d < 0 {
434 return Err(format!("reshape: invalid dim {d}"));
435 } else {
436 dims.push(Dim::Static(d as usize));
437 }
438 }
439 return Ok(Shape {
440 dims,
441 dtype: input.dtype,
442 });
443 }
444
445 let dyn_syms = input.dynamic_symbols();
448 let neg_idx = new_shape.iter().position(|&d| d == -1);
449 let mut out_dims: SmallVec<[Dim; 4]> = SmallVec::new();
450 for (i, &d) in new_shape.iter().enumerate() {
451 if Some(i) == neg_idx {
452 continue;
453 }
454 if d < 0 {
455 return Err(format!("reshape: invalid dim {d}"));
456 }
457 out_dims.push(Dim::Static(d as usize));
458 }
459 if let Some(ni) = neg_idx {
460 let inferred = if dyn_syms.len() == 1 {
461 Dim::Dynamic(dyn_syms[0])
462 } else if dyn_syms.is_empty() {
463 return Err("reshape: cannot infer -1 on static input".into());
464 } else {
465 Dim::Dynamic(crate::dynamic::sym::ROWS)
466 };
467 out_dims.insert(ni, inferred);
468 }
469 Ok(Shape {
470 dims: out_dims,
471 dtype: input.dtype,
472 })
473}
474
475pub fn leading_flatten_fused_shape(input: &Shape) -> Option<Shape> {
477 if input.rank() < 2 {
478 return None;
479 }
480 let Dim::Static(h) = input.dim(input.rank() - 1) else {
481 return None;
482 };
483 let leading = &input.dims()[..input.rank() - 1];
484 let lead_dim = if leading.iter().all(|d| d.is_static()) {
485 Dim::Static(leading.iter().map(|d| d.unwrap_static()).product::<usize>())
486 } else {
487 let mut syms: Vec<u32> = leading
488 .iter()
489 .filter_map(|d| match d {
490 Dim::Dynamic(s) => Some(*s),
491 _ => None,
492 })
493 .collect();
494 syms.sort();
495 syms.dedup();
496 match syms.len() {
497 0 => Dim::Static(leading.iter().map(|d| d.unwrap_static()).product::<usize>()),
498 1 => Dim::Dynamic(syms[0]),
499 _ => Dim::Dynamic(crate::dynamic::sym::ROWS),
500 }
501 };
502 Some(Shape::from_dims(&[lead_dim, Dim::Static(h)], input.dtype()))
503}
504
505pub fn leading_flatten_shape(input: &Shape, new_shape: &[i64]) -> Option<Shape> {
507 if new_shape.len() != 2 {
508 return None;
509 }
510 let flat = leading_flatten_fused_shape(input)?;
511 let Dim::Static(h) = input.dim(input.rank() - 1) else {
512 return None;
513 };
514 if new_shape[1] as usize != h {
515 return None;
516 }
517 match flat.dim(0) {
518 Dim::Static(lead) if new_shape[0] as usize == lead => Some(flat),
519 Dim::Dynamic(_) if new_shape[0] == -1 => Some(flat),
520 _ => None,
521 }
522}
523
524pub fn attention_shape(q: &Shape) -> Shape {
526 q.clone()
527}
528
529impl std::fmt::Display for Shape {
530 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
531 write!(f, "[")?;
532 for (i, d) in self.dims.iter().enumerate() {
533 if i > 0 {
534 write!(f, ", ")?;
535 }
536 write!(f, "{d}")?;
537 }
538 write!(f, "] {}", self.dtype)
539 }
540}
541
542pub fn conv2d_spatial_output(
544 in_size: usize,
545 kernel: usize,
546 stride: usize,
547 padding: usize,
548 dilation: usize,
549) -> usize {
550 let dil_k = dilation.saturating_mul(kernel.saturating_sub(1));
551 (in_size + 2 * padding)
552 .saturating_sub(dil_k)
553 .saturating_sub(1)
554 / stride
555 + 1
556}
557
558pub fn conv_transpose2d_spatial_output(
560 in_size: usize,
561 kernel: usize,
562 stride: usize,
563 padding: usize,
564 dilation: usize,
565 output_padding: usize,
566) -> usize {
567 let dil_k = dilation.saturating_mul(kernel.saturating_sub(1));
568 (in_size - 1) * stride + output_padding + dil_k - 2 * padding + 1
569}
570
571pub fn conv2d_output_shape(
573 input: &Shape,
574 weight: &Shape,
575 kernel_size: [usize; 2],
576 stride: [usize; 2],
577 padding: [usize; 2],
578 dilation: [usize; 2],
579 groups: usize,
580) -> Result<Shape, String> {
581 if input.rank() != 4 || weight.rank() != 4 {
582 return Err("conv2d requires NCHW input and 4-D weight".into());
583 }
584 let n = input.dim(0);
585 let c_in = input.dim(1).unwrap_static();
586 let h = input.dim(2).unwrap_static();
587 let w = input.dim(3).unwrap_static();
588 let c_out = weight.dim(0).unwrap_static();
589 let w_cin = weight.dim(1).unwrap_static();
590 if w_cin * groups != c_in {
591 return Err(format!(
592 "conv2d weight C_in/g={w_cin} * groups={groups} != input C={c_in}"
593 ));
594 }
595 let h_out = conv2d_spatial_output(h, kernel_size[0], stride[0], padding[0], dilation[0]);
596 let w_out = conv2d_spatial_output(w, kernel_size[1], stride[1], padding[1], dilation[1]);
597 Ok(Shape::from_dims(
598 &[
599 n,
600 Dim::Static(c_out),
601 Dim::Static(h_out),
602 Dim::Static(w_out),
603 ],
604 input.dtype(),
605 ))
606}
607
608pub fn im2col_output_shape(
611 input: &Shape,
612 kernel_size: [usize; 2],
613 stride: [usize; 2],
614 padding: [usize; 2],
615 dilation: [usize; 2],
616) -> Result<Shape, String> {
617 if input.rank() != 4 {
618 return Err("im2col requires NCHW input".into());
619 }
620 let c_in = input.dim(1).unwrap_static();
621 let h = input.dim(2).unwrap_static();
622 let w = input.dim(3).unwrap_static();
623 let kh = kernel_size[0];
624 let kw = kernel_size[1];
625 let h_out = conv2d_spatial_output(h, kh, stride[0], padding[0], dilation[0]);
626 let w_out = conv2d_spatial_output(w, kw, stride[1], padding[1], dilation[1]);
627 let k = c_in * kh * kw;
628 let spatial = h_out * w_out;
629 let m = match input.dim(0) {
630 Dim::Static(n) => Dim::Static(n * spatial),
631 Dim::Dynamic(crate::dynamic::sym::BATCH) | Dim::Dynamic(crate::dynamic::sym::ROWS) => {
632 Dim::Dynamic(crate::dynamic::sym::ROWS)
633 }
634 Dim::Dynamic(_) => Dim::Dynamic(crate::dynamic::sym::ROWS),
635 };
636 Ok(Shape::from_dims(&[m, Dim::Static(k)], input.dtype()))
637}
638
639pub fn conv_transpose2d_output_shape(
641 input: &Shape,
642 weight: &Shape,
643 kernel_size: [usize; 2],
644 stride: [usize; 2],
645 padding: [usize; 2],
646 dilation: [usize; 2],
647 output_padding: [usize; 2],
648 groups: usize,
649) -> Result<Shape, String> {
650 if input.rank() != 4 || weight.rank() != 4 {
651 return Err("conv_transpose2d requires NCHW input and 4-D weight".into());
652 }
653 let n = input.dim(0).unwrap_static();
654 let c_in = input.dim(1).unwrap_static();
655 let h = input.dim(2).unwrap_static();
656 let w = input.dim(3).unwrap_static();
657 let w_cin = weight.dim(0).unwrap_static();
658 let c_out_per_g = weight.dim(1).unwrap_static();
659 if w_cin != c_in {
660 return Err(format!(
661 "conv_transpose2d weight C_in={w_cin} != input C={c_in}"
662 ));
663 }
664 let h_out = conv_transpose2d_spatial_output(
665 h,
666 kernel_size[0],
667 stride[0],
668 padding[0],
669 dilation[0],
670 output_padding[0],
671 );
672 let w_out = conv_transpose2d_spatial_output(
673 w,
674 kernel_size[1],
675 stride[1],
676 padding[1],
677 dilation[1],
678 output_padding[1],
679 );
680 Ok(Shape::new(
681 &[n, c_out_per_g * groups, h_out, w_out],
682 input.dtype(),
683 ))
684}
685
686#[cfg(test)]
687mod tests {
688 use super::*;
689
690 #[test]
691 fn static_shape() {
692 let s = Shape::new(&[4, 15, 384], DType::F32);
693 assert_eq!(s.rank(), 3);
694 assert_eq!(s.num_elements(), Some(4 * 15 * 384));
695 assert_eq!(s.size_bytes(), Some(4 * 15 * 384 * 4));
696 assert!(s.is_static());
697 assert_eq!(format!("{s}"), "[4, 15, 384] f32");
698 }
699
700 #[test]
703 fn broadcast_same() {
704 let a = Shape::new(&[4, 15, 384], DType::F32);
705 let r = broadcast(&a, &a).unwrap();
706 assert_eq!(r.dims(), a.dims());
707 }
708
709 #[test]
710 fn broadcast_bias() {
711 let a = Shape::new(&[4, 15, 384], DType::F32);
712 let b = Shape::new(&[384], DType::F32);
713 let r = broadcast(&a, &b).unwrap();
714 assert_eq!(r, Shape::new(&[4, 15, 384], DType::F32));
715 }
716
717 #[test]
718 fn broadcast_scalar() {
719 let a = Shape::new(&[4, 15, 384], DType::F32);
720 let b = Shape::scalar(DType::F32);
721 let r = broadcast(&a, &b).unwrap();
722 assert_eq!(r, a);
723 }
724
725 #[test]
726 fn broadcast_mismatch() {
727 let a = Shape::new(&[4, 15, 384], DType::F32);
728 let b = Shape::new(&[4, 15, 256], DType::F32);
729 assert!(broadcast(&a, &b).is_err());
730 }
731
732 #[test]
733 fn matmul_basic() {
734 let a = Shape::new(&[4, 15, 384], DType::F32);
735 let b = Shape::new(&[384, 1536], DType::F32);
736 let r = matmul_shape(&a, &b).unwrap();
737 assert_eq!(r, Shape::new(&[4, 15, 1536], DType::F32));
738 }
739
740 #[test]
741 fn matmul_batched() {
742 let a = Shape::new(&[4, 15, 384], DType::F32);
743 let b = Shape::new(&[4, 384, 1536], DType::F32);
744 let r = matmul_shape(&a, &b).unwrap();
745 assert_eq!(r, Shape::new(&[4, 15, 1536], DType::F32));
746 }
747
748 #[test]
749 fn matmul_k_mismatch() {
750 let a = Shape::new(&[4, 15, 384], DType::F32);
751 let b = Shape::new(&[512, 1536], DType::F32);
752 assert!(matmul_shape(&a, &b).is_err());
753 }
754
755 #[test]
756 fn reduce_keepdim() {
757 let a = Shape::new(&[4, 15, 384], DType::F32);
758 let r = reduce_shape(&a, &[2], true).unwrap();
759 assert_eq!(r, Shape::new(&[4, 15, 1], DType::F32));
760 }
761
762 #[test]
763 fn reduce_no_keepdim() {
764 let a = Shape::new(&[4, 15, 384], DType::F32);
765 let r = reduce_shape(&a, &[2], false).unwrap();
766 assert_eq!(r, Shape::new(&[4, 15], DType::F32));
767 }
768
769 #[test]
770 fn concat_basic() {
771 let a = Shape::new(&[4, 15, 384], DType::F32);
772 let b = Shape::new(&[4, 15, 384], DType::F32);
773 let r = concat_shape(&[&a, &b], 2).unwrap();
774 assert_eq!(r, Shape::new(&[4, 15, 768], DType::F32));
775 }
776
777 #[test]
778 fn gather_embedding() {
779 let table = Shape::new(&[30522, 384], DType::F32);
780 let indices = Shape::new(&[4, 15], DType::I64);
781 let r = gather_shape(&table, &indices, 0).unwrap();
782 assert_eq!(
783 r,
784 Shape::from_dims(
785 &[Dim::Static(4), Dim::Static(15), Dim::Static(384)],
786 DType::F32
787 )
788 );
789 }
790
791 #[test]
792 fn reshape_with_neg1() {
793 let a = Shape::new(&[4, 15, 384], DType::F32);
794 let r = reshape_shape(&a, &[60, -1]).unwrap();
795 assert_eq!(r, Shape::new(&[60, 384], DType::F32));
796 }
797
798 #[test]
799 fn transpose_basic() {
800 let a = Shape::new(&[4, 15, 384], DType::F32);
801 let r = transpose_shape(&a, &[0, 2, 1]).unwrap();
802 assert_eq!(r, Shape::new(&[4, 384, 15], DType::F32));
803 }
804
805 #[test]
806 fn narrow_basic() {
807 let a = Shape::new(&[4, 15, 1152], DType::F32);
808 let r = narrow_shape(&a, 2, 384).unwrap();
809 assert_eq!(r, Shape::new(&[4, 15, 384], DType::F32));
810 }
811
812 #[test]
813 fn compare_bool_output() {
814 let a = Shape::new(&[4, 15], DType::F32);
815 let b = Shape::new(&[4, 15], DType::F32);
816 let r = compare_shape(&a, &b).unwrap();
817 assert_eq!(r.dtype(), DType::Bool);
818 assert_eq!(r.rank(), 2);
819 }
820
821 #[test]
824 fn dynamic_shape() {
825 let s = Shape::from_dims(
826 &[Dim::Dynamic(0), Dim::Dynamic(1), Dim::Static(384)],
827 DType::F32,
828 );
829 assert_eq!(s.rank(), 3);
830 assert_eq!(s.num_elements(), None);
831 assert!(!s.is_static());
832 assert_eq!(format!("{s}"), "[?0, ?1, 384] f32");
833 }
834
835 #[test]
836 fn dynamic_symbols_lists_distinct_dims() {
837 let s = Shape::from_dims(
838 &[
839 Dim::Dynamic(1),
840 Dim::Static(384),
841 Dim::Dynamic(0),
842 Dim::Dynamic(1),
843 ],
844 DType::F32,
845 );
846 assert_eq!(s.dynamic_symbols(), vec![0, 1]);
847 }
848
849 #[test]
850 fn bind_specializes_known_symbols() {
851 let s = Shape::from_dims(
852 &[Dim::Dynamic(0), Dim::Dynamic(1), Dim::Static(384)],
853 DType::F32,
854 );
855 let mut b = DimBinding::new();
856 b.set(0, 8);
857 b.set(1, 64);
858 let s2 = s.bind(&b);
859 assert!(s2.is_static());
860 assert_eq!(s2.num_elements(), Some(8 * 64 * 384));
861 }
862
863 #[test]
864 fn bind_leaves_unknown_symbols_alone() {
865 let s = Shape::from_dims(&[Dim::Dynamic(0), Dim::Dynamic(99)], DType::F32);
866 let mut b = DimBinding::new();
867 b.set(0, 4);
868 let s2 = s.bind(&b);
869 assert!(!s2.is_static()); assert_eq!(s2.dynamic_symbols(), vec![99]);
871 }
872}
873
874#[derive(Debug, Clone, Default)]
877pub struct DimBinding {
878 map: std::collections::HashMap<u32, usize>,
879}
880
881impl DimBinding {
882 pub fn new() -> Self {
883 Self::default()
884 }
885 pub fn set(&mut self, symbol: u32, size: usize) -> Option<usize> {
886 self.map.insert(symbol, size)
887 }
888 pub fn get(&self, symbol: u32) -> Option<usize> {
889 self.map.get(&symbol).copied()
890 }
891 pub fn is_empty(&self) -> bool {
892 self.map.is_empty()
893 }
894 pub fn len(&self) -> usize {
895 self.map.len()
896 }
897 pub fn iter(&self) -> impl Iterator<Item = (u32, usize)> + '_ {
898 self.map.iter().map(|(&s, &n)| (s, n))
899 }
900}