Ë
    ÄÓ^j5§  ã                   óp  — d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	Z	d dl
mZ d dlmZmZ d dlmZ d dlmZ d d	lmZ g d
¢Z ej,                  e«      xZZ ed¬«       G d„ d«      «       Zde	j6                  j8                  dedefd„Z ed¬«      	 	 	 	 d-dddœdede	j6                  j8                  deegef   de eef   dz  de!dz  de!dz  de!dedz  de!defd„«       Z"deddfd„Z#	 d.d e	jH                  jJ                  jL                  d!e eef   dz  defd"„Z'ded#e eef   de(e ee	jH                  jJ                  jL                  f   e ee eef   f   e ee eef   f   e)e   f   fd$„Z*ded%e ee	jH                  jJ                  jL                  f   d&e ee eef   f   d'e ee eef   f   d(e)e   de ee eef   f   fd)„Z+	 d.ded%e ee	jH                  jJ                  jL                  f   d&e ee eef   f   d'e ee eef   f   d(e)e   dedz  defd*„Z, ed¬«      dd+œded#e eef   dedz  defd,„«       Z-y)/é    N)ÚdefaultdictÚOrderedDict)ÚCallable)ÚAny)Úcompatibility)Ú_LazyGraphModuleÚ_make_graph_module)Úlazy_format_graph_code)ÚGraphModule)ÚNode)Ú	PartitionÚsplit_moduleÚsplit_module_simpleT)Úis_backward_compatiblec                   ó(   — e Zd Zdeddfd„Zdefd„Zy)r   ÚnameÚreturnNc                 óâ   — || _         d|› �| _        g | _        i | _        i | _        i | _        i | _        t        j                  j                  j                  «       | _	        i | _        i | _        y )NÚsubmod_)r   Úsubmod_nameÚ
node_namesÚinputsÚoutputsÚdependenciesÚ
dependentsÚtorchÚfxÚgraphÚGraphÚenvironmentÚtargets)Úselfr   s     úg/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/split_module.pyÚ__init__zPartition.__init__   sc   € ØˆŒ	Ø$ T FÐ+ˆÔØ%'ˆŒØ')ˆŒØ(*ˆŒØ-/ˆÔØ+-ˆŒÜ+0¯8©8¯>©>×+?Ñ+?Ó+AˆŒ
Ø-/ˆÔØ')ˆ�ó    c                 ó¢   — d| j                   › d| j                  › d| j                  › d| j                  › d| j                  › d| j
                  › �S )Nzname: z
,
 nodes: z,
 inputs: z,
 outputs: z,
 partitions depended on: z,
 partition dependents: )r   r   r   r   r   r   )r"   s    r#   Ú__repr__zPartition.__repr__!   sb   € à�T—Y‘Y�Kð  Ø—‘Ð'ð (ØŸ™�}ð %ØŸ™˜ð '(Ø(,×(9Ñ(9Ð':ð ;&Ø&*§o¡oÐ%6ð8ð	
r%   )Ú__name__Ú
__module__Ú__qualname__Ústrr$   r'   © r%   r#   r   r      s!   „ ð
*˜Sð 
* Tó 
*ð
˜#ô 
r%   r   ÚmodÚqualnamer   c                 ó„   — | }|j                  d«      D ])  }t        ||«      st        d|› d�«      ‚t        ||«      }Œ+ |S )Nú.zNode target z not found!)ÚsplitÚhasattrÚAttributeErrorÚgetattr)r-   r.   Úattr_valÚatoms       r#   Ú_get_attr_from_qualnamer7   ,   sP   € Ø€HØ—‘˜sÓ#ò +ˆÜ�x Ô&Ü  <°¨z¸Ð!EÓFÐFÜ˜8 TÓ*‰ð+ð €Or%   F)Úpartition_affixÚtuple_returnÚmÚroot_mÚsplit_callbackÚqualname_mapÚkeep_original_orderÚkeep_original_node_nameÚkeep_original_input_namer8   r9   c                óà!  ‡ ‡‡‡‡‡5‡B‡C‡D‡E‡F‡G‡H‡I‡J‡K‡L— t         j                  dt        d‰ d¬«      «       dt        dt        t
        t        f   dt        t
        t        j                  j                  j                  f   dt        t        t
        t        f   t        t
        t        j                  j                  j                  f   f   fˆCˆˆ fd	„}	d
dl}
i ŠJi ŠHi ŠLdt        dt        dz  ddfˆJˆLfd„ŠKdt        ddfˆˆJˆfd„}t        j                  j                  t        j                  j                  t        j                  j                   g}t#        «       }t#        «       }i }d}t%        «       }‰ j&                  j(                  D �]î  Š5‰5j*                  j-                  d«      x}�st/        |t        j0                  t        j2                  f«      rIt/        |j4                  j6                  x}|
j8                  «      r|‰Lvr‰5‰L|j4                  j6                  <   ‰5j:                  dv rŒ£ |‰5«       ‰5j:                  dk(  �r÷‰5j<                  |v �rè‰5j<                  t        j                  j                   u r“t?        ‰5j@                  «      dk7  r!tC        dt?        ‰5j@                  «      › �«      ‚t/        ‰5j@                  d
   tD        «      s$tC        dtG        ‰5j@                  d
   «      › �«      ‚‰5}t%         ‰‰5«      h«      ||<   �n/‰5j<                  t        j                  j                  u rStI        d„ ‰5j@                  D «       «      stC        d«      ‚|jK                  ‰5«       t%         ‰‰5«      h«      |‰5<   d|‰5<   n¶‰5j<                  t        j                  j                  u r�t?        ‰5j@                  «      dk7  r!tC        dt?        ‰5j@                  «      › �«      ‚|‰5j@                  d
      jK                   ‰‰5«      «       |jM                  ‰5j@                  d
   «       ‰5|‰5j@                  d
   <   |�||   jK                   ‰‰5«      «       |D ]  }||   jK                   ‰‰5«      «       Œ �Œñ tI        d„ |jO                  «       D «       «      stC        d«      ‚|jQ                  «       D ��ci c]  \  }}|tS        |«      “Œ }}}|jQ                  «       D ��ci c]  \  }}|tS        |«      “Œ }}}tT        jW                  tX        jZ                  «      r,tT        j                  d|«       tT        j                  d|«       tE        |«      xs tE        |«      }d}‰ j&                  j(                  D �]  Š5‰5‰H‰5j\                  <   ‰5j:                  dv rŒ"‰5j:                  dk(  r;t        j                  j&                  j_                  ‰5j@                  d
   ˆKfd „«       Œl|r  ‰‰5«      }||kD  rtC        d!|› d"|› �«      ‚|}‰5j<                  |vsŒ�t        j                  j&                  j_                  ‰5j@                  ˆ5ˆKfd#„«       t        j                  j&                  j_                  ‰5j`                  ˆ5ˆKfd$„«       �Œ tc        ‰Jje                  «       «      }g }‰JjQ                  «       D ],  \  }ŠIt?        ‰Ijf                  «      rŒ|ji                  |«       Œ. g }|rw|jk                  «       }|ji                  |«       ‰J|   jl                  D ]A  }‰J|   jf                  jk                  |«       ‰J|   jf                  rŒ1|ji                  |«       ŒC |rŒwt?        |«      t?        ‰J«      k7  rto        d%«      ‚||fD ]ï  } | jQ                  «       D ]Ú  \  Š5}!t?        |!«      d
