Encoder
This document describes the encoder components used in the DMS model.
Base Encoder Layer
The basic building block of the encoder that processes input sequences.
Components
Self-Attention Block
- Multi-head self-attention mechanism
- Layer normalization
- Residual connection
Feed-Forward Block
- Two-layer feed-forward network
- Configurable activation function (ReLU or GELU)
- Layer normalization
- Residual connection
Source Code
directmultistep.model.components.encoder
Encoder
Bases: Module
The encoder module.
Shape suffixes convention
B: batch size C: the length of the input on which conditioning is done (in our case input_max_length) D: model dimension (sometimes called d_model or embedding_dim)
Source code in src/directmultistep/model/components/encoder.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
__init__(vocab_dim, hid_dim, context_window, n_layers, n_heads, ff_mult, ff_activation, dropout, attn_bias, initiate_steps, include_steps)
Initializes the Encoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab_dim
|
int
|
The vocabulary dimension size. |
required |
hid_dim
|
int
|
The hidden dimension size. |
required |
context_window
|
int
|
The context window size. |
required |
n_layers
|
int
|
The number of encoder layers. |
required |
n_heads
|
int
|
The number of attention heads. |
required |
ff_mult
|
int
|
The feed-forward expansion factor. |
required |
ff_activation
|
str
|
The activation function type. |
required |
dropout
|
float
|
The dropout rate. |
required |
attn_bias
|
bool
|
Whether to use bias in the attention layers. |
required |
initiate_steps
|
bool
|
Whether to initiate step embeddings. |
required |
include_steps
|
bool
|
Whether to include step embeddings. |
required |
Source code in src/directmultistep/model/components/encoder.py
forward(src_BC, src_mask_B11C, steps_B1)
Forward pass of the Encoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src_BC
|
Tensor
|
The source input tensor of shape (B, C). |
required |
src_mask_B11C
|
Tensor
|
The source mask tensor of shape (B, 1, 1, C). |
required |
steps_B1
|
Tensor
|
The step tensor of shape (B, 1). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output tensor of shape (B, C, D). |
Source code in src/directmultistep/model/components/encoder.py
EncoderLayer
Bases: Module
A single layer of the encoder.
Shape suffixes convention
B: batch size C: the length of the input on which conditioning is done (in our case input_max_length) D: model dimension (sometimes called d_model or embedding_dim)
Source code in src/directmultistep/model/components/encoder.py
__init__(hid_dim, n_heads, ff_mult, ff_activation, dropout, attn_bias)
Initializes the EncoderLayer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hid_dim
|
int
|
The hidden dimension size. |
required |
n_heads
|
int
|
The number of attention heads. |
required |
ff_mult
|
int
|
The feed-forward expansion factor. |
required |
ff_activation
|
str
|
The activation function type. |
required |
dropout
|
float
|
The dropout rate. |
required |
attn_bias
|
bool
|
Whether to use bias in the attention layers. |
required |
Source code in src/directmultistep/model/components/encoder.py
forward(input_BCD, src_mask_B11C)
Forward pass of the EncoderLayer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_BCD
|
Tensor
|
The input tensor of shape (B, C, D). |
required |
src_mask_B11C
|
Tensor
|
The source mask tensor of shape (B, 1, 1, C). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output tensor of shape (B, C, D). |
Source code in src/directmultistep/model/components/encoder.py
MoEEncoder
Bases: Module
The MoE encoder module.
Shape suffixes convention
B: batch size C: the length of the input on which conditioning is done (in our case input_max_length) D: model dimension (sometimes called d_model or embedding_dim)
Source code in src/directmultistep/model/components/encoder.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
__init__(vocab_dim, hid_dim, n_layers, n_heads, n_experts, top_k, ff_mult, ff_activation, dropout, attn_bias, context_window, initiate_steps, include_steps, capacity_factor)
Initializes the MoEEncoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab_dim
|
int
|
The vocabulary dimension size. |
required |
hid_dim
|
int
|
The hidden dimension size. |
required |
n_layers
|
int
|
The number of encoder layers. |
required |
n_heads
|
int
|
The number of attention heads. |
required |
n_experts
|
int
|
The number of experts in the MoE layer. |
required |
top_k
|
int
|
The number of experts to use in the MoE layer. |
required |
ff_mult
|
int
|
The feed-forward expansion factor. |
required |
ff_activation
|
str
|
The activation function type. |
required |
dropout
|
float
|
The dropout rate. |
required |
attn_bias
|
bool
|
Whether to use bias in the attention layers. |
required |
context_window
|
int
|
The context window size. |
required |
initiate_steps
|
bool
|
Whether to initiate step embeddings. |
required |
include_steps
|
bool
|
Whether to include step embeddings. |
required |
capacity_factor
|
float
|
The capacity factor for the MoE layer. |
required |
Source code in src/directmultistep/model/components/encoder.py
forward(src_BC, src_mask_B11C, steps_B1)
Forward pass of the MoEEncoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src_BC
|
Tensor
|
The source input tensor of shape (B, C). |
required |
src_mask_B11C
|
Tensor
|
The source mask tensor of shape (B, 1, 1, C). |
required |
steps_B1
|
Tensor
|
The step tensor of shape (B, 1). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output tensor of shape (B, C, D). |
Source code in src/directmultistep/model/components/encoder.py
MoEEncoderLayer
Bases: Module
A single layer of the MoE encoder.
Shape suffixes convention
B: batch size C: the length of the input on which conditioning is done (in our case input_max_length) D: model dimension (sometimes called d_model or embedding_dim)
Source code in src/directmultistep/model/components/encoder.py
__init__(hid_dim, n_heads, n_experts, top_k, ff_mult, ff_activation, dropout, attn_bias, capacity_factor)
Initializes the MoEEncoderLayer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hid_dim
|
int
|
The hidden dimension size. |
required |
n_heads
|
int
|
The number of attention heads. |
required |
n_experts
|
int
|
The number of experts in the MoE layer. |
required |
top_k
|
int
|
The number of experts to use in the MoE layer. |
required |
ff_mult
|
int
|
The feed-forward expansion factor. |
required |
ff_activation
|
str
|
The activation function type. |
required |
dropout
|
float
|
The dropout rate. |
required |
attn_bias
|
bool
|
Whether to use bias in the attention layers. |
required |
capacity_factor
|
float
|
The capacity factor for the MoE layer. |
required |
Source code in src/directmultistep/model/components/encoder.py
forward(input_BCD, src_mask_B11C)
Forward pass of the MoEEncoderLayer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_BCD
|
Tensor
|
The input tensor of shape (B, C, D). |
required |
src_mask_B11C
|
Tensor
|
The source mask tensor of shape (B, 1, 1, C). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output tensor of shape (B, C, D). |