Ë
    g^(hsÀ  ã                   óø  — d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZ d dlZd dlmZ d dlmZ d dlmZ d d	lmZmZmZmZ d d
lm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*  ejV                  e,«      Z-d„ Z.dej^                  j`                  dejb                  fd„Z2dejb                  dejf                  dejf                  fd„Z4 G d„ dej^                  jj                  «      Z6 G d„ dej^                  j`                  «      Z7 G d„ de7«      Z8ejr                  ju                  dd«       ejr                  jw                  dd «      d!„ «       Z<ejr                  j{                  d«      d"„ «       Z<ej|                  j~                  jx                  j€                  ZAej„                  j†                  j‰                  eA«       d#„ ZE G d$„ d%e«      ZFeeFeGeHeFf   f   ZI G d&„ d'ej”                  «      ZK G d(„ d)«      ZL G d*„ d+«      ZMd,„ ZNd-„ ZOd.ej(                  j                   d/ej¢                  fd0„ZR G d1„ d2ej^                  j`                  «      ZS G d3„ d4e«      ZT G d5„ d6«      ZUd7„ ZVd8„ ZWdej^                  j`                  d9eGeHeTf   fd:„ZX	 	 	 dCd;ej^                  j`                  d<eYed=f   d>eeGeHef      d?eeGeHeTf      d@eeej                   gej                   f      dAeSfdB„ZZy)Dé    N)Údefaultdict)ÚEnum)Ú	ParameterÚ	SignatureÚ	signature)Ú
MethodType)ÚAnyÚCallableÚOptionalÚUnion)ÚProcessGroup)ÚExportedProgram)Ú_assign_attrÚ	_AttrKindÚ_sink_paramsÚInterpreterModule)Úmap_aggregate)Úsplit_moduleé   )Ú_null_coalesce_accumulateÚstage_backward)Ú_outline_submodules)ÚPipeInfo)Ú_PipelineStagec                 ó  — |du ry |du r*t        | t        j                  «      st        d| › �«      ‚| S t        |t        t
        f«      rƒt        | t        t
        f«      st        d| › d|› �«      ‚t        | «      t        |«      k7  rt        d| › d|› �«      ‚t        | |«      D ]  \  }}t        ||«      }|€Œ|c S  t        d|› �«      ‚t        |t        «      r’t        | t        «      st        d| › d|› �«      ‚t        | j                  «       «      t        |j                  «       «      k7  rt        d| › d|› �«      ‚|D ]  }t        | |   ||   «      }|€Œ|c S  t        d|› �«      ‚t        d	t        |«      › d
�«      ‚)NFTz/Loss spec must specify a dynamic value but got zOutput value z' must match type of loss specification z) must match length of loss specification z)Did not find loss value in specification z' must match keys of loss specification zUnsupported type z in loss specification)Ú
isinstanceÚfxÚNodeÚRuntimeErrorÚtupleÚlistÚlenÚzipÚ_find_loss_from_output_and_specÚdictÚsetÚkeysÚtype)Ú
output_valÚspec_valÚoutÚspecÚloss_valÚks         ú^/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributed/pipelining/_IR.pyr$   r$   &   sÑ  € Ø�5ÑØØ�4ÑÜ˜*¤b§g¡gÔ.ÜØAÀ*ÀÐNóð ð Ðä�(œU¤D˜MÔ*Ü˜*¤u¬d mÔ4ÜØ 
˜|Ð+RØ�*ðóð ô ˆz‹?œc (›mÒ+ÜØ 
˜|Ð+TØ�*ðóð ô ˜Z¨Ó2ò 	 ‰IˆC�Ü6°s¸DÓAˆHØÑ#Ø’ð	 ô ÐFÀxÀjÐQÓRÐRä�(œDÔ!Ü˜*¤dÔ+ÜØ 
˜|Ð+RØ�*ðóð ô ˆz�‰Ó Ó!¤S¨¯©«Ó%9Ò9ÜØ 
˜|Ð+RØ�*ðóð ð ò 	 ˆAÜ6°zÀ!±}ÀhÈqÁkÓRˆHØÑ#Ø’ð	 ô ÐFÀxÀjÐQÓRÐRä
Ð*¬4°«>Ð*:Ð:PÐQÓ
RÐRó    ÚmodÚgc                 óØ  — |j                   D �cg c]  }|j                  dk(  sŒ|‘Œ }}t        |«      dk(  sJ ‚|d   }|j                  d   }d }t	        | t
        «      r-t        |j                  «      dk(  sJ ‚|}t
        j                  }nP|€@t	        |t        «      r+d|j                  «       v r|d   }|D �	ci c]  }	|	|	dk(  “Œ
 }}	nd }d }nt        ||«      }|}|||fS c c}w c c}	w )NÚoutputr   r   Úloss)
ÚnodesÚopr"   Úargsr   ÚTrivialLossWrapperÚ	loss_specr%   r'   r$   )
r1   r2   Úoutput_loss_value_specÚnÚoutput_nodesÚoutput_noder)   Úgenerated_specÚ	loss_noder.   s
             r/   Ú_find_loss_outputrA   U   s  € Ø Ÿw™wÖ;˜!¨!¯$©$°(Ó*:’AÐ;€LÐ;Üˆ|Ó Ò!Ð!Ð!Ø˜q‘/€KØ×!Ñ! !Ñ$€JØ€Nä�#Ô)Ô*ô �;×#Ñ#Ó$¨Ò)Ð)Ð)Øˆ	Ü+×5Ñ5‰Ø	Ð	'ä�j¤$Ô'¨F°j·o±oÓ6GÑ,GØ" 6Ñ*ˆIØ6@ÖA°˜a  f¡™nÐAˆNÑAàˆIØ!‰Nä3°JÐ@VÓWˆ	Ø/ˆà�k >Ð1Ð1ùò1 <ùò  Bs   �C"¤C"Â:C'r@   r>   c           
      óÔ  ‡ ‡‡— i }t        ‰ j                  «      D ]Ú  }|j                  dk(  sŒ|j                  t        j
                  k(  sJ d«       ‚t        |j                  «      dk(  sJ d«       ‚t        |j                  «      \  }}||v rt        ||   «      nd}t        |dz   |«      }t        |«      D �	cg c]  }	d ‘Œ }
}	||v rt        ||   «      D ]
  \  }}||
|<   Œ ||
|<   t        |
«      ||<   ŒÜ |d iŠ|d iŠˆ ˆfd„}‰ j                  |«      5  t        ‰ j                  «      D �]–  }|‰vrŒ	ˆfd„}t        j                  j                  |j                  |«       t        j                  j                  |j                   |«       |j                  d	k(  sŒr||v rB||   }t        ˆfd
„||   D «       «      }t        ||   «      D ��cg c]  \  }}|‰v sŒ|‘Œ }}}n|f}‰|   }dg}t#        |t        «      s|fn|}‰ j%                  t&        ||t)        |j*                  «      |dœ¬«      }t-        |j                   «      }||_        t        j.                  |«      }|j                  }t)        |j*                  «      }t        j.                  |«      }t        |«      D ]  \  }} ||||   j                  «       Œ �Œ™ 	 d d d «       ‰ S c c}	w c c}}w # 1 sw Y   ‰ S xY w)NÚcall_functionzDFound non-getitem call in forward pass. Please report a bug to PiPPyé   z:Found malformed getitem call. Please report a bug to PiPPyéÿÿÿÿr   c                 ól   •— | ‰v r*| j                   dk7  r‰j                  t        ‰|    |f«      }|‰| <   y )NÚplaceholder)r7   rC   r   )Úforward_nodeÚ
grad_valuer2   Úval_to_grads     €€r/   Úassign_or_accumulate_gradzB_insert_stage_symbolic_backward.<locals>.assign_or_accumulate_gradŸ   s@   ø€ Ø˜;Ñ&¨<¯?©?¸mÒ+KØŸ™Ü)Ø˜\Ñ*¨JÐ7óˆJð %/ˆ�LÒ!r0   c                 ó*   •— ‰j                  | d «       y ©N)Ú
setdefault)r<   Ú
live_nodess    €r/   Úadd_to_live_nodesz:_insert_stage_symbolic_backward.<locals>.add_to_live_nodes¬   s   ø€ Ø×%Ñ% a¨Õ.r0   Úcall_modulec              3   óB   •K  — | ]  }‰j                  |d «      –— Œ y ­wrM   )Úget)Ú.0r<   rJ   s     €r/   ú	<genexpr>z2_insert_stage_symbolic_backward.<locals>.<genexpr>µ   s   øè ø€ Ò(XÀa¨¯©¸¸D×)AÑ(Xùs   ƒr   )Ústage_outputÚoutput_gradsÚinput_valuesÚoutputs_with_grads_idxs)Úkwargs)Úreversedr6   r7   ÚtargetÚoperatorÚgetitemr"   r8   r    ÚmaxÚrangeÚ	enumerateÚinserting_beforer   ÚnodeÚmap_argrZ   r   rC   r   r!   Úall_input_nodesr%   ÚProxy)r2   r@   r>   Útuplesrc   Úindexed_valueÚnode_idxÚexisting_list_sizeÚnew_list_sizeÚ_Úreconstructed_listÚiÚvalrK   rP   rV   rW   r<   rY   Ú	grad_callÚkwargs_copyÚgrad_call_proxyÚgradsÚinput_nodesÚgrads_proxyÚ
input_noderO   rJ   s   `                         @@r/   Ú_insert_stage_symbolic_backwardrw   q   s  ú€ ð $&€FÜ˜Ÿ™Ó!ò >ˆØ�7‰7�oÓ%ð —;‘;¤(×"2Ñ"2Ò2ð ØVóÐ2ô �t—y‘y“> QÒ&ð ØLóÐ&ô ',¨D¯I©IÓ&6Ñ#ˆM˜8ð /<¸vÑ.E”�F˜=Ñ)Ô*È2ð ô   ¨1¡Ð.@ÓAˆMä05°mÓ0DÖ!E¨1¢$Ð!EÐÐ!Eð  Ñ&Ü'¨¨}Ñ(=Ó>ò 0‘F�A�sØ,/Ð& qÒ)ð0ð ,0Ð˜xÑ(ä$)Ð*<Ó$=ˆF�=Ò!ð?>ðH ˜TÐ"€JØ5>ÀÐ4E€Kõ/ð 
×	Ñ	˜KÓ	(ñ 0OÜ˜QŸW™WÓ%ó /	OˆDØ˜:Ñ%Øô/ô �G‰G�O‰O˜DŸI™IÐ'8Ô9Ü�G‰G�O‰O˜DŸK™KÐ):Ô;Ø�w‰w˜-Ó'à˜6‘>Ø#)¨$¡<�LÜ#(Ó(XÈ6ÐRVÉ<Ô(XÓ#X�Lä&/°°t±Ó&=÷/Ù"˜a ÀÀjÂšð/Ð+ò /ð %) 7�LØ#.¨tÑ#4�LØ/0¨cÐ+ô & l´EÔ:ð "‘Oà%ð ð ŸO™OÜ"à(4Ø(4Ü(,¨T×-AÑ-AÓ(BØ3Jñ	ð ,ó �	ô # 9×#3Ñ#3Ó4�Ø#.�	Ô ä"$§(¡(¨9Ó"5�Ø'×,Ñ,�ä" 4×#7Ñ#7Ó8�Ü Ÿh™h u›o�Ü%.¨{Ó%;ò O‘M�A�zÙ-¨j¸+Àa¹.×:MÑ:MÕNòOñ]/	O÷0Oðd €HùòY "FùóR/÷0Oðd €Hús2   Â9	KÄBKÆ1KÇK
ÇK
Ç!C&KËKËK'c                   óP   — e Zd Zedej
                  j                  fd„«       Zd„ Zy)ÚPipeSequentialÚsequential_instancec                 ó^   — t        | D �cg c]  }t        j                  |«      ‘Œ c}Ž S c c}w rM   )ry   Úcopy)rz   Úms     r/   Úfrom_sequentialzPipeSequential.from_sequentialÝ   s$   € äÐ6IÖJ°¤§	¡	¨!¥ÒJÐKÐKùÒJs   Š*c                 ót   — t        | «      D ])  \  }} ||«      }|t        | «      dz
  k7  sŒ t        «        Œ+ |S )Nr   )ra   r"   Ú
pipe_split)ÚselfÚinputrn   Úmodules       r/   ÚforwardzPipeSequential.forwardá   s>   € Ü" 4›ò 	‰IˆAˆvÙ˜5“MˆEØ”C˜“I ‘MÓ!Ü•ð	ð ˆr0   N)	Ú__name__Ú
__module__Ú__qualname__ÚstaticmethodÚtorchÚnnÚ
Sequentialr~   r„   © r0   r/   ry   ry   Ü   s-   „ ØðL¨U¯X©X×-@Ñ-@ò Ló ðLór0   ry   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚLossWrapperaJ  
    LossWrapper is a convenient abstract class that allows you to wrap up both
    your model as well as its loss function and specify the connectivity between
    the inputs, model, loss function, and output value. Example::

        class MyModelWrapper(LossWrapper):
            def forward(self, x, targets):
                model_out = self.module(x)
                loss_value = self.loss_fn(model_out, targets)
                return loss_value

    The above example defines a connectivity where we expect the forward/loss/backward
    training procedure to take two arguments (x and targets), pass x into the module
    to get the output of the feedforward computation, pass the model output and the
    targets value into the loss function, and get and return the loss value, which will
    be backpropagated by PiPPy. The above class would then be instantiated like::

        model = ...  # instantiate the model
        loss_fn = torch.nn.MSELoss()  # for the sake of demonstration

        wrapper = MyModelWrapper(model, loss_fn)
        pipe = Pipe.from_tracing(wrapper, ...)

    c                 ó>   •— t         ‰| �  «        || _        || _        y rM   )ÚsuperÚ__init__rƒ   Úloss_fn)r�   rƒ   r’   Ú	__class__s      €r/   r‘   zLossWrapper.__init__  s   ø€ Ü‰ÑÔØˆŒØˆ�r0   c                 ó   — t        d«      ‚)Nz¶This instance of LossWrapper does not have an overriddenforward(). Please implement forward() to specify the arguments, connection between the module and loss, and loss output value.)ÚNotImplementedError©r�   r8   rZ   s      r/   r„   zLossWrapper.forward  s   € Ü!ðó
ð 	
r0   )r…   r†   r‡   Ú__doc__r‘   r„   Ú__classcell__©r“   s   @r/   rŽ   rŽ   é   s   ø„ ñô2ö

r0   rŽ   c                   ó   — e Zd Zd„ ZdZy)r9   c                 óH   — | j                  |«      }| j                  ||«      S rM   )rƒ   r’   )r�   ÚxÚtargetsÚ	model_outs       r/   r„   zTrivialLossWrapper.forward  s    € Ø—K‘K “Nˆ	Ø�|‰|˜I wÓ/Ð/r0   TN)r…   r†   r‡   r„   r:   rŒ   r0   r/   r9   r9     s   „ ò0ð �Ir0   r9   zpippy::_pipe_splitz() -> ()ÚBackendSelectc                   ó   — y rM   rŒ   rŒ   r0   r/   Ú_pipe_splitr¡   3  ó   € àr0   c                   ó   — y rM   rŒ   rŒ   r0   r/   r¡   r¡   8  r¢   r0   c                  óR   — t         j                  j                  j                  «       S )aþ  
    pipe_split is a special operator that is used to mark the boundary between
    stages in a module. It is used to split the module into stages. It is a
    no-op if your annotated module is run eagerly.

    Example:
        >>> # xdoctest: +SKIP
        >>> def forward(self, x):
        >>>     x = torch.mm(x, self.mm_param)
        >>>     x = torch.relu(x)
        >>>     pipe_split()
        >>>     x = self.lin(x)
        >>>     return x

    The above example will be split into two stages.
    )r‰   ÚopsÚpippyr¡   rŒ   r0   r/   r€   r€   F  s   € ô" �9‰9�?‰?×&Ñ&Ó(Ð(r0   c                   ó   — e Zd ZdZdZy)ÚMultiUseParameterConfigr   rD   N)r…   r†   r‡   ÚTRANSMITÚ	REPLICATErŒ   r0   r/   r¨   r¨   Z  s   „ Ø€HØ�Ir0   r¨   c                   óH   ‡ — e Zd ZdZdˆ fd„	Zddœˆ fd„
Zˆ fd„Zˆ fd„Zˆ xZS )	ÚDetachExecutorzÏ
    Special interpreter to run the split_gm in testing that detaches all inputs to
    a module invocation. This is needed so that the values at the boundary are
    leaf modules in autograd execution.
    c                 ó8   •— d}t         ‰| �  ||«       i | _        y )NF)r�   r‘   Úvalue_remap)r�   rƒ   Úgarbage_collect_valuesr“   s      €r/   r‘   zDetachExecutor.__init__i  s!   ø€ Ø!&ÐÜ‰Ñ˜Ð!7Ô8ØˆÕr0   N)Úinitial_envc                ó0   •— i | _         t        ‰| �  |d|iŽS )Nr°   )r®   r�   Úrun)r�   r°   r8   r“   s      €r/   r²   zDetachExecutor.runn  s   ø€ ØˆÔÜ‰w‰{˜DÐ:¨kÑ:Ð:r0   c                 ód   •‡ — ˆ fd„}	 t        ||«      }t        ||«      }t        ‰‰ �	  |||«      S )Nc                 óê   •— t        | t        j                  «      rW| j                  rK| ‰j                  vr.| j                  «       j                  d«      }|‰j                  | <   ‰j                  |    S | S )NT)r   r‰   ÚTensorÚrequires_gradr®   ÚdetachÚrequires_grad_)ÚaÚnew_valr�   s     €r/   Údetach_tensorsz2DetachExecutor.call_module.<locals>.detach_tensorss  sa   ø€ Ü˜!œUŸ\™\Ô*¨q¯ªØ˜D×,Ñ,Ñ,ØŸh™h›j×7Ñ7¸Ó=�GØ*1�D×$Ñ$ QÑ'Ø×'Ñ'¨Ñ*Ð*à�r0   )r   r�   rQ   )r�   r\   r8   rZ   r»   r“   s   `    €r/   rQ   zDetachExecutor.call_moduler  sG   ù€ ô	ð	ô
 ØØó
ˆô ØØó
ˆô
 ‰wÑ" 6¨4°Ó8Ð8r0   c                 ó¸   •— |t         k(  r;t        |«      }|d   D �cg c]  }| j                  j                  ||«      ‘Œ  c}|d<   t        ‰| �  |||«      S c c}w )NrX   )r   r%   r®   rS   r�   rC   )r�   r\   r8   rZ   Úvr“   s        €r/   rC   zDetachExecutor.call_functionŒ  sa   ø€ à”^Ò#Ü˜&“\ˆFà4:¸>Ñ4Jö&Ø/0�× Ñ ×$Ñ$ Q¨Õ*ò&ˆF�>Ñ"ô ‰wÑ$ V¨T°6Ó:Ð:ùò&s   �#A)T)	r…   r†   r‡   r—   r‘   r²   rQ   rC   r˜   r™   s   @r/   r¬   r¬   b  s&   ø„ ñõð
 &*ö ;ô9÷4;ð ;r0   r¬   c                   ó   — e Zd ZU d„ Zeed<   y)Ú_NodeReferencec                 ó   — || _         y rM   )Úname)r�   rÁ   s     r/   r‘   z_NodeReference.__init__—  s	   € Øˆ�	r0   rÁ   N)r…   r†   r‡   r‘   ÚstrÚ__annotations__rŒ   r0   r/   r¿   r¿   –  s   … òð „Ir0   r¿   c                   ó   — e Zd Zd„ Zd„ Zy)Ú_LinearNodeListc           
      óÒ  — g | _         |D ]Û  }t        j                  j                  |j                  d„ «      }t        j                  j                  |j
                  d„ «      }t        j                  d |j                  |j                  |j                  |||j                  ¬«      }t        j                  |j                  «      |_        | j                   j                  |«       ŒÝ y )Nc                 ó,   — t        | j                  «      S rM   ©r¿   rÁ   ©r<   s    r/   ú<lambda>z*_LinearNodeList.__init__.<locals>.<lambda>¡  s   € ¼^ÈAÏFÉFÓ=S€ r0   c                 ó,   — t        | j                  «      S rM   rÈ   rÉ   s    r/   rÊ   z*_LinearNodeList.__init__.<locals>.<lambda>¢  s   € ÄÐPQ×PVÑPVÓAW€ r0   )ÚgraphrÁ   r7   r\   r8   rZ   Úreturn_type)Úserialize_node_listr   rc   rd   r8   rZ   r   rÁ   r7   r\   r(   r|   ÚmetaÚappend)r�   Ú	node_listrc   Ú	node_argsÚnode_kwargsÚserialize_nodes         r/   r‘   z_LinearNodeList.__init__ž  s©   € Ø#%ˆÔ Øò 	<ˆDÜŸ™Ÿ™¨¯	©	Ñ3SÓTˆIÜŸ'™'Ÿ/™/¨$¯+©+Ñ7WÓXˆKÜŸW™WØØ—Y‘YØ—7‘7Ø—{‘{ØØ"Ø ŸI™IôˆNô #'§)¡)¨D¯I©IÓ"6ˆNÔØ×$Ñ$×+Ñ+¨NÕ;ñ	<r0   c           	      óT  ‡— t        j                  «       }i Šˆfd„}| j                  D ]|  }t        |j                  |«      }t        |j
                  |«      }|j                  |j                  |j                  |||j                  |j                  ¬«      }|‰|j                  <   Œ~ |S )Nc                 óF   •— t        | t        «      r‰| j                     S | S rM   )r   r¿   rÁ   )ÚargÚref_str_to_nodes    €r/   Úref_to_nodez-_LinearNodeList.to_graph.<locals>.ref_to_node´  s!   ø€ Ü˜#œ~Ô.Ø& s§x¡xÑ0Ð0à�
r0   )r7   r\   r8   rZ   rÁ   Ú	type_expr)r   ÚGraphrÎ   r   r8   rZ   Úcreate_noder7   r\   rÁ   r(   )r�   rÌ   rÙ   rc   rÒ   rÓ   Ú
deser_noderØ   s          @r/   Úto_graphz_LinearNodeList.to_graph¯  s�   ø€ Ü—‘“
ˆà.0ˆô	ð ×,Ñ,ò 	4ˆDÜ% d§i¡i°Ó=ˆIÜ'¨¯©°[ÓAˆKØ×*Ñ*Ø—7‘7Ø—{‘{ØØ"Ø—Y‘YØŸ)™)ð +ó ˆJð *4ˆO˜DŸI™IÒ&ð	4ð ˆr0   N)r…   r†   r‡   r‘   rÞ   rŒ   r0   r/   rÅ   rÅ   �  s   „ ò<ó"r0   rÅ   c                 ó    —  G d„ dt         j                  j                  «      } || «      }t        j                  ||j                  «       «      S )aË  
    Custom `__reduce__` method for serialization.
    DO AS I SAY -- NOT AS I DO. This violates the principle that
    GraphModules serialize via code export & re-tracing. We allow
    for this here because **PIPE STAGES SHOULD NOT BE PERSISTED
    TO DISK -- THIS IS ONLY FOR TRANSMISSION VIA RPC**. Persisting
    these instances to disk will expose internal implementation
    details of `fx.Graph` and related data structures and is
    NOT advised.
    c                   ó   ‡ — e Zd Zˆ fd„Zˆ xZS )ú6_direct_serialization_deserialize.<locals>.DummyModulec                 óX   •— t         ‰| �  «        | j                  j                  |«       y rM   )r�   r‘   Ú__dict__Úupdate)r�   Úbodyr“   s     €r/   r‘   z?_direct_serialization_deserialize.<locals>.DummyModule.__init__×  s    ø€ Ü‰GÑÔØ�M‰M× Ñ  Õ&r0   )r…   r†   r‡   r‘   r˜   r™   s   @r/   ÚDummyModulerá   Ö  s   ø„ ÷	'ð 	'r0   ræ   )r‰   rŠ   ÚModuler   ÚGraphModulerÞ   )rå   r6   ræ   Údummys       r/   Ú!_direct_serialization_deserializerê   Ê  s:   € ô'”e—h‘h—o‘oô 'ñ
 ˜Ó€Eä�>‰>˜% §¡Ó!1Ó2Ð2r0   c                 óœ   — t        | j                  «      }|j                  d«       t        |t	        | j
                  j                  «      ffS )NÚ_graph)r%   rã   Úpoprê   rÅ   rÌ   r6   )r�   Úserialization_dicts     r/   Ú_direct_serialization_reducerï   à  sC   € Ü˜dŸm™mÓ,ÐØ×Ñ˜8Ô$ä)Ø	œ_¨T¯Z©Z×-=Ñ-=Ó>Ð?ðð r0   ÚgmÚ
new_devicec           	      óÂ  — d}| j                   j                  D �]0  }|j                  dk(  rld|j                  v sŒ"|j                  d   |k7  sŒ5t        j                  d|j                  › d|j                  d   › d|› �«       |j                  d|«       d}Œ|j                  dk(  sŒ�| j                  |j                  «      }t        |t        j                  j                  «      rt        ||«       ŒÛt        |t        «      rt        |j                   |«       �Œt        j#                  d	|j                  › d
t%        |«      › �«       �Œ3 |r| j'                  «        yy)zÅ
    Modify the device argument of all "call_function" nodes in the graph.  This
    is useful for moving the graph to a different device. In particular for
    generator ops, like torch.ones.
    FrC   ÚdevicezChanging device of Node z from ú to TrQ   z+Skipping device modification for submodule z because it is a N)rÌ   r6   r7   rZ   ÚloggerÚdebugrÁ   Úupdate_kwargÚget_submoduler\   r   r‰   r   rè   Ú_modify_graph_op_devicer   Úgraph_moduleÚwarningr(   Ú	recompile)rð   rñ   Úmodifiedrc   Úsubmods        r/   rù   rù   é  s'  € ð €HØ—‘—‘ó ˆØ�7‰7�oÒ%Ø˜4Ÿ;™;Ò&¨4¯;©;°xÑ+@ÀJÓ+NÜ—‘Ø.¨t¯y©y¨k¸ÀÇÁÈHÑ@UÐ?VÐVZÐ[eÐZfÐgôð ×!Ñ! (¨JÔ7Ø‘Ø�W‰W˜Ó%à×%Ñ% d§k¡kÓ2ˆFÜ˜&¤%§(¡(×"6Ñ"6Ô7Ü'¨°
Õ;Ü˜FÔ$5Ô6ä'¨×(;Ñ(;¸ZÖHä—‘ØAÀ$Ç+Á+ÀÐN_Ô`dÐekÓ`lÐ_mÐnöð#ñ* Ø
�‰�ð r0   c                   ó¶  — e Zd Zdej                  dedefd„Zd„ Zdede	j                  j                  fd„Zed	ej                  fd
„«       Ze	 	 	 dde	j                  j                  dedee   deee	j                  j                  ge	j                  j                  f      fd„«       Zd„ Ze	 dde	j                  j                  deedf   deeeef      defd„«       Ze	 	 d de	j                  j                  deedf   deeeef      deeej                  gej                  f      fd„«       Zd„ Zd„ Zdefd„Z	 ddede	j>                  dee    de!fd„Z"y)!ÚPipeÚsplit_gmÚ
num_stagesÚhas_loss_and_backwardc                 óB  — t         j                  j                  j                  | «       || _        t        | j                  «      | _        || _        || _        || _	        |j                  j                  D ]£  }|j                  dv rŒ|j                  |j                  fdt        j                  fk(  rŒ>|j                  |j                  fdk(  rŒZ|j                  |j                  fdt         fk(  rŒ||j                  |j                  fdt"        fk(  rŒžJ |«       ‚ i }| j                  j%                  «       D ]7  \  }}|j'                  «       D ]  \  }	}
|j)                  |
i «       |	||
   |<   Œ! Œ9 |j+                  «       D ��cg c]  \  }}t-        |«      dkD  r|‘Œ c}}| _        | j.                  D ]‡  }|j+                  «       D ]r  \  }}t1        | j                  |«      }|j3                  d«      }|d d D ]  }t1        ||«      }Œ t5        ||d   t7        j8                  t1        ||d   «      «      «       Œt Œ‰ d„ }|| j                  _        d}	 	 d	|› �}t1        | j                  |«      }t<        |j>                  _         |dz  }Œ7c c}}w # tB        $ r Y y w xY w)
N>   r4   rQ   rG   rC   )Úcall_methodÚbackwardr   ú.rE   c                 ó   — t        d«      ‚)NzHTo run pipeline locally, invoke the Pipe object directly, not `split_gm`)r   r–   s      r/   ÚthrowzPipe.__init__.<locals>.throwI  s   € ÜØZóð r0   r   Úsubmod_)"r‰   rŠ   rç   r‘   r  r¬   Úexecutorr  r  r:   rÌ   r6   r7   r\   r]   r^   r   r   Únamed_childrenÚnamed_parametersrN   Úitemsr"   Úreplicated_paramsÚgetattrÚsplitÚsetattrr|   Údeepcopyr„   rï   r“   Ú
__reduce__ÚAttributeError)r�   r  r  r  r:   rc   Úparams_to_usersÚ
m_qualnamer1   Ú
p_qualnameÚparamrl   Úuse_mappingÚparam_mappingÚsubmod_nameÚparam_qualnamerþ   ÚatomsÚatomr	  rn   rÁ   s                         r/   r‘   zPipe.__init__  s¡  € ô 	�‰�‰× Ñ  Ô&Ø(0ˆŒÜ(6°t·}±}Ó(EˆŒØ)ˆŒØ%:ˆÔ"Ø"ˆŒà—N‘N×(Ñ(ò 	ˆDà—‘ÐCÒCØ—G‘G˜TŸ[™[Ð)¨o¼x×?OÑ?OÐ-PÓPØ—G‘G˜TŸ[™[Ð)Ð-HÓHØ—G‘G˜TŸ[™[Ð)¨o¼~Ð-NÓNØ—G‘G˜TŸ[™[Ð)Ø#Ô%>Ð?ó@ðð óð@ð	ð& EGˆà#Ÿ}™}×;Ñ;Ó=ò 	@‰OˆJ˜Ø%(×%9Ñ%9Ó%;ò @Ñ!�
˜EØ×*Ñ*¨5°"Ô5Ø5?� Ñ& zÒ2ñ@ð	@ð #2×"7Ñ"7Ó"9÷8
á��;Ü�;Ó !Ò#ò ó8
ˆÔð "×3Ñ3ò 	VˆMØ/<×/BÑ/BÓ/Dò VÑ+�˜^Ü  §¡°Ó<�Ø&×,Ñ,¨SÓ1�Ø! # 2˜Jò 3�DÜ$ V¨TÓ2‘Fð3ä˜  b¡	¬4¯=©=¼ÀÈÈrÉÓ9SÓ+TÕUñVð	Vò	ð
 !&ˆ�‰Ôð ˆØðØ   �}�Ü  §¡°Ó5�Ü.J�× Ñ Ô+Ø�Q‘�ð ùó98
øôD "ò Ùðús   ÆJÉ5J Ê	JÊJc           	      óX  — |}t        |«      dkD  �rg }| j                  j                  j                  D �]  }|j                  dk(  sŒ|j
                  r[t        |j
                  «      dkD  rC|j                  t        |j                  t        j                  |j
                  d   ¬«      «       Œ{t        j                  }|j                  }|j                  j                  d«      rt        j                  }|dd  }n0|j                  j                  d«      rt        j                  }|dd  }|j                  t        ||«      «       �Œ t        |«      } |j                  |i |¤Ž}	|	j                  «        |	j                   j#                  «       } | j$                  j&                  |Ž }
|
S )Nr   rG   )Údefaultz**rD   Ú*r   )r"   r  rÌ   r6   r7   r8   rÐ   r   r\   ÚPOSITIONAL_OR_KEYWORDÚ
startswithÚVAR_KEYWORDÚVAR_POSITIONALr   ÚbindÚapply_defaultsÚ	argumentsÚvaluesr  r²   )r�   r8   rZ   Úexecutor_argsÚ
parametersrc   Úparameter_kindÚ
param_namer   ÚbaÚress              r/   r„   zPipe.forward[  s`  € ØˆÜˆv‹;˜‹?ØˆJØŸ™×+Ñ+×1Ñ1ó Q�Ø—7‘7˜mÓ+Ø—y’y¤S¨¯©£^°aÒ%7Ø"×)Ñ)Ü%Ø $§¡Ü )× ?Ñ ?Ø(,¯	©	°!©ôõô *3×)HÑ)H˜Ø%)§[¡[˜
ØŸ;™;×1Ñ1°$Ô7Ü-6×-BÑ-B˜NØ)3°A°B¨™JØ!Ÿ[™[×3Ñ3°CÔ8Ü-6×-EÑ-E˜NØ)3°A°B¨˜JØ"×)Ñ)¬)°JÀÓ*OÖPð'Qô( " *Ó-ˆIØ�—‘ Ð0¨Ñ0ˆBØ×ÑÔØŸL™L×/Ñ/Ó1ˆMàˆd�m‰m×Ñ Ð/ˆàˆ
r0   Ú	stage_idxÚreturnc                 óz   — |dk  s|| j                   k\  rt        d|› d�«      ‚t        | j                  d|› �«      S )zS
        Return a stage module corresponding to `stage_idx` of the `pipe`.
        r   zInvalid stage index ú!r
  )r  Ú
ValueErrorr  r  )r�   r1  s     r/   Úget_stage_modulezPipe.get_stage_module|  sC   € ð �qŠ=˜I¨¯©Ò8ÜÐ3°I°;¸aÐ@ÓAÐAÜ�t—}‘}¨°	¨{Ð&;Ó<Ð<r0   rð   c                 ó>  — d}i }| j                   j                  D ]  }|j                  dk(  sŒ|j                  j	                  d«      sŒ/t        |j                  t        d«      d  «      |j                  d<   |j                  |j                  d   «       |dz  }Œ� |S )Nr   rQ   r
  r1  r   )	rÌ   r6   r7   r\   r$  Úintr"   rÏ   rN   )rð   r  Ú
found_idxsrc   s       r/   Ú _number_and_count_forward_stagesz%Pipe._number_and_count_forward_stages„  s‹   € àˆ
Ø&(ˆ
Ø—H‘H—N‘Nò 	 ˆDØ�w‰w˜-Ó'¨D¯K©K×,BÑ,BÀ9Õ,MÜ),¨T¯[©[¼¸Y»Ð9IÐ-JÓ)K�—	‘	˜+Ñ&Ø×%Ñ% d§i¡i°Ñ&<Ô=Ø˜a‘‘
ð		 ð Ðr0   Nr1   Úexported_programÚmulti_use_param_specÚsplit_policyc                 óÊ  ‡.‡/‡0‡1— |j                  «       }|�t        j                  d«        ||«      }t        j                  |j	                  d¬«      «       i }|j
                  j                  D ]z  }|j                  dk(  sŒ|j                  |j                  |«       ||j                     |k7  sŒB|j                  ||j                     «       |j
                  j                  |«       Œ| d}t        «       }	t        |j
                  j                  «      D ]B  \  }
}|j                  |j                  fdt        fk(  sŒ(||
dz
  k(  r|	j                  |«       |
}ŒD |	D ]  }|j
                  j                  |«       Œ |j!                  «        d	Š/d
t"        j$                  fˆ/fd„}t'        || |«      Š0‰0j
                  j)                  «        ‰0j+                  «       D ]…  }t-        |t"        j.                  «      sŒ|j
                  j                  D ]?  }|j                  |j                  fdt0        fk(  sŒ%|j
                  j                  |«       ŒA |j!                  «        Œ‡ ‰0j3                  «       D ]G  \  }}t-        |t"        j.                  «      sŒ!t5        |j
                  «      }‰0j7                  ||«       ŒI d„ }g Š1d„ Š.ˆ.ˆ0ˆ1fd„}t9        t;        d„ ‰0j
                  j                  «      «      }|D ]Š  }t=        |j>                  «      dkD  r0t        j                  d|j                  › d|j>                  › d�«       |j>                  D ]1  }|j                  dk(  sJ ‚ |‰0|j                  |j                  «       Œ3 ŒŒ tA        t        «      }| jC                  d¬«      jE                  «       D ]"  \  }}|tG        |«         j                  |«       Œ$ | jI                  «       D ]"  \  }}|tG        |«         j                  |«       Œ$ i }|D ]t  } ‰.| |j                  «      \  }}t9        |tG        |«         «      }|r|||jJ                  <   ŒB|j                  |jL                  v sŒ[|j                  g||jJ                  <   Œv tA        t8        «      }| jC                  d¬«      jE                  «       D ]}  \  }}‰0j3                  «       D ]e  \  }}t-        |t"        j.                  «      sŒ! ‰.||«      \  }}|sŒ0|�Œ3||   jO                  |«       tQ        ||jS                  d«      d   |«       Œg Œ ‰1D ]  \  }}	 tU        ||«       Œ ‰0j3                  «       D ]W  \  }}t-        |t"        j.                  «      sŒ!tY        ||g «       |j
                  j[                  «        |j!                  «        ŒY |jE                  «       D �]*  \  }} t]        ‰0|«      }t        | «      }!d|fg}"|"rÇ|"j_                  «       \  }#}$t-        |$t"        j.                  t`        f«      r\|$j
                  j                  D ]C  }|j                  dk(  sŒ|#r|#dz   |j                  z   n|j                  }|!jc                  |«       ŒE |$j3                  «       D ]"  \  }%}&|"jO                  |#r|#dz   |%z   n|%|&f«       Œ$ |"rŒÇ|!D ]:  }||jS                  d«      }'}|'dd D ]  }(t]        ||(«      }Œ tU        ||'d   «       Œ< �Œ- |D ][  }te        jd                  |j>                  «      D ]  }|j                  dk(  sJ ‚ |||«       Œ ‰0j
                  j                  |«       Œ] ‰0jg                  «        ‰0j
                  j[                  «        ‰0j!                  «        th        jk                  ‰0«      })d}*|}+|�jtm        | ‰0j
                  |«      \  },}-}+|,�?to        ‰0j
                  |,|-«       ‰0j!                  «        d}*t        j                  d«       n#tq        d|›�«      ‚t        j                  d«       t        j                  d‰0› �«       ti        ‰0|)|*|+«      S # tV        $ r Y �Œ#w xY w)a  
        Additionally, the ``output_loss_value_spec`` value can be specified to disambiguate
        which value in the output of `forward` is the loss value on which PiPPy should apply
        backpropagation. For example, if your ``forward`` returns a tuple ``(loss, model_out)``,
        you can specify ``output_loss_value_spec=(True, False)``. Or, if your ``forward`` returns
        a dict ``{'loss': loss_value, 'model_out': model_out}``, you can specify
        ``output_loss_value_spec={'loss': True, 'model_out': False}``
        NzAuto-splitting modelF)Úprint_outputÚget_attrrE   rC   r   r   r<   c                 ó„   •— | j                   | j                  fdt        fk(  rt        j	                  d‰› �«       ‰dz  Š‰S )NrC   zFound pipe_split r   )r7   r\   Úaten_pipe_split_aliasrõ   rö   )r<   Úpart_idxs    €r/   Úsplit_callbackz)Pipe._from_traced.<locals>.split_callbackÐ  sG   ø€ à—‘�a—h‘hÐØÜ%ð$ò ô —‘Ð0°°
Ð;Ô<Ø˜A‘�ØˆOr0   c           	      ór  — t        |j                  «      dk(  sJ ‚t        |j                  «      D ��cg c]  \  }}|| k(  sŒ|‘Œ }}}t        |«      dk(  sJ ‚t	        |j                  «      }|j                  |d   «       t        |«      |_        t        j                  d| › d|› d|d   › �«       yc c}}w )zÌ
            Delete reference of `node` from `user`'s arg list.
            Args:
                - node: a `get_attr` node at root.
                - user: a submodule node that uses `node`.
            r   r   zDeleted z from user z, arg index = N)	r"   rZ   ra   r8   r!   rí   r    rõ   rö   )rc   Úuserrn   r×   Úuse_idxsÚ	args_copys         r/   Údelete_user_referencez0Pipe._from_traced.<locals>.delete_user_referenceò  s¨   € ô �t—{‘{Ó# qÒ(Ð(Ð(Ü(1°$·)±)Ó(<×L™f˜a ÀÀtÃšÐLˆHÑLÜ�x“= AÒ%Ð%Ð%Ü˜TŸY™Y›ˆIØ�M‰M˜( 1™+Ô&Ü˜iÓ(ˆDŒIÜ�L‰LØ˜4˜& ¨D¨6°ÀÈÁ¸}ÐMõùó Ms   ³B3ÁB3c                 ó¸   — |j                  d«      }|d d D ]  }t        | |«      s yt        | |«      } Œ t        | |d   «      s| d fS t        | |d   «      }| |fS )Nr  rE   ©NN)r  Úhasattrr  )r1   Úfqnr  r  Úattrs        r/   Ú_recursive_getattr_with_parentz9Pipe._from_traced.<locals>._recursive_getattr_with_parent  sq   € à—I‘I˜c“NˆEØ˜c˜r˜
ò )�Ü˜s DÔ)Ù%Ü˜c 4Ó(‘ð)ô ˜3  b¡	Ô*Ø˜D�yÐ Ü˜3  b¡	Ó*ˆDØ˜�9Ðr0   c           	      ó„  •— |j                  d«      } ‰‰	|«      \  }}|d   |j                  v }t        |t        j                  «      s^J d|› dt        j                  › dt        |«      › d�t        |t        j                  j                  «      rd|› d|› d�z   «       ‚d	z   «       ‚| j                  |«      }t        ||«      rJ d
|› d|› �«       ‚|rt        |||t        j                  d¬«       nt        |||t        j                  ¬«       t        j                  d|› d|› �«       ‰
j!                  ||d   f«       y)a'  
            Move a parameter from the root module to a submodule.
            Args:
                root: The root module.
                callee_name: The name of the submodule to move the parameter to.
                param_fqn: The fully qualified name of the parameter to move.
            r  rE   z
Expected 'z' to be z	 but got z It might happen if module 'zu' was passed to some 'leaf function'(see https://pytorch.org/docs/stable/fx.html#fx.wrap). Please inspect usages of 'z' in the traced graph.Ú zModule z already has a parameter named T)Ú	attr_kindÚ
persistent)rR  zMoved parameter rô   N)r  Ú_buffersr   r‰   rµ   r(   rŠ   rç   rø   rL  r   r   ÚBUFFERÚ	PARAMETERrõ   rö   rÐ   )ÚrootÚcallee_nameÚ	param_fqnr  Úmod_itrÚ	param_valÚ	is_bufferÚcalleerO  r  Ú	to_deletes           €€€r/   Úmove_param_to_calleez/Pipe._from_traced.<locals>.move_param_to_callee  sk  ø€ ð —O‘O CÓ(ˆEÙ!?ÀÀyÓ!QÑˆG�Yà˜b™	 W×%5Ñ%5Ð5ˆIô ˜i¬¯©Ô6ð 	Ø˜Y˜K x´·±¨~¸YÄtÈIÃÐFWÐWXÐYô
 " )¬U¯X©X¯_©_Ô=ð 3°9°+ð >"à"+ Ð,BðDñó	Ð6ð ñó	Ð6ð ×'Ñ'¨Ó4ˆFÜ˜v yÔ1ð Ø˜+˜Ð&EÀiÀ[ÐQóÐ1ñ
 ÜØØØÜ'×.Ñ.Ø#öô ØØØÜ'×1Ñ1õ	ô �L‰LÐ+¨I¨;°d¸;¸-ÐHÔIð ×Ñ˜g u¨R¡yÐ1Õ2r0   c                 ó    — | j                   dk(  S )Nr@  )r7   rÉ   s    r/   rÊ   z#Pipe._from_traced.<locals>.<lambda>T  s   € ¨1¯4©4°:Ñ+=€ r0   z
Parameter z used in multiple stages: r  rQ   T)Ú	keep_varsrQ  z5Pipeline is in training mode, backward pass generatedz@Did not find any loss value according to output_loss_value_spec=z:Pipeline is in inference mode, backward pass not generatedzFull pipe model:
)9rƒ   rõ   Úinforö   Úprint_readablerÌ   r6   r7   rN   r\   Úreplace_all_uses_withÚ
erase_noder&   ra   r€   Úaddrü   r   r   r   Úeliminate_dead_codeÚmodulesr   rè   rB  r  r   Úregister_moduler!   Úfilterr"   Úusersr   Ú
state_dictr  ÚidÚnamed_buffersrÁ   Ú	constantsrÐ   r  r  Údelattrr  r   Úlintr  rí   r   Údiscardr|   Údelete_all_unused_submodulesr   r:  rA   rw   r   )2r1   r;  r<  r;   r=  ÚtracedÚget_attr_nodesrc   Úprev_pipe_split_idxÚpipe_split_nodes_to_erasern   rD  Ú	submodulerÁ   Ú
new_submodrI  r_  Ú
attr_nodesrF  Ú
id_to_fqnsrM  ÚtensorÚinputs_to_staterN  rl   ÚfqnsÚadded_attributesrþ   ÚparentÚchildrZ  Ú	last_atomÚ
attributesÚunused_attributesÚstackÚscopeÚ_modÚ_nameÚ_submodr  r  r  r  Úgenerated_loss_specr@   r>   rO  rC  r  r^  s2                                                 @@@@r/   Ú_from_tracedzPipe._from_traced™  sÞ  û€ ð& "×(Ñ(Ó*ˆàÐ#Ü�K‰KÐ.Ô/Ù! &Ó)ˆFä�‰�V×*Ñ*¸Ð*Ó>Ô?ð
 .0ˆØ—L‘L×&Ñ&ò 	2ˆDØ�w‰w˜*Ó$Ø×)Ñ)¨$¯+©+°tÔ<à! $§+¡+Ñ.°$Ó6Ø×.Ñ.¨~¸d¿k¹kÑ/JÔKØ—L‘L×+Ñ+¨DÕ1ð	2ð !ÐÜ$'£EÐ!Ü  §¡×!3Ñ!3Ó4ò 	(‰GˆAˆtØ—‘˜Ÿ™Ð%¨/¼:Ð)FÓFØ&¨!¨a©%Ò/Ø-×1Ñ1°$Ô7Ø&'Ñ#ð		(ð .ò 	*ˆDØ�L‰L×#Ñ# DÕ)ð	*ð 	×ÑÔàˆð	œbŸg™gõ 	ô ˜V S¨.Ó9ˆà�‰×'Ñ'Ô)ð Ÿ™›ò 	&ˆIÜ˜)¤R§^¡^Õ4Ø%ŸO™O×1Ñ1ò 9�DØŸ™ §¡Ð-Ø'Ü-ð2ó ð "Ÿ™×2Ñ2°4Õ8ð9ð ×#Ñ#Õ%ð	&ð  %×3Ñ3Ó5ò 	8‰OˆD�)Ü˜)¤R§^¡^Õ4Ü0°·±ÓA�
