
    sh0                        S SK r S SKrS SKJrJrJrJr  S SKJr  S SK	r	S SK
Jr  S SKJr  S SKJr  S SKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJrJrJr  SS/r \" S\!S9    S#S\"\#\4   S\S\\RH                     S\%S\&S\\   SS4S jj5       r'\" SS9\SSSSSS.S\"\#\4   S\\#\ RP                  S4   S\\   S\\   S\\RH                     S\&SS4S jj5       5       r)    S#S\"\#\4   S\S\\RH                     S\%S\&S\\   SS4S jjr* S$SSSS .S!\\\+\#   \#4      S\\#\ RP                  S4   S\\   S\\RH                     S\"\#\4   4
S" jjjr,g)%    N)AnycastOptionalUnion)
deprecated)_EmptyStateDictLoadPlanner)_dcp_method_logger)Stateful   )_storage_setup)DefaultLoadPlanner)LoadPlanLoadPlanner)StorageReader)_api_bc_check_DistWrapper_profileload_state_dictloadzb`load_state_dict` is deprecated and will be removed in future versions. Please use `load` instead.)categoryF
state_dictstorage_readerprocess_groupcoordinator_rankno_distplannerreturnc           	          UR                  5         [        5          [        U UUUUU5      sSSS5        $ ! , (       d  f       g= f)z3This method is deprecated. Please switch to 'load'.N)resetr   _load_state_dict)r   r   r   r   r   r   s         ڂ/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/checkpoint/state_dict_loader.pyr   r      s9     	
 
