§
    Ÿ²Åj„Ò  ã                   óÜ
  — U d dl Z d dlZd dlZd dl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mZmZmZmZ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#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z,m-Z-m.Z.m/Z/m0Z0m1Z1 d d	l2m3Z3m4Z4 d d
l5m6Z6 d dl7m8Z8 d dl9m:Z; d dl<m=Z= g d¢Z>dZ?dZ@dZAdZBeeC         ZDee6eejE        eFeGeCf         ZHeeHeeH         eeH         eeCdf         f         ZIeeCeIf         ZJeeJ         ZKeeCeeJeKf         f         ZL eM¦   «         ZNee         eOd<   e jP        d„ ¦   «         ZQe G d„ d¦  «        ¦   «         ZRe G d„ deR¦  «        ¦   «         ZS ejT        d¬¦  «        	 	 dIdejU        deCdeVdeVd eDf
d!„¦   «         ZW G d"„ d#¦  «        ZXd$„ ZYddd%œdejU        d&eejZ        j[        d'f         d(eVd)eeejU                          d*eeR         d eSfd+„Z\d,eeCeIf         d-eLd.eSd dfd/„Z]d0eejU        ejZ        j[        f         d1eCd efd2„Z^d3eeCef         d.eSd eeCef         fd4„Z_ ej`        ¦   «         dejU        d.eSd eeCeIf         fd5„¦   «         Za ej`        ¦   «         dejU        d3eeCeIf         d.eSd e8fd6„¦   «         Zbd7ejZ        j[        d dfd8„Zcd3eLd eeCeIf         fd9„Zdd7ejZ        j[        d3eeCeIf         d.eSd eLfd:„Ze ej`        ¦   «         dejU        d;eejZ        j[        d'f         d.eSd eLfd<„¦   «         ZfdejU        d7ejZ        j[        d-eLd.eSd eLf
d=„Zg ej`        ¦   «         dejU        d;eejZ        j[        d'f         d3eLd.eSd df
d>„¦   «         Zhddd%œdejU        d)eeejU                          d*eeR         d eeCeIf         fd?„Ziddd%œdejU        d;eejZ        j[        eejZ        j[                 f         d)eeejU                          d*eeR         d eLf
d@„Zjddd%œdejU        d;eejZ        j[        eejZ        j[                 f         d)eeejU                          d*eeR         d eeeCeIf         eLf         f
dA„ZkdejU        d3eeejU        eeCeIf         f         eeCeIf         f         d eeCeIf         fdB„ZlddCœdejU        d,eeCeIf         d*eeR         d e8fdD„ZmddCœdejU        d;eejZ        j[        eejZ        j[                 f         d-eLd*eeR         d df
dE„ZnddCœdejU        d;eejZ        j[        eejZ        j[                 f         d,eeCeIf         d-eLd*eeR         d e8fdF„ZoeddCœdejU        d*eeR         d dfdG„¦   «         ZpeddCœdejU        d;eejZ        j[        d'f         d*eeR         d dfdH„¦   «         ZqdS )Jé    N)ÚasdictÚ	dataclassÚfield)Úchain)ÚAnyÚCallableÚcastÚDictÚ	GeneratorÚIterableÚListÚno_type_checkÚOptionalÚSetÚTupleÚUnion)ÚShardedTensor)Ú_broadcast_state_dictÚ_distribute_state_dictÚ_flatten_state_dictÚ_gather_state_dictÚ_offload_state_dict_to_cpuÚ_unflatten_state_dict)Ú_CHECKPOINT_PREFIX)ÚFullOptimStateDictConfigÚFullStateDictConfigÚFullyShardedDataParallelÚOptimStateDictConfigÚShardedOptimStateDictConfigÚShardedStateDictConfigÚStateDictConfigÚStateDictType)Ú._get_module_fsdp_state_if_fully_sharded_moduleÚFSDP_WRAPPED_MODULE)ÚDTensor)Ú_IncompatibleKeys)ÚDistributedDataParallel)Útree_map_only)ÚFQNS_TÚPrimitiveTypeÚ	ValueTypeÚDictValueTypeÚListDictValueTypeÚOptimizerStateTypeÚStateDictOptionsÚget_model_state_dictÚget_optimizer_state_dictÚget_state_dictÚset_model_state_dictÚset_optimizer_state_dictÚset_state_dictÚ_flat_paramÚparam_groupsÚparamsÚstater+   Ú_patched_state_dictc               #   óÄ   K  — t          j        ¦   «         } t          j        ¦   «          	 d V — | rt          j        ¦   «          d S d S # | rt          j        ¦   «          w w xY w©N)ÚgcÚ	isenabledÚdisableÚenable)Ú
is_enableds    úXC:\Projects\Historical\venv\Lib\site-packages\torch/distributed/checkpoint/state_dict.pyÚ_gc_contextrC   ]   sh   è è € å”‘”€JÝ„J�L„L€LðØˆˆˆàð 	ÝŒI‰KŒKˆKˆKˆKð	ð 	øˆ:ð 	ÝŒI‰KŒKˆKˆKð	øøøs   ªA ÁAc                   óv   — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   dZeed	<   dZeed
<   dS )r/   ap  
    This dataclass specifies how get_state_dict/set_state_dict will work.

    - ``full_state_dict``: if this is set to True, all the tensors in the
      returned state_dict will be gathered. No ShardedTensor and DTensor
      will be in the returned state_dict.

    - ``cpu_offload``: offload all the tensors to cpu. To prevent CPU OOM, if
      ``full_state_dict`` is also true, then only the rank0 will get the
      state_dict and all other ranks will get empty state_dict.

    - ``ignore_frozen_params``: if the value is True, the returned state_dict
      won't contain any frozen parameters -- the ``requires_grad`` is False.
      The default value is False.

    - ``keep_submodule_prefixes`` (deprecated): when ``submodules`` is not None, this option
      indicates whether to keep the submodule prefixes from the state_dict keys.
      or example, if the submodule is ``module.pretrain`` and the full FQN of
      the parameter is ``pretrain.layer1.weight`` of the param. When this option
      is True, the parameter's key in the returned state_dict will be
      ``pretrain.layer1.weight``. If the options is False, the key will be
      ``layer1.weight``.
      Note that if ``keep_submodule_prefixes`` is False, there may be conflicted
      FQNs, hence there should be only one submodule in ``submodules``.

    - ``strict``: the ``strict`` option when ``set_state_dict`` calls
      model.load_state_dict().

    - ``broadcast_from_rank0``: when the option is True, rank0 should receive a
       full state_dict and will broadcast the tensors in the state_dict/
       optim_state_dict one by one to other ranks. Other ranks will receive
       the tensors and shard according to the local shards in the model and
       optimizer. ``full_state_dict`` must be set to True when using this option.
       This option currently only supports DTensor, not the legacy ShardedTensor.
    FÚfull_state_dictÚcpu_offloadÚignore_frozen_paramsTÚkeep_submodule_prefixesÚstrictÚbroadcast_from_rank0Úflatten_optimizer_state_dictN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   ÚboolÚ__annotations__rF   rG   rH   rI   rJ   rK   © ó    rB   r/   r/   h   s“   € € € € € € ð"ð "ðH "€O�TÐ!Ð!Ñ!Ø€K�ÐÐÑØ!&Ð˜$Ð&Ð&Ñ&Ø$(Ð˜TÐ(Ð(Ñ(Ø€FˆDÐÐÑØ!&Ð˜$Ð&Ð&Ñ&Ø).Ð  $Ð.Ð.Ñ.Ð.Ð.rS   r/   c                   óv  — e Zd ZU  ee¬¦  «        Zeeee	j
        f         eee	j
        f         f         ed<    ee¬¦  «        Zeeee	j
        f         eee	j
        f         f         ed<    ee¬¦  «        Zee         ed<   dZeed<   dZeed<   ej        Zeed<    ee¬¦  «        Zeej                 ed	<   d
S )Ú_StateDictInfo)Údefault_factoryÚfqn_param_mappingÚshared_params_mappingÚsubmodule_prefixesTÚhandle_modelÚhandle_optimÚfsdp_contextÚfsdp_modulesN)rL   rM   rN   r   ÚdictrW   r
   r   ÚstrÚtorchÚTensorr)   rQ   rX   ÚsetrY   r   rZ   rP   r[   Ú
contextlibÚnullcontextr\   r   Úlistr]   r   ÚnnÚModulerR   rS   rB   rU   rU   —   s&  € € € € € € ð 	ˆ˜dÐ#Ñ#Ô#ð �tØˆc�5”<ÐÔ  %¨°´Ð(<Ô"=Ð=ôð $ð $ñ $ð
 	ˆ˜dÐ#Ñ#Ô#ð ˜4Øˆc�5”<ÐÔ  %¨°´Ð(<Ô"=Ð=ôð $ð $ñ $ð $) 5¸Ð#=Ñ#=Ô#=Ð˜˜CœÐ=Ð=Ñ=Ø€L�$ÐÐÑØ€L�$ÐÐÑØ'Ô3€L�(Ð3Ð3Ñ3Ø$) E¸$Ð$?Ñ$?Ô$?€L�$�r”y”/Ð?Ð?Ñ?Ð?Ð?rS   rU   )ÚmaxsizeTÚmodelÚnameÚskip_ddp_prefixÚskip_compiler_prefixÚreturnc                 óz  ‡
— |                      t          d¦  «        }d|vr|hS |                     d¦  «        }g }| }t          |¦  «        D �]À\  }}t	          |t
          ¦  «        r'|dk    sJ ‚|j        }|s|                     |¦  «         ŒBt	          |t          ¦  «        r·|t          |¦  «        dz
  k     rZ||dz            t          k    rFd                     |¦  «        Š
t          |t          ¦  «        }	‰
r‰
› d�Š
ˆ
fd„|	j        D ¦   «         c S t          |t          ¦  «        }|t          k    r%|                     |¦  «         t          ||¦  «        }�Œt	          |t          j        j        j        ¦  «        r(|dk    sJ ‚|j        }|s|                     |¦  «         �ŒZ|                     |¦  «         |t(          j        j        j        k    r'|t          |¦  «        dz
  k    rt/          d¦  «        ‚�Œ°t          ||¦  «        }�ŒÂd                     |¦  «                              t          d¦  «        hS )aá  
    This API is used to convert the name of a parameter to the FQNs. For FSDP
    without `use_orig_params`, the name of FlatParameter can be mapped to
    multiple original parameters. As a result, the return type of this function
    is `Set[str]`.

    Args:
        module (nn.Module): the root model.
        name (str): the name
        skip_ddp_prefix (bool): whether to skip DDP's `module` prefix

    Returns:
        The canonical FQNs based on the model traversal.
    Ú ú.Úmoduleé   c                 ó   •— h | ]}‰› |› �’Œ	S rR   rR   )Ú.0ÚfqnÚprefixs     €rB   ú	<setcomp>z_get_fqns.<locals>.<setcomp>Ð   s$   ø€ ÐEÐEÐE¨S˜6Ð( 3Ð(Ð(ÐEÐEÐErS   Ú	_orig_modz-Expect `_extra_state` to be the last obj name)Úreplacer   ÚsplitÚ	enumerateÚ
isinstanceÚDDPrq   ÚappendÚFSDPÚlenÚ_FLAT_PARAMÚjoinÚgetattrÚ_fqnsr$   r`   Ú_dynamoÚ
eval_frameÚOptimizedModulerx   rf   ÚmodulesÚ_EXTRA_STATE_KEY_SUFFIXÚRuntimeError)ri   rj   rk   rl   Ú	obj_namesÚfqn_obj_namesÚcurr_objÚiÚcurr_obj_nameÚ
flat_paramrv   s             @rB   Ú	_get_fqnsr‘   ¦   sP  ø€ ð. �<Š<Õ*¨BÑ/Ô/€DØ
�$€€Øˆvˆà—
’
˜3‘”€IØ€MØ€HÝ% iÑ0Ô0ð <ñ <Ñˆˆ=Ý�h¥Ñ$Ô$ð 	<Ø  HÒ,Ð,Ð,Ð,Ø”ˆHØ"ð 4Ø×$Ò$ ]Ñ3Ô3Ð3øÝ˜¥$Ñ'Ô'ð 	<Ø•3�y‘>”> AÑ%Ò%Ð%¨)°A¸±EÔ*:½kÒ*IÐ*IØŸš -Ñ0Ô0�Ý$ X­{Ñ;Ô;�
Øð *Ø &˜\˜\˜\�FØEÐEÐEÐE°JÔ4DÐEÑEÔEÐEÐEÐEÝ˜xÕ)<Ñ=Ô=ˆHØÕ 3Ò3Ð3Ø×$Ò$ ]Ñ3Ô3Ð3Ý" 8¨]Ñ;Ô;�ùÝ˜¥%¤-Ô":Ô"JÑKÔKð 	<Ø  KÒ/Ð/Ð/Ð/ØÔ)ˆHØ'ð 4Ø×$Ò$ ]Ñ3Ô3Ð3ùà× Ò  Ñ/Ô/Ð/Ø¥¤
Ô 1Ô IÒIÐIØ�˜I™œ¨Ñ*Ò*Ð*Ý&Ð'VÑWÔWÐWñ +õ # 8¨]Ñ;Ô;�‘à�HŠH�]Ñ#Ô#×+Ò+Õ,>ÀÑCÔCÐDÐDrS   c                   ó   — e Zd ZdS )Ú_EXTRA_STATEN)rL   rM   rN   rR   rS   rB   r“   r“   å   s   € € € € € Ø€DrS   r“   c              #   ó�   ‡‡K  — t          ¦   «         Šdt          j        dt          dt          fˆˆfd„Š ‰| d¦  «        E d {V —† d S )Nrq   Úcurr_fqnrm   c              3   ó*  •K  — ‰                      | ¦  «         |r|› d�nd}|                      ¦   «         D ]"\  }}|‰v rŒ
|› |› �} ‰||¦  «        E d {V —† Œ#t          |                      d¬¦  «        |                      d¬¦  «        ¦  «        D ]\  }}|| j        v rŒ|› |› �}||fV — Œt          | j        dt          j	        j
        ¦  «        t          j	        j
        k    r.|› t          j        j        j        › �}|t          ¦   «         fV — d S d S )Nrp   ro   F)ÚrecurseÚget_extra_state)ÚaddÚnamed_childrenr   Únamed_buffersÚnamed_parametersÚ_non_persistent_buffers_setrƒ   Ú	__class__rf   rg   r˜   rˆ   rq   r‰   r“   )rq   r•   rj   Ú	submoduleÚnew_fqnÚobjr—   Úvisited_moduless         €€rB   r—   z+_iterate_valid_model_state.<locals>.recurseì   sy  øè è € Ø×Ò˜FÑ#Ô#Ð#à%-Ð5�h�>�>�>�>°2ˆØ%×4Ò4Ñ6Ô6ð 	3ð 	3‰OˆD�)Ø˜OÐ+Ð+ØØ!Ð) 4Ð)Ð)ˆGØ�w˜y¨'Ñ2Ô2Ð2Ð2Ð2Ð2Ð2Ð2Ð2Ð2åØ× Ò ¨Ð Ñ/Ô/°×1HÒ1HÐQVÐ1HÑ1WÔ1Wñ
ô 
ð 	ð 	‰IˆD�#ð �vÔ9Ð9Ð9ØØ!Ð) 4Ð)Ð)ˆGØ˜3�,ÐÐÐÐõ �FÔ$Ð&7½¼Ô9RÑSÔSÝŒyÔ(ò)ð )ð "ÐN¥2¤:Ô#4Ô#LÐNÐNˆGØ�<™>œ>Ð)Ð)Ð)Ð)Ð)Ð)ð	)ð )rS   ro   )rb   rf   rg   r_   r   )ri   r—   r¢   s    @@rB   Ú_iterate_valid_model_stater£   é   sw   øøè è € Ý&)¡e¤e€Oð*�œ	ð *­Sð *µYð *ð *ð *ð *ð *ð *ð *ð2 ˆw�u˜bÑ!Ô!Ð!Ð!Ð!Ð!Ð!Ð!Ð!Ð!Ð!rS   )Ú
submodulesÚoptionsÚoptims.Ú
optim_onlyr¤   r¥   c                ód  — |rt          j        dt          ¦  «         |r|st          d¦  «        ‚|pt	          ¦   «         }i }i }t          | ¦  «        D ]¾\  }}t          |t          ¦  «        rŒt          | |¦  «        }	| 	                    |d¦  «        }
|
�Et          t          t                   ||         ¦  «                             |	¦  «         ||         ||<   n|	                     ¦   «         ||<   |	D ]}
t          |t          ¦  «        s|||
<   ŒŒ¿t          |                     ¦   «         ¦  «        D ]'\  }}|D ]}
t          t"          j        |¦  «        ||
<   Œ Œ(t'          ¦   «         }|rzt'          |¦  «        }|                      ¦   «         D ]V\  }}||vrŒ
t          | |¦  «        }	t+          |	¦  «        dk    s
J d¦   «         ‚|                     d„ |	D ¦   «         ¦  «         ŒW|j        r|j        st1          d¦  «        ‚t3          j        | ¦  «        }|r¶|j        rJt7          |j        |j        ¬¦  «        }t;          |j        |j        p|j        ¬¦  «        }t<          j        }n6tA          |j        ¬	¦  «        }tC          |j        ¬	¦  «        }t<          j"        }tF          j$        d
„ ¦   «         }tK          j&        || |||¬¦  «        }ntF          j'        }tQ          di tS          |¦  «        ¤||||t          tT          tV          j,                 |¦  «        | t+          |¦  «        dk    dœ¤ŽS )zW
    Verify the model and options passed by the user and generates _StateDictInfo.
    z¼Getting submodules only model/optim state_dict is deprecated and will be removed in 2.5. This feature can be achieved by manually filtering out the state_dict returned from get_state_dict.z;Optimizers are not passed in but optim_only is set to True.Nrr   z)Submodule FQN should only have 1 instancec              3   ó    K  — | ]	}|› d �V — Œ
dS )rp   NrR   )rt   ru   s     rB   ú	<genexpr>z"_verify_options.<locals>.<genexpr>C  s(   è è € Ð%@Ð%@°C¨ i i iÐ%@Ð%@Ð%@Ð%@Ð%@Ð%@rS   z?full_state_dict must be True when broadcast_from_rank0 is True.)Úoffload_to_cpuÚ
rank0_only)r«   c              3   ó  K  — t          j        ¦   «         5  t          j        ddt          ¬¦  «         t	          j        | |||¬¦  «        5  d V — d d d ¦  «         n# 1 swxY w Y   d d d ¦  «         d S # 1 swxY w Y   d S )NÚignorezFSDP.state_dict_type)ÚmessageÚcategory©rq   Ústate_dict_typeÚstate_dict_configÚoptim_state_dict_config)ÚwarningsÚcatch_warningsÚfilterwarningsÚFutureWarningr   r²   r±   s       rB   Ú$fsdp_state_dict_type_without_warningz=_verify_options.<locals>.fsdp_state_dict_type_without_warninga  s#  è è € õ Ô(Ñ*Ô*ð 
ð 
ÝÔ'ØÐ&<Å}ðñ ô ð õ Ô)Ø!Ø$3Ø&7Ø,Cð	ñ ô ð ð ð �E�E�Eðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð	
ð 
ð 
ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
øøøð 
ð 
ð 
ð 
ð 
ð 
s5   –5A4ÁAÁA4ÁA 	Á A4Á#A 	Á$A4Á4A8Á;A8r±   r   )rW   rX   rY   r\   r]   rZ   r[   rR   )-rµ   Úwarnr¸   rŠ   r/   r£   r|   r“   r‘   Úgetr	   r   r_   ÚupdateÚcopyre   Úitemsr`   ra   rb   Únamed_modulesr€   rJ   rE   Ú
ValueErrorr   r]   r   rF   r   r"   ÚFULL_STATE_DICTr    r   ÚSHARDED_STATE_DICTrc   ÚcontextmanagerÚ	functoolsÚpartialrd   rU   r   r   rf   rg   )ri   r¦   r§   r¤   r¥   rW   rX   rj   ÚparamÚfqnsru   Úparam_Úfqns_rY   rq   r]   r³   r´   r²   r¹   r\   s                        rB   Ú_verify_optionsrÊ     sñ  € ð ð 
ÝŒðIõ ñ		
ô 	
ð 	
ð ð 
˜&ð 
ÝØIñ
ô 
ð 	
ð Ð+Õ)Ñ+Ô+€Gð 	ð ð
 	ð õ 2°%Ñ8Ô8ð /ð /‰ˆˆeÝ�e�\Ñ*Ô*ð 	Øå˜ Ñ%Ô%ˆØ×#Ò# E¨4Ñ0Ô0ˆØˆ?Ý••S”Ð,¨UÔ3Ñ4Ô4×;Ò;¸DÑAÔAÐAØ+<¸UÔ+CÐ! %Ñ(Ð(ð (,§y¢y¡{¤{Ð˜eÑ$Øð 	/ð 	/ˆCÝ˜e¥\Ñ2Ô2ð /Ø).Ð! #Ñ&øð	/õ Ð3×9Ò9Ñ;Ô;Ñ<Ô<ð Dð D‰ˆ�Øð 	Dð 	DˆCÝ)-­e¬l¸FÑ)CÔ)CÐ! #Ñ&Ð&ð	Dõ $'¡5¤5ÐØð AÝ˜‘_”_ˆ