k(  rtC        d&«      ‚‰5‰Jt        |!d
   «         jp                  ‰5<   |!dd D ]–  }"‰Jt        |"«         ŠI‰Ij&                  js                  ‰5j:                  ‰5j<                  t        d'„ ‰5j@                  D «       «      i ‰5jF                  ¬(«      }#‰5j*                  ju                  «       |#_        |#‰Ijp                  ‰5<   Œ˜ ŒÜ Œñ |D �]Ù  }‰J|   ŠIi ŠGd
ŠDg }$g }%‰Ijv                  D ]‹  ŠF‰H‰F   }&|&j:                  d)k(  rdt/        |&j<                  t
        «      rJt/        ty        ‰ |&j<                  «      t        jz                  j|                  «      r|%ji                  ‰F«       Œ{|$ji                  ‰F«       Œ� |$|%z   D �]  ŠF‰H‰F   }&dt        fˆDˆFˆˆGˆHˆIfd*„}'|&j:                  d)k(  r¼t/        |&j<                  t
        «      s!tC        d+tG        |&j<                  «      › �«      ‚ty        ‰ |&j<                  «      }(t/        |(t        jz                  j|                  «      r?‰Ij&                  j                  |&j<                  «      })|(‰Ij€                  |&j<                  <   n |'«       })n |'«       })‰H‰F   j*                  ju                  «       |)_        |)‰Ijp                  ‰H‰F   <   �Œ" ‰G‰I_;        �ŒÜ ‰ j&                  j(                  D �]Ø  Š5tƒ        ‰5d,«      sŒ‰J‰5j„                     ŠI‰Ijp                  ŠEt        j                  j&                  j_                  ‰5j@                  ˆEfd-„«      }*t        j                  j&                  j_                  ‰5j`                  ˆEfd.„«      }+‰5j:                  d/vr‰5j<                  },ncty        ‰ ‰5j<                  «      }-‰5j<                  j‡                  d0d1«      },|-‰Ij€                  |,<   |� ‰Ijˆ                  › d0|,› �}.‰5j<                  ||.<   t/        |*t        «      stC        d2tG        |*«      › �«      ‚t/        |+t        «      stC        d3tG        |+«      › �«      ‚‰r‰5j\                  nd}/‰Ij&                  js                  ‰5j:                  |,|*|+‰5jF                  |/¬4«      }#‰5j*                  ju                  «       |#_        |#‰Ijp                  ‰5<   �ŒÛ |fD ]Å  } t‹        | «      D ]µ  Š5| ‰5   }!t?        |!«      d
k(  rtC        d5«      ‚|!dd D ]�  }"‰Jt        |"«         ŠI|‰5   }0|0€tC        d6«      ‚‰Ij&                  js                  |0j:                  |0j<                  ‰Ijp                  ‰5   fi |0jF                  ¬(«      }#|0j*                  ju                  «       |#_        Œ� Œ· ŒÇ i }1i ŠBt        j                  j&                  j�                  «       ŠCi }2|s)‰ j&                  j(                  D ]  Š5 |	‰5‰B|2«      \  ŠB}2Œ n*‰ j&                  j(                  D ]  Š5‰5|1‰5j\                  <   Œ |s|n|}3t%        «       }4‰ j&                  j(                  D �5cg c]  }5|5j:                  d7k(  sŒ|5‘Œ }6}5|3D �]ï  }‰J|   ŠIt        ˆHˆIfd8„‰IjŽ                  D «       «      }7t?        |7«      dk(  r!|s‰Ij&                  j‘                  |7d
   «       n‰Ij&                  j‘                  |7«       |rt‰Ijv                  D �8cg c]  }8|8|6vr|1|8   ‘Œ }9}8|6D ]%  Š5‰5|4v rŒ |	‰5‰B|2«      \  ŠB}:|4jK                  ‰5«       Œ' |9D ]%  Š5‰5|4v rŒ |	‰5‰B|2«      \  ŠB}2|4jK                  ‰5«       Œ' t“        ‰Ij€                  ‰Ij&                  «      |2‰Ijˆ                  <   ‰Cj•                  ‰Ijˆ                  t        ˆBfd9„‰Ijv                  D «       «      «      };t?        ‰IjŽ                  «      }<|<dkD  s|<dk(  r\|rZt        j                  j–                  j™                  |;«      }=t›        ‰IjŽ                  «      D ]  \  }>}?|=|>   j4                  ‰B|?<   Œ �ŒÈ|<dk(  s�ŒÏ|;‰Bt�        tŸ        ‰IjŽ                  «      «      <   �Œò |r*‰Bs(‰ j&                  j(                  D ]  Š5 |	‰5‰B|2«      \  ŠB}2Œ ‰ j&                  j(                  D ][  Š5‰5j:                  dk(  sŒ‰Cj‘                  t        j                  j&                  j_                  ‰5j@                  d
   ˆBfd:„«      «       Œ] t“        |2‰C«      }@‰JjO                  «       D �Ach c]  }A|Ajˆ                  ’Œ c}A@j*                  d;<   t         j                  dt        d<|@d¬«      «       |@S c c}}w c c}}w c c}5w c c}8w c c}Aw )=a’  
    Creates subgraphs out of main graph

    Args:
        m (GraphModule): Graph module to split
        root_m (torch.nn.Module): root nn module. Not currently used. Included
            because the root nn module is usually transformed via
            torch.fx._symbolic_trace.symbolic_trace (see example below)
        split_callback (Callable[[Node], int]): Callable function
            that maps a given Node instance to a numeric partition identifier.
            split_module will use this function as the policy for which operations
            appear in which partitions in the output Module.
        qualname_map: Optional[Dict[str, str]]: optional output parameter that returns a
            mapping from new target names in the module after split to old target
            names in the original module.
        keep_original_order: Optional[bool]: keep the original order of the GraphModule
            or use the Topological order of the new constructed GraphModule
        keep_original_node_name: Optional[bool]: If the partitioned graphs should
            have the same node names as the original graph.
        keep_original_input_name: bool: If the partitioned graphs should
            have the same input names as the original graph.
        partition_affix: Optional[str]: If specified, the submodules' names will contain
            the affix, e.g. "submod_<affix>_<idx>".
        tuple_return: bool: If True, submodule outputs are always wrapped in a tuple,
            even when there is only a single output value.  This makes all subgraphs
            conform to the convention expected by ``torch._inductor.compile_fx``.

    Returns:
        GraphModule: the module after split.

    Example:

        This is a sample setup:

            import torch
            from torch.fx._symbolic_trace import symbolic_trace
            from torch.fx.graph_module import GraphModule
            from torch.fx.node import Node
            from torch.fx.passes.split_module import split_module

            class MyModule(torch.nn.Module):
                def __init__(self) -> None:
                    super().__init__()
                    self.param = torch.nn.Parameter(torch.rand(3, 4))
                    self.linear = torch.nn.Linear(4, 5)

                def forward(self, x, y):
                    z = self.linear(x + self.param).clamp(min=0.0, max=1.0)
                    w = self.linear(y).clamp(min=0.0, max=1.0)
                    return z + w

            # symbolically trace model
            my_module = MyModule()
            my_module_traced = symbolic_trace(my_module)

            # random mod partitioning
            partition_counter = 0
            NPARTITIONS = 3

            def mod_partition(node: Node):
                global partition_counter
                partition = partition_counter % NPARTITIONS
                partition_counter = (partition_counter + 1) % NPARTITIONS
                return partition

            # split module in module with submodules
            module_with_submodules = split_module(
                my_module_traced, my_module, mod_partition
            )

        Output looks like this. Original graph is broken into partitions

            > print(module_with_submodules)
            GraphModule(
                (submod_0): GraphModule(
                    (linear): Linear(in_features=4, out_features=5, bias=True)
                )
                (submod_1): GraphModule(
                    (linear): Linear(in_features=4, out_features=5, bias=True)
                )
                (submod_2): GraphModule()
            )

            def forward(self, x, y):
                param = self.param
                submod_0 = self.submod_0(x, param, y);  x = param = y = None
                getitem = submod_0[0]
                getitem_1 = submod_0[1];  submod_0 = None
                submod_1 = self.submod_1(getitem, getitem_1);  getitem = getitem_1 = None
                getitem_2 = submod_1[0]
                getitem_3 = submod_1[1];  submod_1 = None
                submod_2 = self.submod_2(getitem_2, getitem_3);  getitem_2 = getitem_3 = None
                return submod_2

        Output of split module is the same as output of input traced module.
        This is an example within a test setting:

            > orig_out = my_module_traced(x, y)
            > submodules_out = module_with_submodules(x, y)
            > self.assertEqual(orig_out, submodules_out)
            True
    z%szpre split_moduleT)ÚcoloredÚnodeÚbase_mod_envÚbase_mod_attrsr   c                 ó²  •— | j                   dk(  �r t        | j                  «      dkD  r| j                  d   nt        j                  j
                  }‰rX|t        j                  j
                  u rdn|f}‰j                  d| j                  || j                  ¬«      || j                  <   n5‰j                  | j                  | j                  |¬«      || j                  <   | j                  j                  «       || j                     _        ||fS | j                   dk(  r´‰j                  | j                  «      || j                  <   | j                  j                  «       || j                     _        t        | j                  t        «      s!t!        dt        | j                  «      › �«      ‚t#        ‰| j                  «      }||| j                  <   ||fS )NÚplaceholderr   r,   )ÚargsÚ	type_expr)rI   Údefault_valueÚget_attrúExpected str target, got )ÚopÚlenrH   ÚinspectÚ	SignatureÚemptyÚcreate_noder   ÚtyperG   ÚtargetÚmetaÚcopyrK   Ú
isinstancer+   ÚAssertionErrorr7   )	rC   rD   rE   rJ   rH   r5   Úbase_mod_graphr?   r:   s	         €€€r#   Úconstruct_graphz%split_module.<locals>.construct_graph¯   s�  ø€ ð
 �7‰7�mÓ#ä # D§I¡I£°Ò 2�—	‘	˜!’¼×8IÑ8I×8OÑ8Oð ñ 'à'¬7×+<Ñ+<×+BÑ+BÑB‘BÈÐHXð ð +9×*DÑ*DØ!Ø—I‘IØØ"Ÿi™ið	 +Eó +�˜TŸY™YÒ'ð +9×*DÑ*DØ—K‘KØ"Ÿi™iØ"/ð +Eó +�˜TŸY™YÑ'ð
 ,0¯9©9¯>©>Ó+;ˆL˜Ÿ™Ñ#Ô(ð ˜^Ð+Ð+ð �W‰W˜
Ò"Ø&4×&=Ñ&=¸d¿k¹kÓ&JˆL˜Ÿ™Ñ#Ø+/¯9©9¯>©>Ó+;ˆL˜Ÿ™Ñ#Ô(Ü˜dŸk™k¬3Ô/Ü$Ð'@ÄÀdÇkÁkÓARÐ@SÐ%TÓUÐUÜ.¨q°$·+±+Ó>ˆHØ*2ˆN˜4Ÿ;™;Ñ'Ø˜^Ð+Ð+r%   r   NÚdef_nodeÚuse_nodec                 ó¬  •— ddl m} t        | dd «      }t        |dd «      }t        j	                  d| j
                  ||�|j
                  nd|«       ||k7  �r||�G‰|   }|j                  j                  | j
                  «       |�|j                  j                  |«       |��/‰|   }|j                  j                  | j
                  «       | j                  j                  d«      x}�Ét         ||«      t        ¬«      D ]¯  }‰|   }	|j                  j                  |	j
                  «       ‰|   j                  dk7  sŒ@t        |	dd «      }
|
€ŒP‰|
   }|j                  j                  |	j
                  «       |j                  j                  |«       |j                  j                  |
«       Œ± |�|j                  j                  |«       y y y y )	Nr   ©Úfree_symbolsÚ_fx_partitionz*record_cross_partition_use %s (%s) %s (%s)ú-Úexample_value©ÚkeyrG   )Ú%torch.fx.experimental.symbolic_shapesr_   r4   ÚlogÚdebugr   r   Ú
setdefaultr   r   rU   ÚgetÚsortedr+   rM   r   )r[   r\   r_   ÚdefinedÚusedÚdef_partitionÚuse_partitionÚdef_valÚsÚs_nodeÚ	s_definedÚs_def_partitionÚ
partitionsÚsymbol_to_nodes               €€r#   Úrecord_cross_partition_usez0split_module.<locals>.record_cross_partition_useØ   s¸  ø€ ÝFä˜( O°TÓ:ˆÜ�x °$Ó7ˆä�	‰	Ø8Ø�M‰MØØ%Ð1ˆH�MŠM°sØô	
ð �d‹?ØÐ"Ø *¨7Ñ 3�Ø×%Ñ%×0Ñ0°·±Ô?ØÐ#Ø!×,Ñ,×7Ñ7¸Ô=àÑØ *¨4Ñ 0�Ø×$Ñ$×/Ñ/°·±Ô>ð  (Ÿ}™}×0Ñ0°ÓAÐA�GÐNÜ#¡L°Ó$9¼sÔCò Q˜Ø!/°Ñ!2˜Ø%×,Ñ,×7Ñ7¸¿¹ÔDØ)¨!Ñ,×/Ñ/°=Ó@ô )0°¸ÈÓ(N˜IØ(Ñ4Ø2<¸YÑ2G Ø /× 7Ñ 7× BÑ BÀ6Ç;Á;Ô OØ /× :Ñ :× EÑ EÀdÔ KØ -× :Ñ :× EÑ EÀiÕ Pð#Qð$ Ð&Ø!×.Ñ.×9Ñ9¸'ÕBð 'ð3  ð r%   c                 ó6  •—  ‰| «      }t        |«      }‰�dj                  ‰|g«      }t        j                  d| j                  |«       ‰j                  |«      }|€t        |«      x‰|<   }|j                  j                  | j                  «       || _	        y )NÚ_z*instantiate_node_partition_mapping %s (%s))
r+   Újoinrf   rg   r   ri   r   r   Úappendr`   )rC   Úpartition_idxÚpartition_nameÚ	partitionr8   rt   r<   s       €€€r#   Ú"instantiate_node_partition_mappingz8split_module.<locals>.instantiate_node_partition_mapping	  s“   ø€ Ù& tÓ,ˆÜ˜]Ó+ˆØÐ&ð !ŸX™X ¸Ð&GÓHˆNä�	‰	Ø8¸$¿)¹)À^ô	
ð
 —N‘N >Ó2ˆ	ØÐÜ5>¸~Ó5NÐNˆJ�~Ñ&¨à×Ñ×#Ñ# D§I¡IÔ.Ø+ˆÕr%   rb   )rG   rK   ÚoutputÚcall_functioné   z*Expected 1 arg for _set_grad_enabled, got zExpected bool arg, got c              3   ó>   K  — | ]  }t        |t        «       –— Œ y ­w©N)rW   r   ©Ú.0Úargs     r#   ú	<genexpr>zsplit_module.<locals>.<genexpr>Y  s   è ø€ ÒJ¸œz¨#¬tÓ4Ô4ÑJùs   ‚z3Expected all args to be python constants, not Nodesz'Expected 1 arg for _exit_autocast, got c              3   ó$   K  — | ]  }|d u–— Œ
 y ­wrƒ   r,   )r…   Úvs     r#   r‡   zsplit_module.<locals>.<genexpr>o  s   è ø€ Ò> ˆq˜Œ}Ñ>ùs   ‚zautocast must exitzautocast_regions: %szgrad_regions: %séÿÿÿÿ)rG   rK   r   c                 ó   •—  ‰| d «      S rƒ   r,   )Únrv   s    €r#   ú<lambda>zsplit_module.<locals>.<lambda>ˆ  s   ø€ Ñ(BÀ1ÀdÓ(K€ r%   zSautocast or set_grad_enabled require monotonically increasing partitions: highest: z, this node's: c                 ó   •—  ‰| ‰«      S rƒ   r,   ©r[   rC   rv   s    €€r#   r�   zsplit_module.<locals>.<lambda>™  s   ø€ Ñ,FÀxÐQUÓ,V€ r%   c                 ó   •—  ‰| ‰«      S rƒ   r,   r�   s    €€r#   r�   zsplit_module.<locals>.<lambda>œ  s   ø€ Ñ.HÈÐSWÓ.X€ r%   z cycle exists between partitions!z%Expected at least one region for nodec              3   ó    K  — | ]  }|–— Œ y ­wrƒ   r,   r„   s     r#   r‡   zsplit_module.<locals>.<genexpr>À  s   è ø€ Ò8 sœsÑ8ùs   ‚©rM   rT   rH   ÚkwargsrI   rK   c                  ó„   •— ‰r‰} n
d‰› �} ‰dz  Š‰j                   j                  | ‰‰   j                  ¬«      }d ‰‰<   |S )NÚarg_r�   ©rI   )r   rG   rS   )r   rG   ÚcounterÚinpr@   Ú
new_inputsÚ
orig_nodesr}   s     €€€€€€r#   Úadd_placeholderz%split_module.<locals>.add_placeholderç  s\   ø€ Ù+Ø‘Dð " ' Ð+�DØ˜q‘L�GØ'Ÿo™o×9Ñ9ØØ(¨™o×2Ñ2ð :ó �ð #'�
˜3‘Ø"Ð"r%   rL   r`   c                 ó   •— ‰|    S rƒ   r,   ©rŒ   r    s    €r#   r�   zsplit_module.<locals>.<lambda>  s   ø€ ÈÐTUÉ€ r%   c                 ó   •— ‰|    S rƒ   r,   r�   s    €r#   r�   zsplit_module.<locals>.<lambda>  s   ø€  {°1¡~€ r%   )Úcall_modulerK   r0   rx   z&Expected tuple for gathered_args, got z'Expected dict for gathered_kwargs, got )rM   rT   rH   r“   rI   r   zExpected at least one regionzMissing exit noderG   c              3   óB   •K  — | ]  }‰j                   ‰|      –— Œ y ­wrƒ   )r    )r…   r   rš   r}   s     €€r#   r‡   zsplit_module.<locals>.<genexpr>p  s&   øè ø€ ò 
Ø8<ˆI×!Ñ! *¨TÑ"2Õ3ñ
ùs   ƒc              3   ó(   •K  — | ]	  }‰|   –— Œ y ­wrƒ   r,   )r…   r   rD   s     €r#   r‡   zsplit_module.<locals>.<genexpr>™  s   øè ø€ ÒB¨�,˜tÕ$ÑBùó   ƒc                 ó"   •— ‰| j                      S rƒ   ©r   )rŒ   rD   s    €r#   r�   zsplit_module.<locals>.<lambda>³  s   ø€ ¸|ÈAÏFÉFÑ?S€ r%   Úpartition_nameszpost split_module)Prf   rg   r
   r   Údictr+   r   r   Úgraph_moduler   ÚtupleÚsympyÚampÚ_enter_autocastÚ_exit_autocastÚ_CÚ_set_grad_enabledr   Úsetr   ÚnodesrU   ri   rW   ÚSymIntÚSymFloatrC   ÚexprÚSymbolrM   rT   rN   rH   rX   ÚboolrS   ÚallÚaddÚremoveÚvaluesÚitemsrj   Ú_LOGGERÚisEnabledForÚloggingÚDEBUGr   Úmap_argr“   ÚlistÚkeysr   rz   Úpopr   ÚRuntimeErrorr    rR   rV   r   r7   ÚnnÚModulerK   r!   r2   r`   Úreplacer   Úreversedr   r   r   r	   rŸ   ÚproxyÚProxyÚ	enumerateÚnextÚiter)Mr:   r;   r<   r=   r>   r?   r@   r8   r9   rZ   r©   r~   ÚGLOBAL_STATE_NODESÚgrad_regionsÚautocast_regionsÚautocast_exitsÚactive_gradÚactive_autocastsÚvalÚs0ÚaÚkr‰   Úassert_monotonically_increasingÚhighest_partitionÚpidÚoriginal_partition_orderÚroot_partitionsr|   Úsorted_partitionsÚroot_partitionÚ	dependentÚregions_mappingÚregionsÚrÚnew_nodeÚplaceholder_inputsÚget_attr_inputsÚ	orig_noder›   Ú	orig_attrrG   Úgathered_argsÚgathered_kwargsrT   Útarget_attrr.   r   Ú	exit_nodeÚorig_mod_envrE   Úconstruct_order_partitionsÚalready_constructed_attr_nodesrC   Úoriginal_orderÚoutput_valsrd   Úorig_mod_attr_nodesÚ_based_mod_attrsÚ
output_valÚnum_outputsÚoutput_val_proxyÚiÚoutput_nameÚretÚprD   rY   r—   r    r˜   r™   rš   r}   rt   rv   ru   sM   ` `  ```                                             `            @@@@@@@@@@@r#   r   r   6   s¥  ÿÿø€ ôh ‡I�IØÜÐ1°1¸dÔCôð
!,Üð!,äœ3¤˜9‘oð!,ô œS¤%§(¡(×"7Ñ"7×"CÑ"CÐCÑDð!,ô 
Œt”Cœ�I‰¤¤S¬%¯(©(×*?Ñ*?×*KÑ*KÐ%KÑ LÐLÑ	M÷	!,óF à')€JØ"$€JØ/1€Nð/C¬Tð /C¼TÀD¹[ð /CÈTö /Cðb,´ð ,¸$÷ ,ô. 	�	‰	×!Ñ!Ü�	‰	× Ñ Ü�‰×"Ñ"ðÐô 1<³€Lô 5@³MÐØ.0€Nà€KÜ“uÐà—‘—‘ó 5:ˆð —I‘I—M‘M /Ó2Ð2ˆSÐ?Ü˜3¤§¡¬u¯~©~Ð >Ô?Ü §¡§¡Ð.˜2°·±Ô=Ø˜.Ñ(à,0ˆN˜3Ÿ8™8Ÿ=™=Ñ)à�7‰7Ð;Ñ;Øá*¨4Ô0à�7‰7�oÓ%¨$¯+©+Ð9KÒ*KØ�{‰{œeŸh™h×8Ñ8Ñ8Ü�t—y‘y“> QÒ&Ü(ØDÄSÈÏÉÃ^ÐDTÐUóð ô " $§)¡)¨A¡,´Ô5Ü(Ð+BÄ4ÈÏ	É	ÐRSÉÓCUÐBVÐ)WÓXÐXØ"�Ü,/±ÀÓ1EÐ0FÓ,G�˜[Ó)Ø—‘¤§	¡	× 9Ñ 9Ñ9äÑJÀÇ	Á	ÔJÔJÜ(ØMóð ð !×$Ñ$ TÔ*Ü),©n¸TÓ.BÐ-CÓ)DÐ  Ñ&Ø'+�˜tÒ$Ø—‘¤§	¡	× 8Ñ 8Ñ8Ü�t—y‘y“> QÒ&Ü(ØAÄ#ÀdÇiÁiÃ.ÐAQÐRóð ð ! §¡¨1¡Ñ.×2Ñ2±>À$Ó3GÔHØ ×'Ñ'¨¯	©	°!©Ô5Ø/3�˜tŸy™y¨™|Ñ,àÐ"Ø˜Ñ%×)Ñ)©.¸Ó*>Ô?à!ò 	:ˆAØ˜QÑ×#Ñ#¡N°4Ó$8Õ9ò	:ði5:ôn Ñ> n×&;Ñ&;Ó&=Ô>Ô>ÜÐ1Ó2Ð2ð 2B×1GÑ1GÓ1I×J©¨¨A˜œ6 !›9™ÐJÐÑJà-9×-?Ñ-?Ó-A×B¡T Q¨�A”v˜a“y‘LÐB€LÑBä×ÑœGŸM™MÔ*Ü�‰Ð,Ð.>Ô?Ü�‰Ð(¨,Ô7ä&*Ð+;Ó&<Ò&RÄÀ\Ó@RÐ#ð ÐØ—‘—‘ó ˆØ $ˆ
�4—9‘9Ñð �7‰7Ð1Ñ1ØØ�7‰7�hÒÜ�H‰H�N‰N×"Ñ"Ø—	‘	˜!‘ÓKôð á*Ù  Ó&ˆCØ  3Ò&Ü$ð,Ø,=Ð+>¸oÈcÈUðTóð ð !$Ðð �;‰;Ð0Ò0Ü�H‰H�N‰N×"Ñ"Ø—	‘	ÔVôô �H‰H�N‰N×"Ñ"Ø—‘ÔXöð9ô@  $ J§O¡OÓ$5Ó6Ðà!#€OØ%/×%5Ñ%5Ó%7ò 3Ñ!ˆ˜	Ü�9×)Ñ)Õ*Ø×"Ñ" >Õ2ð3ð
 $&ÐÙ
Ø(×,Ñ,Ó.ˆØ× Ñ  Ô0Ø# NÑ3×>Ñ>ò 	2ˆIØ�yÑ!×.Ñ.×2Ñ2°>ÔBØ˜iÑ(×5Ó5Ø×&Ñ& yÕ1ð	2ò ô ÐÓ¤ Z£Ò0ÜÐ=Ó>Ð>ð -¨lÐ;ò 7ˆØ,×2Ñ2Ó4ò 	7‰MˆD�'Ü�7‹|˜qÒ Ü$Ð%LÓMÐMà<@ˆJ”s˜7 1™:“Ñ'×3Ñ3°DÑ9ð ˜Q˜R�[ò 7�Ø&¤s¨1£vÑ.�	Ø$Ÿ?™?×6Ñ6Ø—w‘wØŸ;™;ÜÑ8¨d¯i©iÔ8Ó8ØØ"Ÿi™ið 7ó �ð —I‘I—N‘NÓ$ð ”ð /7�	×%Ñ% dÒ+ñ7ñ	7ð7ð. ,ó ;&ˆØ˜~Ñ.ˆ	Ø&(ˆ
àˆð
 )+ÐØ%'ˆØ×#Ñ#ò 	/ˆCØ" 3™ˆIà—‘ 
Ò*Ü˜y×/Ñ/´Ô5ÜÜ+¨A¨y×/?Ñ/?Ó@Ä%Ç(Á(Ç/Á/ôð  ×&Ñ& sÕ+à"×)Ñ)¨#Õ.ð	/ð &¨Ñ7ó "	AˆCØ" 3™ˆIð#¤T÷ #ò #ð �|‰|˜zÒ)Ü! )×"2Ñ"2´CÔ8Ü(Ø3´D¸×9IÑ9IÓ4JÐ3KÐLóð ô 4°A°y×7GÑ7GÓH�	Ü˜i¬¯©¯©Ô9Ø"+§/¡/×":Ñ":¸9×;KÑ;KÓ"L�KØ:C�I×%Ñ% i×&6Ñ&6Ò7á"1Ó"3‘Ká-Ó/�Ø)¨#™×3Ñ3×8Ñ8Ó:ˆKÔØ5@ˆI×!Ñ! *¨S¡/Ó2ðE"	AðF &ˆ	Öðw;&ð| —‘—‘ó *3ˆÜ�4˜Õ)Ø" 4×#5Ñ#5Ñ6ˆIð $×/Ñ/ˆKÜ!ŸH™HŸN™N×2Ñ2°4·9±9Ó>VÓWˆMÜ#Ÿh™hŸn™n×4Ñ4Ø—‘Ó5óˆOð �w‰wÐ9Ñ9ØŸ™‘ä5°a¸¿¹ÓE�ØŸ™×,Ñ,¨S°#Ó6�Ø,7�	×!Ñ! &Ñ)àÐ+ð #,×"7Ñ"7Ð!8¸¸&¸ÐB�HØ-1¯[©[�L Ñ*ä˜m¬UÔ3Ü$Ø<¼TÀ-Ó=PÐ<QÐRóð ô ˜o¬tÔ4Ü$Ø=¼dÀ?Ó>SÐ=TÐUóð ñ !8�4—9’9¸TˆDØ —‘×2Ñ2Ø—7‘7ØØ"Ø&ØŸ)™)Øð 3ó ˆHð !ŸI™IŸN™NÓ,ˆHŒMØ*2ˆI×!Ñ! $Ó'ðU*3ðZ -Ð-ò ˆÜ˜_Ó-ò 	ˆDØ% dÑ+ˆGÜ�7‹|˜qÒ Ü$Ð%CÓDÐDà˜S˜b�\ò �Ø&¤s¨1£vÑ.�	Ø*¨4Ñ0�	ØÐ$Ü(Ð)<Ó=Ð=Ø$Ÿ?™?×6Ñ6Ø —|‘|Ø$×+Ñ+Ø#×/Ñ/°Ñ5Ð7ØØ'Ÿn™nð 7ó �ð —N‘N×'Ñ'Ó)ð •ññ	ðð. %'€Là$&€LÜ+0¯8©8¯>©>×+?Ñ+?Ó+A€NØCE€NÙØ—G‘G—M‘Mò 	ˆDÙ+:Ø�l Nó,Ñ(ˆL™.ñ	ð —G‘G—M‘Mò 	+ˆDØ&*ˆL˜Ÿ™Ò#ð	+ñ "5ÑÐ:Rð ô &)£UÐ"ð ()§w¡w§}¡}ÖQ˜t¸¿¹À=Ó8P’dÐQ€NÐQà4ó 7EˆØ˜~Ñ.ˆ	ô ô 
Ø@I×@QÑ@Qô
ó 
ˆô ˆ{Ó˜qÒ ©Ø�O‰O×"Ñ" ;¨q¡>Õ2à�O‰O×"Ñ" ;Ô/áð %×+Ñ+ö/àØ˜nÑ,ð ˜SÓ!ð/Ðð /ð 'ò 9�ØÐ9Ñ9ØÙ1@Ø˜,¨ó2Ñ.�Ð.ð /×2Ñ2°4Õ8ð9ð ,ò 9�ØÐ9Ñ9ØÙ/>Ø˜,¨ó0Ñ,�˜nð /×2Ñ2°4Õ8ð9ô 1CØ×Ñ˜yŸ™ó1
ˆ�y×,Ñ,Ñ-ð
 $×/Ñ/Ø×!Ñ!ÜÓB°×1AÑ1AÔBÓBó
ˆ
ô
 ˜)×+Ñ+Ó,ˆØ˜Š?˜{¨aÒ/±Lä$Ÿx™xŸ~™~×3Ñ3°JÓ?ÐÜ"+¨I×,=Ñ,=Ó">ò E‘��;Ø,<¸QÑ,?×,DÑ,D�˜[Ò)òEà˜AÔØ:DˆLœœd 9×#4Ñ#4Ó5Ó6Ó7ðo7Eñz ¡<Ø—G‘G—M‘Mò 	ˆDÙ+:Ø�l Nó,Ñ(ˆL™.ð	ð —‘—‘ò ˆØ�7‰7�hÓØ×!Ñ!Ü—‘—‘×&Ñ& t§y¡y°¡|Ó5SÓTõðô ˜^¨^Ó
<€Cð ;E×:KÑ:KÓ:MÖ"N°Q 1§=£=Ò"N€C‡H�HÐÑÜ‡I�IØÜÐ2°CÀÔFôð €JùóW
 KùãBùòj Rùò"/ùò| #Os+   Ò8ACÓ$AC÷ AC!÷AC!ùAC&ÁBAC+c                 óô  — t        | j                  j                  dd¬«      «      }|D �]À  }t        |j                  «      dkD  s|j
                  rŒ)|j                  d   }|j                  j                  d«      }|€ŒVg }| j                  j                  |«      5  t        |j                  «      D ]­  \  }}t        |t        j                  «      rl| j                  j                  t        j                  j                   j"                  j$                  ||f¬«      }||j                  d<   |j'                  |«       ŒŒt        |t$        «      sŒ�|j'                  |«       Œ¯ 	 ddd«       t        |j(                  «      D ]L  }	g }
|	j                  D ])  }||u r|
j+                  |«       Œ|
j'                  |«       Œ+ t-        |
«      |	_        ŒN | j                  j/                  |«       �ŒÃ y# 1 sw Y   ŒŒxY w)	au  Decompose x.size() into per-dim sym_size.int calls.

    torch.Size objects cannot cross split boundaries because aot_autograd
    cannot handle them as submodule outputs. This replaces each size() call
    with individual sym_size.int(x, dim) nodes:
      - Dynamic dims (SymInt) -> new sym_size.int node
      - Static dims (plain int) -> inlined as literal constant
    Úcall_methodÚsize)rM   rT   r�   r   rb   N)rH   )rÀ   r   Ú
find_nodesrN   rH   r“   rU   ri   Úinserting_afterrÊ   ÚshaperW   r   r±   r€   ÚopsÚatenÚsym_sizeÚintrz   ÚusersÚextendr¨   Ú
erase_node)r:   Ú
size_nodesrC   Útensor_nodeÚevÚdimsrõ   Údim_valÚdnÚuserÚnew_argsr†   s               r#   Ú_decompose_size_nodesr  Á  s¥  € ô �a—g‘g×(Ñ(¨MÀ&Ð(ÓIÓJ€Jàó !ˆäˆt�y‰y‹>˜AÒ §¢ØØ—i‘i ‘lˆØ×Ñ×!Ñ! /Ó2ˆØˆ:Øà!#ˆØ�W‰W×$Ñ$ [Ó1ñ 		)Ü'¨¯©Ó1ò )‘
��7Ü˜g¤u§|¡|Ô4ØŸ™×.Ñ.ÜŸ	™	Ÿ™×/Ñ/×3Ñ3¸;ÈÐ:Jð /ó �Bð 07�B—G‘G˜OÑ,Ø—K‘K •OÜ ¬Õ-Ø—K‘K Õ(ñ)÷		)ô ˜Ÿ™Ó$ò 	(ˆDØˆHØ—y‘yò )�Ø˜$‘;Ø—O‘O DÕ)à—O‘O CÕ(ð	)ô
 ˜h›ˆD�Ið	(ð 	
�‰×Ñ˜4Ö ñ;!÷		)ð 		)ús   ÂB2G.ÅG.Ç.G7	r   Úmodulesc                 óà  — t        dt        fi «      }t        j                  |«      }|j                  }d|d<   t        t        t        f   «       |d<   t        t        t        f   «       |d<   |�|nt        t        t        f   «       |d<   t        t           «       |d<   t        «       |d	<   t        «       |d
<   d|d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   t        «       |d<   | |d<   t        t        t        f   «       |d<   || _
        t        j                  |_        |S )aÜ  Construct a lightweight GraphModule that bypasses expensive init overhead.

    Creates a ``_LazyGraphModule`` instance without going through
    ``GraphModule.__new__`` (MRO traversal + per-instance class creation),
    ``nn.Module.__init__`` (17+ container allocations), or
    ``GraphModule.__init__`` (node iteration + ``recompile()`` codegen).
    Per-instance classes are still created for lazy forward dispatch.

    Only suitable for parameterless graph containers (no parameters, buffers,
    or ``get_attr``/``call_module`` targets to copy from a root module).

    The dict entries below mirror nn.Module.__init__ (torch/nn/modules/module.py).
    Update in lockstep if nn.Module adds or removes instance attributes.
    ÚGraphModuleImplFÚtrainingÚ_parametersÚ_buffersNÚ_modulesÚ_non_persistent_buffers_setÚ_backward_pre_hooksÚ_backward_hooksÚ_is_full_backward_hookÚ_forward_hooksÚ_forward_hooks_with_kwargsÚ_forward_hooks_always_calledÚ_forward_pre_hooksÚ_forward_pre_hooks_with_kwargsÚ_state_dict_hooksÚ_state_dict_pre_hooksÚ_load_state_dict_pre_hooksÚ_load_state_dict_post_hooksÚ_graphrU   )rS   r   ÚobjectÚ__new__Ú__dict__r¦   r+   r   r¯   r   Úowning_moduleÚ_lazy_forwardÚforward)r   r  ÚclsÚinstÚds        r#   Ú_make_lite_graph_moduler-  í  s]  € ô$ Ð Ô#3Ð"5°rÓ
:€CÜ�>‰>˜#Ó€DØ�‰€AØ€A€j�MÜœC¤˜H‘~Ó'€A€mÑÜœœc˜‘NÓ$€A€j�MØ&Ð2‘G¼¼SÄ#¸X¹Ó8H€A€j�MÜ'*¬3¡x£z€AÐ#Ñ$Ü*›}€AÐÑÜ&›=€AÐÑØ"&€AÐÑÜ%›-€AÐÑÜ&1£m€AÐ"Ñ#Ü(3«€AÐ$Ñ%Ü)›m€AÐÑÜ*5«-€AÐ&Ñ'Ü(›]€AÐÑÜ!,£€AÐÑÜ&1£m€AÐ"Ñ#Ü'2£}€AÐ#Ñ$Ø€A€h�KÜ”Sœ#�X‘Ó €A€f�IØ€EÔÜ"×0Ñ0€C„KØ€Kr%   Únode_to_partitionc                 ó*  ‡— ddl }ddlmŠ i }t        t        «      }t        t        «      }i }g }t        «       }|fdt        dt        t        t        t        t        f   f   dt        fd„}	| j                  j                  D �]	  }
|
j                  j                  d«      }|�Wt        |d	«      rKt        |j                  d
«      r5|j                  j                  }t!        ||j"                  «      r	||vr|
||<   |
j$                  dk(  rŒˆ|
j$                  dk(  r8t&        j(                  j                  j+                  |
j,                  d   |	«       ŒÏ||
   }||
_        ||vrM|j1                  |«       |j3                  |«       t&        j(                  j                  j5                  «       ||<   ||||fdt        dt        dt        t        t        t        t        f   f   dt        t        t        t        t        f   f   dt        |j"                  t        f   dt        fˆfd„}t&        j(                  j                  j+                  |
j,                  |«       t&        j(                  j                  j+                  |
j6                  |«       �Œ ||||fS )a�  Assign nodes to partitions and detect cross-partition dependencies.

    Returns:
        partition_graphs: Maps partition ID to its Graph.
        partition_inputs: Maps partition ID to ordered dict of input names -> source nodes.
        partition_outputs: Maps partition ID to ordered dict of output names -> source nodes.
        seen_partitions: Partition IDs in order of first appearance.
    r   Nr^   rŒ   Ú_outputsr   c                 óp   — t        | d«      r)|| j                     j                  | j                  | «       | S )Nr`   )r2   r`   rh   r   )rŒ   r0  s     r#   Ú_record_output_depz0_detect_dependencies.<locals>._record_output_dep6  s0   € ô �1�oÔ&Ø�Q—_‘_Ñ%×0Ñ0°·±¸Ô;Øˆr%   rb   rC   r³   rG   r   r[   Úuse_pidÚ_inputsÚ_symc                 ó  •— t        | dd «      }||k7  rí|�||   j                  | j                  | «       ||   j                  | j                  | «       | j                  j	                  d«      }|��|rŽt         ‰
|«      t        ¬«      D ]t  }|j	                  |«      }|€Œ||   j                  |j                  |«       |j                  dk7  sŒFt        |dd «      }	|	€ŒV||	   j                  |j                  |«       Œv | S )Nr`   rb   rc   rG   )r4   rh   r   rU   ri   rj   r+   rM   )r[   r3  r4  r0  r5  Údef_pidro   rp   rq   Ús_pidr_   s             €r#   Ú_record_cross_depz/_detect_dependencies.<locals>._record_cross_depU  sù   ø€ ô ˜h¨¸Ó>ˆGØ˜'Ò!ØÐ&Ø˜WÑ%×0Ñ0°·±ÀÔIØ˜Ñ ×+Ñ+¨H¯M©M¸8ÔDà"Ÿ-™-×+Ñ+¨OÓ<�ØÐ&©4Ü#¡L°Ó$9¼sÔCò P˜Ø!%§¡¨!£˜Ø!˜>Ø$Ø Ñ(×3Ñ3°F·K±KÀÔHØ!Ÿ9™9¨Ó5Ü$+¨F°OÀTÓ$J˜EØ$Ñ0Ø (¨¡× :Ñ :¸6¿;¹;ÈÕ OðPð ˆOr%   )r©   re   r_   r   r¦   r¯   r   r  r+   r   r°   rU   ri   r2   rC   r³   rW   r´   rM   r   r   r¿   rH   r`   rz   r·   r   r“   )r:   r.  r©   Úpartition_graphsÚpartition_inputsÚpartition_outputsru   Úseen_partitionsÚseen_partitions_setr2  rC   rÓ   rÔ   rÙ   r9  r_   s                  @r#   Ú_detect_dependenciesr?    sB  ø€ ó" åBà8:ÐÜ3>¼tÓ3DÐÜ4?ÄÓ4EÐØ/1€NØ!#€OÜ$'£EÐð 0AñÜðä”sœD¤¤d ™OÐ+Ñ,ðô 
óð —‘—‘ó 2?ˆà�i‰i�m‰m˜OÓ,ˆØˆ?œw s¨FÔ3¼ÀÇÁÈ&Ô8QØ—‘—‘ˆBÜ˜"˜eŸl™lÔ+°¸.Ñ0HØ%)�˜rÑ"à�7‰7�mÒ#Øà�7‰7�hÒÜ�H‰H�N‰N×"Ñ" 4§9¡9¨Q¡<Ð1CÔDØà Ñ%ˆØ ˆÔàÐ)Ñ)Ø×"Ñ" 3Ô'Ø×#Ñ# CÔ(Ü$)§H¡H§N¡N×$8Ñ$8Ó$:Ð˜SÑ!ð Ø2BØ3DØ-;ñ	Üð	äð	ô œ#œt¤C¬ I™Ð.Ñ/ð	ô œ3¤¤S¬$ Y¡Ð/Ñ0ð		ô
 �u—|‘|¤TÐ)Ñ*ð	ô õ	ô4 	�‰�‰×Ñ˜tŸy™yÐ*;Ô<Ü�‰�‰×Ñ˜tŸ{™{Ð,=Ö>ðe2?ðh Ð-Ð/@À/ÐQÐQr%   r:  r;  r<  r=  c                 ó<  ‡— t        t        «      }|D ]h  }||   }||   Š||   j                  «       D ]F  \  }}	|j                  ||	j                  ¬«      }
|	j
                  j                  «       |
_        |
‰|	<   ŒH Œj | j                  j                  D ]ù  }t        |d«      sŒ|j                  }||   }||   Št        j                  j                  j                  |j                  ‰j                  «      }t        j                  j                  j                  |j                   ‰j                  «      }|j#                  |j$                  |j&                  |||j                  ¬«      }|j
                  j                  «       |_        |‰|<   Œû |D ]‡  }||   }||   Š||   }t)        ˆfd„|j+                  «       D «       «      }t-        |«      dk(  r|j/                  |d   «       ŒWt-        |«      dkD  r|j/                  |«       Œw|j/                  d«       Œ‰ |S )z§Create placeholders, clone nodes, and set outputs for each partition.

    Returns:
        partition_env: Maps partition ID to {original_node: cloned_node} dict.
    r–   r`   r’   c              3   ó(   •K  — | ]	  }‰|   –— Œ y ­wrƒ   r,   )r…   rå   Úenvs     €r#   r‡   z/_clone_nodes_into_partitions.<locals>.<genexpr>¦  s   øè ø€ ÒO¨y˜C 	�NÑOùr¢   r�   r   r,   )r   r¦   rº   rG   rS   rU   rV   r   r°   r2   r`   r   r   r¿   rH   Ú__getitem__r“   rR   rM   rT   r¨   r¹   rN   r   )r:   r:  r;  r<  r=  Úpartition_envrÙ   ÚgÚinp_namerå   rG   rC   rç   rè   râ   Ú	out_nodesrï   rB  s                    @r#   Ú_clone_nodes_into_partitionsrH  u  sï  ø€ ô 2=¼TÓ1B€Mð ò )ˆØ˜SÑ!ˆØ˜CÑ ˆØ#3°CÑ#8×#>Ñ#>Ó#@ò 	)ÑˆH�iØŸ-™-¨¸I¿N¹N˜-ÓKˆKØ(Ÿ~™~×2Ñ2Ó4ˆKÔØ(ˆC�	ŠNñ	)ð)ð —‘—‘ò ˆÜ�t˜_Ô-ØØ× Ñ ˆØ˜SÑ!ˆØ˜CÑ ˆäŸ™Ÿ™×.Ñ.¨t¯y©y¸#¿/¹/ÓJˆÜŸ(™(Ÿ.™.×0Ñ0°·±¸c¿o¹oÓNˆà—=‘=Ø�w‰wØ—;‘;ØØ"Ø—i‘ið !ó 
ˆð Ÿ	™	Ÿ™Ó(ˆŒØˆˆDŠ	ð%ð* ò 
ˆØ˜SÑ!ˆØ˜CÑ ˆØ% cÑ*ˆ	ÜÓO¸I×<LÑ<LÓ<NÔOÓOˆÜˆ{Ó˜qÒ Ø�H‰H�[ ‘^Õ$Ü�Ó Ò!Ø�H‰H�[Õ!à�H‰H�R�Lð
ð Ðr%   c                 ó   ‡— t         j                  j                  j                  «       }i Ši }| j                  j                  D ]g  }|j
                  dk(  sŒ|j                  |j                  |j                  ¬«      }	|j                  j                  «       |	_	        |	‰|j                  <   Œi |D ]Ü  }
|�	d|› d|
› �}nd|
› �}t        ||
   «      ||<   t        ˆfd„||
   D «       «      }|j                  ||«      }t        ||
   j!                  «       «      }t#        |«      dkD  rOt         j                  j$                  j'                  |«      }t)        |«      D ]  \  }}||   j*                  ‰|<   Œ ŒÆt#        |«      dk(  sŒÕ|‰|d   <   ŒÞ | j                  j                  D ][  }|j
                  dk(  sŒ|j-                  t         j                  j                  j/                  |j0                  d   ˆfd	„«      «       Œ] t        ||¬
