Adding Tasks
Tasks are use-cases for pre-trained foundation models.
Pre-trained foundation models (FMs, backbones) improve performance across a wide range of ML tasks. However, tasks utilize FMs in very different ways, often requiring a unique reimplementation or adaptation for every backbone-task pair, a process that is time-consuming and error-prone. For FM-enabled research and development to be practical, modularity and reusability are essential.
AIDO.ModelGenerator tasks
enable rapid prototyping and experimentation through hot-swappable backbone
and adapter
components, which make use of standard interfaces.
All of this is made possible by the PyTorch Lightning framework, which provides the LightningModule interface for hardware-agnostic training, evaluation, and prediction, as well as configified experiment management and extensive CLI support.
Available Tasks: Inference, MLM, SequenceClassification, TokenClassification, PairwiseTokenClassification, Diffusion, ConditionalDiffusion, SequenceRegression, Embed
Note: Adapters and Backbones are typed as
Callables
, since some args are reserved to automatically configure the adapter with the backbone. Create anAdapterCallable
signature for a task to specify which arguments are configurable, and which are reserved.
Adding Adapters
Adapters serve as a linker between a backbone's output and a task's objective function.
They are simple nn.Module
objects that use the backbone interface to configure their weights and forward pass.
Their construction is handled within the task's configure_model
method.
Each task only tolerates a specific adapter type, which all adapters for that task must subclass.
See the SequenceAdapter
type and implemented LinearCLSAdapter
for SequenceRegression
as an example below.
modelgenerator.tasks.TaskInterface
Bases: LightningModule
Interface class to ensure consistent implementation of essential methods for all tasks.
Note
Tasks will usually take a backbone and adapter as arguments, but these are not strictly required. See SequenceRegression task for an succinct example implementation. Handles the boilerplate of setting up training, validation, and testing steps, as well as the optimizer and learning rate scheduler. Subclasses must implement the init, configure_model, collate, forward, and evaluate methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_legacy_adapter
|
bool
|
Whether to use the adapter from the backbone (HF head support). Defaults to False. |
False
|
strict_loading
|
bool
|
Whether to strictly load the model. Defaults to True. Set it to False if you want to replace the adapter (e.g. for continue pretraining) |
True
|
batch_size
|
int
|
The batch size to use for training. Defaults to None. |
None
|
optimizer
|
OptimizerCallable
|
The optimizer to use for training. Defaults to torch.optim.AdamW. |
AdamW
|
reset_optimizer_states
|
bool
|
Whether to reset the optimizer states. Defaults to False. Set it to True if you want to replace the adapter (e.g. for continue pretraining). |
False
|
lr_scheduler
|
LRSchedulerCallable
|
The learning rate scheduler to use for training. Defaults to None. |
None
|
Source code in modelgenerator/tasks/base.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 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 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 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 |
|
configure_model()
Configures the model for training and interence. Subclasses must implement this method.
Source code in modelgenerator/tasks/base.py
86 87 88 |
|
forward(collated_batch)
Runs a forward pass of the model on the collated batch of data. Subclasses must implement this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collated_batch
|
dict[str, Union[list, Tensor]]
|
The collated batch of data from collate. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The model predictions |
Source code in modelgenerator/tasks/base.py
108 109 110 111 112 113 114 115 116 117 |
|
evaluate(preds, collated_batch, stage=None, loss_only=False)
Calculate loss and update metrics states. Subclasses must implement this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor
|
The model predictions from forward. |
required |
collated_batch
|
dict[str, Union[list, Tensor]]
|
The collated batch of data from collate. |
required |
stage
|
str
|
The stage of training (train, val, test). Defaults to None. |
None
|
loss_only
|
bool
|
If true, only update loss metric. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
dict[str, Union[Tensor, float]]
|
dict[str, Union[Tensor, float]]: The loss and any additional metrics. |
Source code in modelgenerator/tasks/base.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
Examples
modelgenerator.tasks.SequenceRegression
Bases: TaskInterface
Task for fine-tuning a model on a regression task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backbone
|
BackboneCallable
|
The callable that returns a backbone. Defaults to aido_dna_dummy. |
aido_dna_dummy
|
adapter
|
Callable[[int, int], SequenceAdapter]
|
The callable that returns an adapter. Defaults to LinearCLSAdapter. |
LinearCLSAdapter
|
num_outputs
|
int
|
The number of outputs in the regression task. Defaults to 1. |
1
|
optimizer
|
OptimizerCallable
|
The optimizer to use for training. Defaults to torch.optim.AdamW. |
required |
lr_scheduler
|
LRSchedulerCallable
|
The learning rate scheduler to use for training. Defaults to None. |
required |
batch_size
|
int
|
The batch size to use for training. Defaults to None. |
required |
strict_loading
|
bool
|
Whether to strictly load the model. Defaults to True. Set it to False if you want to replace the adapter (e.g. for continue pretraining) |
required |
reset_optimizer_states
|
bool
|
Whether to reset the optimizer states. Defaults to False. Set it to True if you want to replace the adapter (e.g. for continue pretraining). |
required |
Source code in modelgenerator/tasks/tasks.py
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 |
|
configure_model()
Source code in modelgenerator/tasks/tasks.py
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 |
|
forward(collated_batch)
Runs a forward pass of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collated_batch
|
dict[str, Union[list, Tensor]]
|
A collated batch of data containing input_ids and attention_mask. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The regression predictions |
Source code in modelgenerator/tasks/tasks.py
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 |
|
evaluate(preds, collated_batch, stage=None, loss_only=False)
Evaluates the model predictions against the ground truth labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
The model predictions |
required |
collated_batch
|
dict[str, Union[list, Tensor]]
|
The collated batch of data containing labels |
required |
loss_only
|
bool
|
Whether to only return the loss. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
dict[str, Union[Tensor, float]]
|
dict[str, Union[Tensor, float]]: A dictionary of metrics containing loss and mse |
Source code in modelgenerator/tasks/tasks.py
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 |
|
modelgenerator.adapters.SequenceAdapter
Base class only for type hinting purposes. Used for Callable[[int, int] SequenceAdapter] types.
Source code in modelgenerator/adapters/base.py
1 2 3 4 |
|
modelgenerator.adapters.LinearCLSAdapter
Bases: Module
, SequenceAdapter
Simple linear adapter for a 1D embedding
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_features
|
int
|
Number of input features |
required |
out_features
|
int
|
Number of output features |
required |
Source code in modelgenerator/adapters/adapters.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
forward(hidden_states, attention_mask=None)
Forward pass
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
of shape (n, seq_len, in_features) |
required |
attention_mask
|
Tensor
|
of shape (n, seq_len) |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: predictions (n, out_features) |
Source code in modelgenerator/adapters/adapters.py
135 136 137 138 139 140 141 142 143 144 145 146 |
|