o
    i                     @   sp   d dl 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
mZ G dd de jjZG dd	 d	e jjZdS )
    N)DictOptionalTuple)	LayerNorm)FeedForward)EncoderSelfAttentionDecoderSelfAttentionc                          e Zd ZdZ			ddedededededed	ed
ededdf fddZ	ddejde	ej de
eje	ej f fddZ  ZS )RWKV)  RWKV module.

    Args:
        size: Input/Output size.
        linear_size: Feed-forward hidden size.
        attention_size: SelfAttention hidden size.
        context_size: Context size for WKV computation.
        block_id: Block index.
        num_blocks: Number of blocks in the architecture.
        normalization_class: Normalization layer class.
        normalization_args: Normalization layer arguments.
        att_dropout_rate: Dropout rate for the attention module.
        ffn_dropout_rate: Dropout rate for the feed-forward module.

            sizelinear_sizeattention_sizecontext_sizeblock_id
num_blocksatt_dropout_rateffn_dropout_ratedropout_ratereturnNc
           
         h   t    t|| _t|| _t||||||| _tjj	|	d| _
t|||||| _tjj	|	d| _dS zConstruct a RWKV object.)pN)super__init__r   layer_norm_attlayer_norm_ffnr   atttorchnnDropoutdropout_attr   ffndropout_ffn
selfr   r   r   r   r   r   r   r   r   	__class__ O/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/rwkv_bat/rwkv.pyr         


zRWKV.__init__xstatec                 C   T   | j | ||d\}}|| | }| j| ||d\}}|| | }||fS a8  Compute receptance weighted key value.

        Args:
            x: RWKV input sequences. (B, L, size)
            state: Decoder hidden states. [5 x (B, D_att/size, N)]

        Returns:
            x: RWKV output sequences. (B, L, size)
            x: Decoder hidden states. [5 x (B, D_att/size, N)]

        )r-   r   r   r"   r#   r   r$   r&   r,   r-   r   r#   r)   r)   r*   forward9   s
   zRWKV.forwardr   r   r   N__name__
__module____qualname____doc__intfloatr   r   Tensorr   r   r2   __classcell__r)   r)   r'   r*   r
      D    	
r
   c                       r	   )RWKVDecoderLayerr   r   r   r   r   r   r   r   r   r   r   r   Nc
           
         r   r   )r   r   r   r   r   r   r   r   r    r!   r"   r   r#   r$   r%   r'   r)   r*   r   a   r+   zRWKVDecoderLayer.__init__r,   r-   c                 C   r.   r/   r0   r1   r)   r)   r*   r2   {   s
   zRWKVDecoderLayer.forwardr3   r4   r5   r)   r)   r'   r*   r?   P   r>   r?   )r   typingr   r   r   $funasr.models.transformer.layer_normr   (funasr.models.rwkv_bat.rwkv_feed_forwardr   %funasr.models.rwkv_bat.rwkv_attentionr   r   r    Moduler
   r?   r)   r)   r)   r*   <module>   s   B