Lightning basic
This is intended to be a minimal self-container NeMo2 example.
BionemoLightningModule
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/lightning_basic.py
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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
|
__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/lightning_basic.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
|
configure_model()
This configures the model. It is called lazily by the megatron strategy.
Source code in bionemo/example_model/lightning/lightning_basic.py
639 640 641 |
|
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/lightning_basic.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
loss_reduction_class()
Get the loss reduction class the user has specified in their config.
Source code in bionemo/example_model/lightning/lightning_basic.py
643 644 645 |
|
predict_step(batch, batch_idx=None)
Alias for forward step at prediction.
Source code in bionemo/example_model/lightning/lightning_basic.py
611 612 613 |
|
test_loss_reduction()
This is the function that takes batch['loss_mask'] and the logits output by the model and reduces the loss.
Returns: A MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
631 632 633 634 635 636 637 |
|
training_loss_reduction()
This is the function that takes batch['loss_mask'] and the logits output by the model and reduces the loss.
Returns: A MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
615 616 617 618 619 620 621 |
|
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/lightning_basic.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
|
validation_loss_reduction()
This is the function that takes batch['loss_mask'] and the logits output by the model and reduces the loss.
Returns: A MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
623 624 625 626 627 628 629 |
|
validation_step(batch, batch_idx=None)
Alias for forward step at validation.
Source code in bionemo/example_model/lightning/lightning_basic.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
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/lightning_basic.py
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 |
|
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/lightning_basic.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
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/lightning_basic.py
191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
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/lightning_basic.py
489 490 491 492 493 494 495 496 497 498 499 |
|
ExampleFineTuneBothModel
Bases: ExampleModel
Example of taking the example model and adding an output task.
Source code in bionemo/example_model/lightning/lightning_basic.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
ExampleFineTuneConfig
dataclass
Bases: ExampleGenericConfig['ExampleFineTuneConfig', '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/lightning_basic.py
502 503 504 505 506 507 508 509 510 511 512 |
|
ExampleFineTuneModel
Bases: ExampleModelTrunk
Example of taking the example model and replacing output task.
Source code in bionemo/example_model/lightning/lightning_basic.py
411 412 413 414 415 416 417 418 419 420 421 422 |
|
ExampleFineTuneOutput
Bases: ExampleModelOutput
Output for the fine-tuned example model implementation.
Source code in bionemo/example_model/lightning/lightning_basic.py
88 89 90 91 |
|
ExampleGenericConfig
dataclass
Bases: Generic[ExampleModelT, MegatronLossType]
, MegatronBioNeMoTrainableModelConfig[ExampleModelT, MegatronLossType]
ExampleGenericConfig 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/lightning_basic.py
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 470 471 472 473 |
|
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/lightning_basic.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|
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/lightning_basic.py
471 472 473 |
|
ExampleModel
Bases: ExampleModelTrunk
An example model.
Source code in bionemo/example_model/lightning/lightning_basic.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
__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/lightning_basic.py
366 367 368 369 370 371 372 373 374 375 |
|
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/lightning_basic.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
ExampleModelOutput
Bases: TypedDict
Output for the example model implementation.
Source code in bionemo/example_model/lightning/lightning_basic.py
81 82 83 84 85 |
|
ExampleModelTrunk
Bases: MegatronModule
Source code in bionemo/example_model/lightning/lightning_basic.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
__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/lightning_basic.py
336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
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/lightning_basic.py
358 359 360 |
|
MNISTCustomDataset
Bases: MNIST
A Wrapper for the MNIST Dataset.
Source code in bionemo/example_model/lightning/lightning_basic.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
__getitem__(idx)
Wraps the getitem method of the MNIST dataset such that we return a Dict.
This is instead of a Tuple or tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
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/lightning_basic.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
MNISTDataModule
Bases: LightningDataModule
A Megatron Compatible Data Module for MNIST.
Attributes: data_dir: data directory micro_batch_size: batch_size global_batch_size: global batch size max_len: maximal sequence length for megatron sampler rampup_batch_size: ramp up batch size num_workers: number of workers data_sampler: data_sampler set to be a megatron one
Source code in bionemo/example_model/lightning/lightning_basic.py
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 321 322 323 324 325 |
|
__init__(data_dir=str(BIONEMO_CACHE_DIR), batch_size=32, num_workers=0, global_batch_size=None, output_log=True)
Initialize class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_dir
|
str | PathLike
|
data directory |
str(BIONEMO_CACHE_DIR)
|
batch_size
|
int
|
batch_size |
32
|
global_batch_size
|
int | None
|
global batch size |
None
|
num_workers
|
int
|
number of workers |
0
|
output_log
|
bool
|
whether to output logs |
True
|
Source code in bionemo/example_model/lightning/lightning_basic.py
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 |
|
predict_dataloader()
Returns the prediction dataloader.
Source code in bionemo/example_model/lightning/lightning_basic.py
323 324 325 |
|
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/lightning_basic.py
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 |
|
train_dataloader()
Returns the training dataloader.
Source code in bionemo/example_model/lightning/lightning_basic.py
315 316 317 |
|
val_dataloader()
Returns the validation dataloader.
Source code in bionemo/example_model/lightning/lightning_basic.py
319 320 321 |
|
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/lightning_basic.py
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 |
|
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/lightning_basic.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
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/lightning_basic.py
116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
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/lightning_basic.py
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 |
|
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/lightning_basic.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
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/lightning_basic.py
156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
MnistItem
Bases: TypedDict
Training input for the MNIST dataset.
Source code in bionemo/example_model/lightning/lightning_basic.py
73 74 75 76 77 78 |
|
PretrainConfig
dataclass
Bases: ExampleGenericConfig['ExampleModel', 'MSELossReduction']
, IOMixinWithGettersSetters
PretrainConfig 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/lightning_basic.py
478 479 480 481 482 483 484 485 486 |
|
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/lightning_basic.py
67 68 69 70 |
|