Ø!×/Ò/Ñ1Ô1ð 	Að 	A‰LˆD�&Ø˜ZÐ'Ð'ØÝ˜U DÑ)Ô)ˆDÝ�t‘9”9 ’>�>�>Ð#N‘>”>�>Ø×%Ò%Ð%@Ð%@¸4Ð%@Ñ%@Ô%@Ñ@Ô@Ð@Ð@àÔ#ð 
¨GÔ,Cð 
ÝØMñ
ô 
ð 	
õ Ô$ UÑ+Ô+€Lð ð /.àÔ"ð 	?Ý 3Ø&Ô2¸wÔ?Rð!ñ !ô !Ðõ '?Ø&Ô2Ø#Ô/ÐO°7Ô3Oð'ñ 'ô 'Ð#õ ,Ô;ˆOˆOå 6Ø&Ô2ð!ñ !ô !Ðõ 'BØ&Ô2ð'ñ 'ô 'Ð#õ ,Ô>ˆOå	Ô	"ð	ð 	ñ 
#Ô	"ð	õ$ !Ô(Ø0ØØ+Ø/Ø$;ð
ñ 
ô 
ˆˆõ "Ô-ˆåð 	ð 	Ý
�‰/Œ/ð	à+Ø3Ø-Ø!Ý�$�rœyœ/¨<Ñ8Ô8Ø#�^Ý˜&‘k”k A’oð	ð 	ð 	ð 	ð 	rS   Úmodel_state_dictÚoptim_state_dictÚinfoc                 óÒ  — |j         D ]}t          |¦  «        }|€
J d¦   «         ‚Œ|j        rP| sN|j        sG|j        s@|j        r|j        s2|j        r+|j        s$t          dt          j        ¦   «         ›d�¦  «        ‚|j        r)|s'|j        r|j        s|j        st          d|› �¦  «        ‚|                      ¦   «         D ]%}t          |v rt          |› dt          › d�¦  «        ‚Œ&d S )Nz)Expected a fsdp_state with a fsdp module.z}The option indicates that model state_dict is required to save or load, but model state_dict is empty.rank = dist.get_rank()=rp   zgThe option indicates that model state_dict is required to save, or load but optim state_dict is empty. z
 contains z6. This can happen if the model is not the root module.)r]   r#   rZ   rY   rG   rF   rE   rI   rJ   rŠ   ÚdistÚget_rankr[   Úkeysr�   )rË   rÌ   rÍ   rq   Ú
fsdp_stateÚkeys         rB   Ú_verify_state_dictrÔ   Š  s˜  € ð
 Ô#ð Sð SˆÝCÀFÑKÔKˆ
ØÐ%Ð%Ð'RÑ%Ô%Ð%Ð%ð
 	Ôð
à ð
ð Ô'ð
ð Ô)ð	
ð
 Ô!ð
ð
 '+Ô&:ð
ð ŒKð
ð Ô)ð
õ ð*å”m‘o”oð*ð *ð *ñ
ô 
ð 	
ð Ôð 	à ð	àÔ%ð	à*.Ô*>ð	ð Ô.ð	õ
 ðMØ:JðMð Mñô ð ð
  ×$Ò$Ñ&Ô&ð ð ˆÝ˜#ÐÐÝØð *ð *¥+ð *ð *ð *ñô ð ð ðð rS   r¡   Úapic                 óŠ   — t          | |¦  «        }|t          v r)t          j        t          | j        |¦  «        | ¬¦  «        }|S )N)Úself)rƒ   r:   rÄ   rÅ   rž   )r¡   rÕ   Úcalls      rB   Ú_state_dict_fnrÙ   ·  sC   € Ý�3˜ÑÔ€DØÕ"Ð"Ð"ÝÔ ¥¨¬¸Ñ!<Ô!<À3ÐGÑGÔGˆØ€KrS   Ú
state_dictc                 óÀ   — |j         r@|j        rt          j                             ¦   «         sdnd}t          | |j        |¬¦  «        S |j        rt          | ¦  «        S | S )NrR   )r   )rF   Ú
ranks_only)rE   rF   r`   ÚdistributedÚis_initializedr   r   )rÚ   rÍ   rÜ   s      rB   Ú_maybe_full_or_cpu_state_dictrß   ¾  sƒ   € ð Ôð ð Ô$ðÝ,1Ô,=×,LÒ,LÑ,NÔ,NðˆBˆBàð 	õ
 "Ø DÔ$4Àð
ñ 
ô 
ð 	
ð 
Ô	ð Ý)¨*Ñ5Ô5Ð5àÐrS   c                 óp  — |j         si S |                     ¦   «         5   t          | d¦  «        ¦   «         }d d d ¦  «         n# 1 swxY w Y   t          |                     ¦   «         ¦  «        D ]—}t          | |¦  «        }t          |¦  «        dk    sJ ||f¦   «         ‚t          t          |¦  «        ¦  «        }||k    rDdt          fd„} |||¦  «        st          d|› d|› �¦  «        ‚|                     |¦  «        ||<   Œ˜|j        rpi }|                     ¦   «         D ]W}|j        D ]M}|                     |¦  «        sŒ|j        r||         ||<   Œ+|t          |¦  «        d …         }	||         ||	<   ŒNŒX|}|j        rL|                      ¦   «         D ]7\  }}
|
j        rŒt          | |¦  «        }|D ]}|                     |¦  «         ŒŒ8t          |                     ¦   «         ¦  «        D ]5\  }}t'          j        |¦  «        r|j        r|                     |¦  «         Œ6t-          ||¦  «        S )NrÚ   rr   rm   c                 óT  — t          |¦  «        t          | ¦  «        k    rdS |                     d¦  «        }|                      d¦  «        }d}t          |¦  «        D ]I\  }}|||         k    r1|dz  }|t          |¦  «        k    r|t          |¦  «        dz
  k    c S ŒB|dv rŒG dS dS )NFrp   r   rr   )rq   rx   T)r€   rz   r{   )rÓ   ru   Ú	fqn_splitÚ	key_splitÚfqn_idxÚkey_idxÚkey_names          rB   Úverifyz%_get_model_state_dict.<locals>.verifyâ  sÃ   € Ý�s‘8”8�s 3™xœxÒ'Ð'Ø ˜5ØŸIšI c™NœN�	ØŸIšI c™NœN�	Ø�Ý)2°9Ñ)=Ô)=ð %ð %Ñ%�G˜XØ 9¨WÔ#5Ò5Ð5Ø 1™˜Ø"¥c¨)¡n¤nÒ4Ð4Ø#*­c°)©n¬n¸qÑ.@Ò#@Ð@Ð@Ð@ð 5à!Ð%<Ð<Ð<Ø à$˜u˜uØ�trS   zAn unexpected key, z, exists. FQN is )rZ   r\   rÙ   re   rÑ   r‘   r€   ÚnextÚiterrP   rŠ   ÚpoprY   Ú
startswithrH   rG   rœ   Úrequires_gradr¾   r`   Ú	is_tensorÚis_metarß   )ri   rÍ   rÚ   rÓ   rÇ   ru   rç   Únew_state_dictrv   r    rÆ   Úps               rB   Ú_get_model_state_dictrñ   Ð  sÚ  € ð Ôð Øˆ	à	×	Ò	Ñ	Ô	ð ;ð ;Ø8•^ E¨<Ñ8Ô8Ñ:Ô:ˆ
ð;ð ;ð ;ñ ;ô ;ð ;ð ;ð ;ð ;ð ;ð ;øøøð ;ð ;ð ;ð ;õ �J—O’OÑ%Ô%Ñ&Ô&ð 2ð 2ˆÝ˜ Ñ$Ô$ˆÝ�4‰yŒy˜AŠ~ˆ~ˆ~  T˜{‰~Œ~ˆ~Ý•4˜‘:”:ÑÔˆØ�#Š:ˆ:ð¥Dð ð ð ð ð" �6˜#˜sÑ#Ô#ð VÝ"Ð#T¸Ð#TÐ#TÈsÐ#TÐ#TÑUÔUÐUØ(Ÿnšn¨SÑ1Ô1ˆJ�s‰OøàÔð $Ø/1ˆà—?’?Ñ$Ô$ð 	>ð 	>ˆCØÔ1ð >ð >�Ø—~’~ fÑ-Ô-ð ØØÔ/ð >Ø*4°S¬/�N 3Ñ'Ð'à!¥# f¡+¤+ - -Ô0�GØ.8¸¬o�N 7Ñ+Ð+ð>ð $ˆ
àÔ ð $Ø×0Ò0Ñ2Ô2ð 	$ð 	$‰JˆC�ØÔ"ð ØÝ˜U CÑ(Ô(ˆDØð $ð $�Ø—’˜sÑ#Ô#Ð#Ð#ð$õ �z×'Ò'Ñ)Ô)Ñ*Ô*ð  ð  ‰ˆˆQÝŒ?˜1ÑÔð 	  !¤)ð 	 Ø�NŠN˜3ÑÔÐøå(¨°TÑ:Ô:Ð:s   žAÁAÁ
Ac           	      ó.  — |j         r	|s|j        st          i i ¦  «        S i }t          | ¦  «        D ]\  }}t	          | |¦  «        }t	          | |dd¬¦  «        }t          ||¦  «        D ]F\  }}	|j        rt          j        ¦   «         dk    r||	k    r|                     |¦  «        ||	<   |||	<   ŒGŒ€d}
|j        s|j	        ród }| 
                    ¦   «         D ]H\  }}t          j        |¦  «        r/|                     ¦   «         dk    r|€|j        }Œ;||j        k    sJ ‚ŒI|€J ‚|t          j        d¦  «        k    r t          j                             ¦   «         }d}
