@@ -78,49 +78,71 @@ def __init__(self, extra_permutable_ops: set | None = None) -> None:
7878 exir_ops .edge .aten .view .default ,
7979 )
8080
81- _UNSQUEEZE_OPS = (exir_ops .edge .aten .unsqueeze_copy .default ,)
81+ @staticmethod
82+ def _concrete_shape (node : torch .fx .Node ) -> list [int ] | None :
83+ """Return a node's shape, or None if it is missing or symbolic."""
84+ val = node .meta .get ("val" )
85+ if val is None :
86+ return None
87+ shape = val .shape
88+ if not all (isinstance (d , int ) for d in shape ):
89+ return None
90+ return [int (d ) for d in shape ]
8291
83- _SQUEEZE_OPS = (exir_ops .edge .aten .squeeze_copy .dim ,)
92+ def _view_shapes (self , node : torch .fx .Node ) -> tuple [list [int ], list [int ]] | None :
93+ """Concrete (input, output) shapes of a view op, else None."""
94+ if node .target not in self ._VIEW_OPS :
95+ return None
96+ inp = node .args [0 ] if node .args else None
97+ if not isinstance (inp , torch .fx .Node ):
98+ return None
99+ in_shape = self ._concrete_shape (inp )
100+ out_shape = self ._concrete_shape (node )
101+ if in_shape is None or out_shape is None :
102+ return None
103+ return in_shape , out_shape
84104
85105 _PAD_OPS = (
86106 exir_ops .edge .aten .constant_pad_nd .default ,
87107 exir_ops .edge .aten .pad .default ,
88108 )
89109
90110 @staticmethod
91- def _find_extra_one (longer : list [int ], shorter : list [int ]) -> int :
92- """If longer has exactly one more element of value 1, return its index. Else -1."""
93- if len (longer ) != len (shorter ) + 1 :
94- return - 1
95- for i in range (len (shorter )):
96- if longer [i ] != shorter [i ]:
97- if longer [i ] == 1 and shorter [i :] == longer [i + 1 :]:
98- return i
99- return - 1
100- return len (shorter ) if longer [- 1 ] == 1 else - 1
111+ def _find_extra_ones (longer : list [int ], shorter : list [int ]) -> list [int ] | None :
112+ """Positions in ``longer`` whose removal yields ``shorter``.
113+
114+ Every such position must hold a size-1 dim, so the two shapes differ by
115+ unit dims alone. Returns None when no such set of positions exists.
116+ """
117+ extra : list [int ] = []
118+ j = 0
119+ for i , dim in enumerate (longer ):
120+ if j < len (shorter ) and dim == shorter [j ]:
121+ j += 1
122+ continue
123+ if dim != 1 :
124+ return None
125+ extra .append (i )
126+ return extra if j == len (shorter ) else None
101127
102128 def _is_squeeze_unsqueeze_view (self , node : torch .fx .Node ) -> bool :
103- """Check if a node is a squeeze, unsqueeze, or view_copy that only
104- adds or removes a single dim of size 1."""
129+ """Check if a node is a view_copy that only adds or removes dims of
130+ size 1."""
105131 if node in self ._sq_unsq_cache :
106132 return self ._sq_unsq_cache [node ]
107133 result = self ._check_squeeze_unsqueeze_view (node )
108134 self ._sq_unsq_cache [node ] = result
109135 return result
110136
111137 def _check_squeeze_unsqueeze_view (self , node : torch .fx .Node ) -> bool :
112- if node .target in self ._UNSQUEEZE_OPS or node .target in self ._SQUEEZE_OPS :
113- return True
114- if node .target not in self ._VIEW_OPS :
138+ shapes = self ._view_shapes (node )
139+ if shapes is None :
115140 return False
116- inp = node .args [0 ]
117- assert isinstance (inp , torch .fx .Node )
118- in_shape = inp .meta ["val" ].shape
119- out_shape = node .meta ["val" ].shape
120- if len (out_shape ) == len (in_shape ) + 1 :
121- return self ._find_extra_one (out_shape , in_shape ) != - 1
122- if len (in_shape ) == len (out_shape ) + 1 :
123- return self ._find_extra_one (in_shape , out_shape ) != - 1
141+ in_shape , out_shape = shapes
142+ if len (out_shape ) > len (in_shape ):
143+ return self ._find_extra_ones (out_shape , in_shape ) is not None
144+ if len (in_shape ) > len (out_shape ):
145+ return self ._find_extra_ones (in_shape , out_shape ) is not None
124146 return False
125147
126148 def _is_permutation_sink_view (self , node : torch .fx .Node ) -> bool :
@@ -151,71 +173,45 @@ def _adapt_permute_across_view(
151173 Adapts from input-rank to output-rank space (downstream direction).
152174 Returns the adjusted permutation, or None if not possible.
153175 """
154- inp = node .args [0 ]
155- assert isinstance (inp , torch .fx .Node )
156- inp_val = inp .meta .get ("val" )
157- if inp_val is None :
176+ shapes = self ._view_shapes (node )
177+ if shapes is None :
158178 return None
159- in_shape = inp_val . shape
179+ in_shape , out_shape = shapes
160180 # ``permute`` must live in the view's input-rank space. It does not when
161181 # the view is reached by upstream traversal, where the permutation is
162- # expressed at the view's output rank; bail out so the caller drops the
163- # subgraph instead of indexing out of range or silently mis-adapting.
182+ # expressed at the view's output rank.
164183 if len (permute ) != len (in_shape ):
165184 return None
166185
167- # Handle explicit unsqueeze_copy(dim)
168- if node .target in self ._UNSQUEEZE_OPS :
169- dim = cast (int , node .args [1 ])
170- rank = len (permute )
171- index = dim if dim >= 0 else dim + rank + 1
172- # `index` is a POSITION in the permuted output; the un-permuted
173- # position the new dim lands at is the permutation VALUE there
174- # (or the end, when appending). permute_subgraph rewrites the dim
175- # arg to exactly this value, so the two must agree -- using `index`
176- # here instead silently desynchronises them whenever
177- # permute[index] != index.
178- inserted_value = permute [index ] if index < rank else rank
179- new_perm = [x + 1 if x >= inserted_value else x for x in permute ]
180- new_perm .insert (index , inserted_value )
186+ if len (out_shape ) > len (in_shape ):
187+ # unsqueeze: insert an identity mapping at each added position.
188+ # Positions are ascending and index into the output, so inserting
189+ # them in order lands each one at its final index.
190+ positions = self ._find_extra_ones (out_shape , in_shape )
191+ if positions is None :
192+ return None
193+ new_perm = list (permute )
194+ for index in positions :
195+ new_perm = [x + 1 if x >= index else x for x in new_perm ]
196+ new_perm .insert (index , index )
181197 return new_perm
182198
183- # Handle explicit squeeze_copy(dim)
184- if node .target in self ._SQUEEZE_OPS :
185- dim = cast (int , node .args [1 ])
186- rank = len (permute )
187- index = dim if dim >= 0 else dim + rank
188- # index is a POSITION in the tensor; the permutation VALUE at
199+ if len (in_shape ) > len (out_shape ):
200+ positions = self ._find_extra_ones (in_shape , out_shape )
201+ # Positions index into the node's input rank, which can differ from
202+ # the permutation's rank when the view is reached via upstream
203+ # traversal after an earlier rank change.
204+ if positions is None or positions [- 1 ] >= len (permute ):
205+ return None
206+ # A position is a POSITION in the tensor; the permutation VALUE at
189207 # that position is the logical dim being removed.
190- squeezed_value = permute [index ]
191- new_perm = [
192- x - 1 if x > squeezed_value else x
208+ squeezed_values = { permute [p ] for p in positions }
209+ return [
210+ x - sum ( 1 for v in squeezed_values if v < x )
193211 for x in permute
194- if x != squeezed_value
212+ if x not in squeezed_values
195213 ]
196- return new_perm
197214
198- # Handle view_copy (squeeze/unsqueeze-like reshape)
199- out_shape = node .meta ["val" ].shape
200-
201- if len (out_shape ) == len (in_shape ) + 1 :
202- # unsqueeze: insert identity mapping at the new dim
203- index = self ._find_extra_one (out_shape , in_shape )
204- new_perm = [x + 1 if x >= index else x for x in permute ]
205- new_perm .insert (index , index )
206- return new_perm
207- elif len (in_shape ) == len (out_shape ) + 1 :
208- # squeeze via view_copy: find the squeezed dim and remove it
209- index = self ._find_extra_one (in_shape , out_shape )
210- # index is a POSITION in in_shape; the permutation VALUE at
211- # that position is the logical dim being removed.
212- squeezed_value = permute [index ]
213- new_perm = [
214- x - 1 if x > squeezed_value else x
215- for x in permute
216- if x != squeezed_value
217- ]
218- return new_perm
219215 return None
220216
221217 def call (self , graph_module : torch .fx .GraphModule ) -> PassResult : # noqa: C901
@@ -377,12 +373,13 @@ def visit( # noqa: C901
377373 return False
378374 elif user .op == "output" :
379375 return False
380- elif not self ._is_squeeze_unsqueeze_view (
381- user
382- ) and self ._is_permutation_sink_view (user ):
376+ elif self ._is_permutation_sink_view (user ):
383377 # The permutation dies at this reshape (see
384378 # _is_permutation_sink_view), so terminate the region here with
385379 # no compensating permute and no further downstream traversal.
380+ # Checked before the rank-change handling below: a sink always
381+ # terminates cleanly, whereas crossing it would leave the region
382+ # hunting for an end permute that layout-invariance made moot.
386383 continue
387384 elif not self .visit (
388385 user , subgraph , processed_nodes , downstream_end , downstream_start
@@ -526,20 +523,6 @@ def permute_subgraph(self, subgraph: Subgraph) -> bool: # noqa: C901
526523 self .update_pad (node , node_start_perm )
527524 elif node .target in self ._VIEW_OPS :
528525 self .update_view_copy (node , node_start_perm )
529- elif node .target in self ._UNSQUEEZE_OPS :
530- # unsqueeze dim is in output space (rank + 1)
531- dim = cast (int , node .args [1 ])
532- rank = len (node_start_perm )
533- index = dim if dim >= 0 else dim + rank + 1
534- if index < rank :
535- node .update_arg (1 , node_start_perm [index ])
536- else :
537- # Inserting at or beyond existing dims — position unchanged
538- node .update_arg (1 , index )
539- elif node .target in self ._SQUEEZE_OPS :
540- # squeeze dim is in input space (rank)
541- dim = get_arg (node , "dim" , int )
542- set_arg (node , "dim" , node_start_perm [dim ])
543526
544527 # Skip incoming permutes.
545528 for inp , out in subgraph .edges_in :
@@ -649,34 +632,35 @@ def update_view_copy(self, node: torch.fx.Node, start_permute: list[int]) -> Non
649632 After removing the start permute, the view's input is in the original
650633 (un-permuted) layout. Recompute the view's target shape accordingly.
651634 """
652- inp = node .args [0 ]
653- assert isinstance (inp , torch .fx .Node )
654-
655- in_shape = inp .meta ["val" ].shape
656- out_shape = node .meta ["val" ].shape
635+ shapes = self ._view_shapes (node )
636+ if shapes is None :
637+ return
638+ in_shape , out_shape = shapes
657639
658640 # Compute un-permuted input shape
659641 inverse_permute = [start_permute .index (i ) for i in range (len (start_permute ))]
660642 unpermuted_in = [in_shape [inverse_permute [i ]] for i in range (len (in_shape ))]
661643
662- if len (out_shape ) == len (in_shape ) + 1 :
663- # unsqueeze: find the inserted dim in the permuted output,
664- # then determine where it goes in the un-permuted layout
665- index = self ._find_extra_one (out_shape , in_shape )
666- if index != - 1 :
667- new_shape = list (unpermuted_in )
644+ if len (out_shape ) > len (in_shape ):
645+ # unsqueeze: the added unit dims keep their output positions in the
646+ # un-permuted layout too
647+ positions = self ._find_extra_ones (out_shape , in_shape )
648+ if positions is None :
649+ return
650+ new_shape = list (unpermuted_in )
651+ for index in positions :
668652 new_shape .insert (index , 1 )
669- node .update_arg (1 , new_shape )
670- elif len (in_shape ) == len (out_shape ) + 1 :
671- # squeeze: find the removed dim in the permuted input ,
672- # map it to the un-permuted position, and remove it
673- index = self ._find_extra_one (in_shape , out_shape )
674- if index != - 1 :
675- # Map the squeezed dim from permuted to un-permuted space
676- unpermuted_index = start_permute [ index ]
677- new_shape = list ( unpermuted_in )
678- del new_shape [unpermuted_index ]
679- node .update_arg (1 , new_shape )
653+ node .update_arg (1 , new_shape )
654+ elif len (in_shape ) > len (out_shape ):
655+ # squeeze: map each removed dim from permuted to un- permuted space ,
656+ # deleting from the back so earlier indices stay valid
657+ positions = self ._find_extra_ones (in_shape , out_shape )
658+ if positions is None or positions [ - 1 ] >= len ( start_permute ) :
659+ return
660+ new_shape = list ( unpermuted_in )
661+ for index in sorted (( start_permute [ p ] for p in positions ), reverse = True ):
662+ del new_shape [index ]
663+ node .update_arg (1 , new_shape )
680664
681665 def get_permutation (self , permute_node : torch .fx .Node ) -> list [int ] | None :
682666 assert permute_node .target == exir_ops .edge .aten .permute_copy .default
0 commit comments