«      S )zDBuild the outer stitching graph that calls each partition submodule.rG   r–   r   rx   c              3   ó(   •K  — | ]	  }‰|   –— Œ y ­wrƒ   r,   )r…   r   Úbase_envs     €r#   r‡   z)_build_stitching_graph.<locals>.<genexpr>Î  s   øè ø€ ÒM¨t˜H T�NÑMùr¢   r�   r   r   c                 ó"   •— ‰| j                      S rƒ   r¤   )rŒ   rK  s    €r#   r�   z(_build_stitching_graph.<locals>.<lambda>Ü  s   ø€ ¸xÈÏÉÑ?O€ r%   )r  )r   r   r   r   r°   rM   rG   rT   rS   rU   rV   r   r-  r¨   rŸ   rÀ   rÁ   rN   rÈ   rÉ   rÊ   rC   r   r¿   rH   )r:   r:  r;  r<  r=  r8   Ú
base_graphÚbase_modulesrC   rø   rÙ   r   Úinput_nodesrò   Ú	out_namesrô   rõ   r   rK  s                     @r#   Ú_build_stitching_graphrQ  ±  sæ  ø€ ô —‘—‘×%Ñ%Ó'€JØ "€HØ+-€Là—‘—‘ò $ˆØ�7‰7�mÓ#Ø×&Ñ&Ø—‘ØŸ)™)ð 'ó ˆAð —Y‘Y—^‘^Ó%ˆAŒFØ"#ˆH�T—Y‘YÒð$ð ò 0ˆØÐ&Ø# OÐ#4°A°c°UÐ;‰Kà# C 5˜/ˆKÜ$;Ð<LÈSÑ<QÓ$Rˆ�[Ñ!äÓMÐ7GÈÑ7LÔMÓMˆØ×+Ñ+¨K¸ÓEˆ
äÐ*¨3Ñ/×4Ñ4Ó6Ó7ˆ	Üˆy‹>˜AÒÜ$Ÿx™xŸ~™~×3Ñ3°JÓ?ÐÜ$ YÓ/ò :‘��4Ø!1°!Ñ!4×!9Ñ!9�˜’ñ:ä�‹^˜qÓ Ø%/ˆH�Y˜q‘\Ò"ð!0ð$ —‘—‘ò ˆØ�7‰7�hÓØ×ÑÜ—‘—‘×&Ñ& t§y¡y°¡|Ó5OÓPõðô # :°|ÔDÐDr%   )r8   c                ób   — t        | |«      \  }}}}t        | ||||«       t        | |||||«      S )uú  Lightweight graph splitter for simple partition patterns.

    A faster alternative to :func:`split_module` for inference-only graphs
    from ``torch.compile``/Dynamo. Because these graphs have no autocast/grad
    regions, no ``get_attr`` nodes, and no non-linear partition dependencies,
    we can skip the topological sort, autocast tracking, and ``get_attr``
    special-casing that ``split_module`` performs. More importantly, we
    construct partition submodules as lightweight ``_LazyGraphModule``
    instances that bypass ``nn.Module.__init__`` and defer ``recompile()``
    codegen â€” this eliminates the dominant cost when creating 70-100+
    partition submodules for large models.

    Args:
        m: Graph module to split.
        node_to_partition: Maps each operational node to a partition ID.
            Placeholders, get_attr, and output nodes should NOT be included.
        partition_affix: If set, submodule names become
            ``submod_{affix}_{idx}`` instead of ``submod_{idx}``.
    )r?  rH  rQ  )r:   r.  r8   r:  r;  r<  r=  s          r#   r   r   â  sY   € ô6 	˜QÐ 1Ó2ñ KÐÐ&Ð(9¸?ô !Ø	ÐÐ-Ð/@À/ôô "Ø	ØØØØØóð r%   )NFFTrƒ   ).rO   r½   Úcollectionsr   r   Úcollections.abcr   Útypingr   r   Útorch.fx._compatibilityr   Útorch.fx._lazy_graph_moduler   r	   Útorch.fx._utilsr
   Útorch.fx.graph_moduler   Útorch.fx.noder   Ú__all__Ú	getLoggerr(   rf   r»   r   rÄ   rÅ   r+   r7   r  r¦   rµ   r   r  r   r   r   r-  r¨   rÀ   r?  rH  rQ  r   r,   r%   r#   ú<module>r]     s¦  ðÛ Û ß 0Ý $Ý ã Ý 1ß LÝ 2Ý -Ý ò ?€Ø!�×!Ñ! (Ó+Ð +€€gñ  dÔ+÷
ð 
ó ,ð
ð0 §¡§¡ð ¸Cð ÀCó ñ  dÔ+ð
 +/Ø',Ø+0Ø%)ðG
ð #'ØòG
ØðG
à�H‰H�O‰OðG
ð ˜d˜V S˜[Ñ)ðG
ð �s˜C�x‘. 4Ñ'ð	G
ð
  ™ðG
ð " D™[ðG
ð #ðG
ð ˜4‘ZðG
ð ðG
ð òG
ó ,ðG
ðT)!˜[ð )!¨Tó )!ð\ .2ñ*Ø�8‰8�>‰>×Ñð*à�#�{Ð"Ñ# dÑ*ð*ð ó*ðZXRØðXRà˜D #˜I‘ðXRð Øˆˆe�h‰h�n‰n×"Ñ"Ð	"Ñ#Øˆˆd�3˜�9‰oÐ	ÑØˆˆd�3˜�9‰oÐ	ÑØˆ�IðñóXRðv9Øð9à˜3 §¡§¡× 4Ñ 4Ð4Ñ5ð9ð ˜3  S¨$ Y¡Ð/Ñ0ð9ð ˜C  c¨4 i¡Ð0Ñ1ð	9ð
 ˜#‘Yð9ð 
ˆ#ˆt�D˜$�JÑÐ
Ñ ó9ðD #'ñ.EØð.Eà˜3 §¡§¡× 4Ñ 4Ð4Ñ5ð.Eð ˜3  S¨$ Y¡Ð/Ñ0ð.Eð ˜C  c¨4 i¡Ð0Ñ1ð	.Eð
 ˜#‘Yð.Eð ˜4‘Zð.Eð ó.Eñb  eÔ,ð
 #'ò	(Øð(à˜D #˜I‘ð(ð ˜4‘Zð	(ð
 ò(ó -ñ(r%   