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
26 27 28 29 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 |
|
configure_model()
Configures the model for training and interence. Subclasses must implement this method.
Source code in modelgenerator/tasks/base.py
82 83 84 |
|
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
104 105 106 107 108 109 110 111 112 113 |
|
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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
Examples
modelgenerator.tasks.SequenceRegression
Bases: TaskInterface
Task for fine-tuning a model on single-/multi-task regression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backbone
|
BackboneCallable
|
The callable that returns a backbone. |
required |
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
|
loss_func
|
Callable
|
Loss function for regression tasks. Defaults to nn.MSELoss. |
MSELoss
|
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
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 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
|
configure_model()
Source code in modelgenerator/tasks/tasks.py
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 |
|
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
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 |
|
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
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 |
|
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
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
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
228 229 230 231 232 233 234 235 236 237 238 239 |
|