finetune/trainer API¶
dnallm.finetune.trainer ¶
DNA Language Model Trainer Module.
This module implements the training process management for DNA language models, with the following main features:
- DNATrainer Class
- Unified management of model training, evaluation, and prediction processes
- Support for multiple task types ( classification, regression, masked language modeling)
- Integration of task-specific prediction heads
- Training parameter configuration
-
Training process monitoring and model saving
-
Core Features:
- Model initialization and device management
- Training parameter configuration
- Training loop control
- Evaluation metrics calculation
- Model saving and loading
-
Prediction result generation
-
Supported Training Features:
- Automatic evaluation and best model saving
- Training log recording
- Flexible batch size settings
- Learning rate and weight decay configuration
- Distributed training support
- LoRA (Low-Rank Adaptation) for efficient fine-tuning
Usage Example
trainer = DNATrainer(
model=model,
config=config,
datasets=datasets
)
metrics = trainer.train()
Classes¶
DNATrainer ¶
DNATrainer(
model,
config,
datasets=None,
extra_args=None,
use_lora=False,
)
DNA Language Model Trainer that supports multiple model types.
This trainer class provides a unified interface for training, evaluating, and predicting with DNA language models. It supports various task types including classification, regression, and masked language modeling. Early stopping is supported via the callbacks configuration in TrainingConfig. QLoRA (4-bit quantized LoRA) is supported via use_qlora in TrainingConfig.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
The DNA language model to be trained |
|
task_config |
Configuration for the specific task |
|
train_config |
Configuration for training parameters |
|
datasets |
Dataset for training and evaluation |
|
extra_args |
Additional training arguments |
|
trainer |
HuggingFace Trainer instance |
|
training_args |
Training arguments configuration |
|
data_split |
Available dataset splits |
Examples:
Standard LoRA training:
trainer = DNATrainer(
model=model,
config=config,
datasets=datasets,
use_lora=True,
)
metrics = trainer.train()
QLoRA training (4-bit quantization):
# Model must be loaded with quantization_config before passing to trainer
model, tokenizer = load_model_and_tokenizer(
model_name,
task_config=task_config,
quantization_config={
"load_in_4bit": True,
"bnb_4bit_compute_dtype": "float16",
"bnb_4bit_use_double_quant": True,
"bnb_4bit_quant_type": "nf4",
},
)
trainer = DNATrainer(
model=model,
config=config,
datasets=datasets,
use_lora=True,
)
metrics = trainer.train()
Plotting
After training, generate visualization plots:
trainer.plot_history(output_dir="./plots")
Initialize the DNA trainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Any
|
The DNA language model to be trained config: Configuration dictionary containing task and training settings |
required |
datasets
|
DNADataset | None
|
Dataset for training and evaluation |
None
|
extra_args
|
dict | None
|
Additional training arguments to override defaults |
None
|
use_lora
|
bool
|
Whether to use LoRA for efficient fine-tuning |
False
|
Source code in dnallm/finetune/trainer.py
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 | |
Methods:¶
compute_task_metrics ¶
compute_task_metrics()
Compute task-specific evaluation metrics.
This method returns a callable function that computes appropriate metrics for the specific task type (classification, regression, etc.).
Returns:
Callable: A function that computes metrics for the specific task type
Source code in dnallm/finetune/trainer.py
318 319 320 321 322 323 324 325 326 327 328 | |
customize_trainer ¶
customize_trainer(trainer_cls)
Customize the HuggingFace Trainer instance.
This method allows users to replace the default Trainer instance with a custom one, enabling advanced customization of the training process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer_cls
|
Trainer
|
A custom HuggingFace Trainer instance to replace the default one |
required |
Source code in dnallm/finetune/trainer.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
evaluate ¶
evaluate()
Evaluate the model on the evaluation dataset.
This method runs evaluation on the configured evaluation dataset and returns task-specific metrics.
Returns:
Dictionary containing evaluation metrics for the current model state
Source code in dnallm/finetune/trainer.py
471 472 473 474 475 476 477 478 479 480 481 482 | |
infer ¶
infer()
Generate inference results on the test dataset.
This method generates inference results on the test dataset if available and returns both predictions and evaluation metrics.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary containing inference results and |
metrics if test dataset exists,
otherwise empty dictionary
Source code in dnallm/finetune/trainer.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
plot_history ¶
plot_history(output_dir=None, plot_loss=True, plot_lr=True)
Generate training visualization plots from trainer state.
This is a convenience method that delegates to the standalone plotting utilities in dnallm.utils.training_plots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | None
|
Directory to save plots. Defaults to training output_dir. |
None
|
plot_loss
|
bool
|
Whether to generate loss curve plot. |
True
|
plot_lr
|
bool
|
Whether to generate learning rate schedule plot. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Path]
|
Dictionary mapping plot names to saved file paths. |
Source code in dnallm/finetune/trainer.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
search ¶
search(save_tokenizer=True)
Run hyperparameter search using Optuna backend.
This method runs multiple training trials with different hyperparameters and returns the best run configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_tokenizer
|
bool
|
Whether to save the tokenizer with the best model. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Source code in dnallm/finetune/trainer.py
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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
set_up_trainer ¶
set_up_trainer()
Set up the HuggingFace Trainer with appropriate configurations.
This method configures the training environment by: 1. Setting up training arguments from configuration 2. Configuring dataset splits (train/eval/test) 3. Setting up task-specific metrics computation 4. Configuring appropriate data collator for different task types 5. Initializing the HuggingFace Trainer instance
The method automatically handles: - Dataset split detection and validation - Task-specific data collator selection - Evaluation strategy configuration - Metrics computation setup
Source code in dnallm/finetune/trainer.py
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 | |
train ¶
train(save_tokenizer=True)
Train the model and return training metrics.
This method executes the training process using the configured HuggingFace Trainer, automatically saving the best model and optionally the tokenizer.
Args:
save_tokenizer: Whether to save the tokenizer along with the model,
default True
Returns:
Dictionary containing training metrics including loss, learning
rate, etc.
Source code in dnallm/finetune/trainer.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |