summaryrefslogtreecommitdiff
path: root/text_recognizer/network/transformer/encoder.py
blob: ce30372a9b0d4c5f5e957af0464798ba29f8fbcc (plain)
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
"""Transformer encoder module."""
from torch import Tensor, nn

from .attention import Attention


class Encoder(nn.Module):
    def __init__(
        self,
        dim: int,
        heads: int,
        dim_head: int,
        ff_mult: int,
        depth: int,
        dropout_rate: float = 0.0,
        use_rotary_emb: bool = False,
        one_kv_head: bool = False,
    ) -> None:
        super().__init__()
        self.norm = nn.LayerNorm(dim)
        self.layers = nn.ModuleList(
            [
                Attention(
                    dim=dim,
                    heads=heads,
                    causal=False,
                    dim_head=dim_head,
                    ff_mult=ff_mult,
                    dropout_rate=dropout_rate,
                    use_flash=True,
                    norm_context=False,
                    use_rotary_emb=use_rotary_emb,
                    one_kv_head=one_kv_head,
                )
                for _ in range(depth)
            ]
        )

    def forward(
        self,
        x: Tensor,
    ) -> Tensor:
        """Applies decoder block on input signals."""
        for self_attn in self.layers:
            x = x + self_attn(x)
        return self.norm(x)