+
    +þÎj‹5  ã                   óà   € R t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIt^ RIH	t	 RR.t
]! 0 Rm4      tR R ltR R ltR	 R
 lt]! 4       3R R lltR R ltR R ltR]! 4       RR/R R lltR# )a’  Canonicalize an FX graph's node order and names.

Provides two passes:

- ``canonicalize_graph``: reorders nodes into a deterministic topological order
  using Kahn's algorithm with a caller-supplied canonical key function and
  barrier predicate.
- ``rename_nodes_to_canonical``: renames all nodes to canonical names derived
  from their target, using the same naming scheme as ``Graph.create_node``.
N)ÚCallableÚcanonicalize_graphÚrename_nodes_to_canonicalc                óÌ   € V ^8„  d   QhR\         P                  R\        \         P                  \        3,          R\        \        \
        \        \        R3,          3,          /# )é   ÚnodeÚcanonical_idxÚreturn.)ÚfxÚNodeÚdictÚintÚtupleÚstr)Úformats   "ÚR/opt/data/home/.local/lib/python3.14/site-packages/torch/fx/passes/canonicalize.pyÚ__annotate__r   -   sN   € ÷ Fñ FÜ
�'‰'ðFÜ"&¤r§w¡w´ |Õ"4ðFä
Œ3””Uœ3 ˜8•_Ð$Õ%ñFó    c                óà   a€ \         ;QJ d!    . V3R lV P                   4       F  NK  	  5M! V3R lV P                   4       4      p^V P                  P                  V P                  4      V3# )zVCanonical heap key for a computation node (call_function / call_method / call_module).c              3   ó6   <"  € T F  pSV,          x € K  	  R # 5i©N© )Ú.0Únr   s   & €r   Ú	<genexpr>Ú(_computation_node_key.<locals>.<genexpr>1   s   øé € ÐIÑ4H¨q˜-¨×*Ò*Ó4Hùs   ƒ)r   Úall_input_nodesÚgraphÚ_target_to_strÚtarget)r   r   Úinput_indicess   &f r   Ú_computation_node_keyr!   -   sO   ø€ ÷ ”EÔI°D×4HÒ4HÓI—E‘EÔI°D×4HÒ4HÓIÓI€MØˆt�z‰z×(Ñ(¨¯©Ó5°}ÐEÐEr   c                ó†   € V ^8„  d   QhR\         P                  R\        \         P                  \        3,          R\        /# )r   r   r   r	   )r
   r   r   r   Úobject)r   s   "r   r   r   5   s2   € ÷ :ñ :œbŸg™gð :´d¼2¿7¹7ÄC¸<Õ6Hð :ÌVñ :r   c                óÆ   € V P                   R8X  d   \        R4      hV P                   R8X  d   ^\        V P                  4      3# V P                   R8X  d   R# \	        W4      # )zÆCanonical heap key for get_attr, output, and computation nodes.