|j        rt#          ||||j        ¬¦  «         n|j	        rt'          |||¬¦  «         | 
                    ¦   «         D ]
\  }}|||<   Œ|                     ¦   «         5  t+          t           t-          | d¦  «        ||j        |
¬	¦  «        ¦  «        cd d d ¦  «         S # 1 swxY w Y   d S )
NF)rk   rl   r   ÚmetaT)ÚdevicerI   ©rô   Úload_state_dict)rÚ   rI   Úassign)rZ   rJ   r&   r£   r‘   ÚziprÏ   rÐ   rê   rE   r¾   r`   rí   Údimrô   Údistributed_c10dÚ_get_pg_default_devicer   rI   r   r\   r	   rÙ   )ri   rÚ   rÍ   Úlocal_state_dictrÓ   ÚvaluerÇ   Úfqns_with_prefixru   Úfqn_with_prefixr÷   rô   Úlocal_states                rB   Ú_load_model_state_dictr    sß  € ð Ôð ) Zð )¸Ô8Qð )Ý   RÑ(Ô(Ð(àÐÝ0°Ñ7Ô7ð 6ð 6‰
ˆˆUÝ˜ Ñ$Ô$ˆÝ$Ø�3¨ÀEð
ñ 
ô 
Ðõ %(¨Ð.>Ñ$?Ô$?ð 	6ð 	6Ñ ˆC�àÔ-ðBÝ15´±´ÀAÒ1EÐ1EØ˜Ò(Ð(Ø.8¯nªn¸SÑ.AÔ.A�
˜?Ñ+Ø05Ð˜_Ñ-Ð-ð	6ð €FØÔ ð * DÔ$8ð *ØˆØ*×0Ò0Ñ2Ô2ð 	2ð 	2‰JˆC�ÝŒ˜uÑ%Ô%ð 2¨%¯)ª)©+¬+¸ª/¨/Ø�>Ø"œ\�F�Fà! U¤\Ò1Ð1Ð1Ð1øØÐ!Ð!Ð!Ø•U”\ &Ñ)Ô)Ò)Ð)ÝÔ*×AÒAÑCÔCˆFØˆFØÔ$ð 	PÝ!ØÐ,°VÀDÄKðñ ô ð ð ð Ô!ð 	PÝ" :Ð/?ÈÐOÑOÔOÐOØ 0× 6Ò 6Ñ 8Ô 8ð 	*ð 	*ÑˆC�Ø)ˆJ�s‰OˆOà	×	Ò	Ñ	Ô	ð 
ð 
ÝÝØ4�N˜5Ð"3Ñ4Ô4Ø%¨d¬kÀ&ðñ ô ñ
ô 
ð
ð 
ð 
ð 
ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
øøøð 
ð 
ð 
ð 
ð 
ð 
s   Ç	4H
È
HÈHÚoptimc                 óB  — | j         rdS | j        D ]}|t                   D ]}|j        �  dS ŒŒ| j        D ]2}|t                   D ]"}|j        rt          j        |¦  «        |_        Œ#Œ3g }| j        D ]Z}d|v rT|                     |d         ¦  «         t          |d         t
          j	        ¦  «        rt          j
        d¦  «        nd|d<   Œ[|                      d¬¦  «         | j        D ]}d|v r|                     d¦  «        |d<   Œ|                      d¬¦  «         dS )zH
    Initialize optim states by calling the step() with zero grads.
    NÚlrg        )Úclosurer   T)Úset_to_none)r9   r7   Ú_PARAMSÚgradrì   r`   Ú
zeros_liker~   r|   ra   ÚtensorÚsteprê   Ú	zero_grad)r  Úparam_grouprÆ   Úlrss       rB   Ú_init_optim_stater  J  sx  € ð „{ð àˆð Ô)ð ð ˆØ ¥Ô)ð 	ð 	ˆEØŒzÐ%Ø���ð &ð	ð Ô)ð 5ð 5ˆØ ¥Ô)ð 	5ð 	5ˆEØÔ"ð 5Ý"Ô-¨eÑ4Ô4�”
øð	5ð €CØÔ)ð ð ˆØ�;ÐÐØ�JŠJ�{ 4Ô(Ñ)Ô)Ð)õ ˜k¨$Ô/µ´Ñ>Ô>ð•”˜SÑ!Ô!Ð!àð ˜Ñøð
 
