MCPcopy Create free account
hub / github.com/FunAudioLLM/FunMusic / DecoderLayer

Class DecoderLayer

inspiremusic/transformer/decoder_layer.py:22–132  ·  view source on GitHub ↗

Single decoder layer module. Args: size (int): Input dimension. self_attn (torch.nn.Module): Self-attention module instance. `MultiHeadedAttention` instance can be used as the argument. src_attn (torch.nn.Module): Inter-attention module instance.

Source from the content-addressed store, hash-verified

20
21
22class DecoderLayer(nn.Module):
23 """Single decoder layer module.
24
25 Args:
26 size (int): Input dimension.
27 self_attn (torch.nn.Module): Self-attention module instance.
28 `MultiHeadedAttention` instance can be used as the argument.
29 src_attn (torch.nn.Module): Inter-attention module instance.
30 `MultiHeadedAttention` instance can be used as the argument.
31 If `None` is passed, Inter-attention is not used, such as
32 CIF, GPT, and other decoder only model.
33 feed_forward (torch.nn.Module): Feed-forward module instance.
34 `PositionwiseFeedForward` instance can be used as the argument.
35 dropout_rate (float): Dropout rate.
36 normalize_before (bool):
37 True: use layer_norm before each sub-block.
38 False: to use layer_norm after each sub-block.
39 """
40
41 def __init__(
42 self,
43 size: int,
44 self_attn: nn.Module,
45 src_attn: Optional[nn.Module],
46 feed_forward: nn.Module,
47 dropout_rate: float,
48 normalize_before: bool = True,
49 ):
50 """Construct an DecoderLayer object."""
51 super().__init__()
52 self.size = size
53 self.self_attn = self_attn
54 self.src_attn = src_attn
55 self.feed_forward = feed_forward
56 self.norm1 = nn.LayerNorm(size, eps=1e-5)
57 self.norm2 = nn.LayerNorm(size, eps=1e-5)
58 self.norm3 = nn.LayerNorm(size, eps=1e-5)
59 self.dropout = nn.Dropout(dropout_rate)
60 self.normalize_before = normalize_before
61
62 def forward(
63 self,
64 tgt: torch.Tensor,
65 tgt_mask: torch.Tensor,
66 memory: torch.Tensor,
67 memory_mask: torch.Tensor,
68 cache: Optional[torch.Tensor] = None
69 ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
70 """Compute decoded features.
71
72 Args:
73 tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
74 tgt_mask (torch.Tensor): Mask for input tensor
75 (#batch, maxlen_out).
76 memory (torch.Tensor): Encoded memory
77 (#batch, maxlen_in, size).
78 memory_mask (torch.Tensor): Encoded memory mask
79 (#batch, maxlen_in).

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected