
    Ph                        d dl mZmZmZ d dl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 ddlmZ ddlmZmZ d	d
gZ	 	 	 	 ddeeef   dedeej,                     dededee   ddfdZdd ddddeeef   dedeej,                     dededee   ddfdZ	 	 	 	 ddeeef   dedeej,                     dededee   ddfdZy)    )AnyDictOptionalN)Stateful   )StorageReader)LoadPlanner)DefaultLoadPlanner)_DistWrapper_all_gather_keysload_state_dictloadF
state_dictstorage_readerprocess_groupcoordinator_rankno_distplannerreturnc                 L    t        j                  d       t        | |||||      S )z3This method is deprecated. Please switch to 'load'.zb'load_state_dict' is deprecated and will be removed in future versions. Please use 'load' instead.)warningswarn_load_state_dict)r   r   r   r   r   r   s         yC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/checkpoint/state_dict_loader.pyr   r      s-     MMl JGWY`bijj    )r   r   r   r   c                   t        |       }|t        | j                               k7  rt        j                  d       i }|D ]1  }|| vr| |   }	t        |	t              r|	j                         n|	||<   3 t        ||||||       |D ]5  }|| vr| |   }	t        |	t              r|	j                  ||          |	| |<   7 y)a  
    Load a distributed ``state_dict`` in SPMD style.

    Each rank will try to read the least amount of data necessary
    to fullfill 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.

    .. 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:
        This function can be used for local inference and load a checkpoint
        produced by ``save_state_dict`` without having a process group initialized
        by passing ``no_dist=True`` and by using Tensors instead of ShardedTensors.

    Args:
        state_dict (Dict[str, Any]) : The state_dict to load. Note that this
            state dict will updated in place.
        storage_reader (StorageReader): StorageReader used to load data from.
        process_group (ProcessGroup):
            ProcessGroup to be used for cross-rank synchronization.
        coordinator_rank (int):
            Rank to use to coordinate the checkpoint.
            rank0 is used by default.
        no_dist (bool): If ``True``, distributed checkpoint will not load
            in SPMD style. (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()``.
    z|Detected mismatched keys in state dict after all gather! This behavior is unsupported and may cause errors may cause errors.N)
r   sortedkeysr   r   
isinstancer   r   r   r   )
r   r   r   r   r   r   r   statetful_sdkeyelems
             r   r   r   !   s    Z J'Dvjoo'((S	

 Lj #1;D(1KDOO-QUS	  \>=BRT[]dej #dH%  c!23
3 r   c                     
 t         j                  j                  d       t        || |      
t	                fd}fd}j                  d||      

fd}j                  d|      }	y )Nz,torch.distributed.checkpoint.load_state_dictc                      J j                         } j                  | j                         j                  | j                         j	                         }j                  |      }|S 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_stepz$_load_state_dict.<locals>.local_step   so    """!//1z8U5I5IJ,,Xu7K7KL..0
#66zB
r   c                 T    J j                  |       } j                  |       } | S r%   )create_global_planprepare_global_plan)all_local_plansr   r   s    r   global_stepz%_load_state_dict.<locals>.global_step   s5    """!44_E(<<_Mr   planc                  t    J j                        } j                  |       }|j                          y r%   )finish_plan	read_datawait)final_local_plan	all_readscentral_planr   r   s     r   r8   z#_load_state_dict.<locals>.read_data   s@    """"..|<",,-=wG	r   read)torch_C_log_api_usage_oncer   r
   reduce_scatter
all_gather)r   r   r   r   r   r   r/   r4   r8   _r<   r.   s   ``   `    @@r   r   r      so     
HH  !OPG5EFE$& ''
KHL 	+Ar   )Nr   FN)typingr   r   r   r   r>   torch.distributeddistributeddist%torch.distributed.checkpoint.statefulr   storager   r   r	   default_plannerr
   utilsr   r   __all__strProcessGroupintboolr   r   r    r   r   <module>rR      s   & &     : ! / 1f
%
 26%)kS#Xk!k D--.k 	k
 k k"k 
k& 26%)bS#Xb!b D--.	b
 b b k"b 
bN 26%)),S#X),!), D--.), 	),
 ), k"), 
),r   