‡J‚J�t€JÑÔÐð Ô)ð +ð +ˆØ�;ÐÐØ #§¢¨¡
¤
ˆK˜ÑøØ	‡O‚O €OÑ%Ô%Ð%Ð%Ð%rS   c           
      ó,  — d„ }i }t          t          | t                   ¦  «                             ¦   «         D ]O\  }}t          t          |¦  «                             ¦   «         D ]"\  }} ||¦  «         ||t          › d|› d|› �<   Œ#ŒPt          t          | t
                   ¦  «        D ]k}|                     t          ¦  «        }t          t          t                   |¦  «        D ].}|                     ¦   «         D ]\  }}||t
          › d|› d|› �<   ŒŒ/Œl|S )aI  
    This API flattens the optimizer state_dict to support optimizer resharding for
    MPMD, e.g., pipeline parallelism.

    Without the API, the original optimizer state_dict looks like:
    {
        "state": {
            "layer1.weight": {
                "step": 10, "exp_avg": SomeTensor, "exp_avg_sq": SomeTensor
            },
            "layer2.weight": {
                "step": 10, "exp_avg": SomeTensor, "exp_avg_sq": SomeTensor
            },
        },
        "param_group": [
            {
                "lr": 0.0,
                "betas": (0.9, 0.95), ...,
                "params": ["layer1.weight", "layer2.weight"]
            }
        ]
    }

    With this API, the optimizer state_dict looks like:
    {
        "state.layer1.weight.step": 10,
        "state.layer2.weight.step": 10,
        "state.layer1.weight.exp_avg": SomeTensor,
        "state.layer2.weight.exp_avg": SomeTensor,
        "state.layer1.weight.exp_avg_sq": SomeTensor,
        "state.layer2.weight.exp_avg_sq": SomeTensor,
        "param_group.layer1.weight.lr" : 0.1,
        "param_group.layer2.weight.lr" : 0.1,
        "param_group.layer1.weight.betas" : (0.9, 0.95),
        "param_group.layer2.weight.betas" : (0.9, 0.95),
    }

    Note that if any of the value is a container, like the betas in the example,
    this API won't flattent it.
    c                 ó”   — t          | t          j        t          t          f¦  «        s t          dt          | ¦  «        › d�¦  «        ‚d S )NzUFlattening optimizer state_dict only supports tensor, int, float states now. Type is rp   )r|   r`   ra   ÚintÚfloatÚNotImplementedErrorÚtype)Úvs    rB   Ú_raise_if_type_not_supportedz?_flatten_optim_state_dict.<locals>._raise_if_type_not_supportedž  sV   € Ý˜!�eœl­CµÐ7Ñ8Ô8ð 	Ý%ð&å ™7œ7ð&ð &ð &ñô ð ð	ð 	rS   rp   )
r	   r,   Ú_STATEr¾   r-   Ú_PGrê   r  r   r_   )	rÚ   r  Úretru   r9   Úkr  r  rÇ   s	            rB   Ú_flatten_optim_state_dictr  t  sJ  € ðTð ð ð !#€CÝ�=¨*µVÔ*<Ñ=Ô=×CÒCÑEÔEð +ð +‰
ˆˆUÝ�¨Ñ.Ô.×4Ò4Ñ6Ô6ð 	+ð 	+‰DˆAˆqØ(Ð(¨Ñ+Ô+Ð+Ø)*ˆC•6Ð%Ð%˜CÐ%Ð% !Ð%Ð%Ñ&Ð&ð	+õ Õ-¨z½#¬Ñ?Ô?ð ,ð ,ˆØ�Š�wÑ'Ô'ˆÝ��Sœ	 4Ñ(Ô(ð 	,ð 	,ˆCØ#×)Ò)Ñ+Ô+ð ,ð ,‘��1Ø*+�•sÐ&Ð&˜SÐ&Ð& 1Ð&Ð&Ñ'Ð'ð,ð	,ð €JrS   c                 óz  — i }g }t           |t          |i}| j        D �]�}|                     t          g i¦  «         |t                   D ]±}|j        |         D ]¡}|d         t                   }	t          |	t          ¦  «        sJ ‚|	                     |¦  «         |j        sŒIi ||<   | j	        |          
                    ¦   «         D ]3}
|t           › d|› d|
› �         t          t          ||         ¦  «        |
<   Œ4Œ¢Œ²t          t          t                   |d         t                   ¦  «        d         }| 
                    ¦   «         D ]s}|t          k    rŒ|t          › d|› d|› �         }||d         vr||d         |<   Œ9|d         |         |k    r(t          d|› d|› d|› d|d         |         › d�	¦  «        ‚Œt�ŒŸ|S )zœ
    This API unflattens the state_dict generated by _flatten_optim_state_dict().
    See the docstring of _flatten_optim_state_dict() for more detail.
    éÿÿÿÿrp   r   zaAll the parameters in the same parameter group should have the same saved param_group value. But z is z while other(s) is )r  r  r7   r~   r  rW   r|   re   rì   r9   rÑ   r	   r,   r   r_   rŠ   )r  rÚ   rÍ   r9   Úpg_stateÚ
return_osdr  rÆ   ru   r8   Ú
state_nameÚfirst_param_fqnr  rý   s                 rB   Ú_unflatten_optim_state_dictr#  ´  s1  € ð €EØ"$€HÝ&,¨eµS¸(Ð%C€JàÔ)ð ñ ˆØ�Š� "˜Ñ&Ô&Ð&Ø ¥Ô)ð 	ð 	ˆEØÔ-¨eÔ4ð 
ð 
�Ø! "œ¥gÔ.�Ý! &­$Ñ/Ô/Ð/Ð/Ð/Ø—’˜cÑ"Ô"Ð"ØÔ*ð ØØ��c‘
Ø"'¤+¨eÔ"4×"9Ò"9Ñ";Ô";ð ð �JØBLÝ!Ð6Ð6 CÐ6Ð6¨*Ð6Ð6ôC•D�¨¨c¬
Ñ3Ô3°JÑ?Ð?ðð
õ �t¥Cœy¨(°2¬,µwÔ*?Ñ@Ô@ÀÔCˆØ×!Ò!Ñ#Ô#ð 	ð 	ˆAØ•GŠ|ˆ|ØØ¥#Ð=Ð=¨Ð=Ð=¸!Ð=Ð=Ô>ˆEØ˜ œÐ$Ð$Ø"'�˜”˜Q‘�Ø˜"”˜a” EÒ)Ð)Ý"ðGØ=LðGð GØOPðGð GàðGð Gà4<¸R´LÀ´OðGð Gð Gñô ð ð *ñ	ð ÐrS   Ú
optimizersc                 óÎ  ‡— |j         si S t          i t          g i}|D �]}t          |¦  «          t	          |d¦  «        ¦   «         }|j        rí|                     ¦   «         5  t          j        | ||¦  «        }d d d ¦  «         n# 1 swxY w Y   |sŒvt          |t                    
                    ¦   «         ¦  «        D ]H}d|v rB|t                                        |¦  «        |t                   |                     dd¦  «        <   ŒI|t                   D ]#}d„ |t                   D ¦   «         }||t          <   Œ$�nqt          t          j        d„ |j        D ¦   «         ¦  «        ¦  «        }t#          t%          |t'          t)          |¦  «        ¦  «        ¦  «        ¦  «        }	i Š|                      ¦   «         D ]]\  }
}t-          | |
¦  «        }t)          |¦  «        dk    sJ ‚t/          t1          |¦  «        ¦  «        }||	vrŒK|	|         }|‰|<   |‰|<   Œ^t          |t                    
                    ¦   «         ¦  «        D ]8}
‰|
         }|t                                        |
¦  «        |t                   |<   Œ9|t                   D ]#}ˆfd„|t                   D ¦   «         |t          <   Œ$|s�Œ“t3          t4          |t                   ¦  «                             |t                   ¦  «         t3          t8          |t                   ¦  «                             |t                   ¦  «         �Œ|j        r"t3          t>          tA          |¦  «        ¦  «        }tC          ||¦  «        S )	NrÚ   rx   ú
_orig_mod.ro   c                 ó:   — g | ]}|                      d d¦  «        ‘ŒS )r&  ro   ©ry   ©rt   r  s     rB   ú
<listcomp>z)_get_optim_state_dict.<locals>.<listcomp>ü  s&   € ÐJÐJÐJ¸!˜!Ÿ)š) L°"Ñ5Ô5ÐJÐJÐJrS   c              3   ó0   K  — | ]}|t                    V — Œd S r<   )r  )rt   Úgs     rB   rª   z(_get_optim_state_dict.<locals>.<genexpr>ÿ  s&   è è € Ð-UÐ-U¸Q¨aµ¬jÐ-UÐ-UÐ-UÐ-UÐ-UÐ-UrS   rr   c                 ó    •— g | ]
}‰|         ‘ŒS rR   rR   )rt   ÚpidÚfqn_pid_mappings     €rB   r*  z)_get_optim_state_dict.<locals>.<listcomp>  s   ø€ Ð!QÐ!QÐ!Q¸3 /°#Ô"6Ð!QÐ!QÐ!QrS   )"r[   r  r  r  rÙ   r]   r\   r   rÌ   re   rÑ   rê   ry   r  r   Úfrom_iterabler7   r^   rø   Úranger€   rœ   r‘   rè   ré   r	   r,   r¼   r-   ÚextendrK   r.   r  rß   )ri   r$  rÍ   rÌ   r  Úosdr  r,  r8   Úparam_pid_mappingrÓ   rÆ   rÇ   ru   r.  Úgroupr/  s                   @rB   Ú_get_optim_state_dictr6  á  sŠ  ø€ ð Ôð Øˆ	å,2°B½¸RÐ+@ÐØð ,Hñ ,HˆÝ˜%Ñ Ô Ð Ø1�n˜U LÑ1Ô1Ñ3Ô3ˆØÔð #	RØ×"Ò"Ñ$Ô$ð ?ð ?ÝÔ+¨E°5¸#Ñ>Ô>�ð?ð ?ð ?ñ ?ô ?ð ?ð ?ð ?ð ?ð ?ð ?øøøð ?ð ?ð ?ð ?ð ð ØÝ˜#�fœ+×*Ò*Ñ,Ô,Ñ-Ô-ð Rð R�Ø !Ð#Ð#Ø?BÅ6¼{¿ºÈqÑ?QÔ?Q�C�”K §	¢	¨,¸Ñ ;Ô ;Ñ<øØ�”Xð $ð $�ØJÐJ¸qÅ¼zÐJÑJÔJ�Ø#�•'‘
�
ñ$õ �%Ô-Ð-UÐ-UÀ%ÔBTÐ-UÑ-UÔ-UÑUÔUÑVÔVˆFÝ $¥S¨µµs¸6±{´{Ñ1CÔ1CÑ%DÔ%DÑ EÔ EÐØ ˆOØ#×4Ò4Ñ6Ô6ð +ð +‘
��UÝ  ¨Ñ,Ô,�Ý˜4‘y”y A’~�~�~�~Ý�4 ™:œ:Ñ&Ô&�ØÐ 1Ð1Ð1ØØ'¨Ô.�Ø'*� Ñ$Ø'*� Ñ$Ð$å˜C¥œK×,Ò,Ñ.Ô.Ñ/Ô/ð 8ð 8�Ø% cÔ*�Ø#&¥v¤;§?¢?°3Ñ#7Ô#7�•F”˜CÑ Ð à�Sœð Rð R�Ø!QÐ!QÐ!QÐ!QÀ%ÍÄ.Ð!QÑ!QÔ!Q�•g‘�àð 	Ùå�]Ð,­VÔ4Ñ5Ô5×<Ò<¸SÅ¼[ÑIÔIÐIÝÕÐ 0µÔ 5Ñ6Ô6×=Ò=¸cÅ#¼hÑGÔGÐGÑGàÔ(ð 
ÝÝÕ 9Ð:JÑ KÔ Kñ
ô 
Ðõ )Ð)9¸4Ñ@Ô@Ð@s   Á"BÂB		ÂB		c           
      óþ  — i }g }t           |t          |i}i }t          d„ t          t          |t                    ¦  «                             ¦   «         D ¦   «         ¦  «        r|S |j        D �]”}|                     t          g i¦  «         |t                   D �]f}	|j	        |	         D �]T}
|
|j
        v rWd}t          t          |t                   ¦  «        D ]3}|
t          t          t                   |t                   ¦  «        v rd} nŒ4nd}|sŒh|d         t                   }t          |t          ¦  «        sJ ‚|                     |
¦  «         |	j        r)t          t          |t                    ¦  «        |
         ||
<   t          t          |t                   ¦  «        D ]\}|
t          t          t                   |t                   ¦  «        v r-t#          |t                   ¦  «        dz
  |t%          |¦  «        <   Œ]�ŒV�Œh�Œ–t          t          |t                   ¦  «        D ]]}|                     t%          |¦  «        d¦  «        }|dk    rŒ,|                     ¦   «         D ]\  }}|t          k    rŒ|||         |<   ŒŒ^|S )að  
    Extract the corresponding optim state_dict from ``optim_state_dict`` for
    ``optim`` and return the result optim state_dict.

    Args:
        model (nn.Module): the root model.
        optim (torch.optim.Optimizer): the optimizer.
        optim_state_dict (Dict[str, ValueType]): the superset optim state_dict that
            contains the optim state_dict of ``optim``.
        info (_StateDictInfo): state dict information.

    Returns:
        The optim state_dict of ``optim``.
    c              3   ó@   K  — | ]}t          |t          ¦  «        V — Œd S r<   )r|   r  r)  s     rB   rª   z*_split_optim_state_dict.<locals>.<genexpr>;  s=   è è € ð ð Ø �
�1•cÑÔðð ð ð ð ð rS   FTr  rr   )r  r  Úallr	   r,   rÑ   r7   r~   r  rW   rX   r-   r   r_   r|   re   rì   r€   Úidr»   r¾   )ri   r  rÌ   rÍ   r9   r  r   Ú
pg_mappingr  rÆ   ru   Ú	in_paramsÚloaded_param_groupr8   ÚidxrÓ   rý   s                    rB   Ú_split_optim_state_dictr?  !  s¸  € ð* €EØ"$€HÝ&,¨eµS¸(Ð%C€JØ!#€Jå
ð ð Ý$(­Ð8HÍÔ8PÑ$QÔ$Q×$VÒ$VÑ$XÔ$Xðñ ô ñ ô ð  ð  ÐàÔ)ð Vñ VˆØ�Š� "˜Ñ&Ô&Ð&Ø ¥Ô)ð 	Vñ 	VˆEØÔ-¨eÔ4ð Vñ V�Ø˜$Ô4Ð4Ð4Ø %�IÝ.2Ý)Ð+;½CÔ+@ñ/ô /ð "ð "Ð*ð ¥$¥t­C¤yÐ2DÅWÔ2MÑ"NÔ"NÐNÐNØ(,˜IØ!˜Eð Oøð !%�IØ ð Øà! "œ¥gÔ.�Ý! &­$Ñ/Ô/Ð/Ð/Ð/Ø—’˜cÑ"Ô"Ð"ØÔ&ð TÝ!%¥mÐ5EÅfÔ5MÑ!NÔ!NÈsÔ!S�E˜#‘JÝ*.Ý%Ð'7½Ô'<ñ+ô +ð Vð VÐ&ð �d¥4­¤9Ð.@ÅÔ.IÑJÔJÐJÐJÝ=@ÀÍCÄÑ=QÔ=QÐTUÑ=U˜
¥2Ð&8Ñ#9Ô#9Ñ:øñ	Vñ'Vñ	Võ4 Õ-Ð/?ÅÔ/DÑEÔEð 'ð 'ˆØ�nŠn�R ™_œ_¨bÑ1Ô1ˆØ�"Š9ˆ9ØØ%×+Ò+Ñ-Ô-ð 	'ð 	'‰JˆC�Ø•gŠ~ˆ~Øà!&ˆH�SŒM˜#ÑÐð		'ð ÐrS   c           	      ó,  ‡‡‡— |j         sd S |D �]}t          |¦  «         |rSt          |v rt          | |||¦  «        }n9t	          |t          t          t          t          f         |¦  «        |¦  «        }ni }|j	        �r”|  
                    ¦   «         D �];\  }}t          | |¦  «        }t          | |d¬¦  «        }	||	k    rŒ/t          |¦  «        dk    sJ ‚|                     ¦   «         Š|	                     ¦   «         Š|t                   D ]M}
t          t          t          t          f         |
¦  «        }ˆˆfd„|t                    D ¦   «         }||t           <   ŒNt          t"          |t                   ¦  «        }t%          |                     ¦   «         ¦  «        D ]2}‰|v r,|                     |¦  «        ||                     ‰‰¦  «        <   Œ3�Œ=|                     ¦   «         5  t-          j        | ||¦  «        }d d d ¦  «         n# 1 swxY w Y   nä|j        rÝd|_        t3          | |f|¦  «        }d|_        d Šˆfd„}t5          t6          j        ||¦  «        }‰€J ‚t;          |¦  «        \  }}t;          |¦  «        \  }}|j        rt?          ||‰¬¦  «         ntA          ||‰¬¦  «         |                     ¦   «         D ]"}||vr||v sJ ‚||         ||<   ||         ||<   Œ#tC          ||¦  «        } tE          |d¦  «        |¬	¦  «         �Œd S )
NF)rl   rr   c                 ó<   •— g | ]}|                      ‰‰¦  «        ‘ŒS rR   r(  )rt   rÓ   ru   Úfqn_with_compilers     €€rB   r*  z*_load_optim_state_dict.<locals>.<listcomp>�  s5   ø€ ð ð ð Ø@C˜Ÿš CÐ):Ñ;Ô;ðð ð rS   Tc                 ó€   •— |                       ¦   «         dk    r$‰€| j        Šn‰| j        k    rt          d¦  «        ‚| S )Nr   zDevice mismatch)rù   rô   rÀ   )Útrô   s    €rB   Ú_devicez'_load_optim_state_dict.<locals>._device£  sC   ø€ Ø—5’5‘7”7˜Q’;�;à�~Ø!"¤˜˜Ø 1¤8Ò+Ð+Ý(Ð):Ñ;Ô;Ð;Ø�rS   rõ   rö   )rÚ   )#r[   r  r  r?  r#  r	   r
   r_   r+   r]   rœ   r‘   r€   rê   r  r   r  r,   re   rÑ   ry   r\   r   Úoptim_state_dict_to_loadrE   r6  r(   r`   ra   r   rJ   r   r   r   rÙ   )ri   r$  rÚ   rÍ   r  rÌ   Úoriginal_fqnÚ_rÇ   Úfqns_with_compilerr,  Úvalr8   Ú	osd_stater  rü   rE  Úflatten_osdÚosd_mappingÚflatten_local_osdÚlocal_osd_mappingÚ	optim_keyrô   ru   rB  s                         @@@rB   Ú_load_optim_state_dictrQ  i  s  øøø€ ð Ôð Øˆàð QNñ QNˆÝ˜%Ñ Ô Ð Øð 
	"Ý˜Ð#Ð#Ý#:Ø˜5 *¨dñ$ô $Ð Ð õ $?Ø�4¥¥S­) ^Ô 4°jÑAÔAÀ4ñ$ô $Ð Ð ð  "ÐØÔñ ?	ð $)×#9Ò#9Ñ#;Ô#;ð Xñ X‘�˜aÝ  ¨Ñ5Ô5�Ý%.Ø˜<¸eð&ñ &ô &Ð"ð Ð-Ò-Ð-Øå˜4‘y”y A’~�~�~�~Ø—h’h‘j”j�Ø$6×$:Ò$:Ñ$<Ô$<Ð!Ø)­#Ô.ð *ð *�AÝ�t¥C­ Hœ~¨qÑ1Ô1�Cðð ð ð ð ØGJÍ7Ä|ðñ ô �Fð $*�C�‘L�LÝ ¥Ð0@ÅÔ0HÑIÔI�	Ý˜iŸnšnÑ.Ô.Ñ/Ô/ð Xð X�AØ˜a�x�xØGPÇ}Â}ÐUVÑGWÔGW˜	 !§)¢)¨CÐ1BÑ"CÔ"CÑDøñXð ×"Ò"Ñ$Ô$ð ð Ý#'Ô#@Ø˜5Ð"2ñ$ô $Ð ðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð øð Ô!ð "	Ø#(ˆDÔ Ý4°U¸U¸HÀdÑKÔKÐØ#'ˆDÔ ØˆFðð ð ð ð õ �eœl¨GÐ5EÑFÔFˆAØÐ%Ð%Ð%Ý':Ð;KÑ'LÔ'LÑ$ˆK˜Ý3FÐGWÑ3XÔ3XÑ0ÐÐ0ØÔ(ð VÝ% kÐ3DÈVÐTÑTÔTÐTÐTå& {Ð4EÈfÐUÑUÔUÐUð
 )×-Ò-Ñ/Ô/ð Jð J�	ØÐ$5Ð5Ð5Ø$¨Ð3Ð3Ð3Ð3Ø3>¸yÔ3IÐ% iÑ0Ø3>¸yÔ3IÐ% iÑ0øÝ4Ø!Ð#4ñ ô  Ðð 	1��uÐ/Ñ0Ô0Ð<LÐMÑMÔMÐMÑMðcQNð QNs   Ç%HÈH	ÈH	c                óÀ   — t          ¦   «         5  t          | dd||¬¦  «        }t          | |¦  «        }t          |i |¦  «         |cddd¦  «         S # 1 swxY w Y   dS )aH  
    Return the model state_dict of ``model``.

    See ``get_state_dict`` for the detail usage.

    Args:
        model (nn.Module): the nn.Module to the model.
        submodules (deprecated): Optional[Set[nn.Module]]: only return the model parameters
            that belong to the submodules.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be returned. See
            `StateDictOptions` for the details.

    Returns:
        The state_dict for ``model``.

    :rtype: typing.Dict[str, ValueType]
    rR   F©r§   r¤   r¥   N)rC   rÊ   rñ   rÔ   )ri   r¤   r¥   rÍ   rË   s        rB   r0   r0   Ç  s½   € õ0 
