
    Pi
                     l    d dl Z d dlmZ d dlmc mZ  G d dej                  Z G d d          ZdS )    Nc                   6    e Zd ZdZdej        dej        fdZdS )Lineara  
    nn.Module used in :func:`~torchtune.modules.tied_linear.TiedLinear`, added to work with the hooks
    :class:`~torchtune.training._activation_offloading.NoOpManager` that ignore activation
    offloading context manager.

    Without this class, we can't add NoOp hooks, and we will offload the activation of
    the tied linear layer, which is slow.

    For more information, see how NoOpManager is called in the recipes.
    xweightc                 ,    t          j        ||          S )N)Flinear)selfr   r   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/modules/tied_linear.pyforwardzLinear.forward   s    x6"""    N)__name__
__module____qualname____doc__torchTensorr    r   r   r   r      sD        	 	# #u| # # # # # #r   r   c                   L    e Zd ZdZdej        fdZdej        dej        fdZ	dS )
TiedLinearaz  
    A tied linear layer, without bias, that shares the same weight as another linear layer.
    This is useful for models that use tied weights, such as :func:`~torchtune.models.qwen2_0_5b`,
    :func:`~torchtune.models.qwen2_1_5b` and all of the :func:`~torchtune.models.gemma` and
    :func:`~torchtune.models.llama3_2` models.

    It requires as input an nn.Module, instead of the weight of the module, so it
    can work with FSDP. When FSDP is applied, the memory pointer to the weight is different,
    but the nn.Module remains the same. This is why we need to pass the nn.Module instead of
    the weight, if we want to keep the weights tied.

    Args:
        tied_module (nn.Module): The module whose weight is shared. Only
            the weight is used. The bias is ignored.
    Raises:
        AttributeError: If the provided module does not have an attribute 'weight'.
    tied_modulec                 x    || _         t                      | _        t          |d          st	          d          d S )Nr   zPProvided module does not have attribute 'weight'. Please check your tied_module.)r   r   r	   hasattrAttributeError)r
   r   s     r   __init__zTiedLinear.__init__/   sF    &hh{H-- 	 b  	 	r   r   returnc                 B    |                      || j        j                  S )aa  
        Args:
            x (torch.Tensor): Input tensor. Should have shape ``(..., in_dim)``, where ``in_dim``
                is the input dimension of the tied module.
        Returns:
            torch.Tensor: The output tensor, having shape ``(..., out_dim)``, where ``out_dim`` is                 the output dimension of the tied module.
        )r	   r   r   )r
   r   s     r   __call__zTiedLinear.__call__7   s     {{1d.5666r   N)
r   r   r   r   nnModuler   r   r   r   r   r   r   r   r      sa         $BI    	7%, 	75< 	7 	7 	7 	7 	7 	7r   r   )	r   torch.nnr   torch.nn.functional
functionalr   r    r   r   r   r   r   <module>r$      s                   # # # # #RY # # # $7 $7 $7 $7 $7 $7 $7 $7 $7 $7r   