Callers must handle placeholder nodes themselves (the ordering strategy
differs between Dynamo and export) and never pass them here.
Úplaceholderz0callers must handle placeholder nodes themselvesÚget_attrÚoutput)é   )ÚopÚAssertionErrorr   r   r!   )r   r   s   &&r   Ú_canonical_node_keyr+   5   sX   € ð ‡w�w�-ÔÜÐOÓPÐPØ‡w�w�*ÔØ”3�t—{‘{Ó#Ð$Ð$Ø	�‰�HÔ	Øˆä$ TÓ9Ð9r   c                óD   € V ^8„  d   QhR\         P                  R\        /# ©r   r   r	   )r
   r   Úbool)r   s   "r   r   r   E   s   € ÷ )ñ )œbŸg™gð )¬$ñ )r   c                óæ  € V P                   R8X  d!   V P                  P                  R4      '       * # V P                   R8X  d   V P                  4       '       * # V P                   R8w  d   R# V P                  4       '       d   R# \	        V P                  \
        P                  P                  4      '       g»   \        V P                  RR4      pVP                  R4      '       d   R# \        V P                  R	R4      R
8X  d   V\        9   d   R# \	        V P                  P                  R4      \        P                  4      '       d   R# VR8X  d   R# V P                  '       g   R# VR9   d   R# R# )a  Check if a node is safe to reorder during graph canonicalization.

Builds on Node.is_impure() (used by DCE) with two additional checks for
cases it doesn't cover: in-place call_method nodes and non-OpOverload
state-changing functions detected by a no-node-arguments heuristic.
Úcall_methodÚ_Úcall_moduleÚcall_functionTFÚ__name__Ú Ú
__module__Ú	_operatorÚoutÚtriton_kernel_wrapper_mutation)Ú_add_batch_dimÚ_remove_batch_dim)r)   r   ÚendswithÚ	is_impureÚ
isinstanceÚtorchÚ_opsÚ
OpOverloadÚgetattrÚ_IN_PLACE_OPERATORSÚkwargsÚgetr
   r   r   )r   Únames   & r   Ú_is_safe_to_reorderrG   E   s  € ð ‡w�w�-ÔØ—;‘;×'Ñ'¨Ó,Ô,Ð,Ø‡w�w�-ÔØ—>‘>Ó#Ô#Ð#Ø‡w�w�/Ô!ÙØ‡~�~×ÒÙÜ�d—k‘k¤5§:¡:×#8Ñ#8×9Ò9Ü�t—{‘{ J°Ó3ˆØ�=‰=˜×ÒÙä�D—K‘K ¨rÓ2°kÔAØÔ+Ô+áÜ�d—k‘k—o‘o eÓ,¬b¯g©g×6Ò6Ùð Ð3Ô3Ùð ×#×#Ð#ÙàÐ:Ô:ÙÙr   c                óˆ   € V ^8„  d   QhR\         P                  R\        \        ,          R\        \        \        3,          /# )r   r   Úskip_opsr	   )r
   ÚGraphÚ	frozensetr   r   )r   s   "r   r   r   q   s6   € ÷  ñ  Ü�8‰8ð äœ�nð ô 
Œ#Œsˆ(…^ñ r   c                óD  € ^ RI Hp / pV! 4       pV P                   Fz  pVP                  V9   d   VP	                  VP
                  V4       K2  VP
                  pV P                  VP                  4      pVP	                  Wu4      pW†8w  d   WƒV&   W…n        K|  	  W@n        V# )aý  Rename all nodes in the graph to canonical names based on their target.

Uses the same naming scheme as FX ``Graph.create_node`` (auto-generated
names from the target string). After renaming, replaces the graph's
namespace so future node creation stays consistent.

Args:
    graph: The FX graph whose nodes to rename.
    skip_ops: Node ops to skip renaming (their names are reserved in the
        new namespace but left unchanged).

Returns a mapping from old name to new name for nodes that were renamed.
)Ú
_Namespace)	Útorch.fx.graphrM   Únodesr)   Úcreate_namerF   r   r   Ú_graph_namespace)	r   rI   rM   ÚrenamedÚnsr   Úold_nameÚ	candidateÚnew_names	   &&       r   r   r   q   s�   € õ" *à €GÙ	‹€BØ—”ˆØ�7‰7�hÔØ�N‰N˜4Ÿ9™9 dÔ+ÙØ—9‘9ˆØ×(Ñ(¨¯©Ó5ˆ	Ø—>‘> )Ó2ˆØÔØ (�HÑØŽ	ñ ð  ÔØ€Nr   c                óR   € V ^8„  d   QhR\         \        P                  ,          RR/# ©r   Úorderr	   N©Úlistr
   r   )r   s   "r   r   r   ”   s"   € ÷  ñ  ¤¤R§W¡W¥ð  °$ñ  r   c                óZ  a	a
€ V  Uu. uF  qP                   R8w  g   K  VNK  	  upo	V  Uu. uF  qP                   R8X  g   K  VNK  	  ppV'       g   R# \        S	4       UUu/ uF  w  r1WbK	  	  uppo
\        P                  ! \        4      pV FŠ  pVP
                  '       d"   \        V	V
3R lVP
                   4       4      pM=S	'       d+   S	R,          P                   R8X  d   \        S	4      ^,
          M
\        S	4      pWF,          P                  V4       KŒ  	  V P                  4        \        S	4       F7  w  r7V P                  VP                  VR4      4       V P                  V4       K9  	  VP                  4        F  pV P                  V4       K  	  R# u upi u upi u uppi )a$  Move each get_attr node to right before its earliest consumer.

By default, Kahn's algorithm places get_attr nodes (which have no data
dependencies) at the top of the graph.  This post-processing step sinks
each one to just before its first consumer, keeping definitions close to
their uses.
r&   Nc              3   óZ   <"  € T F   pSP                  V\        S4      4      x € K"  	  R # 5ir   )rE   Úlen)r   ÚuÚnon_gaÚposs   & €€r   r   Ú'_sink_get_attr_nodes.<locals>.<genexpr>¥   s#   øé € ÐC¹(°Q˜Ÿ™ ¤C¨£K×0Ð0»(ùs   ƒ(+r'   éÿÿÿÿr   )r)   Ú	enumerateÚcollectionsÚdefaultdictr[   ÚusersÚminr^   ÚappendÚclearÚextendÚpopÚvalues)rY   r   ÚgasÚiÚinsertsÚgar   r   Ú	remainingr`   ra   s   &        @@r   Ú_sink_get_attr_nodesrs   ”   sF  ù€ ñ Ó5™�A§$¡$¨*Ñ"4�aˆa™Ñ5€FÙÓ
2‘e�Ÿt™t zÑ1�1ˆ1‘e€CÐ
2ßÙä% fÔ-Ô
.Ñ-‘D�Aˆ1Š4Ñ-Ò
.€CÜ(3×(?Ò(?ÄÓ(E€GÛˆØ�8�8ˆ8ÜÕC¸"¿(º(ÓCÓC‰F÷ $*¨f°R­j¯m©m¸xÔ.G”�F“˜a–ÌSÐQWË[ð ð 	�×Ñ˜rÖ"ñ ð 
‡K�K„MÜ˜VÖ$‰ˆØ�‰�W—[‘[  BÓ'Ô(Ø�‰�TÖñ %ð —^‘^Ö%ˆ	Ø�‰�YÖó &ùò) 6ùÚ
2ùó /s   ‡FžF«F"ÁF"Á"F'c                óR   € V ^8„  d   QhR\         \        P                  ,          RR/# rX   rZ   )r   s   "r   r   r   ´   s"   € ÷ 1ñ 1¤¤R§W¡W¥ð 1°$ñ 1r   c                ó‚  aaa€ \         P                  ! \        4      o\        4       pV  Fp  pVP                  R8X  g   K  VP
                  \        P                  J g   K6  SVP                  ^ ,          ,          P                  V4       VP                  V4       Kr  	  V'       g   R# R R lpSP                  4        F  pVP                  VR7       K  	  . oR VVV3R lloV  F  pW!9  g   K  S! V4       K  	  \        S4      \        V 4      8w  d%   \        R\        S4       R	\        V 4       R
24      hSV R&   R# )aÅ  Move each getitem node to immediately after its producer, in index order.

Serialization does not store getitem nodes as free-standing nodes; the
deserializer reconstructs them right after their producer, index-ordered
(see ``generate_getitems`` in ``torch/_export/serde/serialize.py``).  If
canonicalization leaves getitems interleaved with other computation, the
serialize/deserialize roundtrip produces a different node order.  This pass
normalizes getitem placement to match the deserializer so the canonical
form is roundtrip-stable.  Nested getitems (from list-typed outputs) are
emitted depth-first, matching the recursive reconstruction.

Relies on each getitem's producer (``args[0]``) being a node present in
``order``, and on a getitem sharing its producer's ``nn_module_stack``
scope (so pulling it adjacent stays within that scope).  Both hold for
graphs FX/export produce; the count assertion below guards against silent
node loss if they ever don't.
r3   Nc                óp   € V ^8„  d   QhR\         P                  R\        \        \        \        3,          /# r-   )r
   r   r   r   r   )r   s   "r   r   Ú*_group_getitem_nodes.<locals>.__annotate__Ï   s-   € ÷ Jñ J”R—W‘Wð J¤¤s¬C´ }Õ!5ñ Jr   c                 óz   € V P                   ^,          p\        V\        4      '       d   ^ VR3# ^^ \        V4      3# )é   r5   )Úargsr>   r   r   )r   Úidxs   & r   Ú_indexÚ$_group_getitem_nodes.<locals>._indexÏ   s7   € Ø�i‰i˜�lˆÜ)¨#¬s×3Ò3��3˜ˆ|ÐI¸!¸QÄÀCÃÐ9IÐIr   )Úkeyc                ó<   € V ^8„  d   QhR\         P                  RR/# )r   r   r	   N)r
   r   )r   s   "r   r   rw   Ø   s   € ÷ ñ ”B—G‘Gð  ñ r   c                 ól   <€ SP                  V 4       SP                  V R4       F  pS! V4       K  	  R # )Nr   )ri   rE   )r   ÚchildÚ_emitÚchildrenÚ	new_orders   & €€€r   r‚   Ú#_group_getitem_nodes.<locals>._emitØ   s-   ø€ Ø×Ñ˜ÔØ—\‘\ $¨Ö+ˆEÙ�%ŽLó ,r   zgetitem grouping lost nodes: z != z1 (a getitem's producer is missing from the order):NNN)re   rf   r[   Úsetr)   r   ÚoperatorÚgetitemrz   ri   Úaddrm   Úsortr^   r*   )rY   Úgetitemsr   r|   Úgroupr‚   rƒ   r„   s   &    @@@r   Ú_group_getitem_nodesr�   ´   s  ú€ ô$ .9×-DÒ-DÄTÓ-J€HÜ ›U€HÛˆØ�7‰7�oÖ%¨$¯+©+¼×9IÑ9IÕ*IØ�T—Y‘Y˜q•\Õ"×)Ñ)¨$Ô/Ø�L‰L˜Öñ ÷ ÙõJð —‘Ö"ˆØ�
‰
�vˆ
Öñ #ð  "€I÷ò ó
 ˆØÖÙ�$ŽKñ ô ˆ9ƒ~œ˜U›Ô#ÜØ+¬C°	«NÐ+;¸4ÄÀEÃ
¸|ð L?ð ?ó
ð 	
ð €Eˆ!ƒHr   Úskip_rename_opsÚgroup_getitemsFc                ó\  € V ^8„  d   QhR\         P                  R\        \         P                  \        \         P                  \
        3,          .\        3,          R\        \         P                  .\        3,          R\        \        ,          R\        R\        \        \        3,          /# )r   r   Úcanonical_key_fnÚis_safe_to_reorderrŽ   r�   r	   )
r
   rJ   r   r   r   r   r#   r.   rK   r   )r   s   "r   r   r   è   s“   € ÷ CFñ CFÜ�8‰8ðCFä¤§¡¬¬b¯g©g´s¨lÕ);Ð<¼fÐDÕEðCFô !¤"§'¡' ¬D Õ1ðCFô
 œs•^ðCFô ðCFô 
Œ#Œsˆ(…^ñCFr   c               ó˜  € V P                    Uu/ uF  qU\        VP                  4      bK  	  pp\        P                  ! \
        4      pRp. p	V P                    F­  pVP                  R9   d   K  V! V4      '       * p
V
'       d7   V	 F.  pW{,          P                  V4       We;;,          ^,          uu&   K0  	  . p	Ve,   Wx,          P                  V4       We;;,          ^,          uu&   V
'       d   TpKœ  V	P                  V4       K¯  	  / p^ p. pV P                    F4  pWe,          ^ 8X  g   K  VP                  V! W\4      WÕ34       V^,          pK6  	  \        P                  ! V4       . pV'       dÃ   \        P                  ! V4      w   ppVP                  V4       \        V4      VV&   \        P                  ! VP                  VP                  VR4      4       FR  pVV;;,          ^,          uu&   VV,          ^ 8X  g   K(  \        P                  ! VV! VV4      VV34       V^,          pKT  	  KÊ  \        V4      \        V P                   4      8w  dR   V Uu. uF  pVV,          ^ 8w  g   K  VNK  	  pp\!        R\        V4       R\        V P                   4       RV 24      h\#        V4       V'       d   \%        V4       V P&                  pVP(                  pVVJd3   VP(                  pVP*                  '       d   VP-                  4        TpK8  TpV F  pVP                  V4       TpK  	  \/        WR7      # u upi u upi )a&  Reorder graph nodes into a canonical topological order and rename them.

This ensures that structurally equivalent graphs produce identical node
names and ordering, regardless of the order in which nodes were originally
traced.

Uses Kahn's algorithm with a canonical tiebreaker provided by
``canonical_key_fn``.

Args:
    graph: The FX graph to canonicalize. Modified in-place.
    canonical_key_fn: ``(node, canonical_idx) -> comparable tuple``.
        Called when a node becomes ready.  ``canonical_idx`` maps already-
        ordered nodes to their position.  The returned tuple is used as the
        primary heap key.
    is_safe_to_reorder: ``(node) -> bool``.  Nodes for which this returns
        ``False`` act as barriers: they are chained in original order, and
        pure nodes are confined to their barrier segment.
    skip_rename_ops: Node ops to skip renaming. Skipped nodes keep their
        original names.
    group_getitems: If True, place getitem nodes immediately after their
        producer (index-ordered) so the order matches what serialization's
        deserializer reconstructs. Enable for graphs that get serialized
        (e.g. export) to keep the serialize/deserialize roundtrip stable.

Returns:
    A mapping from old node name to new node name for nodes that were
    renamed.
Nz#Canonicalization failed: processed z of z nodes. Remaining: )rI   )r%   r&   r'   r   )rO   r^   r   re   rf   r[   r)   ri   ÚheapqÚheapifyÚheappopÚ	itertoolsÚchainrg   rE   ÚheappushÚRuntimeErrorrs   r�   Ú_rootÚ_nextÚ_erasedÚ_remove_from_listr   )r   r‘   r’   rŽ   r�   r   ÚindegÚextra_usersÚprev_barrierÚsegment_reorderableÚ
is_barrierÚreorderabler   ÚcounterÚreadyÚcanonical_orderr1   ÚcurÚuserr   rr   ÚrootÚnxtÚcursors   &&&$$                   r   r   r   è   sá  € ðL 5:·K²Kó!Ù4?¨DŒc�$×&Ñ&Ó'Ò'±Kð 
ð !ô 1<×0GÒ0GÌÓ0M€KØ#'€LØ)+ÐØ—”ˆØ�7‰7Ð;Ô;ÙÙ+¨DÓ1Ô1ˆ
ßÛ2�ØÕ(×/Ñ/°Ô5Ø—˜qÕ •ñ  3ð #%ÐØÒ#ØÕ%×,Ñ,¨TÔ2Ø�K˜1Õ‹KßØŠLà×&Ñ& tÖ,ñ ð" )+€Mð €GØ/1€EØ—”ˆØ�;˜!ÖØ�L‰LÑ*¨4Ó?ÀÐOÔPØ�q�LŠGñ ô 
‡M‚M�%Ôà%'€Oç
Ü—M’M %Ó(‰	ˆˆ1ˆcØ×Ñ˜sÔ#Ü  Ó/ˆ�cÑä—O’O C§I¡I¨{¯©¸sÀBÓ/GÖHˆDØ�$�K˜1Õ‹KØ�T�{˜aÖÜ—’ØÙ% d¨MÓ:¸GÀTÐJôð ˜1•’ó Iô ˆ?Óœs 5§;¡;Ó/Ô/Ù %Ó7¡˜1¨¨q­°Q©—Q�Q¡ˆ	Ð7ÜØ1´#°oÓ2FÐ1GÀtÜ�5—;‘;ÓÐ Ð 3°I°;ð@ó
ð 	
ô
 ˜Ô)çÜ˜_Ô-ð �;‰;€DØ�:‰:€DØ
�dÓ
Ø�j‰jˆØ�<�<ˆ<Ø×"Ñ"Ô$ØŠð €FÛˆØ�‰�dÔØŠñ  ô % UÔEÐEùò}!ùò~ 8s   �MÉMÉ2M>   ÚiorÚiaddÚiandÚimodÚimulÚipowÚisubÚixorÚiconcatÚilshiftÚimatmulÚirshiftÚitruedivÚ	ifloordiv)Ú__doc__re   r”   r—   r‡   Úcollections.abcr   r?   Útorch.fxr
   Ú__all__rK   rC   r!   r+   rG   r   rs   r�   r   r   r   r   Ú<module>r¿      s’   ðñ	ó Û Û Û Ý $ã Ý ð  Ð!<Ð
=€ñ  òóÐ õ(Fõ:õ )ñ\  )›{÷ õF õ@1ðhCFñ
 '0£kðCFð !÷CFñ CFr   