s	   5
AT)log_exceptions)checkpoint_idr   r   r   r   r#   c          
         U=(       d;    [         R                  " 5       (       + =(       d    [         R                  " 5       (       + nU(       a  [        R                  " S5        [        5          [        [        [        X!SS95      n[        U R                  5       5      n0 nU H7  nX;  a  M
  X   n	[        U	[        5      (       a  U	R                  5       OU	Xx'   M9     [        UUUUUS9  U H>  nX;  a  M
  X   n	[        U	[        5      (       a  U	R                  Xx   5        M8  Xx   X'   M@     SSS5        g! , (       d  f       g= f)a  
Load a checkpoint into a distributed state dict in SPMD style.

Each rank must have the same keys in their ``state_dict`` provided to this
API. Mismatched keys may result in hangs or errors. If unsure, you can use
the ``utils._assert_same_keys`` API to check (but may incur communication
costs).

Each rank will try to read the least amount of data necessary
to fulfill the requested `state_dict`. When loading :class:`ShardedTensor`
or :class:`DTensor` instances, each rank only reads data for their local shards.

For each ``Stateful`` object (having both a ``state_dict`` and a ``load_state_dict``),
load will first call ``state_dict`` before attempting deserialization, followed by
``load_state_dict`` once the deserialization is complete.
For each non-``Stateful`` object, load will deserailize the object, and then replace
it in the ``state_dict`` with the deserialized object.

.. warning::
    All tensors in ``state_dict`` must be allocated on their
    destination device *prior to* calling this function.

    All non-tensor data is loaded using `torch.load()` and modified in place
    on state_dict.

.. warning::
    Users must call `load_state_dict` on the root module to ensure load
    pos-processing and non-tensor data properly propagates.

.. note:
    If no process group is initialized, this function will assume the intent
    is to load a checkpoint into the local process. This can be useful in the
    case of local inference, and when using regular Tensors (as opposed to DTensor
     or ShardedTensor)

.. note:
    Rank 0 is assumed to be the coordinator rank.

Args:
    state_dict (Dict[str, Any]): The state_dict to load the checkpoint into.
    checkpoint_id (Union[str, os.PathLike, None]):
        The ID of this checkpoint instance. The meaning of the checkpoint_id
        depends on the storage. It can be a path to a folder or to a file.
        It can also be a key if the storage is a key-value store.
        (Default: ``None``)
    storage_reader (Optional[StorageReader]):
        Instance of StorageWriter used to perform reads. If this is not
        specified, DCP will automatically infer the reader based on the
        checkpoint_id. If checkpoint_id is also None, an exception will
        be raised. (Default: ``None``)
    planner (Optional[LoadPlanner]):
        Instance of LoadPlanner. If this is not specificed, the default
        planner will be used. (Default: ``None``)
    process_group (Optional[ProcessGroup]):
        ProcessGroup to be used for cross-rank synchronization.
        (Default: ``None``)
    no_dist (bool): If ``True``, this function will assume the intent is to load
        a checkpoint without using cross-rank synchronization. (Default: ``False``)
Returns:
    None.

Examples
    >>> # xdoctest: +SKIP
    >>> my_model = MyModule()
    >>> optimizer = Adagrad(my_model.parameters())
    >>> model_state_dict = my_model.state_dict()
    >>> fs_storage_reader = torch.distributed.checkpoint.FileSystemReader(
    ...     "/checkpoint/1"
    ... )

    >>> torch.distributed.checkpoint.load_state_dict(
    >>>     state_dict=model_state_dict,
    >>>     storage_reader=fs_storage_reader,
    >>> )

    >>> # module.load_state_dict() function might have customized steps
    >>> # to flush the state_dict, must call it to
    >>> # ensure correct behavior.
    >>> my_model.load_state_dict(model_state_dict)

.. note::
    load_state_dict uses collectives to coordinate reads across ranks.
    For NCCL-based process groups, internal tensor representations of
    objects must be moved to the GPU device before communication takes place.
    In this case, the device used is given by ``torch.cuda.current_device()``
    and it is the user's responsibility to ensure that this is set so that each
    rank has an individual GPU, via ``torch.cuda.set_device()``.
zptorch.distributed is disabled, unavailable or uninitialized, assuming the intent is to load in a single process.Treaderr   r   r   r   r   N)distis_availableis_initializedwarningswarnr   r   r   r   sortedkeys
isinstancer
   r   r    r   )
r   r#   r   r   r   r   r.   statetful_sdkeyelems
             r!   r   r   3   s   H Qd//11Q4;N;N;P7PG~	
 
>.PTU
 joo'(C$?D%/h%?%?!T 	  	#)'	
 C$?D$)) $$\%67 #/"3
 7 
s   ,CD77
Ec                   ^ ^^^^ [         R                  R                  S5        [        X$(       + U5      mTc
  [	        5       m0 n[        TSS 5      =nb  XvS'   TR                  US'   [        S	0 UD6UUU U4S j5       n[        S	0 UD6UU4S j5       n	TR                  SX5      m[        S	0 UD6UUU4S j5       n
TR                  SU
5      ng )
Nz,torch.distributed.checkpoint.load_state_dictr#   r   c                     > Tc   eTR                  5       n TR                  TU TR                  5        TR                  U TR                  5        TR	                  5       nTR                  U5      nU$ N)read_metadataset_up_planneris_coordinatorset_up_storage_readercreate_local_planprepare_local_plan)metadata
local_plandistWr   r   r   s     r!   
local_step$_load_state_dict.<locals>.local_step   so    """!//1z8U5I5IJ,,Xu7K7KL..0
#66zB
    c                 V   > Tc   eTR                  U 5      n TR                  U 5      n U $ r5   )create_global_planprepare_global_plan)all_local_plansr   r   s    r!   global_step%_load_state_dict.<locals>.global_step   s5    """!44_E(<<_MrA   planc                  v   > Tc   eTR                  T5      n TR                  U T5      nUR                  5         g r5   )finish_plan	read_datawait)final_local_plan	all_readscentral_planr   r   s     r!   rK   #_load_state_dict.<locals>.read_data   s@    """"..|<",,-=wG	rA   read )
torch_C_log_api_usage_oncer   r   getattrgroupr	   reduce_scatter
all_gather)r   r   r   r   r   r   ckpt_kwargsckpt_idr?   rF   rK   _rO   r>   s   ``   `      @@r!   r    r       s     
HH  !OP5EFE$&K>?DAAN'.O$',{{O$&+& ' &+& ' #11&*RL&+& ' 	+ArA   )r#   r   r   r.   c          
         [         R                  R                  S5        [        R                  " 5       =(       a    [        R
                  " 5       (       + nU(       a  [        R                  " S5        [        [        [        X!SS95      n[        U [        5      (       a  U 1n 0 n[        UUUU[        U =(       d
    [        5       S9S9  U$ )a  
Load only the specified keys from the checkpoint, if no keys are specified, the entire
checkpoint will be loaded. Note, this method completely loads the checkpoint into the
current process and is not distributed.

.. warning::


.. warning::

    All non-tensor data is loaded using `torch.load()`

.. note:
    As opposed to the usual pattern, this function does not take a state dict as input
    and does not load inplace. Instead, a new state dict is directly initialized and read
    from file.

.. note:
    If no process group is initialized, this function will assume the intent
    is to load a checkpoint into the local process. This can be useful in the
    case of local inference, and when using regular Tensors (as opposed to DTensor
     or ShardedTensor)

.. note:
    Rank 0 is assumed to be the coordinator rank.

Args:
    keys (Optional[Union[set[str], str]]):
        Loads any key specified in this set. If no keys are specified, the entire checkpoint
        is loaded.
    checkpoint_id (Union[str, os.PathLike, None]):
        The ID of this checkpoint instance. The meaning of the checkpoint_id
        depends on the storage. It can be a path to a folder or to a file.
        It can also be a key if the storage is a key-value store.
        (Default: ``None``)
    storage_reader (Optional[StorageReader]):
        Instance of StorageWriter used to perform reads. If this is not
        specified, DCP will automatically infer the reader based on the
        checkpoint_id. If checkpoint_id is also None, an exception will
        be raised. (Default: ``None``)
    process_group (Optional[ProcessGroup]):
        ProcessGroup to be used for cross-rank synchronization.
        (Default: ``None``)

Returns:
    State dict from specified keys
z7torch.distributed.checkpoint._load_state_dict_from_keyszftorch.distributed is unavailable or uninitialized, assuming the intent is to load in a single process.Tr%   )r.   r'   )rS   rT   rU   r(   r)   r*   r+   r,   r   r   r   r/   strr    r   set)r.   r#   r   r   r   sds         r!   _load_state_dict_from_keysra      s    l 
HH  A $$&@4+>+>+@AGt	
 ~nDQN $vB%#*> IrA   )Nr   FNr5   )-osr+   typingr   r   r   r   typing_extensionsr   rS   torch.distributeddistributedr(   ,torch.distributed.checkpoint.default_plannerr   #torch.distributed.checkpoint.loggerr	   %torch.distributed.checkpoint.statefulr
   _storage_utilsr   default_plannerr   r   r   r   storager   utilsr   r   r   __all__FutureWarningdictr^   ProcessGroupintboolr   PathLiker   r    r_   ra   rR   rA   r!   <module>ru      sr   
  - - (    S B : * / * " 8 8 f
% ! 26%)
S#X
!
 D--.
 	

 
 k"
 



, 4( 48.2%)15M4S#XM4 bkk4/0M4 ]+	M4
 k"M4 D--.M4 M4 
M4  )M4f 26%)0,S#X0,!0, D--.0, 	0,
 0, k"0, 
0,h ,0P 48.215P
5S3'
(P bkk4/0P ]+	P
 D--.P 
#s(^PrA   