Lightning basic
This is intended to be a minimal self-container NeMo2 example.
ClassifierLossReduction
Bases: MegatronLossReduction
A class used for calculating the loss, and for logging the reduced loss across micro batches.
Source code in bionemo/example_model/lightning_basic.py
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 |
|
forward(batch, forward_out)
Calculates the loss within a micro-batch. A micro-batch is a batch of data on a single GPU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
MnistItem
|
A batch of data that gets passed to the original forward inside LitAutoEncoder. |
required |
forward_out
|
Tensor
|
the output of the forward method inside LitAutoEncoder. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
A tuple containing [ |
Source code in bionemo/example_model/lightning_basic.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
reduce(losses_reduced_per_micro_batch)
Works across micro-batches. (data on single gpu).
Note: This currently only works for logging and this loss will not be used for backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
a list of the outputs of forward |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor that is the mean of the losses. (used for logging). |
Source code in bionemo/example_model/lightning_basic.py
186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
ExampleConfig
dataclass
Bases: ExampleGenericConfig['ExampleModel', 'MSELossReduction']
, IOMixinWithGettersSetters
ExampleConfig is a dataclass that is used to configure the model.
Timers from ModelParallelConfig are required for megatron forward compatibility.
Source code in bionemo/example_model/lightning_basic.py
350 351 352 353 354 355 356 357 358 |
|
ExampleFineTuneBothConfig
dataclass
Bases: ExampleGenericConfig['ExampleFineTuneBothModel', 'MSEPlusClassifierLossReduction']
, IOMixinWithGettersSetters
ExampleConfig is a dataclass that is used to configure the model.
Timers from ModelParallelConfig are required for megatron forward compatibility.
Source code in bionemo/example_model/lightning_basic.py
361 362 363 364 365 366 367 368 369 370 371 |
|
ExampleFineTuneBothModel
Bases: ExampleModel
Example of taking the example model and adding an output task.
Source code in bionemo/example_model/lightning_basic.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
ExampleFineTuneDropParentConfig
dataclass
Bases: ExampleGenericConfig['ExampleFineTuneDropParentModel', 'ClassifierLossReduction']
, IOMixinWithGettersSetters
ExampleConfig is a dataclass that is used to configure the model.
Timers from ModelParallelConfig are required for megatron forward compatibility.
Source code in bionemo/example_model/lightning_basic.py
374 375 376 377 378 379 380 381 382 383 384 |
|
ExampleFineTuneDropParentModel
Bases: ExampleModelTrunk
Example of taking the example model and replacing output task.
Source code in bionemo/example_model/lightning_basic.py
283 284 285 286 287 288 289 290 291 292 293 294 |
|
ExampleFineTuneOutput
Bases: ExampleModelOutput
Output for the fine-tuned example model implementation.
Source code in bionemo/example_model/lightning_basic.py
83 84 85 86 |
|
ExampleGenericConfig
dataclass
Bases: Generic[ExampleModelT, MegatronLossType]
, MegatronBioNeMoTrainableModelConfig[ExampleModelT, MegatronLossType]
ExampleConfig is a dataclass that is used to configure the model.
Timers from ModelParallelConfig are required for megatron forward compatibility.
Source code in bionemo/example_model/lightning_basic.py
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 342 343 344 345 |
|
configure_model()
Uses model_cls and loss_cls to configure the model.
Note: Must pass self into Model since model requires having a config object.
Returns:
Type | Description |
---|---|
ExampleModelT
|
The model object. |
Source code in bionemo/example_model/lightning_basic.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
get_loss_reduction_class()
Use loss_cls to configure the loss, since we do not change the settings of the loss based on the config.
Source code in bionemo/example_model/lightning_basic.py
343 344 345 |
|
ExampleModel
Bases: ExampleModelTrunk
Source code in bionemo/example_model/lightning_basic.py
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 |
|
__init__(config)
Constructor of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
ModelParallelConfig
|
The config object is responsible for telling the strategy what model to create. |
required |
Source code in bionemo/example_model/lightning_basic.py
238 239 240 241 242 243 244 245 246 247 |
|
forward(x)
Forward pass of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input data. |
required |
Returns:
Name | Type | Description |
---|---|---|
x_hat |
ExampleModelOutput
|
The result of the last linear layer of the network. |
Source code in bionemo/example_model/lightning_basic.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
ExampleModelOutput
Bases: TypedDict
Output for the example model implementation.
Source code in bionemo/example_model/lightning_basic.py
76 77 78 79 80 |
|
ExampleModelTrunk
Bases: MegatronModule
Source code in bionemo/example_model/lightning_basic.py
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 |
|
__init__(config)
Constructor of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
ModelParallelConfig
|
The config object is responsible for telling the strategy what model to create. |
required |
Source code in bionemo/example_model/lightning_basic.py
210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
set_input_tensor(input_tensor)
This would be needed for model parallel and other kinds of more complicated forward passes in megatron.
Source code in bionemo/example_model/lightning_basic.py
232 233 234 |
|
LitAutoEncoder
Bases: LightningModule
, IOMixin
, LightningPassthroughPredictionMixin
A very basic lightning module for testing the megatron strategy and the megatron-nemo2-bionemo contract.
Source code in bionemo/example_model/lightning_basic.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|
__init__(config)
Initializes the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
MegatronBioNeMoTrainableModelConfig
|
a Config object necessary to construct the actual nn.Module (the thing that has the parameters). |
required |
Source code in bionemo/example_model/lightning_basic.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
forward(batch, batch_idx)
This forward will be called by the megatron scheduler and it will be wrapped.
Note
The training_step
defines the training loop and is independent of the forward
method here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict
|
A dictionary of data. |
required |
batch_idx
|
int
|
The index of the batch. |
required |
Returns:
Type | Description |
---|---|
Any
|
The output of the model. |
Source code in bionemo/example_model/lightning_basic.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
loss_reduction_class()
Get the loss reduction class the user has specified in their config.
Source code in bionemo/example_model/lightning_basic.py
467 468 469 |
|
training_step(batch, batch_idx=None)
The training step is where the loss is calculated and the backpropagation is done.
Background:
- NeMo's Strategy overrides this method.
- The strategies' training step will call the forward method of the model.
- That forward method then calls the wrapped forward step of MegatronParallel which wraps the forward method of the model.
- That wrapped forward step is then executed inside the Mcore scheduler, which calls the _forward_step
method from the
MegatronParallel class.
- Which then calls the training_step function here.
In this particular use case, we simply call the forward method of this class, the lightning module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
A dictionary of data. requires |
required | |
batch_idx
|
Optional[int]
|
The index of the batch. |
None
|
Source code in bionemo/example_model/lightning_basic.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
MNISTCustom
Bases: MNIST
Source code in bionemo/example_model/lightning_basic.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
__getitem__(index)
Wraps the getitem method of the MNIST dataset such that we return a Dict instead of a Tuple or tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
The index we want to grab, an int. |
required |
Returns:
Type | Description |
---|---|
MnistItem
|
A dict containing the data ("x"), label ("y"), and index ("idx"). |
Source code in bionemo/example_model/lightning_basic.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
MNISTDataModule
Bases: LightningDataModule
Source code in bionemo/example_model/lightning_basic.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
|
setup(stage)
Sets up the datasets
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stage
|
str
|
can be one of train / test / predict. |
required |
Source code in bionemo/example_model/lightning_basic.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
MSELossReduction
Bases: MegatronLossReduction
A class used for calculating the loss, and for logging the reduced loss across micro batches.
Source code in bionemo/example_model/lightning_basic.py
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 |
|
forward(batch, forward_out)
Calculates the loss within a micro-batch. A micro-batch is a batch of data on a single GPU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
MnistItem
|
A batch of data that gets passed to the original forward inside LitAutoEncoder. |
required |
forward_out
|
Dict[str, Tensor]
|
the output of the forward method inside LitAutoEncoder. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
A tuple containing [ |
Source code in bionemo/example_model/lightning_basic.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
reduce(losses_reduced_per_micro_batch)
Works across micro-batches. (data on single gpu).
Note: This currently only works for logging and this loss will not be used for backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
a list of the outputs of forward |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor that is the mean of the losses. (used for logging). |
Source code in bionemo/example_model/lightning_basic.py
111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
MSEPlusClassifierLossReduction
Bases: MegatronLossReduction
A class used for calculating the loss, and for logging the reduced loss across micro batches.
Source code in bionemo/example_model/lightning_basic.py
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 |
|
forward(batch, forward_out)
Calculates the loss within a micro-batch. A micro-batch is a batch of data on a single GPU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
MnistItem
|
A batch of data that gets passed to the original forward inside LitAutoEncoder. |
required |
forward_out
|
ExampleFineTuneOutput
|
the output of the forward method inside LitAutoEncoder. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
A tuple containing [ |
Source code in bionemo/example_model/lightning_basic.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
reduce(losses_reduced_per_micro_batch)
Works across micro-batches. (data on single gpu).
Note: This currently only works for logging and this loss will not be used for backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
a list of the outputs of forward |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor that is the mean of the losses. (used for logging). |
Source code in bionemo/example_model/lightning_basic.py
151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
MnistItem
Bases: TypedDict
Training input for the MNIST dataset.
Source code in bionemo/example_model/lightning_basic.py
68 69 70 71 72 73 |
|
SameSizeLossDict
Bases: TypedDict
This is the return type for a loss that is computed for the entire batch, where all microbatches are the same size.
Source code in bionemo/example_model/lightning_basic.py
62 63 64 65 |
|