
    9j)              	       4   d dl Z d dlZd dlmZ d dlmZ d dlZd dlmZ	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZ d dlmZmZmZmZmZ d d	lmZmZ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' g dZ( G d de%      Z) G d de      Z*de+ejX                  z  de+ejX                  z  fdZ-de+ejX                  z  de+ejX                  z  fdZ.e/dk(  r G d de      Z0 e jb                         Z2e2jg                  de+de0D  cg c]  } | jh                   c} e0jj                         e2jg                  de+d        e2jg                  d!e+d"        e2jm                         Z7 e8d#e7jr                   d$e7jt                   d%e7jv                   d&       d'e7jr                   d(Z<e7jv                  e0jj                  jh                  k(  rLejz                  j}                  e7jr                        r e.e7jr                  e7jt                         y e8e<       ye7jv                  e0j~                  jh                  k(  rLejz                  j                  e7jr                        r e-e7jr                  e7jt                         y e8e<       y eAd)e7jv                         yc c} w )*    N)Enum)cast)narrow_tensor_by_index)FileSystemReaderFileSystemWriter)flatten_state_dict)_EmptyStateDictLoadPlannerDefaultLoadPlanner)MetadataSTATE_DICT_TYPESTORAGE_TYPESTensorPropertiesTensorStorageMetadata)LoadItemTypeLoadPlanLoadPlanner)_create_chunk_list)_load_state_dict)_save_state_dict)StorageReader)Future)dcp_to_torch_savetorch_save_to_dcpBroadcastingTorchSaveReaderDynamicMetaLoadPlannerc                      e Zd ZdZ	 	 ddeej                  z  dz  deddfdZde	fdZ
ded	eded   fd
Zde	deddfdZdedefdZdee   dee   fdZddeej                  z  dz  ddfdZedeej                  z  defd       Zy)r   aI  
    StorageReader for reading a Torch Save file. This reader will read the entire checkpoint
    on the coordinator rank, and then broadcast and shard each tensor to all ranks.

    . N.B. Intended to be used with DynamicMetaLoadPlanner

    .. warning::
        Current implementation only supports loading Tensors.

    >>> # xdoctest: +SKIP("undefined vars")
    >>> sd = {"mode": model}
    >>> dcp.load(
    >>>    sd,
    >>>    storage_reader=BroadcastingTorchSaveReader(),
    >>>    planner=DynamicMetaLoadPlanner(),
    >>>    checkpoint_id="path_to_model.pt"
    >>> )
    Ncheckpoint_idcoordinator_rankreturnc                      || _         || _        y N)r   r   )selfr   r   s      i/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/distributed/checkpoint/format_utils.py__init__z$BroadcastingTorchSaveReader.__init__;   s    
 + 0    c                     t        i       S )zGExtends the default StorageReader to support building the metadata filestate_dict_metadata)r   )r"   s    r#   read_metadataz)BroadcastingTorchSaveReader.read_metadataD   s     B//r%   planplannerc           
         t        t        |      }| j                  rT| j                  t	        d      t        j                  | j                  dd      }|j                  rt        |      \  }}nd}|j                  D ]  }|j                  t        j                  k(  r9t        d|j                  j                   dt        |       j                   d      | j                  rGt         j"                  j%                         }||j                  j                     j'                  |      }n6t        j(                  |j*                  |j                  j                           }t!        j,                  || j.                  d	       t1        ||j2                  |j4                        }|j7                  |      j9                         }|j;                         |j;                         k(  s:t	        d
|j                   d|j;                          d|j;                                |j=                  |       |j?                  ||        tA               }	|	jC                  d       |	S )z
        Reads torch save data on the coordinator rank, and broadcast afterwards
        this incurrs a communication cost, but avoids having to load
        the entire checkpoint on each rank, hopefully preventing OOM issues
        Nz-checkpoint_id must be set before reading datacpuF)map_locationweights_onlyNon-tensor value identified at . At this time  only supports loading Tensors.)srcasync_opzreq z mismatch sizes, z vs )"r   r
   is_coordinatorr   AssertionErrortorchloadr   itemstyper   BYTE_IORuntimeErrorstorage_indexfqn__name__distdistributed_c10d_get_pg_default_deviceto
empty_like
state_dict	broadcastr   r   storage_offsetslengthsresolve_tensordetachsizecopy_commit_tensorr   
set_result)
r"   r*   r+   torch_state_dict_req	pg_devicetensortarget_tensorfuts
             r#   	read_dataz%BroadcastingTorchSaveReader.read_dataJ   s    )73 !!)$%TUU$zz""U  ))&89I&J# !#:: 	6Cxx<///"5c6G6G6K6K5L M$$(J$7$7#88WY  "" 11HHJ	)#*;*;*?*?@CCIN))'*<*<S=N=N=R=R*STNN6t'<'<uM+FC4G4GUF#2237>>@M %%'6;;=8$3,,-->$))+,DA  '!!#}53	66 ht
r%   metadatar5   c                     || _         | j                   rNt        j                         | j                  k(  s-t	        d| j                   dt        j                                | j
                  t	        d      y)*Implementation of the StorageReader methodz$Coordinator rank mismatch: expected z, got Nz:checkpoint_id must be set before setting up storage reader)r5   r@   get_rankr   r6   r   )r"   rW   r5   s      r#   set_up_storage_readerz1BroadcastingTorchSaveReader.set_up_storage_reader   s|    ,==?d&;&;;$:4;P;P:Q R==?+- 
 % L  &r%   c                     |S rY    )r"   r*   s     r#   prepare_local_planz.BroadcastingTorchSaveReader.prepare_local_plan   s    r%   global_planc                     |S r]   r^   )r"   r`   s     r#   prepare_global_planz/BroadcastingTorchSaveReader.prepare_global_plan   s    r%   c                     || _         y)rY   N)r   )r"   r   s     r#   resetz!BroadcastingTorchSaveReader.reset   s
    *r%   c                 @    t         j                  j                  |      S r]   )ospathisfile)clsr   s     r#   validate_checkpoint_idz2BroadcastingTorchSaveReader.validate_checkpoint_id   s     ww~~m,,r%   )Nr   r!   )r?   
__module____qualname____doc__strrf   PathLikeintr$   r   r)   r   r   r   rV   boolr[   r_   listrb   rd   classmethodrj   r^   r%   r#   r   r   '   s    * 37 !1R[[(4/1 1 
	10x 04h 4 4 4nh  QU x H tH~ $x. +3#4t#; +t + -33D - - -r%   r   c            	       @     e Zd ZdZ	 	 ddededz  deddf fdZ xZS )	r   a  
    Extension of DefaultLoadPlanner, which creates a new Metadata object based on the passed in state dict,
    avoiding the need to read metadata from disk. This is useful when reading formats which don't have a
    metadata file, like Torch Save files.

    . N.B. Intended to be used with BroadcastingTorchSaveReader

    .. warning::
        Current implementation only supports loading Tensors.

    >>> # xdoctest: +SKIP("undefined vars")
    >>> sd = {"mode": model}
    >>> dcp.load(
    >>>    sd,
    >>>    storage_reader=BroadcastingTorchSaveReader(),
    >>>    planner=DynamicMetaLoadPlanner(),
    >>>    checkpoint_id="path_to_model.pt"
    >>> )
    NrE   rW   r5   r   c           	      |   t         |   |||       i }| j                  j                         D ]z  \  }}t	        j
                  |      s%t        d| dt        |       j                   d      t        t        |j                        |j                         t        |            ||<   | t        |      | _        y)zdSetups of the planner, extnding default behavior by creating the Metadata object from the state dictr0   r1   r2   )dtyper'   N)superset_up_plannerrE   r9   r7   	is_tensorr<   r:   r?   r   r   rv   rK   r   r   rW   )r"   rE   rW   r5   r(   keyrS   	__class__s          r#   rx   z%DynamicMetaLoadPlanner.set_up_planner   s     	z8^D8:??002 	KC??6*"5cU ;$$(J$7$7#88WY 
 (= v||4"6*($	 !5HIr%   )NF)	r?   rk   rl   rm   r   r   rq   rx   __classcell__)r{   s   @r#   r   r      sJ    . %)$	J#J T/J 	J
 
J Jr%   r   dcp_checkpoint_dirtorch_save_pathc                 t    i }t        |t        |       t               d       t        j                  ||       y)aq  
    Given a directory containing a DCP checkpoint, this function will convert it into a
    Torch save file.

    Args:
        dcp_checkpoint_dir: Directory containing the DCP checkpoint.
        torch_save_path: Filename to store the converted Torch save file.

    .. warning::
        To avoid OOM, it's recommended to only run this function on a single rank.
    T)storage_readerr+   no_distN)r   r   r	   r7   save)r}   r~   sds      r#   r   r      s6     B
'(:;*,	 
JJr?#r%   c                 `    t        j                  | d      }t        |t        |      d       y)aB  
    Given the location of a torch save file, converts it into a DCP checkpoint.

    Args:
        torch_save_path: Filename of the Torch save file.
        dcp_checkpoint_dir: Directory to store the DCP checkpoint.

    .. warning::
        To avoid OOM, it's recommended to only run this function on a single rank.
    F)r/   T)storage_writerr   N)r7   r8   r   r   )r~   r}   rE   s      r#   r   r      s-     O%@J #34F#GQUr%   __main__c                       e Zd ZdZdZy)
FormatModetorch_to_dcpdcp_to_torchN)r?   rk   rl   TORCH_TO_DCPDCP_TO_TORCHr^   r%   r#   r   r     s    %%r%   r   modezConversion mode)r:   helpchoicesdefaultr3   zPath to the source model)r:   r   dstzPath to the destination modelzConverting checkpoint from z to z using method: ''zNo checkpoint found at z. Skipping conversion.zUnknown conversion mode: )Bargparserf   enumr   typingr   r7   torch.distributeddistributedr@   torch.distributed._shard._utilsr   torch.distributed.checkpointr   r   )torch.distributed.checkpoint._nested_dictr   ,torch.distributed.checkpoint.default_plannerr	   r
   %torch.distributed.checkpoint.metadatar   r   r   r   r   $torch.distributed.checkpoint.plannerr   r   r   ,torch.distributed.checkpoint.planner_helpersr   .torch.distributed.checkpoint.state_dict_loaderr   -torch.distributed.checkpoint.state_dict_saverr   $torch.distributed.checkpoint.storager   torch.futuresr   __all__r   r   rn   ro   r   r   r?   r   ArgumentParserparseradd_argumentvaluer   
parse_argsargsprintr3   r   r   checkpoint_missing_warningrg   rh   r   isdir
ValueError)ms   0r#   <module>r      sN    	      B K H  U T K K J >  x-- x-v+J/ +J\$bkk)$2;;&$22;;&bkk). z&T &
 %X$$&F
",-Q-''   C.HI
C.MND	
%dhhZtDHH:=Mdii[XYZ "$((+AB  yyJ++11177>>$((#dhh1,-	j--33	377=="dhh1,-4TYYK@AAI  .s   J