U
    <ºc¨  ã                   @   s6   d dl Z d dlmZ d dlmZ G dd„ dejƒZdS )é    N)ÚShardedTensorc                       sL   e Zd Zdejf‡ fdd„	Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
‡  ZS )ÚSimpleMegatronLMNc                    sd   t ƒ  ¡  tj|d d|iŽ| _t ¡ | _tj|d d|iŽ| _|d k	r`| j |¡ | j |¡ d S )Nr   Údtypeé   )	ÚsuperÚ__init__ÚnnZLinearÚfc1ZGELUÚgeluÚfc2Zcuda)ÚselfZlinear_sizeZrankr   ©Ú	__class__© úZ/tmp/pip-unpacked-wheel-gikjz4vx/torch/testing/_internal/distributed/_shard/test_common.pyr      s    

zSimpleMegatronLM.__init__c                 C   s   |   |  |  |¡¡¡S ©N)r   r
   r	   )r   Úinpr   r   r   Úforward   s    zSimpleMegatronLM.forwardc                 C   sP   t | jjtƒr| jj ¡ }n| jj}t | jjtƒr@| jj ¡ }n| jj}||fS r   )Ú
isinstancer	   Úweightr   Zlocal_tensorr   )r   Zweight1Zweight2r   r   r   Úget_weights   s    zSimpleMegatronLM.get_weightsc                 C   s   | j j| jjfS r   )r	   Úbiasr   ©r   r   r   r   Ú
get_biases!   s    zSimpleMegatronLM.get_biasesc                 C   s   | j jj| jjjfS r   )r	   r   Úgradr   r   r   r   r   Úget_weight_grads$   s    z!SimpleMegatronLM.get_weight_gradsc                 C   s   | j jj| jjjfS r   )r	   r   r   r   r   r   r   r   Úget_bias_grads'   s    zSimpleMegatronLM.get_bias_grads)Ú__name__Ú
__module__Ú__qualname__ÚtorchZfloat32r   r   r   r   r   r   Ú__classcell__r   r   r   r   r      s   	r   )r    Ztorch.nnr   Z'torch.distributed._shard.sharded_tensorr   ÚModuler   r   r   r   r   Ú<module>   s   