配置模型
DataConfig
基类:BaseModel
、Generic[DataModuleT]
、ABC
所有数据配置的基类。
此类用于定义所有数据配置的接口。它用于定义将在训练循环中使用的数据模块。
源代码位于 bionemo/llm/run/config_models.py
中
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
construct_data_module(global_batch_size)
abstractmethod
从配置构建数据模块。不能通用地定义。
源代码位于 bionemo/llm/run/config_models.py
中
84 85 86 87 |
|
custom_model_validator(global_cfg)
使用此方法的自定义实现来定义 global_config 内部的内容。
以下表达式将始终为真
global_cfg.data_config == self
源代码位于 bionemo/llm/run/config_models.py
中
89 90 91 92 93 94 95 96 |
|
ExperimentConfig
基类:BaseModel
用于设置和管理实验参数的配置类。
属性
名称 | 类型 | 描述 |
---|---|---|
save_every_n_steps |
int
|
保存检查点之间的步数。 |
result_dir |
str | Path
|
将保存结果的目录。 |
experiment_name |
str
|
实验名称。 |
restore_from_checkpoint_path |
Optional[str]
|
从检查点恢复的路径。注意:这不会按预期调用检查点回调。 |
save_last_checkpoint |
bool
|
保存最后一个检查点的标志。默认为 True。 |
metric_to_monitor_for_checkpoints |
str
|
用于保存 top-k 检查点的监控指标。默认为 "reduced_train_loss"。 |
save_top_k |
int
|
要根据监控指标保存的 top 检查点数量。默认为 2。 |
create_tensorboard_logger |
bool
|
创建 TensorBoard 日志记录器的标志。默认为 False。 |
create_checkpoint_callback |
bool
|
创建 ModelCheckpoint 回调的标志 |
源代码位于 bionemo/llm/run/config_models.py
中
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 |
|
ExposedModelConfig
基类:BaseModel
、Generic[ModelConfigT]
、ABC
BioNeMo 模型配置类,封装了 TransformerConfig 及其友元。
此类用于定义所有模型配置的接口。它是暴露的,以防止底层配置对象中出现类型错误或定义不明确的字段。ModelConfigT
声明了底层配置的相关类型(最常见的是 BioBertGenericConfig,但也可能是 TransformerConfig 或类似的东西)。子类应尝试公开用户配置模型所需的最小字段集,同时将更深奥的配置保留在底层 ModelConfigT 中。
源代码位于 bionemo/llm/run/config_models.py
中
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 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 |
|
custom_model_validator(global_cfg)
使用此方法的自定义实现来定义 global_config 内部的内容。
以下表达式将始终为真
global_cfg.bionemo_model_config == self
源代码位于 bionemo/llm/run/config_models.py
中
122 123 124 125 126 127 128 129 |
|
exposed_to_internal_bionemo_model_config()
将公开的数据类转换为底层 Transformer 配置。
底层的 ModelConfigT 可能既不完整也不可序列化。我们使用此转换作为一种隐藏 Pydantic 无法序列化或我们不想公开的字段的方法。
源代码位于 bionemo/llm/run/config_models.py
中
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
model_class()
返回此配置包装的底层模型类。
源代码位于 bionemo/llm/run/config_models.py
中
118 119 120 |
|
precision_validator(v)
classmethod
验证精度类型并返回相应的 torch dtype。
源代码位于 bionemo/llm/run/config_models.py
中
249 250 251 252 253 |
|
serialize_activation_func(v)
将给定的激活函数序列化为其相应的字符串表示形式。
默认情况下,torch.nn.functional
中的所有激活函数都序列化为其名称。用户定义的激活函数也应在此处定义,并在位于此文件顶部的 CUSTOM_ACTIVATION_FNS 中进行自定义映射。这允许我们的 Pydantic 模型序列化和反序列化激活函数。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
v
|
Callable[[Tensor, Any], Tensor]
|
要序列化的激活函数。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
str |
str
|
如果激活函数是标准 PyTorch 函数,则返回其名称;如果它是自定义激活函数,则返回相应的序列化键。 |
引发
类型 | 描述 |
---|---|
ValueError
|
如果不支持该激活函数。 |
源代码位于 bionemo/llm/run/config_models.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 |
|
serialize_dtypes(v)
将 torch dtype 序列化为相应的精度类型。
源代码位于 bionemo/llm/run/config_models.py
中
255 256 257 258 |
|
validate_activation_func(activation_func)
classmethod
验证激活函数,假设此函数存在于 torch.nn.functional 中。
对于自定义激活函数,请使用模块中的 CUSTOM_ACTIVATION_FUNCTIONS 字典。此方法验证提供的激活函数字符串,并基于验证上下文使用基类中提供的验证器返回可调用函数。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
activation_func
|
str
|
要验证的激活函数。 |
必需 |
context
|
ValidationInfo
|
验证的上下文。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Callable |
Callable
|
验证后的可调用函数。 |
另请参阅
CUSTOM_ACTIVATION_FNS
源代码位于 bionemo/llm/run/config_models.py
中
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 |
|
MainConfig
基类:BaseModel
、Generic[ExModelConfigT, DataConfigT]
BioNeMo 的主配置类。所有序列化的有效 MainConfig 的配置都应该是可运行的。
此类用于定义 BioNeMo 的主配置。它定义了使用 NeMo2 训练 API 执行训练作业所需的最小配置部分。它接受两个泛型类型参数,用户必须在自己的执行环境中定义这两个参数。
此外,此类假设 ExposedModelConfig 和 DataConfig 的配置可能具有对整个 MainConfig 进行操作的自定义验证器。这避免了在此类中进行基于类型的条件判断的需要,同时仍然允许在底层类中实现自定义验证全局逻辑。例如,某些模型可能希望将其 Datamodule 的 seq_length 限制为特定值。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
data_config
|
包含关于实例化所需 DataModule 的指令的泛型配置类型。 |
必需 | |
parallel_config
|
模型的并行配置。 |
必需 | |
training_config
|
模型的训练配置。 |
必需 | |
bionemo_model_config
|
泛型 ExposedModelConfig 类型。此类隐藏了底层模型配置中的额外配置参数,并提供了 |
必需 | |
optim_config
|
模型的优化器/调度器配置。 |
必需 | |
experiment_config
|
模型的实验配置。 |
必需 | |
wandb_config
|
可选,模型的 wandb 配置。 |
必需 |
源代码位于 bionemo/llm/run/config_models.py
中
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 |
|
run_bionemo_model_config_model_validators()
在 bionemo_model_config 上运行模型验证器。
源代码位于 bionemo/llm/run/config_models.py
中
423 424 425 426 |
|
run_data_config_model_validators()
在 data_config 上运行模型验证器。
源代码位于 bionemo/llm/run/config_models.py
中
428 429 430 431 |
|
validate_checkpointing_setting()
验证主配置对象。
源代码位于 bionemo/llm/run/config_models.py
中
433 434 435 436 437 |
|
validate_master_config()
验证主配置对象。
源代码位于 bionemo/llm/run/config_models.py
中
417 418 419 420 421 |
|
OptimizerSchedulerConfig
基类:BaseModel
优化器和学习率调度器的配置。
属性
名称 | 类型 | 描述 |
---|---|---|
lr |
float
|
优化器的学习率。默认为 1e-4。 |
optimizer |
str
|
要使用的优化器类型。默认为 "adam"。 |
interval |
str
|
更新学习率调度器的间隔。默认为 "step"。 |
monitor |
str
|
用于学习率调整的监控指标。默认为 "val_loss"。 |
interval |
str
|
更新学习率调度器的间隔。默认为 "step"。 |
monitor |
str
|
用于学习率调整的监控指标。默认为 "val_loss"。 |
warmup_steps |
int
|
与 warmup 退火学习率调度器一起使用的预热步数。默认为 0。 |
lr_scheduler |
Literal['warmup_anneal', 'cosine']
|
要使用的学习率调度器类型。默认为 'warmup_anneal'。注意:这可能会更改。 |
max_steps |
Optional[int]
|
优化器中使用的 max_steps。默认为 None,它使用 TrainingConfig 中的 max_steps。 |
源代码位于 bionemo/llm/run/config_models.py
中
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
ParallelConfig
基类:BaseModel
ParallelConfig 是用于在模型训练中设置并行性的配置类。
属性
名称 | 类型 | 描述 |
---|---|---|
tensor_model_parallel_size |
int
|
张量模型并行的大小。默认为 1。 |
pipeline_model_parallel_size |
int
|
管道模型并行的大小。默认为 1。 |
accumulate_grad_batches |
int
|
要累积梯度的批次数。默认为 1。 |
ddp |
Literal['megatron']
|
要使用的分布式数据并行方法。默认为 "megatron"。 |
remove_unused_parameters |
bool
|
是否删除未使用的参数。默认为 True。 |
num_devices |
int
|
要使用的设备数。默认为 1。 |
num_nodes |
int
|
要使用的节点数。默认为 1。 |
方法
名称 | 描述 |
---|---|
validate_devices |
根据张量和管道模型并行大小验证设备数量。 |
源代码位于 bionemo/llm/run/config_models.py
中
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 |
|
validate_devices()
根据张量和管道模型并行大小验证设备数量。
源代码位于 bionemo/llm/run/config_models.py
中
285 286 287 288 289 290 |
|
TrainingConfig
基类:BaseModel
TrainingConfig 是用于训练模型的配置类。
属性
名称 | 类型 | 描述 |
---|---|---|
max_steps |
int
|
最大训练步数。 |
limit_val_batches |
int | float
|
要使用的验证批次数量。可以是分数或计数。 |
val_check_interval |
int
|
检查验证的间隔(以步数为单位)。 |
precision |
Literal['32', 'bf16-mixed', '16-mixed']
|
用于训练的精度。默认为 "bf16-mixed"。 |
accelerator |
str
|
用于训练的加速器类型。默认为 "gpu"。 |
gc_interval |
int
|
运行同步垃圾回收的全局步长间隔。在执行分布式训练时,可用于同步垃圾回收。默认为 0。 |
include_perplexity |
bool
|
是否在验证日志中包含困惑度。默认为 False。 |
enable_checkpointing |
bool
|
是否启用检查点,并在没有用户定义的 ModelCheckpoint 时配置默认的 ModelCheckpoint 回调。对应于 pl.Trainer 中的相同参数名称 |
源代码位于 bionemo/llm/run/config_models.py
中
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
deserialize_str_to_path(path)
用于字符串/路径对象的通用反序列化。由于 YAML 没有 pathlib.Path 的本机表示形式,因此我们序列化为字符串。将此方法作为 @field_validator 导入。
源代码位于 bionemo/llm/run/config_models.py
中
49 50 51 |
|
serialize_path_or_str(path)
用于字符串/路径对象的通用序列化。由于 YAML 没有 pathlib.Path 的本机表示形式,因此我们序列化为字符串。将此方法作为 @field_serializer 导入。
源代码位于 bionemo/llm/run/config_models.py
中
54 55 56 57 58 59 60 61 |
|