File size: 6,671 Bytes
34bd885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import torch \n",
    "import torch.nn as nn\n",
    "\n",
    "class InputEmbeddingsLayer(nn.Module):\n",
    "    def __init__(self, d_model: int, vocab_size: int) -> None:\n",
    "        super().__init__()\n",
    "        self.d_model = d_model\n",
    "        self.vocab_size = vocab_size\n",
    "        self.embedding = nn.Embedding(vocab_size, d_model)\n",
    "    def forward(self, x):\n",
    "        return self.embedding(x) * math.sqrt(self.d_model)\n",
    "\n",
    "class PositionalEncodingLayer(nn.Module):\n",
    "    def __init__(self, d_model: int, sequence_length: int, dropout: float) -> None:\n",
    "        super().__init__()\n",
    "        self.d_model = d_model\n",
    "        self.sequence_length = sequence_length\n",
    "        self.dropout = nn.Dropout(dropout)\n",
    "\n",
    "        PE = torch.zeros(sequence_length, d_model)\n",
    "        Position = torch.arange(0, sequence_length, dtype=torch.float).unsqueeze(1)\n",
    "        deviation_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))\n",
    "        \n",
    "        PE[:, 0::2] = torch.sin(Position * deviation_term)\n",
    "        PE[:, 1::2] = torch.cos(Position * deviation_term)\n",
    "        PE = PE.unsqueeze(0)\n",
    "        self.register_buffer(\"PE\", PE)\n",
    "    def forward(self, x):\n",
    "        x = x + (self.PE[:, :x.shape[1], :]).requires_grad_(False)\n",
    "        return self.dropout(x)\n",
    "\n",
    "class NormalizationLayer(nn.Module):\n",
    "    def __init__(self, Epslone: float = 10**-6) -> None:\n",
    "        super().__init__()\n",
    "        self.Epslone = Epslone\n",
    "        self.Alpha = nn.Parameter(torch.ones(1))\n",
    "        self.Bias = nn.Parameter(torch.ones(1))\n",
    "    def forward(self, x):\n",
    "        mean = x.mean(dim = -1, keepdim = True)\n",
    "        std = x.std(dim = -1, keepdim = True)\n",
    "        return self.Alpha * (x - mean) / (std + self.Epslone) + self.Bias\n",
    "\n",
    "class FeedForwardBlock(nn.Module):\n",
    "    def __init__(self, d_model: int, d_ff: int, dropout: float) -> None:\n",
    "        super().__init__()\n",
    "        self.Linear_1 = nn.Linear(d_model, d_ff)\n",
    "        self.dropout = nn.Dropout(dropout)\n",
    "        self.Linear_2 = nn.Linear(d_ff, d_model)\n",
    "    def forward(self, x):\n",
    "        return self.Linear_2(self.dropout(torch.relu(self.Linear_1(x))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MultiHeadAttentionBlock(nn.Module):\n",
    "    def __init__(self, d_model: int, heads: int, dropout: float) -> None:\n",
    "        super().__init__()\n",
    "        self.d_model = d_model\n",
    "        self.heads = heads \n",
    "        assert d_model % heads == 0, \"d_model is not divisable by heads\"\n",
    "\n",
    "        self.d_k = d_model // heads\n",
    "\n",
    "        self.W_Q = nn.Linear(d_model, d_model)\n",
    "        self.W_K = nn.Linear(d_model, d_model)\n",
    "        self.W_V = nn.Linear(d_model, d_model)\n",
    "\n",
    "        self.W_O = nn.Linear(d_model, d_model)\n",
    "        self.dropout = nn.Dropout(dropout)\n",
    "    \n",
    "    @staticmethod\n",
    "    def Attention(Query, Key, Value, mask, dropout: nn.Module):\n",
    "        d_k = Query.shape[-1]\n",
    "\n",
    "        self_attention_score = (Query @ Key.transpose(-2,-1)) / math.sqrt(d_k)\n",
    "        if mask is not None:\n",
    "            self_attention_score.masked_fill_(mask == 0, -1e9)\n",
    "        self_attention_score = self_attention_score.softmax(dim = -1)\n",
    "\n",
    "        if dropout is not None:\n",
    "            self_attention_score = dropout(self_attention_score)\n",
    "        return self_attention_score @ Value\n",
    "    def forward(self, query, key, value, mask):\n",
    "        Query = self.W_Q(query)\n",
    "        Key = self.W_K(key)\n",
    "        Value = self.W_V(value)\n",
    "\n",
    "        Query = Query.view(Query.shape[0], Query.shape[1], self.heads, self.d_k).transpose(1,2)\n",
    "        Key = Key.view(Key.shape[0], Key.shape[1], self.heads, self.d_k).transpose(1,2)\n",
    "        Value = Value.view(Value.shape[0], Value.shape[1], self.heads, self.d_k).transpose(1,2)\n",
    "\n",
    "        x, self.self_attention_score = MultiHeadAttentionBlock.Attention(Query, Key, Value, mask, self.dropout)\n",
    "        x = x.transpose(1,2).contiguous().view(x.shape[0], -1, self.heads * self.d_k)\n",
    "        return self.W_O(x)\n",
    "\n",
    "class ResidualConnection(nn.Module):\n",
    "    def __init__(self, dropout: float) -> None:\n",
    "        super().__init__()\n",
    "        self.dropout = nn.Dropout(dropout)\n",
    "        self.normalization = NormalizationLayer()\n",
    "    def forward(self, x, subLayer):\n",
    "        return x + self.dropout(subLayer(self.normalization(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Building the encoder block \n",
    "class EncoderBlock(nn.Module):\n",
    "    def __init__(self, encoder_self_attention_block: MultiHeadAttentionBlock, encoder_feed_forward_block: FeedForwardBlock, dropout: float) -> None:\n",
    "        super().__init__()\n",
    "        self.encoder_self_attention_block = encoder_self_attention_block\n",
    "        self.encoder_feed_forward_block = encoder_feed_forward_block\n",
    "        self.residual_connection = nn.ModuleList([ResidualConnection(dropout) for _ in range(2)])\n",
    "    def forward(self, x, source_mask):\n",
    "        x = self.residual_connection[0](x, lambda x: self.encoder_self_attention_block(x, x, x, source_mask))\n",
    "        x = self.residual_connection[1](x, self.encoder_feed_forward_block)\n",
    "        return x\n",
    "\n",
    "class Encoder(nn.Module):\n",
    "    def __init__(self, Layers: nn.ModuleList) -> None:\n",
    "        super().__init__()\n",
    "        self.Layers = Layers\n",
    "        self.normalization = NormalizationLayer()\n",
    "    def forward(self, x, source_mask):\n",
    "        for layer in self.Layers:\n",
    "            x = layer(x, source_mask)\n",
    "        return self.normalization(x)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}