‰Œð 
 ð 
 ÝØØØØ!Øð
ñ 
ô 
ˆõ 1°¸Ñ=Ô=ÐÝÐ+¨R°Ñ6Ô6Ð6Øð
 ð 
 ð 
 ð 
 ñ 
 ô 
 ð 
 ð 
 ð 
 ð 
 ð 
 ð 
 øøøð 
 ð 
 ð 
 ð 
 ð 
 ð 
 s   �7AÁAÁAc                ó$  — t          ¦   «         5  t          |t          j        j        ¦  «        r|fnt          |¦  «        }t          | |d||¬¦  «        }t          | ||¦  «        }t          i ||¦  «         |cddd¦  «         S # 1 swxY w Y   dS )aË  
    Return the combined state_dict for optimizers.

    See ``get_state_dict`` for the detail usage.

    Args:
        model (nn.Module): the nn.Module to the model.
        optimizers (Union[None, Optimizer, Iterable[Optimizer]]):
            The optimizers that are used to optimize ``model``.
        submodules (deprecated): Optional[Set[nn.Module]]: only return the model parameters
            that belong to the submodules.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be returned. See
            `StateDictOptions` for the details.

    Returns:
        The state_dict for ``optimizers``.

    :rtype: OptimizerStateType
    TrS  N)	rC   r|   r`   r  Ú	OptimizerÚtuplerÊ   r6  rÔ   )ri   r$  r¤   r¥   rÍ   rÌ   s         rB   r1   r1   ì  sð   € õ6 
‰Œð  ð  õ ˜*¥e¤kÔ&;Ñ<Ô<ð#ˆZˆMˆMå�zÑ"Ô"ð 	õ
 ØØØØ!Øð
ñ 
ô 
ˆõ 1°¸
ÀDÑIÔIÐÝ˜2Ð/°Ñ6Ô6Ð6Øð ð  ð  ð  ñ  ô  ð  ð  ð  ð  ð  ð  øøøð  ð  ð  ð  ð  ð  s   �A)BÂB	ÂB	c                óH  — t          ¦   «         5  t          |t          j        j        ¦  «        r|fnt          |¦  «        }t          | |d||¬¦  «        }t          | |¦  «        }t          | ||¦  «        }t          |||¦  «         ||fcddd¦  «         S # 1 swxY w Y   dS )aÖ  
    Return the model state_dict and optimizers state_dict.

    ``get_state_dict`` can process any module that is parallelized by PyTorch
    FSDP/fully_shard, DDP/replicate, tensor_parallel/parallelize_module, and any
    combination of these parallelisms. The main functions of ``get_state_dict``
    are: 1.) returning a model and optimizer state_dict that can be resharded
    with a different number of trainers and/or different parallelisms.
    2.) hiding the parallelism-specific state_dict APIs. Users don't have to call
    these APIs.
    3.) sanity checking the result state_dict.

    The keys of the result state dictionary are the canonical FQNs (Fully
    Qualified Names).  A canonical FQN refers to the FQN based on a parameter's
    position in an nn.Module hierarchy. More specifically, a canonical FQN to a
    parameter is the FQN returned by ``module.named_parameters()`` or
    ``module.named_buffers()`` when the module is not distributed by any
    parallelisms. Since the optimizer internally uses parameter IDs to represent
    a parameter, there will be a conversion from the parameter IDs to the
    canonical FQNs when calling this API.

    ``get_state_dict`` can also process a module that is not parallelized. In
    such a case, ``get_state_dict`` only performs one function -- converting the
    optimizer parameter IDs to the canonical FQNs.

    Example:
        >>> # xdoctest: +SKIP
        >>> import torch
        >>> from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
        >>> from torch.nn.parallel import DistributedDataParallel as DDP
        >>> from torch.distributed.checkpoint.state_dict import get_state_dict

        >>> fsdp_model = FSDP(copy.deepcopy(model))
        >>> fsdp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)
        >>> ddp_model = DDP(copy.deepcopy(model))
        >>> ddp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)


        >>> ddp_state_dict, ddp_optim_state_dict = get_state_dict(ddp_model, ddp_optim)
        >>> fsdp_state_dict, fsdp_optim_state_dict = get_state_dict(fsdp_model, fsdp_optim)

        >>> # if we simply call ddp_model.state_dict() and fsdp_model.state_dict(),
        >>> # the asserts will fail.
        >>> assert ddp_state_dict == fsdp_state_dict
        >>> assert ddp_optim_state == fsdp_optim_state_dict


    Args:
        model (nn.Module): the nn.Module to the model.
        optimizers (Union[None, Optimizer, Iterable[Optimizer]]):
            The optimizers that are used to optimize ``model``.
        submodules (deprecated): Optional[Set[nn.Module]]: only return the model parameters
            that belong to the submodules.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be returned. See
            `StateDictOptions` for the details.

    Returns:
        ``Tuple`` that contain model state_dict and optimizer state_dict.

    :rtype: typing.Tuple[typing.Dict[str, ValueType], OptimizerStateType]
    FrS  N)
rC   r|   r`   r  rU  rV  rÊ   rñ   r6  rÔ   )ri   r$  r¤   r¥   rÍ   rË   rÌ   s          rB   r2   r2     s  € õL 
‰Œð 2ð 2õ ˜*¥e¤kÔ&;Ñ<Ô<ð#ˆZˆMˆMå�zÑ"Ô"ð 	õ
 ØØØØ!Øð
