Lightning
DataStep = Callable[[Iterator[DataT]], DataT]
module-attribute
将单个示例的迭代器批处理在一起。
对于与 Megatron 的兼容性是必要的。此函数类型类似于 PyTorch 的整理函数。
DataStep
函数接受单个示例的迭代器。每个示例可以是张量、张量序列或一组命名的张量(以 dict
形式提供,将 str
名称映射到每个 Tensor
)。每次迭代都必须产生相同的类型。
此函数的输出将反映每个生成示例的相同结构。它将是迭代器中所有示例的串联。
ForwardStep = Callable[[MegatronModelType, DataT], DataT]
module-attribute
与 Megatron 兼容的前向传递函数。
BionemoLightningModule
基类:Generic[MegatronModelType, MegatronLossType]
、LightningModule
、IOMixin
、ConnectorMixin
、LightningPassthroughPredictionMixin
用于 Megatron 模型的、可重用的 PyTorch Lightning 模块,与 NeMo 的约定兼容。
源代码位于 bionemo/llm/lightning.py
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 321 322 323 324 325 326 327 |
|
__init__(config, forward_step, data_step, optimizer, model_transform=None, **model_construct_args)
构造函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
BionemoTrainableModelConfig[MegatronModelType, MegatronLossType]
|
可序列化配置对象,允许用户构造新的模型实例和损失函数。对于基于 Megatron 的训练是必要的,因为模型本身无法序列化并分发到节点。相反,我们序列化用于创建模型的过程并分发它。 |
必需 |
forward_step
|
ForwardStep
|
使用模型和一批数据执行前向传递。 |
必需 |
data_step
|
DataStep
|
模型的自定义批创建函数。 |
必需 |
optimizer
|
MegatronOptimizerModule
|
与 Megatron 兼容的分布式优化器实例。默认为使用学习率为 1e-4 的 ADAM。 |
必需 |
model_construct_args
|
可选。在 |
{}
|
|
model_transform
|
Optional[Callable[[MegatronModelType], MegatronModelType]]
|
可选。模型转换函数。 |
None
|
**model_construct_args
|
可选。提供的模型配置的 |
{}
|
源代码位于 bionemo/llm/lightning.py
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 |
|
configure_model()
更新内部状态:从对象的配置实例化模型,并分配给 model
属性。
注意:此方法是幂等的;连续调用无效。模型仅初始化一次。
源代码位于 bionemo/llm/lightning.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
forward(*args, **kwargs)
调用底层模型的前向方法,并返回其输出。
源代码位于 bionemo/llm/lightning.py
279 280 281 282 283 284 285 |
|
forward_step(batch)
Megatron 必需:模型的训练前向步骤,需要生成损失。
通常,模型的前向传递意味着其推理。损失是使用前向传递的预测结果与标签进行计算的。不幸的是,Megatron 混淆了这两个不同的概念,而是让模型的“forward”方法生成损失。有关详细信息,请参阅 Megatron 文档:https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/pipeline_parallel/schedules.py#L170
要获得实际预测,请改用 :func:forward
方法。
源代码位于 bionemo/llm/lightning.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
predict_step(batch, batch_idx=None)
forward_step 的别名。
源代码位于 bionemo/llm/lightning.py
313 314 315 316 317 |
|
training_loss_reduction()
此函数接受 batch['loss_mask'] 和模型输出的 logits,并减少损失。
源代码位于 bionemo/llm/lightning.py
319 320 321 |
|
training_step(batch, batch_idx=None)
在 mcore 中,当提供标签时,损失函数是前向传递的一部分。
源代码位于 bionemo/llm/lightning.py
305 306 307 |
|
validation_step(batch, batch_idx=None)
在 mcore 中,当提供标签时,损失函数是前向传递的一部分。
源代码位于 bionemo/llm/lightning.py
309 310 311 |
|
LightningPassthroughPredictionMixin
一种混入,允许您的模型通过劫持 nemo 的损失减少机制在预测步骤中进行推理。
源代码位于 bionemo/llm/lightning.py
187 188 189 190 191 192 |
|
predict_loss_reduction()
对于预测步骤,传递前向传递输出。
源代码位于 bionemo/llm/lightning.py
190 191 192 |
|
PassthroughLossReduction
基类:MegatronLossReduction
、Generic[DataT]
一种用于 nemo/megatron 执行推理的变通方法。
在 NeMo2.0 内部,前向步骤始终应返回损失减少类,而 forward 应返回损失。此类劫持该机制,而是将前向输出未经扰动地作为损失传递(以在预测步骤中启用推理),然后 reduce 方法用于将一批前向输出整理为单个批次。这支持模型前向输出为张量、字典、元组或张量列表。内部类型必须始终为张量。
源代码位于 bionemo/llm/lightning.py
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 |
|
forward(batch, forward_out)
将 forward_out
值作为第二个元组元素传递。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
DataT
|
已传递到模型以生成输出的数据批次。注意:此值将被忽略。 |
必需 |
forward_out
|
DataT
|
模型前向传递的输出。 |
必需 |
返回
类型 | 描述 |
---|---|
Tuple[Tensor, DataT]
|
包含损失张量(在本例中为虚拟)和前向输出(未修改)的元组。 |
源代码位于 bionemo/llm/lightning.py
170 171 172 173 174 175 176 177 178 179 180 |
|
reduce(forward_out)
将模型输出列表整理为单个输出。
源代码位于 bionemo/llm/lightning.py
182 183 184 |
|
PerplexityLoggingCallback
基类:Callback
、CallbackMethods
Megatron 回调,用于记录验证中的困惑度,以及可选的训练中的困惑度。
NeMo2.0 检查回调是否为 {LightningModule,LightningDataModule,Callback} 的实例,但只有 megatron_hooks 有用。
源代码位于 bionemo/llm/lightning.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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 391 392 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 |
|
__init__(log_train=False, log_val=True)
初始化 PerplexityLoggingCallback。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
log_train
|
bool
|
是否记录训练困惑度。默认为 False。 |
False
|
log_val
|
bool
|
是否记录验证困惑度。默认为 True。 |
True
|
源代码位于 bionemo/llm/lightning.py
343 344 345 346 347 348 349 350 351 352 |
|
on_megatron_reduce_microbatches_end(step, microbatch_outputs, loss_reduction, reduced)
在调用 MegatronReductionLoss.reduce 后记录。
预期的 microbatch_outputs 是带有以下键的字典列表
- batch:带有以下键的张量字典
- labels: [b s]
- loss_mask: [b s];1 表示包含,0 表示忽略
- forward_out:带有以下键的张量字典
- token_logits: [b s vocab]
源代码位于 bionemo/llm/lightning.py
388 389 390 391 392 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 |
|
batch_collator(batches, batch_dim=0, batch_dim_key_defaults={'token_logits': 1})
接受一批序列并将它们整理成一个批次。
This is distinct from the standard pytorch default_collator since it does
not add the batch dimension, it's assumed the batch
dimension is already present in the input, as would be the case when
parallelizing across minibatches.
重要提示:底层数据原语必须是 torch Tensor。此函数的输入是递归类型,字典、元组和列表之间可以有任意数量的嵌套,只要内部类型是 n 维张量。
示例
外部容器 = 字典:[{'a': Tensor([1]), 'b': Tensor([2])}, {'a': Tensor([2]), 'b': Tensor([3])}] -> {'a': Tensor([1, 2]), 'b': Tensor([2, 3])} 外部容器 = 列表:[[Tensor([1]), Tensor([2])], [Tensor([2]), Tensor([3])]] -> [Tensor([1, 2]), Tensor([2, 3])] 外部容器 = 元组:([Tensor([1]), Tensor([2])], [Tensor([2]), Tensor([3])]) -> (Tensor([1, 2]), Tensor([2, 3]))
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batches
|
Optional[Sequence[ReductionT]]
|
要整理成一个批次的批次序列。 |
必需 |
batch_dim
|
int
|
如果您知道要连接的批次的批处理维度不是第 0 维(例如,它是序列优先),请提供该维度。 |
0
|
batch_dim_key_defaults
|
键到整数的字典
|
如果您的批次是字典,并且您知道某些键具有非标准(0)批处理维度,请在此处提供这些键。默认情况下,“token_logits”的批处理维度为 1,否则所有键都假定批处理维度为 0。 |
{'token_logits': 1}
|
返回
类型 | 描述 |
---|---|
Optional[ReductionT]
|
与输入序列元素类型相同的单个批次。 |
源代码位于 bionemo/llm/lightning.py
87 88 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 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 |
|
default_megatron_optimizer()
默认分布式优化器使用 Adam,学习率为 1e-4。
源代码位于 bionemo/llm/lightning.py
330 331 332 333 334 |
|
some_first(seq)
从序列返回第一个非 None 值,否则失败
源代码位于 bionemo/llm/lightning.py
54 55 56 57 58 59 |
|