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)
初始化模型。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
MegatronBioNeMoTrainableModelConfig
|
构建实际 nn.Module(具有参数的对象)所需的 Config 对象。 |
必需 |
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()
这会配置模型。它由 megatron 策略延迟调用。
Source code in bionemo/example_model/lightning/lightning_basic.py
639 640 641 |
|
forward(batch, batch_idx)
此 forward 将由 megatron 调度器调用,并且将被包装。
注意
training_step
定义了训练循环,并且与此处的 forward
方法无关。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
Dict
|
数据字典。 |
必需 |
batch_idx
|
int
|
批次的索引。 |
必需 |
返回
类型 | 描述 |
---|---|
Any
|
模型的输出。 |
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()
获取用户在其配置中指定的损失缩减类。
Source code in bionemo/example_model/lightning/lightning_basic.py
643 644 645 |
|
predict_step(batch, batch_idx=None)
预测时前向步骤的别名。
Source code in bionemo/example_model/lightning/lightning_basic.py
611 612 613 |
|
test_loss_reduction()
此函数采用 batch['loss_mask'] 和模型输出的 logits,并减少损失。
返回:MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
631 632 633 634 635 636 637 |
|
training_loss_reduction()
此函数采用 batch['loss_mask'] 和模型输出的 logits,并减少损失。
返回:MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
615 616 617 618 619 620 621 |
|
training_step(batch, batch_idx=None)
训练步骤是计算损失并完成反向传播的地方。
背景:- NeMo 的 Strategy 覆盖了此方法。 - 策略的训练步骤将调用模型的前向方法。 - 该前向方法然后调用 MegatronParallel 的包装前向步骤,后者包装模型的前向方法。 - 然后在 Mcore 调度器内部执行该包装的前向步骤,该调度器从 MegatronParallel 类调用 _forward_step
方法。 - 然后调用此处的 training_step 函数。
在此特定用例中,我们只需调用此类的 forward 方法,即 lightning 模块。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
数据字典。默认情况下需要 |
必需 | |
batch_idx
|
Optional[int]
|
批次的索引。 |
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()
此函数采用 batch['loss_mask'] 和模型输出的 logits,并减少损失。
返回:MegatronLossReduction
Source code in bionemo/example_model/lightning/lightning_basic.py
623 624 625 626 627 628 629 |
|
validation_step(batch, batch_idx=None)
验证时前向步骤的别名。
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
用于计算损失以及记录跨微批次的缩减损失的类。
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)
计算微批次内的损失。微批次是单个 GPU 上的数据批次。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
MnistItem
|
传递到 LitAutoEncoder 内部原始 forward 的数据批次。 |
必需 |
forward_out
|
Tensor
|
LitAutoEncoder 内部 forward 方法的输出。 |
必需 |
返回
类型 | 描述 |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
包含以下内容的元组:[ |
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)
跨微批次工作。(单个 gpu 上的数据)。
注意:这目前仅适用于日志记录,并且此损失不会用于反向传播。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
forward 输出的列表 |
必需 |
返回
类型 | 描述 |
---|---|
Tensor
|
作为损失均值的张量。(用于日志记录)。 |
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 是一个 dataclass,用于配置模型。
ModelParallelConfig 中的计时器是 megatron 前向兼容性所必需的。
Source code in bionemo/example_model/lightning/lightning_basic.py
489 490 491 492 493 494 495 496 497 498 499 |
|
ExampleFineTuneBothModel
Bases: ExampleModel
获取示例模型并添加输出任务的示例。
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 是一个 dataclass,用于配置模型。
ModelParallelConfig 中的计时器是 megatron 前向兼容性所必需的。
Source code in bionemo/example_model/lightning/lightning_basic.py
502 503 504 505 506 507 508 509 510 511 512 |
|
ExampleFineTuneModel
Bases: ExampleModelTrunk
获取示例模型并替换输出任务的示例。
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
微调示例模型实现的输出。
Source code in bionemo/example_model/lightning/lightning_basic.py
88 89 90 91 |
|
ExampleGenericConfig
dataclass
Bases: Generic[ExampleModelT, MegatronLossType]
, MegatronBioNeMoTrainableModelConfig[ExampleModelT, MegatronLossType]
ExampleGenericConfig 是一个 dataclass,用于配置模型。
ModelParallelConfig 中的计时器是 megatron 前向兼容性所必需的。
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()
使用 model_cls 和 loss_cls 来配置模型。
注意:必须将 self 传递到 Model 中,因为模型需要具有 config 对象。
返回
类型 | 描述 |
---|---|
ExampleModelT
|
模型对象。 |
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()
使用 loss_cls 来配置损失,因为我们不根据配置更改损失的设置。
Source code in bionemo/example_model/lightning/lightning_basic.py
471 472 473 |
|
ExampleModel
Bases: ExampleModelTrunk
一个示例模型。
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)
模型的构造函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
ModelParallelConfig
|
config 对象负责告诉策略要创建什么模型。 |
必需 |
Source code in bionemo/example_model/lightning/lightning_basic.py
366 367 368 369 370 371 372 373 374 375 |
|
forward(x)
模型的前向传递。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
x
|
Tensor
|
输入数据。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
x_hat |
ExampleModelOutput
|
网络最后一层线性的结果。 |
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
示例模型实现的输出。
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)
模型的构造函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
ModelParallelConfig
|
config 对象负责告诉策略要创建什么模型。 |
必需 |
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)
对于模型并行和 megatron 中其他更复杂的前向传递,这将是必需的。
Source code in bionemo/example_model/lightning/lightning_basic.py
358 359 360 |
|
MNISTCustomDataset
Bases: MNIST
MNIST 数据集的包装器。
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)
包装 MNIST 数据集的 getitem 方法,以便我们返回 Dict。
这是代替 Tuple 或张量。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
idx
|
int
|
我们要抓取的索引,一个整数。 |
必需 |
返回
类型 | 描述 |
---|---|
MnistItem
|
包含数据(“x”)、标签(“y”)和索引(“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
用于 MNIST 的 Megatron 兼容数据模块。
属性:data_dir:数据目录 micro_batch_size:batch_size global_batch_size:全局批次大小 max_len:megatron 采样器的最大序列长度 rampup_batch_size:ramp up 批次大小 num_workers:工作线程数 data_sampler:设置为 megatron 的 data_sampler
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)
初始化类。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data_dir
|
str | PathLike
|
数据目录 |
str(BIONEMO_CACHE_DIR)
|
batch_size
|
int
|
batch_size |
32
|
global_batch_size
|
int | None
|
全局批次大小 |
None
|
num_workers
|
int
|
工作线程数 |
0
|
output_log
|
bool
|
是否输出日志 |
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()
返回预测数据加载器。
Source code in bionemo/example_model/lightning/lightning_basic.py
323 324 325 |
|
setup(stage)
设置数据集。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
stage
|
str
|
可以是 train / test / predict 之一。 |
必需 |
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()
返回训练数据加载器。
Source code in bionemo/example_model/lightning/lightning_basic.py
315 316 317 |
|
val_dataloader()
返回验证数据加载器。
Source code in bionemo/example_model/lightning/lightning_basic.py
319 320 321 |
|
MSELossReduction
Bases: MegatronLossReduction
用于计算损失以及记录跨微批次的缩减损失的类。
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)
计算微批次内的损失。微批次是单个 GPU 上的数据批次。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
MnistItem
|
传递到 LitAutoEncoder 内部原始 forward 的数据批次。 |
必需 |
forward_out
|
Dict[str, Tensor]
|
LitAutoEncoder 内部 forward 方法的输出。 |
必需 |
返回
类型 | 描述 |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
包含以下内容的元组:[ |
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)
跨微批次工作。(单个 gpu 上的数据)。
注意:这目前仅适用于日志记录,并且此损失不会用于反向传播。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
forward 输出的列表 |
必需 |
返回
类型 | 描述 |
---|---|
Tensor
|
作为损失均值的张量。(用于日志记录)。 |
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
用于计算损失以及记录跨微批次的缩减损失的类。
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)
计算微批次内的损失。微批次是单个 GPU 上的数据批次。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
MnistItem
|
传递到 LitAutoEncoder 内部原始 forward 的数据批次。 |
必需 |
forward_out
|
ExampleFineTuneOutput
|
LitAutoEncoder 内部 forward 方法的输出。 |
必需 |
返回
类型 | 描述 |
---|---|
Tuple[Tensor, SameSizeLossDict]
|
包含以下内容的元组:[ |
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)
跨微批次工作。(单个 gpu 上的数据)。
注意:这目前仅适用于日志记录,并且此损失不会用于反向传播。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
losses_reduced_per_micro_batch
|
Sequence[SameSizeLossDict]
|
forward 输出的列表 |
必需 |
返回
类型 | 描述 |
---|---|
Tensor
|
作为损失均值的张量。(用于日志记录)。 |
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
MNIST 数据集的训练输入。
Source code in bionemo/example_model/lightning/lightning_basic.py
73 74 75 76 77 78 |
|
PretrainConfig
dataclass
Bases: ExampleGenericConfig['ExampleModel', 'MSELossReduction']
, IOMixinWithGettersSetters
PretrainConfig 是一个 dataclass,用于配置模型。
ModelParallelConfig 中的计时器是 megatron 前向兼容性所必需的。
Source code in bionemo/example_model/lightning/lightning_basic.py
478 479 480 481 482 483 484 485 486 |
|
SameSizeLossDict
Bases: TypedDict
这是为整个批次计算的损失的返回类型,其中所有微批次的大小都相同。
Source code in bionemo/example_model/lightning/lightning_basic.py
67 68 69 70 |
|