Skip to content

Commit 7b72872

Browse files
author
Shayne Fletcher
committed
[ndslice]: slice: Slice::view
Differential Revision: [D75993130](https://our.internmc.facebook.com/intern/diff/D75993130/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D75993130/)! ghstack-source-id: 288271741 Pull Request resolved: #167
1 parent 597f65a commit 7b72872

File tree

3 files changed

+189
-37
lines changed

3 files changed

+189
-37
lines changed

controller/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl ControllerMessageHandler for ControllerActor {
430430

431431
let slice = Slice::new(0usize, vec![self.world_size], vec![1])
432432
.unwrap()
433-
.view_limit(Limit::from(CASTING_FANOUT_SIZE));
433+
.reshape_with_limit(Limit::from(CASTING_FANOUT_SIZE));
434434

435435
self.comm_actor_ref.send(
436436
this,

ndslice/src/reshape.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
//!
1919
//! For [`Shape`]s, reshaping also expands dimension labels using a
2020
//! `label/N` naming convention, preserving the semantics of the
21-
//! original shape in the reshaped view_limit.
21+
//! original shape in the reshaped reshape_with_limit.
2222
//!
23-
//! See [`view_limit`] and [`reshape_shape`] for entry points.
24-
23+
//! See [`reshape_with_limit`] and [`reshape_shape`] for entry points.
2524
use std::fmt;
2625

2726
use crate::shape::Shape;
@@ -34,7 +33,6 @@ pub type Coord = Vec<usize>;
3433
/// A reshaped version of a `Shape`, with factored dimensions and
3534
/// updated labels.
3635
///
37-
///
3836
/// This type preserves coordinate bijections with the original shape
3937
/// and provides access to the transformed layout and label mappings.
4038
pub struct ReshapedShape {
@@ -151,7 +149,7 @@ pub fn to_original_coord<'a>(
151149
///
152150
/// This limit controls how a given dimension is factored during
153151
/// reshaping. Values larger than `limit` are recursively decomposed
154-
/// into smaller factors (e.g., `view_limit([1024],
152+
/// into smaller factors (e.g., `reshape_with_limit([1024],
155153
/// Limit::new(32))` → `[32, 32]`).
156154
///
157155
/// The default limit is `32`, which balances fanout depth and layout
@@ -195,7 +193,7 @@ impl From<usize> for Limit {
195193
/// view by factoring large extents into smaller ones.
196194
///
197195
/// This is implemented for [`Slice`], enabling ergonomic access to
198-
/// [`view_limit`] as a method.
196+
/// [`reshape_with_limit`] as a method.
199197
///
200198
/// # Example
201199
/// ```
@@ -204,7 +202,7 @@ impl From<usize> for Limit {
204202
/// use ndslice::reshape::ReshapeSliceExt;
205203
///
206204
/// let slice = Slice::new_row_major(vec![1024]);
207-
/// let reshaped = slice.view_limit(Limit::new(32));
205+
/// let reshaped = slice.reshape_with_limit(Limit::new(32));
208206
/// assert_eq!(reshaped.sizes(), &[32, 32]);
209207
/// ```
210208
/// # Returns
@@ -214,20 +212,20 @@ pub trait ReshapeSliceExt {
214212
/// Returns a reshaped version of this structure by factoring each
215213
/// dimension into smaller extents no greater than `limit`,
216214
/// preserving memory layout and flat index semantics. See
217-
/// [`view_limit`] for full behavior and rationale.
215+
/// [`reshape_with_limit`] for full behavior and rationale.
218216
///
219217
/// # Arguments
220218
/// - `limit`: maximum size allowed in any reshaped dimension
221219
///
222220
/// # Returns
223221
/// A reshaped [`Slice`] with increased dimensionality and a
224222
/// bijective mapping to the original.
225-
fn view_limit(&self, limit: Limit) -> Slice;
223+
fn reshape_with_limit(&self, limit: Limit) -> Slice;
226224
}
227225

228226
impl ReshapeSliceExt for Slice {
229-
fn view_limit(&self, limit: Limit) -> Slice {
230-
view_limit(self, limit)
227+
fn reshape_with_limit(&self, limit: Limit) -> Slice {
228+
reshape_with_limit(self, limit)
231229
}
232230
}
233231

@@ -244,7 +242,7 @@ impl ReshapeShapeExt for Shape {
244242
}
245243
}
246244

247-
/// For convenient `slice.view_limit()`, `shape.reshape()`
245+
/// For convenient `slice.reshape_with_limit()`, `shape.reshape()`
248246
/// syntax, `use reshape::prelude::*`.
249247
pub mod prelude {
250248
pub use super::ReshapeShapeExt;
@@ -274,13 +272,13 @@ pub mod prelude {
274272
/// ```
275273
/// use ndslice::Slice;
276274
/// use ndslice::reshape::Limit;
277-
/// use ndslice::reshape::view_limit;
275+
/// use ndslice::reshape::reshape_with_limit;
278276
///
279277
/// let slice = Slice::new_row_major(vec![1024]);
280-
/// let reshaped = view_limit(&slice, Limit::new(32));
278+
/// let reshaped = reshape_with_limit(&slice, Limit::new(32));
281279
/// assert_eq!(reshaped.sizes(), &[32, 32]);
282280
/// ```
283-
pub fn view_limit(slice: &Slice, limit: Limit) -> Slice {
281+
pub fn reshape_with_limit(slice: &Slice, limit: Limit) -> Slice {
284282
let orig_sizes = slice.sizes();
285283
let orig_strides = slice.strides();
286284

@@ -309,7 +307,7 @@ pub fn view_limit(slice: &Slice, limit: Limit) -> Slice {
309307
/// smaller ones, producing a new shape with expanded dimensionality
310308
/// and updated labels.
311309
///
312-
/// This uses [`view_limit`] on the underlying slice and [`expand_labels`]
310+
/// This uses [`reshape_with_limit`] on the underlying slice and [`expand_labels`]
313311
/// to generate labels for each factored dimension.
314312
///
315313
/// # Arguments
@@ -325,7 +323,7 @@ pub fn view_limit(slice: &Slice, limit: Limit) -> Slice {
325323
/// occur unless the reshaped slice and labels are inconsistent (a
326324
/// programming logic error).
327325
pub fn reshape_shape(shape: &Shape, limit: Limit) -> ReshapedShape {
328-
let reshaped_slice = shape.slice().view_limit(limit);
326+
let reshaped_slice = shape.slice().reshape_with_limit(limit);
329327
let original_labels = shape.labels();
330328
let original_sizes = shape.slice().sizes();
331329

@@ -446,7 +444,7 @@ mod tests {
446444
#[test]
447445
fn test_reshape_split_1d_row_major() {
448446
let s = Slice::new_row_major(vec![1024]);
449-
let reshaped = s.view_limit(Limit::from(8));
447+
let reshaped = s.reshape_with_limit(Limit::from(8));
450448

451449
assert_eq!(reshaped.offset(), 0);
452450
assert_eq!(reshaped.sizes(), &vec![8, 8, 8, 2]);
@@ -462,7 +460,7 @@ mod tests {
462460
#[test]
463461
fn test_reshape_6_with_limit_2() {
464462
let s = Slice::new_row_major(vec![6]);
465-
let reshaped = view_limit(&s, Limit::from(2));
463+
let reshaped = reshape_with_limit(&s, Limit::from(2));
466464
assert_eq!(factor_dims(s.sizes(), Limit::from(2)), vec![vec![2, 3]]);
467465
assert_layout_preserved!(&s, &reshaped);
468466
}
@@ -471,7 +469,7 @@ mod tests {
471469
fn test_reshape_identity_noop_2d() {
472470
// All dimensions ≤ limit.
473471
let original = Slice::new_row_major(vec![4, 8]);
474-
let reshaped = original.view_limit(Limit::from(8));
472+
let reshaped = original.reshape_with_limit(Limit::from(8));
475473

476474
assert_eq!(reshaped.sizes(), original.sizes());
477475
assert_eq!(reshaped.strides(), original.strides());
@@ -491,7 +489,7 @@ mod tests {
491489
fn test_reshape_empty_slice() {
492490
// 0-dimensional slice.
493491
let original = Slice::new_row_major(vec![]);
494-
let reshaped = view_limit(&original, Limit::from(8));
492+
let reshaped = reshape_with_limit(&original, Limit::from(8));
495493

496494
assert_eq!(reshaped.sizes(), original.sizes());
497495
assert_eq!(reshaped.strides(), original.strides());
@@ -504,7 +502,7 @@ mod tests {
504502
fn test_reshape_mixed_dims_3d() {
505503
// 3D slice with one dimension exceeding the limit.
506504
let original = Slice::new_row_major(vec![6, 8, 10]);
507-
let reshaped = original.view_limit(Limit::from(4));
505+
let reshaped = original.reshape_with_limit(Limit::from(4));
508506

509507
assert_eq!(
510508
factor_dims(original.sizes(), Limit::from(4)),
@@ -519,7 +517,7 @@ mod tests {
519517
fn test_reshape_all_large_dims() {
520518
// 3D slice with all dimensions exceeding the limit.
521519
let original = Slice::new_row_major(vec![12, 18, 20]);
522-
let reshaped = original.view_limit(Limit::from(4));
520+
let reshaped = original.reshape_with_limit(Limit::from(4));
523521

524522
assert_eq!(
525523
factor_dims(original.sizes(), Limit::from(4)),
@@ -534,7 +532,7 @@ mod tests {
534532
fn test_reshape_split_1d_factors_3_3_2_2() {
535533
// 36 = 3 × 3 × 2 × 2.
536534
let original = Slice::new_row_major(vec![36]);
537-
let reshaped = view_limit(&original, Limit::from(3));
535+
let reshaped = reshape_with_limit(&original, Limit::from(3));
538536

539537
assert_eq!(
540538
factor_dims(original.sizes(), Limit::from(3)),
@@ -548,7 +546,7 @@ mod tests {
548546
fn test_reshape_large_prime_dimension() {
549547
// Prime larger than limit, cannot be factored.
550548
let original = Slice::new_row_major(vec![7]);
551-
let reshaped = view_limit(&original, Limit::from(4));
549+
let reshaped = reshape_with_limit(&original, Limit::from(4));
552550

553551
// Should remain as-is since 7 is prime > 4
554552
assert_eq!(factor_dims(original.sizes(), Limit::from(4)), vec![vec![7]]);
@@ -561,7 +559,7 @@ mod tests {
561559
fn test_reshape_split_1d_factors_5_3_2() {
562560
// 30 = 5 × 3 × 2, all ≤ limit.
563561
let original = Slice::new_row_major(vec![30]);
564-
let reshaped = view_limit(&original, Limit::from(5));
562+
let reshaped = reshape_with_limit(&original, Limit::from(5));
565563

566564
assert_eq!(
567565
factor_dims(original.sizes(), Limit::from(5)),
@@ -577,7 +575,7 @@ mod tests {
577575
fn test_reshape_factors_2_6_2_8_8() {
578576
// 12 = 6 × 2, 64 = 8 × 8 — all ≤ 8
579577
let original = Slice::new_row_major(vec![2, 12, 64]);
580-
let reshaped = original.view_limit(Limit::from(8));
578+
let reshaped = original.reshape_with_limit(Limit::from(8));
581579

582580
assert_eq!(
583581
factor_dims(original.sizes(), Limit::from(8)),
@@ -593,7 +591,7 @@ mod tests {
593591
fn test_reshape_all_dims_within_limit() {
594592
// Original shape: [2, 3, 4] — all ≤ limit (4).
595593
let original = Slice::new_row_major(vec![2, 3, 4]);
596-
let reshaped = original.view_limit(Limit::from(4));
594+
let reshaped = original.reshape_with_limit(Limit::from(4));
597595

598596
assert_eq!(
599597
factor_dims(original.sizes(), Limit::from(4)),
@@ -610,7 +608,7 @@ mod tests {
610608
fn test_reshape_degenerate_dimension() {
611609
// Degenerate dimension should remain unchanged.
612610
let original = Slice::new_row_major(vec![1, 12]);
613-
let reshaped = original.view_limit(Limit::from(4));
611+
let reshaped = original.reshape_with_limit(Limit::from(4));
614612

615613
assert_eq!(
616614
factor_dims(original.sizes(), Limit::from(4)),
@@ -633,7 +631,7 @@ mod tests {
633631

634632
// Reshape the selected slice using limit=2 in row-major
635633
// layout.
636-
let reshaped = selected.slice().view_limit(Limit::from(2));
634+
let reshaped = selected.slice().reshape_with_limit(Limit::from(2));
637635

638636
assert_eq!(
639637
factor_dims(selected.slice().sizes(), Limit::from(2)),
@@ -654,7 +652,7 @@ mod tests {
654652
let selected = original.select("host", 2).unwrap();
655653
// Reshape the selected slice using limit=2 in row-major
656654
// layout.
657-
let reshaped = selected.slice().view_limit(Limit::from(2));
655+
let reshaped = selected.slice().reshape_with_limit(Limit::from(2));
658656

659657
assert_layout_preserved!(selected.slice(), &reshaped);
660658
}
@@ -670,7 +668,7 @@ mod tests {
670668
let selected_host = selected_zone.select("host", 2).unwrap();
671669
assert_eq!(selected_host.slice().sizes(), &[1, 1, 5]);
672670
// Reshape with limit = 2.
673-
let reshaped = selected_host.slice().view_limit(Limit::from(2));
671+
let reshaped = selected_host.slice().reshape_with_limit(Limit::from(2));
674672

675673
assert_eq!(
676674
factor_dims(selected_host.slice().sizes(), Limit::from(2)),
@@ -694,7 +692,7 @@ mod tests {
694692
assert_eq!(selected_host.slice().sizes(), &[1, 1, 8]);
695693

696694
// Reshape with limit = 2 → gpu=8 should factor
697-
let reshaped = selected_host.slice().view_limit(Limit::from(2));
695+
let reshaped = selected_host.slice().reshape_with_limit(Limit::from(2));
698696

699697
assert_eq!(
700698
factor_dims(selected_host.slice().sizes(), Limit::from(2)),
@@ -748,7 +746,7 @@ mod tests {
748746
assert_eq!(reshaped.shape.labels(), &["gpu/0", "gpu/1", "gpu/2"]);
749747
assert_eq!(reshaped.shape.slice().sizes(), &[2, 2, 2]);
750748

751-
let expected = shape.slice().view_limit(Limit::from(2));
749+
let expected = shape.slice().reshape_with_limit(Limit::from(2));
752750
assert_eq!(reshaped.shape.slice(), &expected);
753751
}
754752

@@ -778,7 +776,7 @@ mod tests {
778776
);
779777
assert_eq!(reshaped.shape.slice().sizes(), &[2, 2, 2, 2]);
780778

781-
let expected = shape.slice().view_limit(Limit::from(2));
779+
let expected = shape.slice().reshape_with_limit(Limit::from(2));
782780
assert_eq!(reshaped.shape.slice(), &expected);
783781
}
784782

@@ -808,7 +806,7 @@ mod tests {
808806
assert_eq!(reshaped.shape.slice().sizes(), &[1, 1, 2, 2, 2]);
809807

810808
// Check against low-level equivalent reshaped slice
811-
let expected = selected_host.slice().view_limit(Limit::from(2));
809+
let expected = selected_host.slice().reshape_with_limit(Limit::from(2));
812810
assert_eq!(reshaped.shape.slice(), &expected);
813811
}
814812
}

0 commit comments

Comments
 (0)