à×%Ñ% d¨JÕ7ð		8ò	ð& ˆ	ò
	ö>	3ôB œ&Ñ!=¸u¿{¹{×?PÑ?PÓQÓRˆ
Øò 	ˆDä�4—:‘:‹ Ò"Ü—‘Ø  §¡ Ð-GÈÏ
É
À|ÐSTÐUôð Ÿ
™
ò �Ø—w‘w -Ò/Ð/Ð/á$ØØ—K‘KØ—K‘Kõñð	ô" +6´cÓ*:ˆ
ØŸ>™>°D˜>Ó9×?Ñ?ÓAò 	,‰KˆC�Ø”r˜&“zÑ"×&Ñ& sÕ+ð	,à×,Ñ,Ó.ò 	,‰KˆC�Ø”r˜&“zÑ"×&Ñ& sÕ+ð	,ð 13ˆØò 	;ˆDÙ6°s¸D¿K¹KÓH‰IˆAˆvÜ˜
¤2 f£:Ñ.Ó/ˆDÙØ-1� §	¡	Ò*Ø—‘Ð 0× :Ñ :Ò:Ø.2¯k©k¨]� §	¡	Ò*ð	;ô 2=¼TÓ1BÐØŸ>™>°D˜>Ó9×?Ñ?ÓAò 	D‰KˆC�Ø %× 4Ñ 4Ó 6ò D‘��fÜ˜f¤b§n¡nÕ5Ù$BÀ6È3Ó$O‘M�F˜Eâ 5¡=à(¨Ñ.×5Ñ5°cÔ:Ü ¨¯	©	°#«°rÑ(:¸FÕCñDð	Dð #,ò 	ÑˆG�YðÜ˜ Õ+ð	ð "×0Ñ0Ó2ò 	#‰LˆD�&Ü˜&¤"§.¡.Õ1Ü˜V _°bÔ9Ø—‘×!Ñ!Ô#Ø× Ñ Õ"ð		#ð !1× 6Ñ 6Ó 8ó 	,ÑˆD�*Ü˜U DÓ)ˆFÜ # J£Ðà˜&�\�NˆEÙØ#Ÿi™i›k‘��tÜ˜d¤R§^¡^Ô5FÐ$GÔHØ $§
¡
× 0Ñ 0ò ;˜ØŸ7™7 jÓ0á?D %¨#¡+°·±Ò";È$Ï+É+˜CØ-×5Ñ5°cÕ:ð	;ð
 '+×&9Ñ&9Ó&;ò U‘N�E˜7Ø—L‘L¹ %¨#¡+°Ò"5ÀEÈ7Ð!SÕTðUò ð *ò ,�Ø!'¨¯©°C«˜�Ø! # 2˜Jò 5�DÜ% g¨tÓ4‘Gð5ä˜  r¡Õ+ò	,ð!	,ð, ò 	)ˆDäŸ	™	 $§*¡*Ó-ò 2�Ø—w‘w -Ò/Ð/Ð/Ù% d¨DÕ1ð2ð �K‰K×"Ñ" 4Õ(ð	)ð 	×*Ñ*Ô,Ø�‰×ÑÔØ�‰Ôä×:Ñ:¸5ÓAˆ
à %ÐØ4Ðà!Ð-Ü:KØ�U—[‘[Ð"8ó;Ñ7ˆI�{Ð$7ð Ð$Ü/Ø—K‘KØØôð
 —‘Ô!Ø(,Ð%Ü—‘ÐTÕUä"ØWÐ@VÐ?XÐYóð ô �L‰LÐUÔVä�‰Ð)¨%¨Ð1Ô2äØØØ!Øó	
ð 	
øôW "ò âðús   Õaá	a"á!a"c                 ó8   — | j                   j                  «        y)z~
        Print the pipe in a human-readable format.
        This will print both the root pipe and each stage module.
        N)r  rc  ©r�   s    r/   rc  zPipe.print_readableÞ  s   € ð
 	�‰×$Ñ$Õ&r0   Úexample_args.Úexample_kwargsc                 ó®   — t         j                  d«       	 t        j                  j	                  | ||«      }|S # t
        $ r}t        d«      |‚d }~ww xY w)NzTracing model ...a.  It seems that we cannot capture your model as a full graph. Typical reasons include graph breaks, data/shape-dependent control flow, or missing meta kernels for custom operators. You can use our manual pipeline interfaces, or try to fix the graph breaks, see https://pytorch.org/docs/stable/export.html)rõ   rb  r‰   ÚexportÚexport_for_trainingÚ	Exceptionr   )r1   rŽ  r�  ÚepÚes        r/   Ú_trace_with_exportzPipe._trace_with_exportå  sd   € ô 	�‰Ð'Ô(ð	Ü—‘×1Ñ1ØØØóˆBð ˆ	øô ò 	ÜðPóð ðûð	ús   —!: º	AÁAÁAc                 ó^  — t         j                  }d }	 t        j                  | ||«      }t        j	                  | ||||¬«      }|j
                  }|j                  «       }	t        t        |j                  «       «      «      }
t        |
j                  «      }t        |	j                  «      }t        |j                  «      t        |j                  «      k7  rDt        j                  dt        |j                  «      › dt        |j                  «      › d�«       |S t!        j"                  |	j$                  j&                  «      |
j$                  _        |
j$                  j&                  j(                  j+                  d ¬«      |
j$                  j&                  _        |
j-                  «        |S )N)r;   r=  zOriginal model takes z) args but the first pipeline stage takes z4. Please provide args to respective pipeline stages.)Úout_spec)r¨   rª   r   r–  r‹  r  rƒ   ÚnextÚiterÚchildrenr   r„   r"   r,  rõ   rb  r|   r  rÌ   Ú_codegenÚpytree_infoÚ_replacerü   )r1   rŽ  r�  r=  r<  r;   r;  Úpiper  rt  Úsubmod0Úsubmod0_signÚ
model_signs                r/   Úfrom_tracingzPipe.from_tracingý  s}  € ô  7×@Ñ@Ðð '+Ðð	ô  ×2Ñ2ØØØó
Ðô × Ñ ØØØ Ø#9Ø%ð !ó 
ˆð —‘ˆØ!×(Ñ(Ó*ˆÜ”t˜EŸN™NÓ,Ó-Ó.ˆÜ  §¡Ó1ˆÜ˜vŸ~™~Ó.ˆ
Üˆz×$Ñ$Ó%¬¨\×-DÑ-DÓ)EÒEô �K‰KØ'¬¨J×,AÑ,AÓ(BÐ'Cð D.Ü.1°,×2IÑ2IÓ.JÐ-Kð LEðEôð  ˆô &*§]¡]°6·<±<×3HÑ3HÓ%IˆG�M‰MÔ"ð
 —‘×&Ñ&×2Ñ2×;Ñ;ÀTÐ;ÓJð �M‰M×"Ñ"Ô.ð ×ÑÔàˆr0   c                 ó6   — | j                   j                  «       S rM   )r  Ú__str__r�  s    r/   r¥  zPipe.__str__@  s   € Ø�}‰}×$Ñ$Ó&Ð&r0   c                 ó6   — | j                   j                  «       S rM   )r  Ú__repr__r�  s    r/   r§  zPipe.__repr__C  s   € Ø�}‰}×%Ñ%Ó'Ð'r0   c                 ón   — t        | j                  j                  | j                  | j                  ¬«      S )z¢
        Get information about the pipe.

        Returns
        -------
        PipeInfo
            A dataclass containing information about the pipe.
        )rÌ   r  r  )r   r  rÌ   r  r  r�  s    r/   rb  z	Pipe.infoF  s.   € ô Ø—-‘-×%Ñ%Ø—‘Ø"&×"<Ñ"<ô
ð 	
r0   Ústage_indexró   Úgroupc                 ó  — | j                  |«      }t        |t        j                  j                  «      rt        ||«       n!t        j                  dt        |«      › �«       | j                  «       }t        |||||«      S )z“
        Create a `PipelineStage` given a stage index and distributed group.
        The `PipelineStage` can run with `PipelineSchedule`s.
        z*Expected a `torch.fx.GraphModule` but got )r6  r   r‰   r   rè   rù   rõ   rû   r(   rb  r   )r�   r©  ró   rª  Ústage_moduleÚ	pipe_infos         r/   Úbuild_stagezPipe.build_stageU  sp   € ð ×,Ñ,¨[Ó9ˆô �l¤E§H¡H×$8Ñ$8Ô9Ü# L°&Õ9ä�N‰NØ<¼TÀ,Ó=OÐ<PÐQôð —I‘I“Kˆ	Ü˜l¨K¸ÀFÈEÓRÐRr0   ©NNNrM   rK  )#r…   r†   r‡   r   rè   r8  Úboolr‘   r„   r‰   rŠ   rç   r6  rˆ   r:  r   r   ÚMultiUseParamSpecr
   r‹  rc  r    r	   r%   rÂ   r–  r£  r¥  r§  r   rb  ró   r   r   r®  rŒ   r0   r/   r   r     s!  „ ðLà—.‘.ðLð ðLð  $ó	Lò\ðB=¨#ð =°%·(±(·/±/ó =ð ð¨R¯^©^ò ó ðð( ð =AØ#ð ñB
Ø�X‰X�_‰_ðB
à)ðB
ð 'Ð'8Ñ9ðB
ð
 Ø�e—h‘h×*Ñ*Ð+¨U¯X©X×-AÑ-AÐAÑBñ