ñ 
ô 
ˆõ 1°¸Ñ=Ô=ÐÝ0°¸
ÀDÑIÔIÐÝÐ+Ð-=¸tÑDÔDÐDØÐ!1Ð1ð!2ð 2ð 2ð 2ñ 2ô 2ð 2ð 2ð 2ð 2ð 2ð 2øøøð 2ð 2ð 2ð 2ð 2ð 2s   �A;BÂBÂBc                 ó  ‡	— |si S t          t          t          |                     ¦   «         ¦  «        ¦  «        t          j        ¦  «        �rt          j        dt          ¦  «         t          t          t          j        t          t          t          f         f         |¦  «        }i }|                     ¦   «         D ]¥\  }}|                      ¦   «         D ]‹\  }}||k    rŒt          | |¦  «        }t!          |¦  «        dk    s
J d¦   «         ‚t          t          |¦  «        ¦  «        › d�Š	|                     ˆ	fd„|                     ¦   «         D ¦   «         ¦  «         ŒŒŒ¦|S t          t          t          t          f         |¦  «        S )NzÑPassing model_state_dict as a ``Dict[nn.Module, Dict[str, Any]]``is deprecated and will be removed in 2.5. If you need this feature, please preprocessing the model_state_dict to achieve the same functionality.rr   z/FQNs for a submodule should only have 1 elementrp   c                 ó"   •— i | ]\  }}‰|z   |“ŒS rR   rR   )rt   Úsubfqnrý   rv   s      €rB   ú
<dictcomp>z/_unflatten_model_state_dict.<locals>.<dictcomp>Œ  s#   ø€ ÐXÐXÐX±°¸�V˜f‘_ eÐXÐXÐXrS   )r|   rè   ré   rÑ   rf   rg   rµ   rº   r¸   r	   r
   r_   r+   r¾   r¿   r‘   r€   r¼   )
ri   rÚ   Úcast_state_dictrï   rŸ   Úsub_state_dictrj   ÚmrÇ   rv   s
            @rB   Ú_unflatten_model_state_dictr_  r  s  ø€ ð ð Øˆ	å•$•t˜JŸOšOÑ-Ô-Ñ.Ô.Ñ/Ô/µ´Ñ;Ô;ñ 6ÝŒð"õ ñ	
ô 	
ð 	
õ �t¥B¤I­tµC½°NÔ/CÐ$CÔDÀjÑQÔQˆØ/1ˆØ)8×)>Ò)>Ñ)@Ô)@ð 
	ð 
	Ñ%ˆI�~Ø ×.Ò.Ñ0Ô0ð 	ð 	‘��aØ˜	’>�>Øå  ¨Ñ-Ô-�Ý˜4‘y”y A’~�~�~Ð'X‘~”~�~Ý ¥ d¡¤Ñ,Ô,Ð/Ð/Ð/�Ø×%Ò%ØXÐXÐXÐXÀ×AUÒAUÑAWÔAWÐXÑXÔXñô ð ð ð	ð Ðå•D��i˜Ô(¨*Ñ5Ô5Ð5rS   )r¥   c                óÜ   — t          | |¦  «        }t          ¦   «         5  t          | dd|¬¦  «        }t          |i |¦  «         t	          | ||¦  «        cddd¦  «         S # 1 swxY w Y   dS )a=  Load the model state_dict.

    The counterpart of ``get_model_state_dict`` to set the state_dict to the
    model. See ``set_state_dict`` for the detail usage.

    Args:
        model (nn.Module): the nn.Module to the model.
        model_state_dict: (Dict[str, ValueType]):
           the model state_dict to load. If the key of the ``model_state_dict``
           is nn.Module, the key is a submodule of ``model`` and the value should
           be the state_dict of the submodule. When loading the state_dict,
           the prefix of the submodule will be append to the state_dict.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be loaded. See
            `StateDictOptions` for the details.

    Returns:
        ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields:
            * **missing_keys** is a list of str containing the missing keys
            * **unexpected_keys** is a list of str containing the unexpected keys

    :type model_state_dict: typing.Dict[str, ValueType]
    rR   F©r§   r¥   N)r_  rC   rÊ   rÔ   r  )ri   rË   r¥   rÍ   s       rB   r3   r3   “  sØ   € õ: .IØÐñ.ô .Ðõ 
‰Œð Eð EÝ˜u b°UÀGÐLÑLÔLˆåÐ+¨R°Ñ6Ô6Ð6Ý% eÐ-=¸tÑDÔDð	Eð Eð Eð Eñ Eô Eð Eð Eð Eð Eð Eð Eøøøð Eð Eð Eð Eð Eð Es   Ÿ5A!Á!A%Á(A%c                ó"  — t          ¦   «         5  t          |t          j        j        ¦  «        r|fnt          |¦  «        }t          | |d|¬¦  «        }t          i ||¦  «         t          | |||¦  «         ddd¦  «         dS # 1 swxY w Y   dS )aÚ  Load the optimizers state_dict.

    The counterpart of ``get_optimizer_state_dict`` to set the state_dict to the
    optimizers. See ``set_state_dict`` for the detail usage.

    Args:
        model (nn.Module): the nn.Module to the model.
        optimizers (Union[Optimizer, Iterable[Optimizer]]):
            The optimizers that are used to optimize ``model``.
        optim_state_dict: OptimizerStateType:
            the optimizer state_dict to load.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be loaded. See
            `StateDictOptions` for the details.

    Returns:
        None

    :type optim_state_dict: typing.OptimizerStateType
    Tra  N)	rC   r|   r`   r  rU  rV  rÊ   rÔ   rQ  )ri   r$  rÌ   r¥   rÍ   s        rB   r4   r4   º  sõ   € õ6 
‰Œð 	Jð 	Jõ ˜*¥e¤kÔ&;Ñ<Ô<ð#ˆZˆMˆMå�zÑ"Ô"ð 	õ
 ˜u j¸TÈ7ÐSÑSÔSˆå˜2Ð/°Ñ6Ô6Ð6Ý˜u jÐ2BÀDÑIÔIÐIð	Jð 	Jð 	Jñ 	Jô 	Jð 	Jð 	Jð 	Jð 	Jð 	Jð 	Jð 	Jøøøð 	Jð 	Jð 	Jð 	Jð 	Jð 	Js   �A(BÂBÂBc                ód  — t          | |¦  «        }t          ¦   «         5  t          |t          j        j        ¦  «        r|fnt          |¦  «        }t          | || |¬¦  «        }t          |||¦  «         t          | |||¦  «         t          | ||¦  «        cddd¦  «         S # 1 swxY w Y   dS )a4  Load the model state_dict and optimizers state_dict.

    The counterpart of ``get_state_dict`` to set the state_dict to the model and
    optimizers.  The given ``model_state_dict`` and ``optim_state_dict`` do not
    have to be returned by ``get_state_dict`` but must meet the following
    requirements: 1) all FQNs are canonical FQNs as defined in ``get_state_dict``,
    2) if a tensor is sharded, it must be either a ShardedTensor or DTensor,
    3) optimizer state_dict cannot contain the parameter IDs; the keys should be
    the canonical FQNs.

    Args:
        model (nn.Module): the nn.Module to the model.
        optimizers (Union[Optimizer, Iterable[Optimizer]]):
            The optimizers that are used to optimize ``model``.
        model_state_dict: (Union[Dict[nn.Module, Dict[str, ValueType]], Dict[str, ValueType]]):
           the model state_dict to load. If the key of the ``model_state_dict``
           is nn.Module, the key is a submodule of ``model`` and the value should
           be the state_dict of the submodule. When loading the state_dict,
           the prefix of the submodule will be append to the state_dict.
        optim_state_dict: OptimizerStateType:
            the optimizer state_dict to load.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be loaded. See
            `StateDictOptions` for the details.

    Returns:
        ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields:
            * **missing_keys** is a list of str containing the missing keys of the model state_dict.
            * **unexpected_keys** is a list of str containing the unexpected keys of the model state_dict.

    :type model_state_dict: typing.Dict[str, ValueType]
    :type optim_state_dict: typing.OptimizerStateType
    ra  N)r_  rC   r|   r`   r  rU  rV  rÊ   rÔ   rQ  r  )ri   r$  rË   rÌ   r¥   rÍ   s         rB   r5   r5   á  s-  € õT .IØÐñ.ô .Ðõ 
‰Œð Eð Eõ ˜*¥e¤kÔ&;Ñ<Ô<ð#ˆZˆMˆMå�zÑ"Ô"ð 	õ
 Ø�:Ð.>Ð*>Èð
