o
    eih$                     @   s   d dl Z d dlmZ d dl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 dZG d	d
 d
e
ZdededefddZG dd dZeed	ddejdeejj dB dejfddZdejdefddZdS )    N)Iterable)AnyNoReturn)_State)DistributedDataParallel   )_get_registrycontract c                       s   e Zd ZU ejed< d fddZefdej	de
ej	 de
ej d	eddf
d
dZdddZdej	de
ej	 ddfddZdddZdddZdej	deedf deeef defddZdej	deej dejdejfddZ  ZS )_ReplicateState_ddp_weakrefreturnNc                    sN   t    t | _d| _t | _| j| _g | _d| _	d | _
i | _g | _d S )NF)super__init__nnParameterListmodulehas_initialized_param_list_orig_module_param_names_no_sync
_init_args_init_kwargs_comm_hook_argsself	__class__ q/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/distributed/_composable/replicate.pyr      s   



z_ReplicateState.__init__r   ignored_modulesignored_paramsprefixc           
      C   s   t |rd S ||v rd S |tkr| dnt}|jddD ]\}}||vr5| j| | j| |  q| D ]\}}	| j|	||| | d q:d S )N.F)recurse)r#   )_is_fully_sharded_ROOT_MODULE_PREFIXnamed_parametersr   appendr   named_children_collect_params)
r   r   r!   r"   r#   recurse_prefixnpnamechild_moduler   r   r    r+   "   s&   
z_ReplicateState._collect_paramsc                    s"   t jdd fdd}|  d S )NT)	recursivec                      s:    j d usJ  j j i  j    d _ i  _d S )Nr   )r   initr   register_comm_hookr   r   r   r    
_lazy_initC   s
   
z-_ReplicateState.lazy_init.<locals>._lazy_init)torch_disable_dynamo)r   r4   r   r   r    	lazy_initB   s   

z_ReplicateState.lazy_initc                 K   s   | j rd S d| _ || _dd |D }| D ]}t|r#||  qddlm} |||d | ||| d|v rd|d d ur[|d }t	|t
jrU|jdkrUd |d	< n
|g|d	< nd |d	< |d t| jfi || _t| jt| j_d S )
NTc                 S   s   h | ]}|  D ]}|qqS r   )
parameters).0mr.   r   r   r    	<setcomp>X   s    z'_ReplicateState.init.<locals>.<setcomp>r   )_localize_dtensor)r"   	device_idcpu
device_ids)r   r   modulesr&   updater8   %torch.distributed.tensor.parallel.ddpr<   r+   
isinstancer5   devicetypepopr   r   _ddpweakrefref	replicatestater   )r   r   r!   kwargsr"   	submoduler<   r=   r   r   r    r2   M   s,   

z_ReplicateState.initc                 C   s0   | j D ]\}}| jj|i | q| j   d S N)r   rG   r3   clear)r   	comm_argscomm_kwargsr   r   r    r3   w   s   z"_ReplicateState.register_comm_hookc                 O   s   || _ || _d S rN   )r   r   r   argsrL   r   r   r    record_init_args|   s   
z _ReplicateState.record_init_argsrS   .rL   c                 C   s:   | j s| jr
|   | j | j_| jt_| jj|i |S rN   )	r   r   r7   r   rG   require_backward_grad_syncr   _active_ddp_module_pre_forward)r   r   rS   rL   r   r   r    forward_pre_hook   s
   z _ReplicateState.forward_pre_hookinputoutputc                 C   s   d t _| j|S rN   )r   rV   rG   _post_forward)r   r   rY   rZ   r   r   r    forward_post_hook   s   z!_ReplicateState.forward_post_hookr   N)__name__
__module____qualname__rH   rI   __annotations__r   r'   r   Moduleset	Parameterstrr+   r7   r2   r3   rT   tupler   dictrX   r5   Tensorr\   __classcell__r   r   r   r    r      sV   
 


 

*



	r   rS   rL   r   c                  O   s   t d)NzGDDP does not support deepcopy. Please use state dict for serialization.)AssertionError)rS   rL   r   r   r    unimplemented_deepcopy   s   rk   c                   @   s0   e Zd Zdd ZdeddfddZd
dd	ZdS )DDPc                 O   s"   | j d }|j|g|R i |S )z
        Override ``__new__`` to remove the DDP class and directly construct
        the original class for cases like indexing into a container module.
           )__mro____new__)clsrS   rL   orig_clsr   r   r    ro      s   
zDDP.__new__requires_gradient_syncr   Nc                 C   s   | t | _dS )a  
        Sets if the module should sync gradients. This can be used to implement
        gradient accumulation without communication.

        Args:
            requires_gradient_sync (bool): Whether to reduce gradients for the
                module's parameters.
        N)rJ   rK   r   )r   rr   r   r   r    set_requires_gradient_sync   s   	zDDP.set_requires_gradient_syncc                 O   s   t | j||f d S rN   )rJ   rK   r   r)   rR   r   r   r    r3      s   zDDP.register_comm_hookr]   )r^   r_   r`   ro   boolrs   r3   r   r   r   r    rl      s    
rl   )	state_clsr   r!   c                 K   s  t jd d|v rt|d tt jfstdt|d  t| r'td|du r.i }nt	|}t
| }| j|jdd |d}|durb| }||krbd	d
lm}m} | | | | | |j |j| |fi | | j}dti}	td|j t|f|	}
|
| _| S )zReplicates a module

    Args:
        module (torch.nn.Module): module to replicate

    Example::
        >>> # xdoctest: +REQUIRES(module:torch._C._distributed_c10d)
        >>> module = nn.Linear(3, 3)
        >>> replicate(module)
    ztorch.distributed.replicater=   z6Expected device_id to be int or torch.device, but got zGCannot apply `replicate()` on a Module already managed by `fully_shard`NT)with_kwargsdevice_meshr   )r<   _reconstruct_dtensor__deepcopy__rl   )r5   _C_log_api_usage_oncerC   intrD   RuntimeErrorrE   r&   rc   rJ   rK   register_forward_pre_hookrX   get_get_root_meshrB   r<   rx   register_forward_hookr\   rT   r   rk   r^   rl   )r   r!   rL   rK   rw   	root_meshr<   rx   rp   dctnew_clsr   r   r    rJ      s>   




rJ   c                 C   s   t | }|du r
dS d|v S )z+Check if module is marked with fully_shard.NFfully_shard)r   )r   registryr   r   r    r&      s   r&   rN   )rH   collections.abcr   typingr   r   r5   torch.nnr   #torch.distributed._composable_stater   torch.nn.parallelr   r	   r   r'   r   rk   rl   rb   rJ   rt   r&   r   r   r   r    <module>   s.    F