Spaces:
Runtime error
Runtime error
File size: 4,822 Bytes
0102e16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
# MIT License (https://opensource.org/licenses/MIT)
import torch
from typing import Dict, Optional, Tuple
from funasr_detach.models.transformer.layer_norm import LayerNorm
from funasr_detach.models.rwkv_bat.rwkv_feed_forward import FeedForward
from funasr_detach.models.rwkv_bat.rwkv_attention import (
EncoderSelfAttention,
DecoderSelfAttention,
)
class RWKV(torch.nn.Module):
"""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.
"""
def __init__(
self,
size: int,
linear_size: int,
attention_size: int,
context_size: int,
block_id: int,
num_blocks: int,
att_dropout_rate: float = 0.0,
ffn_dropout_rate: float = 0.0,
dropout_rate: float = 0.0,
) -> None:
"""Construct a RWKV object."""
super().__init__()
self.layer_norm_att = LayerNorm(size)
self.layer_norm_ffn = LayerNorm(size)
self.att = EncoderSelfAttention(
size, attention_size, context_size, block_id, att_dropout_rate, num_blocks
)
self.dropout_att = torch.nn.Dropout(p=dropout_rate)
self.ffn = FeedForward(
size, linear_size, block_id, ffn_dropout_rate, num_blocks
)
self.dropout_ffn = torch.nn.Dropout(p=dropout_rate)
def forward(
self,
x: torch.Tensor,
state: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
"""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)]
"""
att, state = self.att(self.layer_norm_att(x), state=state)
x = x + self.dropout_att(att)
ffn, state = self.ffn(self.layer_norm_ffn(x), state=state)
x = x + self.dropout_ffn(ffn)
return x, state
class RWKVDecoderLayer(torch.nn.Module):
"""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.
"""
def __init__(
self,
size: int,
linear_size: int,
attention_size: int,
context_size: int,
block_id: int,
num_blocks: int,
att_dropout_rate: float = 0.0,
ffn_dropout_rate: float = 0.0,
dropout_rate: float = 0.0,
) -> None:
"""Construct a RWKV object."""
super().__init__()
self.layer_norm_att = LayerNorm(size)
self.layer_norm_ffn = LayerNorm(size)
self.att = DecoderSelfAttention(
size, attention_size, context_size, block_id, att_dropout_rate, num_blocks
)
self.dropout_att = torch.nn.Dropout(p=dropout_rate)
self.ffn = FeedForward(
size, linear_size, block_id, ffn_dropout_rate, num_blocks
)
self.dropout_ffn = torch.nn.Dropout(p=dropout_rate)
def forward(
self,
x: torch.Tensor,
state: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
"""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)]
"""
att, state = self.att(self.layer_norm_att(x), state=state)
x = x + self.dropout_att(att)
ffn, state = self.ffn(self.layer_norm_ffn(x), state=state)
x = x + self.dropout_ffn(ffn)
return x, state
|