@@ -13,7 +13,6 @@ use pyrefly_derive::Visit;
1313use pyrefly_derive:: VisitMut ;
1414use pyrefly_util:: display:: commas_iter;
1515
16- use crate :: class:: ClassType ;
1716use crate :: dimension:: Int ;
1817use crate :: dimension:: ShapeError ;
1918use crate :: dimension:: canonicalize;
@@ -28,140 +27,7 @@ use crate::tuple::Tuple;
2827use crate :: type_level_dsl:: TypeShapeDslDomain ;
2928use crate :: types:: Type ;
3029
31- // ============================================================================
32- // Shaped Array Types
33- // ============================================================================
34-
35- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
36- #[ derive( Visit , VisitMut , TypeEq ) ]
37- enum ShapedArrayShapeStorage {
38- Inline ( IntTuple ) ,
39- TupleCarrier { index : usize } ,
40- }
41-
42- /// A class instance with shape information.
43- /// Example: Tensor[[2, 3]] represents a 2x3 tensor
44- /// Example: Tensor (no brackets) represents a shapeless tensor (`IntTuple`)
45- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
46- #[ derive( Visit , VisitMut , TypeEq ) ]
47- pub struct ShapedArrayType {
48- /// Base shaped-array class (e.g., torch.Tensor)
49- pub base_class : ClassType ,
50- shape : ShapedArrayShapeStorage ,
51- }
52-
53- impl ShapedArrayType {
54- /// Create a shaped-array type with shape information.
55- pub fn new ( base_class : ClassType , shape : IntTuple ) -> Self {
56- Self {
57- base_class,
58- shape : ShapedArrayShapeStorage :: Inline ( shape) ,
59- }
60- }
61-
62- /// Create a shapeless shaped-array type (compatible with any shape).
63- pub fn shapeless ( base_class : ClassType ) -> Self {
64- Self {
65- base_class,
66- shape : ShapedArrayShapeStorage :: Inline ( IntTuple :: shapeless ( ) ) ,
67- }
68- }
69-
70- pub fn with_tuple_carrier_shape_arg ( mut self , index : usize ) -> Self {
71- self . shape = ShapedArrayShapeStorage :: TupleCarrier { index } ;
72- self
73- }
74-
75- pub fn to_type ( self ) -> Type {
76- Type :: ShapedArray ( Box :: new ( self ) )
77- }
78-
79- pub fn tuple_carrier_shape_arg_index ( & self ) -> Option < usize > {
80- match self . shape {
81- ShapedArrayShapeStorage :: Inline ( _) => None ,
82- ShapedArrayShapeStorage :: TupleCarrier { index } => Some ( index) ,
83- }
84- }
85-
86- pub fn set_tuple_carrier_shape_arg ( & mut self , index : usize ) {
87- self . shape = ShapedArrayShapeStorage :: TupleCarrier { index } ;
88- }
89-
90- pub fn shape ( & self ) -> IntTuple {
91- match & self . shape {
92- ShapedArrayShapeStorage :: Inline ( shape) => shape. clone ( ) ,
93- ShapedArrayShapeStorage :: TupleCarrier { index } => {
94- let shape_arg = self
95- . base_class
96- . targs ( )
97- . as_slice ( )
98- . get ( * index)
99- . expect ( "shape argument index should point to a class type argument" ) ;
100- IntTuple :: from_shape_arg_or_tuple_carrier ( shape_arg)
101- . expect ( "registered shaped-array shape argument should project to IntTuple" )
102- }
103- }
104- }
105-
106- pub fn set_shape ( & mut self , shape : IntTuple ) {
107- match & mut self . shape {
108- ShapedArrayShapeStorage :: Inline ( stored_shape) => * stored_shape = shape,
109- ShapedArrayShapeStorage :: TupleCarrier { index } => {
110- let shape_arg = self
111- . base_class
112- . targs_mut ( )
113- . as_mut ( )
114- . get_mut ( * index)
115- . expect ( "shape argument index should point to a class type argument" ) ;
116- * shape_arg = shape. to_shape_arg_type ( ) ;
117- }
118- }
119- }
120-
121- /// Returns rank if shape is concrete, None for variadic/shapeless
122- pub fn rank ( & self ) -> Option < usize > {
123- match self . shape ( ) . view ( ) {
124- IntTupleView :: Concrete ( dims) => Some ( dims. len ( ) ) ,
125- IntTupleView :: Gradual | IntTupleView :: Unpacked { .. } => None ,
126- }
127- }
128-
129- /// Returns true if the shaped array has no shape information.
130- /// (represented as a gradual `IntTuple`)
131- pub fn is_shapeless ( & self ) -> bool {
132- is_shapeless ( & self . shape ( ) )
133- }
134-
135- /// Materialize gradual dimensions of an inline shape (see `Int::materialize`).
136- /// `TupleCarrier` shapes store their dimensions as `Type` arguments on
137- /// `base_class`, so they are materialized by the generic `Type::materialize`
138- /// traversal instead.
139- pub fn materialize_inline_shape ( & mut self ) {
140- if let ShapedArrayShapeStorage :: Inline ( shape) = & mut self . shape {
141- shape. materialize ( ) ;
142- }
143- }
144- }
145-
146- impl Display for ShapedArrayType {
147- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
148- let shape = self . shape ( ) ;
149- if is_shapeless ( & shape) {
150- write ! ( f, "{}" , self . base_class. name( ) )
151- } else if self . tuple_carrier_shape_arg_index ( ) . is_some ( ) {
152- write ! (
153- f,
154- "{}[{}]" ,
155- self . base_class. name( ) ,
156- fmt_tuple_carrier( & shape)
157- )
158- } else {
159- write ! ( f, "{}[{}]" , self . base_class. name( ) , shape)
160- }
161- }
162- }
163-
164- /// Shape of a shaped array.
30+ /// An integer tuple describing an array shape.
16531///
16632/// The storage is deliberately not a `Tuple`: fixed dimensions are always
16733/// canonical `Int`s, while variadic middles carry the original tuple/type
@@ -729,32 +595,6 @@ fn fmt_unpacked_middle(middle: &Type) -> String {
729595 }
730596}
731597
732- fn fmt_tuple_carrier ( shape : & IntTuple ) -> String {
733- match shape. view ( ) {
734- IntTupleView :: Concrete ( dims) => {
735- format ! ( "[{}]" , commas_iter( || dims. iter( ) ) )
736- }
737- IntTupleView :: Gradual => {
738- // No unbounded shape reaches here: the only caller (`Display`) handles
739- // the shapeless `IntTuple` case before calling this.
740- unreachable ! ( "shapeless IntTuple is handled before fmt_tuple_carrier" )
741- }
742- IntTupleView :: Unpacked {
743- prefix,
744- middle,
745- suffix,
746- } => {
747- if prefix. is_empty ( ) && suffix. is_empty ( ) && is_tuple_carrier_shape_middle ( middle) {
748- return middle. to_string ( ) ;
749- }
750- let mut parts: Vec < String > = prefix. iter ( ) . map ( |d| d. to_string ( ) ) . collect ( ) ;
751- parts. push ( format ! ( "*{}" , fmt_unpacked_middle( middle) ) ) ;
752- parts. extend ( suffix. iter ( ) . map ( |d| d. to_string ( ) ) ) ;
753- format ! ( "[{}]" , parts. join( ", " ) )
754- }
755- }
756- }
757-
758598// ============================================================================
759599// Tuple-carrier conversion
760600// ============================================================================
0 commit comments