+
    ˜°³jÆ1  ã                   ó®   € ^ RI t ^ RIHt ^ RIHtHt ^ RIt^ RIHt  ! R R]4      tR R lt	R	 R
 lt
R R ltR R lt] P                  R R l4       tR# )é    N)Údefaultdict)ÚAnyÚ
NamedTuple)ÚTensorc                   ó0   a € ] tR t^
t o RtV 3R ltRtV tR# )Ú_GroupSwapinInfoaõ  Per-group bookkeeping for ``swap_in_optimizer_params_and_state``.

Fields:
    live_group: live ``optimizer.param_groups[i]`` dict, mutated in place.
    swapin_group: per-group hyperparameter dict from the input state_dict
        (e.g. lr, betas, ...) plus packed ``params`` ids.
    swapin_params: replacement parameter tensors for ``live_group["params"]``.
        Same order as ``swapin_group["params"]`` (i.e. the i-th tensor
        corresponds to the i-th packed id), so the two are zippable.
c                óz   <€ V ^8„  d   Qh/ S[ S[S[3,          ;R&   S[ S[S[3,          ;R&   S[S[,          ;R&   # )é   Ú
live_groupÚswapin_groupÚswapin_params)ÚdictÚstrr   Úlistr   )ÚformatÚ__classdict__s   "€ÚW/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/optim/_stateless.pyÚ__annotate__Ú_GroupSwapinInfo.__annotate__
   s@   ø‡ ‚ ñ ‘S™#�X•Ññ ñ ‘s™C�x•.Ñ ñ ñ ™•<Ñò ó    © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)r   s   @r   r   r   
   s   ø‡ € ñ	÷ ƒ r   r   c                óR   € V ^8„  d   QhR\         R\        \         \         3,          /# )r
   ÚstateÚreturn)r   r   )r   s   "r   r   r      s"   € ÷ ñ ¤ð ¬¬c´3¨h­ñ r   c                óÂ  € \        V \        4      '       g#   \        R\        V 4      P                   R24      h\
        ;QJ d    R V  4       F  '       g   K   RM	  RM! R V  4       4      '       d   \        R4      h\
        ;QJ d*    R V P                  4        4       F  '       g   K   RM	  RM! R V P                  4        4       4      '       d   \        R4      hV # )	zPCheck ``state`` is a dict mapping packed parameter ids to per-param state dicts.z�swap_in_optimizer_params_and_state requires swapin_optim_state['state'] to be a dict mapping packed parameter ids to per-param state dicts, got Ú.c              3   óV   "  € T F  p\        V\        P                  4      x € K!  	  R # 5i©N)Ú
isinstanceÚtorchr   )Ú.0Úks   & r   Ú	<genexpr>Ú(_validate_state_field.<locals>.<genexpr>#   s   é € Ð
6±¨1Œ:�aœŸ™×&Ð&³ùs   ‚')TFzmswap_in_optimizer_params_and_state requires optimizer.state_dict()-style state keyed by packed parameter ids.c              3   ó€   "  € T F4  w  r\        V\        4      ;'       d    \        V\        4      '       * x € K6  	  R # 5ir&   )r'   Úintr   )r)   r*   Úvs   &  r   r+   r,   (   s.   é € Ð
TÁm¹d¸aŒ:�aœÓ×9Ð9¤j°´DÓ&9Ô"9Ô9Ãmùs   ‚> >zeswap_in_optimizer_params_and_state requires per-parameter optimizer state entries to be dictionaries.)r'   r   ÚRuntimeErrorÚtyper   ÚanyÚitems)r!   s   &r   Ú_validate_state_fieldr4      sµ   € ä�eœT×"Ò"Üðä˜“;×'Ñ'Ð(¨ð+ó
ð 	
÷
 ƒsÑ
6±Ó
6‡s‡s‚sÑ
6±Ó
6×6Ò6Üð3ó
ð 	
÷ ƒsÑ
TÀeÇkÁkÄmÓ
T‡s‡s‚sÑ
TÀeÇkÁkÄmÓ
T×TÒTÜð0ó
ð 	
ð €Lr   c          	      ól   € V ^8„  d   QhRRR\         R\        \        \        \         3,          ,          /# )r
   Ú	optimizerútorch.optim.OptimizerÚparam_groupsr"   )r   r   r   r   )r   s   "r   r   r   0   s1   € ÷ ñ Ø&ðÜ69ðä	Œ$Œs”Cˆx�.Õñr   c                óÒ   € \        V\        4      '       g#   \        R\        V4      P                   R24      h\        V P                  4      \        V4      8w  d   \        R4      hV# )zW``param_groups`` must be a list whose length matches the live optimizer's param groups.zvswap_in_optimizer_params_and_state requires swapin_optim_state['param_groups'] to be a list of param-group dicts, got r$   zVswapin_optim_state has a different number of parameter groups than the live optimizer.)r'   r   r0   r1   r   Úlenr8   )r6   r8   s   &&r   Ú_validate_param_groups_fieldr;   0   sq   € ô �l¤D×)Ò)Üð6Ü6:¸<Ó6H×6QÑ6QÐ5RÐRSðUó
ð 	
ô ˆ9×!Ñ!Ó"¤c¨,Ó&7Ô7Üð"ó
ð 	
ð Ðr   c          	      ó€   € V ^8„  d   QhR\         R\        \        \        3,          R\        R\        \         ,          /# )r
   ÚidxÚgroupr   r"   )r.   r   r   r   r   )r   s   "r   r   r   A   s<   € ÷ (ñ (Ü	ð(ä””S��>ð(ô ð(ô 
Œ#…Yñ	(r   c           	     ó@  € \        V\        4      '       g   \        R4      hVP                  R4      p\        V\        4      '       d;   \
        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       g   \        R4      h\        VR,          4      \        V4      8w  d   \        RV  R24      hV Uu. uF  qDV9  g   K  VNK  	  ppV Uu. uF  qDV9  g   K  VNK  	  ppV'       g	   V'       d   \        R	V  R
V RV R24      hV# u upi u upi )a>  Validate a single swap-in param group against its live counterpart and
return its packed parameter ids.

Asserts:
1. swap-in group is a dict.
2. ``group['params']`` is a list of ints, and its length matches the params of the live optim group.
3. Keys inside swapin group should match the keys in the live optim group.
zZswap_in_optimizer_params_and_state requires each optimizer param group to be a dictionary.Úparamsc              3   óB   "  € T F  p\        V\        4      x € K  	  R # 5ir&   )r'   r.   )r)   Úpids   & r   r+   Ú/_validate_group_against_live.<locals>.<genexpr>T   s   é € ð 9Ù(8 Œ
�3œ×ÐÓ(8ùs   ‚FTz‰swap_in_optimizer_params_and_state requires optimizer.state_dict()-style param_groups[*]['params'] entries keyed by packed parameter ids.zswapin_optim_state param group zF has a different number of params than the live optimizer param group.z“swap_in_optimizer_params_and_state requires optimizer.state_dict()-style param group keys to exactly match the live optimizer group keys for group z. Keys only in swap-in: z. Keys only in live: r$   )r'   r   r0   Úgetr   Úallr:   )r=   r>   r   Úswapin_param_idsr*   Úswapin_onlyÚ	live_onlys   &&&    r   Ú_validate_group_against_liverI   A   s;  € ô �l¤D×)Ò)Üð"ó
ð 	
ð $×'Ñ'¨Ó1ÐÜÐ&¬×-Ò-·S³Sñ 9Ù(8ó9·S·S²Sñ 9Ù(8ó9÷ 6ò 6ô ðOó
ð 	
ô ˆ5��?ÓœsÐ#3Ó4Ô4ÜØ-¨c¨Uð 3:ð :ó
ð 	
ñ +Ó=™l˜°u©n—1�1™l€KÐ=Ù!Ó;™E�q¨lÑ%:—�™E€IÐ;ß—iÜðà�Eð %Ø%0 MÐ1FÀyÀkÐQRðTó
ð 	
ð Ðùò >ùÚ;s   ÃDÃDÃDÃ'Dc                óÞ   € V ^8„  d   QhRRR\         \        \        3,          R\         \        \        3,          R\        \         \        \        3,          \
        \        ,          3,          /# )r
   r6   r7   Úswapin_parametersÚswapin_optim_stater"   )r   r   r   r   Útupler   r   )r   s   "r   r   r   l   s_   € ÷ Q,ñ Q,Ø&ðQ,äœC¤˜KÕ(ðQ,ô œS¤#˜X�ðQ,ô Œ4””S��>œ4Ô 0Õ1Ð1Õ2ñ	Q,r   c           	     óÚ  € V P                   '       g   \        R4      h\        V\        4      '       g   \        R4      h\	        VP                  R4      4      p\        WP                  R4      4      p\        VP                  4       4      p^ p\        4       p. p\        \        V P                  VRR7      4       Fu  w  p	w  r«\        WšV4      pVP                  V4       V\        V4      ,           pV\        V4      8”  d   \        R4      hWVV pTpVP!                  \#        V
VVR7      4       Kw  	  V Uu. uF  qÿV9  g   K  VNK  	  ppV'       d   \        R	V: R
24      hW83# u upi )a�  
Validate and normalize optimizer state for ``swap_in_optimizer_params_and_state``.

This follows the same structural assumptions as DCP-compatible optimizers,
but consumes the raw ``optimizer.state_dict()`` format:
``state`` is keyed by packed parameter ids and each param group contains
the live optimizer group fields plus a packed ``params`` list whose order
matches ``optimizer.param_groups``.
zHswap_in_optimizer_params_and_state requires initialized optimizer state.zMswap_in_optimizer_params_and_state requires a DCP-style optimizer state_dict.r!   r8   T©Ústrictzrswap_in_optimizer_params_and_state requires the explicit parameter state to match optimizer.param_groups ordering.)r   r   r   z swap_in_optimizer_params_and_state requires swapin_optim_state['state'] to be keyed only by packed parameter ids from param_groups[*]['params']; got extra keys r$   )r!   r0   r'   r   r4   rD   r;   r   ÚvaluesÚsetÚ	enumerateÚzipr8   rI   Úupdater:   Úappendr   )r6   rK   rL   Úswapin_stateÚswapin_param_groupsÚflat_parametersÚflat_param_offsetÚseen_param_idsÚgroup_swapin_infosr=   r>   r   rF   Únext_offsetr   r*   Ú
extra_keyss   &&&              r   Ú_prepare_swap_inr_   l   s’  € ð �?�?ˆ?ÜØVó
ð 	
ô Ð(¬$×/Ò/ÜØ[ó
ð 	
ô )Ð);×)?Ñ)?ÀÓ)HÓI€LÜ6Ø×)Ñ)¨.Ó9óÐô Ð,×3Ñ3Ó5Ó6€OØÐÜ"›u€NØÐÜ&/ÜˆI×"Ñ"Ð$7ÀÔEö'Ñ"ˆÑ"ˆeô 8¸ÀLÓQÐØ×ÑÐ.Ô/à'¬#Ð.>Ó*?Õ?ˆØœ˜_Ó-Ô-Üð9óð ð (¸+ÐFˆØ'Ðð  	×!Ñ!ÜØ Ø)Ø+ôö	
ñ;'ñJ *ÓE™\˜°nÑ-D—!�!™\€JÐEßÜð9à9C¹ÀaðIó
ð 	
ð
 Ð+Ð+ùò Fs   Ä8E(ÅE(c                óx   € V ^8„  d   QhRRR\         \        \        3,          R\         \        \        3,          /# )r
   r6   r7   rK   rL   )r   r   r   r   )r   s   "r   r   r   Á   s<   € ÷ k)ñ k)Ø&ðk)äœC¤˜KÕ(ðk)ô œS¤#˜X�ñk)r   c              #  óH  "  € \        WV4      w  r4V P                  pV P                   Uu. uF  p\        V4      NK  	  pp \	        \        4      pV F¥  p	V	P
                  V	P                  R&   V	P                  P                  4        F  w  r«V
R8X  d   K  W¹P                  V
&   K  	  \        V	P
                  V	P                  R,          RR7       F"  w  rÍ\        VP                  V/ 4      4      WŒ&   K$  	  K§  	  W€n        Rx € \        V P                  VRR7       F&  w  rïVP                  4        VP                  V4       K(  	  WPn        R# u upi   \        T P                  TRR7       F&  w  rïTP                  4        TP                  T4       K(  	  YPn        i ; i5i)uú	  Temporarily replace an optimizer's parameters and state with the
supplied params and optim states, then restore them on exit.

For the duration of the context, all optimizer APIs (including
user hooks) see the swap-in values; the live optimizer is restored on
exit.

The difference between this API and ``optimizer.load_state_dict`` is
that ``optimizer.load_state_dict`` only updates the optimizer's state
and leaves the parameters in ``param_groups`` untouched. This API also
swaps in the parameters, so that ``optimizer.step()`` acts on the
swap-in parameter tensors you supply.

Args:
    optimizer: the live optimizer; its state must already be
        initialized.
    swapin_parameters: tensors to use as parameters during the context,
        provided in the same order as the existing input parameters to
        the optimizer (most commonly in ``model.named_parameters()``
        order).
    swapin_optim_state: an ``optimizer.state_dict()``-shaped dict
        (``{"state": ..., "param_groups": ...}``) holding the state to
        install. ``"state"`` is keyed by packed integer parameter ids
        and ``"param_groups"`` mirrors ``optimizer.param_groups``,
        with each ``"params"`` entry as a list of those packed ids
        and the remaining keys carrying per-group hyperparameters
        (``lr``, ``betas``, ``foreach``, ``capturable``, ...).
        Only in-place tensor edits propagate back to the user supplied
        ``swapin_optim_state``; all other side-effects (e.g.,
        assigning a new tensor to the optim state)
        are ignored.

Example:

    One use of this API is to run ``optimizer.step()`` against
    ``FakeTensor`` versions of the parameters and state for nonstrict
    tracing â€” capturing an FX graph of the step without touching the
    live optimizer::

        from torch.fx.experimental.proxy_tensor import make_fx
        from torch._subclasses import FakeTensorMode
        from torch.utils import _pytree as pytree

        fake_mode = FakeTensorMode(allow_non_fake_inputs=True)
        with fake_mode:
            fake_params = {
                n: fake_mode.from_tensor(p) for n, p in model.named_parameters()
            }
            fake_osd = pytree.tree_map_only(
                torch.Tensor, fake_mode.from_tensor, optimizer.state_dict()
            )


        def step_fn(params, osd):
            with swap_in_optimizer_params_and_state(optimizer, params, osd):
                optimizer.step()
            return params, osd


        gm = make_fx(step_fn)(fake_params, fake_osd)
r@   TrO   N)r_   r!   r8   r   r   r   r   r   r3   rT   rD   ÚclearrU   )r6   rK   rL   r!   r\   Úoriginal_stateÚgÚoriginal_group_snapshotsrW   ÚinfoÚkeyÚvalueÚswapin_paramÚparam_idr>   Úsnapshots   &&&             r   Ú"swap_in_optimizer_params_and_staterl   À   s  é € ôF !1ØÐ&8ó!Ñ€Eð —_‘_€Nð
 2;×1GÒ1GÓHÑ1G¨A¤ Q¦Ñ1GÐÐHð)Ü1<¼TÓ1Bˆã&ˆDð )-×(:Ñ(:ˆD�O‰O˜HÑ%Ø"×/Ñ/×5Ñ5Ö7‘
�Ø˜(”?ÙØ',—‘ Ó$ñ 8ô
 +.Ø×"Ñ" D×$5Ñ$5°hÕ$?È÷+Ñ&�ô .2°%·)±)¸HÀbÓ2IÓ-J�Ó*ó+ñ 'ð$ 'ŒÛô  #Ø×"Ñ"Ð$<ÀT÷ 
‰OˆEð �K‰KŒMØ�L‰L˜Ö"ñ	 
ð
 )ŽùòA  Iøô6  #Ø×"Ñ"Ð$<ÀT÷ 
‰OˆEð �K‰KŒMØ�L‰L˜Ö"ñ	 
ð
 )�üs*   ‚)F"«E¿F"ÁCE ÄAF"ÅA
FÆF")Ú
contextlibÚcollectionsr   Útypingr   r   r(   r   r   r4   r;   rI   r_   Úcontextmanagerrl   r   r   r   Ú<module>rq      sS   ðã Ý #ß "ã Ý ô �zô  õ"õ*õ"(õVQ,ðh ×Ñôk)ó òk)r   