o
    @Tim                     @   s*   d dl Z d dl mZ G dd dejZdS )    N)nnc                	       sb   e Zd ZdZ		ddedededef fdd	Zd
ej	dej	de
ej	ej	f dB fddZ  ZS )	AdaLNZeroa  
    Adaptive Layer Normalization Zero (AdaLNZero) module.

    Combines LayerNorm with adaptive conditioning to produce shift, scale, and gate values.
    The gate is used to scale features before residual connection.

    Args:
        dim: Feature dimension
        condition_dim: Conditioning dimension
        eps: LayerNorm epsilon
        return_gate: If True, returns gate value for scaling.
    h㈵>Tdimcondition_dimepsreturn_gatec                    s   t    || _|| _|| _tj||dd| _|rd| nd| }tt	 t
||| _tj| jd j tj| jd j d S )NF)r   elementwise_affine         )super__init__r   r   r   r   	LayerNormnorm
SequentialSiLULinearcondition_projinitzeros_weightbias)selfr   r   r   r   
output_dim	__class__ O/home/ubuntu/.local/lib/python3.10/site-packages/linacodec/module/adaln_zero.pyr      s   

zAdaLNZero.__init__x	conditionreturnNc           	      C   s^   |  |}| |}| jr|jddd\}}}n|jddd\}}d}|d|  | }||fS )a>  
        Args:
            x: Input tensor of shape (B, L, dim)
            condition: Conditioning tensor of shape (B, L, condition_dim) or (B, 1, condition_dim)

        Returns:
            modulated_x: Normalized and modulated features
            gate: Gate values for scaling (None if return_gate=False)
        r
   )r   r   Nr   )r   r   r   chunk)	r   r   r    x_normcondition_paramsshiftscalegatemodulated_xr   r   r   forward0   s   


zAdaLNZero.forward)r   T)__name__
__module____qualname____doc__intfloatboolr   torchTensortupler*   __classcell__r   r   r   r   r      s    2r   )r2   r   Moduler   r   r   r   r   <module>   s   