òB
ó ðB
òH
'ð ð 48ñØ�X‰X�_‰_ðà˜C ˜H‘oðð !  c¨3 h¡Ñ0ðð 
ò	ó ðð. ð 48ØMQñ	@Ø�X‰X�_‰_ð@à˜C ˜H‘oð@ð !  c¨3 h¡Ñ0ð@ð ˜x¨¯©Ð(8¸"¿.¹.Ð(HÑIÑJò	@ó ð@òD'ò(ð
�hó 
ð& )-ñ	 Sàð Sð —‘ð Sð ˜Ñ%ð	 Sð
 
ô Sr0   r   c                   ó   — e Zd ZdZdZdZy)Ú
SplitPointac  
    Enum representing the points at which a split can occur in the execution of a submodule.
    Attributes:
        BEGINNING: Represents adding a split point *before* the execution of a certain submodule in the `forward` function.
        END: Represents adding a split point *after* the execution of a certain submodule in the `forward` function.
    r   rD   N)r…   r†   r‡   r—   Ú	BEGINNINGÚENDrŒ   r0   r/   r³  r³  x  s   „ ñð €IØ
�Cr0   r³  c                   ó   — e Zd ZeZy)ÚPipeSplitWrapperN)r…   r†   r‡   r³  rŒ   r0   r/   r·  r·  †  s   „ à�Jr0   r·  c                 ó:   — t        «         | j                  |i |¤ŽS rM   )r€   Ú_orig_forwardr–   s      r/   Ú_split_before_forwardrº  ‹  s   € Ü„LØˆ4×Ñ˜tÐ. vÑ.Ð.r0   c                 óZ   — 	  | j                   |i |¤Žt        «        S # t        «        w xY wrM   )r¹  r€   r–   s      r/   Ú_split_after_forwardr¼  �  s'   € ðØ!ˆt×!Ñ! 4Ð2¨6Ñ2ä�øŒ
�ús   ‚ ž*r,   c                 ó  — |j                  «       D ]·  \  }}|j                  d«      }| }t        |d d «      D ]  \  }}	 t        ||«      }Œ t        ||d   «      }	|	j                  |	_        |t        j                  k(  rt        t        |	«      |	_        Œ…|t        j                  k(  rt        t        |	«      |	_        Œ®t        d«      ‚ y # t        $ r,}t	        d|› ddj                  |d |dz    «      › �«      |‚d }~ww xY w)Nr  rE   zSpecified target z referenced nonexistent module r   zUnknown split point type.)r  r  ra   r  r  Újoinr„   r¹  r³  r´  r   rº  rµ  r¼  r5  )
r1   r,   ÚqualnameÚ
split_typer  Úpredecessor_modulern   r  r•  Úmod_to_wraps
             r/   Úannotate_split_pointsrÃ  —  s  € à $§
¡
£ò :Ñˆ�*Ø—‘˜sÓ#ˆØ ÐÜ   s¨ Ó,ò 	‰GˆAˆtðÜ%,Ð-?ÀÓ%FÑ"ð	ô Ð0°%¸±)Ó<ˆØ$/×$7Ñ$7ˆÔ!Øœ×-Ñ-Ò-Ü",Ô-BÀKÓ"PˆKÕØœ:Ÿ>™>Ò)Ü",Ô-AÀ;Ó"OˆKÕäÐ8Ó9Ð9ñ':øô "ò Ü$Ø'¨ zð 2*Ø*-¯(©(°5¸¸1¸q¹5°>Ó*BÐ)CðEóð ðûðús   ¿CÃ	DÃ'C<Ã<Drƒ   Úmb_args.Ú	mb_kwargsÚ
split_specr=  r2  c                 óž   — |�|�t        d«      ‚|�$t        | |«       t        j                  | ||¬«      S t        j                  | |||¬«      S )a9  
    Split a module based on a specification.

    See `Pipe` for more details.

    Arguments
    ---------
    module:
        The module to be splitted.
    mb_args:
        Example positional inputs, in micro-batch form.
    mb_kwargs:
        Example keyword inputs, in micro-batch form. (default: `None`)
    split_spec:
        A dictionary using submodule names as split marker. (default: `None`)
    split_policy:
        The policy to use for splitting the module. (default: `None`)

    Returns
    -------
    A pipeline representation of class `Pipe`.
    zQCannot specify both `split_spec` and `split_policy`. Please use only one of them.)r1   rŽ  r�  )r1   rŽ  r�  r=  )r5  rÃ  r   r£  )rƒ   rÄ  rÅ  rÆ  r=  s        r/   ÚpipelinerÈ  ¯  sx   € ð: Ð ,Ð":ÜØ_ó
ð 	
ð Ðä˜f jÔ1Ü× Ñ ØØ Ø$ð !ó 
ð 	
ô × Ñ ØØ Ø$Ø%ð	 !ó 
ð 	
r0   r¯  )[r|   Úloggingr]   Úcollectionsr   Úenumr   Úinspectr   r   r   Útypesr   Útypingr	   r
   r   r   r‰   Útorch.fxr   Útorch.distributedr   Útorch.exportr   Útorch.export.unflattenr   r   r   r   Útorch.fx.noder   Útorch.fx.passes.split_moduler   Ú	_backwardr   r   Ú
_unflattenr   Ú_utilsr   Ústager   Ú	getLoggerr…   rõ   r$   rŠ   rç   rÛ   rA   r   rw   r‹   ry   rŽ   r9   ÚlibraryÚdefineÚimplr¡   Úregister_faker¥   r¦   r!  rB  rc   Ú_side_effectful_functionsrf  r€   r¨   r%   rÂ   r±  ÚInterpreterr¬   r¿   rÅ   rê   rï   rè   ró   rù   r   r³  r·  rº  r¼  rÃ  r    rÈ  rŒ   r0   r/   ú<module>rà     sõ  ðó Û Û Ý #Ý ß 3Ñ 3Ý ß 1Ó 1ã Ý Ý *Ý (÷ó õ (Ý 5ç @Ý +Ý Ý !ð 
ˆ×	Ñ	˜8Ó	$€ò,Sð^2˜5Ÿ8™8Ÿ?™?ð 2¨r¯x©xó 2ð8hØ	‡x�xðhà�w‰wðhð —‘óhôV
�U—X‘X×(Ñ(ô 
ô%
�%—(‘(—/‘/ô %
ôP˜ô ð> ‡�× Ñ Ð)¨:Ô 6ð ‡�×ÑÐ(¨/Ó:ñó ;ðð ‡�×ÑÐ1Ó2ñó 3ðð
 Ÿ	™	Ÿ™×3Ñ3×;Ñ;Ð ð ‡�× !Ñ !× %Ñ %Ð&;Ô <ò)ô(˜dô ð
 Ð1°4¸Ð=TÐ8TÑ3UÐUÑVÐ ô1;�R—^‘^ô 1;÷hñ ÷*ñ *òZ3ò,ð Ø�‰×Ñð à—‘ó ôFi	Sˆ5�8‰8�?‰?ô i	SôX	�ô 	÷ñ ò
/ò
ð:˜uŸx™xŸ™ð :°d¸3À
¸?Ñ6Kó :ð6 +/Ø26ØIMñ1
Ø�H‰H�O‰Oð1
à�3˜�8‰_ð1
ð ˜˜S #˜X™Ñ'ð1
ð ˜˜c :˜oÑ.Ñ/ð	1
ð
 ˜8 R§^¡^Ð$4°b·n±nÐ$DÑEÑFð1
ð 
ô1
r0   