ñ 
ô 
ˆõ 	Ð+Ð-=¸tÑDÔDÐDÝ˜u jÐ2BÀDÑIÔIÐIÝ% eÐ-=¸tÑDÔDðEð Eð Eð Eñ Eô Eð Eð Eð Eð Eð Eð Eøøøð Eð Eð Eð Eð Eð Es   ŸA9B%Â%B)Â,B)c                óF  ‡‡— t          j        t          | |¬¦  «        Šˆfd„}|| _        t          j        t          | |¬¦  «        Šdt
          t          t          f         fˆfd„}|| _        t           
                    |¦  «         t           
                    |¦  «         dS )aó  Patch the ``state_dict`` and ``load_state_dict`` attributes of ``model``.

    Patch the ``state_dict`` and ``load_state_dict`` attributes of ``model`` to
    be a partial function to call ``get_state_dict`` and ``set_state_dict``.

    Example:
        from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
        from torch.distributed.checkpoint.state_dict import patch_model_state_dict

        model = fsdp(model)
        patch_model_state_dict(model)

    Args:
        model (nn.Module): the nn.Module to the model.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be loaded. See
            `StateDictOptions` for the details.
    Returns:
        None
    )ri   r¥   c                  ó   •—  ‰ ¦   «         S r<   rR   ©Ú_state_dict_calls   €rB   Ústate_dict_callz0_patch_model_state_dict.<locals>.state_dict_call@  ó   ø€ ØÐÑ!Ô!Ð!rS   rÚ   c                 ó    •—  ‰| ¬¦  «         d S )N)rË   rR   ©rÚ   Ú_load_state_dict_calls    €rB   Úload_state_dict_callz5_patch_model_state_dict.<locals>.load_state_dict_callK  ó   ø€ ØÐ¨zÐ:Ñ:Ô:Ð:Ð:Ð:rS   N)rÄ   rÅ   r0   rÚ   r3   r
   r_   r   rö   r:   r™   )ri   r¥   rh  rm  rl  rg  s       @@rB   Ú_patch_model_state_dictro    sÑ   øø€ õ6 !Ô(ÝØØðñ ô Ðð"ð "ð "ð "ð "ð '€EÔå%Ô-ÝØØðñ ô Ðð;­­cµ3¨h¬ð ;ð ;ð ;ð ;ð ;ð ;ð 1€EÔå×Ò˜OÑ,Ô,Ð,Ý×ÒÐ0Ñ1Ô1Ð1Ð1Ð1rS   c                ó¶  ‡‡— t          j        t          | ||¬¦  «        Šˆfd„}t          j        t          | ||¬¦  «        Šdt          t
          t          f         fˆfd„}t                               |¦  «         t                               |¦  «         t          |t          j        j        ¦  «        r|fnt          |¦  «        }|D ]}||_        ||_        ŒdS )a¤  Patch the ``state_dict`` and ``load_state_dict`` attributes of ``optimizers``.

    Patch the ``state_dict`` and ``load_state_dict`` attributes of ``optimizers`` to
    be a partial function to call ``get_state_dict`` and ``set_state_dict``.

    Note that if there are multiple optimizers, all of the optimizers will be patched.
    So users only need to call one of the state_dict() to get the full result.

    Example:
        from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
        from torch.distributed.checkpoint.state_dict import patch_model_state_dict

        model = fsdp(model)
        patch_model_state_dict(model)

    Args:
        model (nn.Module): the nn.Module to the model.
        options (StateDictOptions): the options to control how
            model state_dict and optimizer state_dict should be loaded. See
            `StateDictOptions` for the details.
    Returns:
        None
    )ri   r$  r¥   c                  ó   •—  ‰ ¦   «         S r<   rR   rf  s   €rB   rh  z4_patch_optimizer_state_dict.<locals>.state_dict_call|  ri  rS   rÚ   c                 ó    •—  ‰| ¬¦  «         d S )N)rÌ   rR   rk  s    €rB   rm  z9_patch_optimizer_state_dict.<locals>.load_state_dict_call†  rn  rS   N)rÄ   rÅ   r1   r4   r
   r_   r   r:   r™   r|   r`   r  rU  rV  rÚ   rö   )ri   r$  r¥   rh  rm  r  rl  rg  s         @@rB   Ú_patch_optimizer_state_dictrs  V  s  øø€ õ> !Ô(Ý ØØØð	ñ ô Ðð"ð "ð "ð "ð "õ &Ô-Ý ØØØð	ñ ô Ðð;­­cµ3¨h¬ð ;ð ;ð ;ð ;ð ;ð ;õ ×Ò˜OÑ,Ô,Ð,Ý×ÒÐ0Ñ1Ô1Ð1õ �j¥%¤+Ô"7Ñ8Ô8ð	ˆˆˆå�:ÑÔð ð
 ð 5ð 5ˆØ*ˆÔØ 4ˆÔÐð5ð 5rS   )TT)rrc   rÄ   r=   rµ   Údataclassesr   r   r   Ú	itertoolsr   Útypingr   r   r	   r
   r   r   r   r   r   r   r   r   r`   Útorch.distributedrÝ   rÏ   Útorch.nnrf   Ú'torch.distributed._shard.sharded_tensorr   Ú#torch.distributed._state_dict_utilsr   r   r   r   r   r   Ú;torch.distributed.algorithms._checkpoint.checkpoint_wrapperr   Útorch.distributed.fsdpr   r   r   r   r   r   r    r!   r"   Ú$torch.distributed.fsdp._common_utilsr#   r$   Útorch.distributed.tensorr%   Útorch.nn.modules.moduler&   Útorch.nn.parallelr'   r}   Útorch.utils._pytreer(   Ú__all__r�   r  r  r  r_   r)   ra   r  r  r*   r+   r,   r-   r.   rb   r:   rQ   rÃ   rC   r/   rU   Ú	lru_cacherg   rP   r‘   r“   r£   r  rU  rÊ   rÔ   rÙ   rß   Úno_gradrñ   r  r  r  r#  r6  r?  rQ  r0   r1   r2   r_  r3   r4   r5   ro  rs  rR   rS   rB   ú<module>r…     se  ðà Ð Ð Ð Ð Ø Ð Ð Ð Ø 	€	€	€	Ø €€€Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð Ø AÐ AÐ AÐ AÐ AÐ Aðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðð ð ð ð ð ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ðð ð ð ð ð ð ð ð -Ð ,Ð ,Ð ,Ð ,Ð ,Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø <Ð <Ð <Ð <Ð <Ð <Ø -Ð -Ð -Ð -Ð -Ð -ðð ð €ð" €Ø€Ø
€Ø	€à	ˆSŒ€Ø�g˜}¨e¬l¸CÀÈÐKÔL€ØØ�4˜Ô&¨¨mÔ(<¸dÀ3ÈÐCSÔ>TÐTô€	ð �S˜)�^Ô$€Ø˜Ô'Ð Ø˜#˜u ]Ð4EÐ%EÔFÐFÔGÐ ð &) S¡U¤UÐ �S˜”]Ð *Ð *Ñ *ð Ôðð ñ Ôðð ð+/ð +/ð +/ð +/ð +/ñ +/ô +/ñ „ð+/ð\ ð@ð @ð @ð @ð @Ð%ñ @ô @ñ „ð@ð €Ô˜TÐ"Ñ"Ô"ð !Ø!%ð	;Eð ;EØŒ9ð;Eà
ð;Eð ð;Eð ð	;Eð
 ð;Eð ;Eð ;Eñ #Ô"ð;Eð|	ð 	ð 	ð 	ð 	ñ 	ô 	ð 	ð"ð "ð "ðH ,0Ø*.ðð ð ØŒ9ðà�%”+Ô'¨Ð,Ô-ðð ðð
 ˜˜RœYœÔ(ðð Ð&Ô'ðð ðð ð ð ðD*Ø˜3 	˜>Ô*ð*à(ð*ð ð*ð 
ð	*ð *ð *ð *ðZ˜˜bœi¨¬Ô)>Ð>Ô?ð Àcð Èhð ð ð ð ðØ�S˜#�X”ðØ&4ðà	ˆ#ˆsˆ(„^ðð ð ð ð$ €„�„ð@;ØŒ9ð@;Ø*ð@;à	ˆ#ˆyˆ.Ôð@;ð @;ð @;ñ „ð@;ðF €„�„ð2
ØŒ9ð2
à�S˜)�^Ô$ð2
ð ð2
ð ð	2
ð 2
ð 2
ñ „ð2
ðj'&˜Uœ[Ô2ð '&°tð '&ð '&ð '&ð '&ðT=Ð*<ð =ÀÀcÈ9ÀnÔAUð =ð =ð =ð =ð@*ØŒ;Ô ð*à�S˜)�^Ô$ð*ð ð*ð ð	*ð *ð *ð *ðZ €„�„ð<AØŒ9ð<Aà�e”kÔ+¨SÐ0Ô1ð<Að ð<Að ð	<Að <Að <Añ „ð<Að~EØŒ9ðEàŒ;Ô ðEð )ðEð ð	Eð
 ðEð Eð Eð EðP €„�„ðZNØŒ9ðZNà�e”kÔ+¨SÐ0Ô1ðZNð #ðZNð ð	ZNð
 
ðZNð ZNð ZNñ „ðZNð@ ,0Ø*.ð	" ð " ð " ØŒ9ð" ð ˜˜RœYœÔ(ð" ð Ð&Ô'ð	" ð
 
ˆ#ˆyˆ.Ôð" ð " ð " ð " ðR ,0Ø*.ð* ð * ð * ØŒ9ð* à�e”kÔ+¨X°e´kÔ6KÔ-LÐLÔMð* ð ˜˜RœYœÔ(ð	* ð
 Ð&Ô'ð* ð ð* ð * ð * ð * ðb ,0Ø*.ðV2ð V2ð V2ØŒ9ðV2à�e”kÔ+¨X°e´kÔ6KÔ-LÐLÔMðV2ð ˜˜RœYœÔ(ð	V2ð
 Ð&Ô'ðV2ð ˆ4��Y�ÔÐ!3Ð3Ô4ðV2ð V2ð V2ð V2ðr6ØŒ9ð6à�d˜2œ9 d¨3°	¨>Ô&:Ð:Ô;¸TÀ#ÀyÀ.Ô=QÐQÔRð6ð 
ˆ#ˆyˆ.Ôð6ð 6ð 6ð 6ðJ +/ð	$Eð $Eð $EØŒ9ð$Eà˜3 	˜>Ô*ð$Eð Ð&Ô'ð	$Eð
 ð$Eð $Eð $Eð $EðX +/ð$Jð $Jð $JØŒ9ð$Jà�e”kÔ+¨X°e´kÔ6KÔ-LÐLÔMð$Jð )ð$Jð
 Ð&Ô'ð$Jð 
ð$Jð $Jð $Jð $JðZ +/ð9Eð 9Eð 9EØŒ9ð9Eà�e”kÔ+¨X°e´kÔ6KÔ-LÐLÔMð9Eð ˜3 	˜>Ô*ð	9Eð
 )ð9Eð Ð&Ô'ð9Eð ð9Eð 9Eð 9Eð 9Eð| ð +/ð12ð 12ð 12ØŒ9ð12ð Ð&Ô'ð12ð 
ð	12ð 12ð 12ñ „ð12ðl ð
 +/ð	;5ð ;5ð ;5ØŒ9ð;5ð �e”kÔ+¨SÐ0Ô1ð;5ð Ð&Ô'ð	;5ð
 
ð;5ð ;5ð ;5ñ „ð;5ð ;5ð ;5rS   