Lightning
BertBatch
Bases: BertBatchCore
BERT-like 模型推理的输入数据类型。
源代码在 bionemo/llm/model/biobert/lightning.py
中
78 79 80 81 |
|
BertBatchCore
Bases: TypedDict
BERT-like 模型推理的输入数据类型。
源代码在 bionemo/llm/model/biobert/lightning.py
中
66 67 68 69 70 |
|
BertModel
Bases: Protocol[DataT]
BERT-like 模型的接口。
源代码在 bionemo/llm/model/biobert/lightning.py
中
52 53 54 55 56 57 58 59 60 61 62 63 |
|
forward(input_ids, attention_mask, packed_seq_params=None)
BERT-like 模型的推理。
BERT-like 模型的推理需要其 token 化的 ID 输入、输入上的注意力掩码,以及原始序列长度(如果序列被打包到密集批次中)。
源代码在 bionemo/llm/model/biobert/lightning.py
中
55 56 57 58 59 60 61 62 63 |
|
BioBertLightningModule
Bases: BionemoLightningModule
源代码在 bionemo/llm/model/biobert/lightning.py
中
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
__init__(*args, data_step_function=biobert_data_step, forward_step_function=bert_forward_step, **kwargs)
已弃用!请使用 BionemoLightningModule。这里是为了我们可以加载旧的检查点。这会将旧名称 forward_step_function
映射到新名称 forward_step
,并将 data_step_function
映射到 data_step
。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
*args
|
所有 args 都传递给 BionemoLightningModule |
()
|
|
data_step_function
|
DataStepFunction
|
数据步骤函数。默认为 biobert_data_step。 |
biobert_data_step
|
forward_step_function
|
ForwardStepFunction
|
前向步骤函数。默认为 bert_forward_step。 |
bert_forward_step
|
**kwargs
|
所有其他 kwargs 都传递给 BionemoLightningModule。 |
{}
|
源代码在 bionemo/llm/model/biobert/lightning.py
中
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
SequenceBatch
Bases: SequenceBatchCore
BERT-like 模型推理的输入数据类型。
源代码在 bionemo/llm/model/biobert/lightning.py
中
90 91 92 93 94 |
|
SequenceBatchCore
Bases: TypedDict
BERT-like 模型推理的输入数据类型。
源代码在 bionemo/llm/model/biobert/lightning.py
中
84 85 86 87 |
|
bert_default_optimizer(model)
返回 BERT 模型的默认优化器。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model
|
Module
|
BERT 模型。 |
required |
返回
类型 | 描述 |
---|---|
FusedAdam
|
为此 BERT 模块的参数初始化的默认优化器。 |
FusedAdam
|
使用 1e-4 的学习率和 1e-2 的权重衰减。 |
源代码在 bionemo/llm/model/biobert/lightning.py
中
185 186 187 188 189 190 191 192 193 194 195 |
|
bert_forward_step(model, batch)
使用批次执行模型的前向传递,以实现 Megatron 兼容性。
这会将批次键子集化为模型前向传递实际使用的键,然后调用模型的前向传递。如果批次中定义了“cu_seqsens”,则还会将打包的序列参数传递给模型以提高前向传递效率。
源代码在 bionemo/llm/model/biobert/lightning.py
中
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
biobert_data_step(dataloader_iter)
预处理 GeneFormer 模型的数据批次,并从数据加载器迭代器中提取单个数据批次。仅必要的批次键被子集化并传递给模型的前向传递和损失前向传递,具体取决于阶段。TODO 记录 parallel_state 管道阶段如何工作。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
dataloader_iter
|
数据加载器上的迭代器。 |
required |
返回
名称 | 类型 | 描述 |
---|---|---|
output |
Dict[str, Tensor]
|
此批次的字典,限制为相关键。 |
源代码在 bionemo/llm/model/biobert/lightning.py
中
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 |
|
biobert_lightning_module(config, optimizer=None, tokenizer=None, data_step=biobert_data_step, forward_step=bert_forward_step, model_transform=None, **model_construct_args)
用于 BioBert 派生模型的 pytorch lightning 模块。
此模块旨在与 Megatron-LM 策略和 nemo 2.0 约定一起使用。要更改您的损失,请传入不同的配置对象,该对象返回不同的损失减少类。要更改您的模型及其输出,请传入不同的配置对象,该对象返回不同的模型。除非您需要更改更高级别的逻辑,否则请勿修改此函数。您可能需要修改此文件底部的各种步骤和前向函数,以处理批次中的新键/不同键。将来,可能需要将其中一些函数重构到配置对象或其他位置,以便它们更接近模型定义。
源代码在 bionemo/llm/model/biobert/lightning.py
中
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 |
|
get_batch_on_this_context_parallel_rank(batch, in_place=True)
确保输入批次的格式适合上下文并行 rank。
如果上下文并行世界大小大于 1,则根据上下文并行 rank 修改批次数据。否则,批次按原样返回。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
Dict[str, Tensor]
|
输入批次数据。 |
required |
in_place
|
bool
|
如果为 true,则输入将被修改。返回的字典是对输入的引用。否则,输入数据始终进行浅复制,并且此副本将被修改并返回。 |
True
|
返回
名称 | 类型 | 描述 |
---|---|---|
dict |
Dict[str, Tensor]
|
基于上下文并行 rank 修改的批次数据。 |
源代码在 bionemo/llm/model/biobert/lightning.py
中
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 |
|
get_packed_seq_params(batch)
获取给定批次的打包序列参数。
仅当批次中定义了 cu_seqlens
时,才应调用此函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
batch
|
SequenceBatch
|
要打包的输入批次。 |
required |
返回
名称 | 类型 | 描述 |
---|---|---|
PackedSeqParams |
PackedSeqParams
|
打包的序列参数,包含以下属性: - cu_seqlens_q (Tensor):查询的序列长度。 - cu_seqlens_kv (Tensor):键和值的序列长度。 - max_seqlen_q (Tensor, optional):查询的最大序列长度(可选)。 - max_seqlen_kv (Tensor, optional):键和值的最大序列长度(可选)。 - qkv_format (str):查询、键和值张量的格式。 |
源代码在 bionemo/llm/model/biobert/lightning.py
中
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 |
|