Lightning
BertBatch
Bases: BertBatchCore
Input datatype for inference with BERT-like models.
Source code in bionemo/llm/model/biobert/lightning.py
78 79 80 81 |
|
BertBatchCore
Bases: TypedDict
Input datatype for inference with BERT-like models.
Source code in bionemo/llm/model/biobert/lightning.py
66 67 68 69 70 |
|
BertModel
Bases: Protocol[DataT]
Interface for BERT-like models.
Source code in 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)
Inference for BERT-like models.
Inference for BERT-like models require their tokenized inputs by IDs, an attention mask over the input, and the original sequence lengths if the sequences are packed into a dense batch.
Source code in bionemo/llm/model/biobert/lightning.py
55 56 57 58 59 60 61 62 63 |
|
BioBertLightningModule
Bases: BionemoLightningModule
Source code in 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)
DEPRECATED! Please use BionemoLightningModule. This is here so we can load older checkpoints.
This maps the old name forward_step_function
to the new name forward_step
and data_step_function
to
data_step
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
all args are passed through to BionemoLightningModule |
()
|
|
data_step_function
|
DataStepFunction
|
The data step function. Defaults to biobert_data_step. |
biobert_data_step
|
forward_step_function
|
ForwardStepFunction
|
The forward step function. Defaults to bert_forward_step. |
bert_forward_step
|
**kwargs
|
all other kwargs are passed through to BionemoLightningModule. |
{}
|
Source code in 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
Input datatype for inference with BERT-like models.
Source code in bionemo/llm/model/biobert/lightning.py
90 91 92 93 94 |
|
SequenceBatchCore
Bases: TypedDict
Input datatype for inference with BERT-like models.
Source code in bionemo/llm/model/biobert/lightning.py
84 85 86 87 |
|
bert_default_optimizer(model)
Returns the default optimizer for the BERT model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The BERT model. |
required |
Returns:
Type | Description |
---|---|
FusedAdam
|
The default optimizer initialized for this BERT module's parameters. |
FusedAdam
|
Uses a learning rate of 1e-4 and weight decay of 1e-2. |
Source code in bionemo/llm/model/biobert/lightning.py
185 186 187 188 189 190 191 192 193 194 195 |
|
bert_forward_step(model, batch)
Performs the model's forward pass using the batch, for Megatron compatibility.
This subsets the batch keys to the ones actually used by forward pass of the model, and then calls the model's forward pass. if "cu_seqsens" are defined in the batch, then the packed sequence parameters are also passed to the model for forward pass efficiency.
Source code in 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)
Preprocesses a batch of data for the GeneFormer model, and ingest a single batch of data from the dataloader iterator. only necessary batch keys are subsetted and passed to the model's forward pass, and the loss forward pass, depending on stage. TODO document how parallel_state pipeline stages work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader_iter
|
An iterator over the dataloader. |
required |
Returns:
Name | Type | Description |
---|---|---|
output |
Dict[str, Tensor]
|
A dictionary of this batch limiting to relevant keys. |
Source code in 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)
A pytorch lightning module for BioBert-derived models.
This module is designed to be used with the Megatron-LM strategy and nemo 2.0 conventions. To change your loss, pass in a different config object that returns a different loss reduction class. To change your model and what it outputs, pass in a different config object that returns a different model. Do not modify this function unless you need to change higher level logic. You may need to modify the various step and forward functions towards the bottom of this file to handle new/different keys in the batch. In the future some of those functions may need to be refactored out into the config object or a different place so that they live closer to the model definition.
Source code in 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)
Ensures that the input batch is in the right format for context parallel rank.
Modifies the batch data based on the context parallel rank, if the context parallel world size is greater than 1. Otherwise, the batch is returned as-is.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict[str, Tensor]
|
The input batch data. |
required |
in_place
|
bool
|
If true, then the input is mutated. The returned dict is a reference to the input. Otherwise, the input data is always shallow-copied and this copy is modified and returned. |
True
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict[str, Tensor]
|
The modified batch data based on the context parallel rank. |
Source code in 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)
Get the packed sequence parameters for the given batch.
This function should only be called if cu_seqlens
is defined in the batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
SequenceBatch
|
The input batch to pack. |
required |
Returns:
Name | Type | Description |
---|---|---|
PackedSeqParams |
PackedSeqParams
|
The packed sequence parameters containing the following attributes: - cu_seqlens_q (Tensor): The sequence lengths for query. - cu_seqlens_kv (Tensor): The sequence lengths for key and value. - max_seqlen_q (Tensor, optional): The maximum sequence length for query. - max_seqlen_kv (Tensor, optional): The maximum sequence length for key and value. - qkv_format (str): The format of query, key, and value tensors. |
Source code in 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 |
|