gpt2
optimus_dl.modules.model.gpt2
¶
Full definition of a GPT Language Model, all of it in this single file. References: 1) the official GPT-2 TensorFlow implementation released by OpenAI: https://github.com/openai/gpt-2/blob/master/src/model.py 2) huggingface/transformers PyTorch implementation: https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py
Block
¶
Bases: Module
A single Transformer block with self-attention and MLP.
Source code in optimus_dl/modules/model/gpt2.py
GPT
¶
Bases: BaseModel
GPT Language Model architecture.
Implements a decoder-only transformer with causal self-attention, absolute position embeddings, and standard GPT-2 layer ordering (LayerNorm before attention/MLP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
GPT model configuration. |
required |
Source code in optimus_dl/modules/model/gpt2.py
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 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 243 244 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 | |
forward(input_ids, **kwargs)
¶
Compute model output for the given input tokens.
Source code in optimus_dl/modules/model/gpt2.py
fully_shard(**fsdp_kwargs)
¶
Apply FSDP sharding to each transformer block.
Source code in optimus_dl/modules/model/gpt2.py
generate(idx, max_new_tokens, temperature=1.0, top_k=None)
¶
Autoregressive generation of new tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
Starting token sequence (LongTensor). |
required | |
max_new_tokens
|
Number of tokens to generate. |
required | |
temperature
|
Sampling temperature. |
1.0
|
|
top_k
|
Optional top-k sampling threshold. |
None
|
Returns:
| Type | Description |
|---|---|
|
LongTensor containing original and generated tokens. |
Source code in optimus_dl/modules/model/gpt2.py
make_parameter_groups()
¶
Divide parameters into decayed and non-decayed groups.
Excludes biases and 1D parameters (normalization weights, embeddings) from weight decay. Handles weight tying correctly.
Returns:
| Type | Description |
|---|---|
|
List of dictionaries for PyTorch optimizer. |
Source code in optimus_dl/modules/model/gpt2.py
GPTConfig
dataclass
¶
Bases: RegistryConfigStrict
Configuration for GPT-style language models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_size
|
int
|
Maximum context length. Determines max pos embeddings |
1024
|
vocab_size
|
int
|
Vocabulary size |
50304
|
n_layer
|
int
|
Number of transformer blocks |
12
|
n_head
|
int
|
Number of attention heads |
12
|
n_embd
|
int
|
Embedding dimensionality |
768
|
head_dim
|
int | None
|
Head dimension. If None, will be set to n_embd // n_head |
None
|
dropout
|
float
|
Dropout probability |
0.0
|
bias
|
bool
|
Whether to use bias in linear layers and norms |
True
|
tie_word_embeddings
|
bool
|
Share weights between token embeddings and LM head |
True
|
shard_every_ith_layer
|
int
|
Control FSDP sharding granularity. Shard every i-th layer, 1 means all layers are sharded (if global reshard_after_forward is True) |
1
|
padding_token_id
|
int | None
|
Padding token id for the model embeddings |
None
|
Source code in optimus_dl/modules/model/gpt2.py
MLP
¶
Bases: Module
Standard GPT-2 MLP with GELU activation.