finetune/trainer API¶
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()
DNATrainer
¶
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.
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:
trainer = DNATrainer(
model=model,
config=config,
datasets=datasets
)
metrics = trainer.train()
Source code in dnallm/finetune/trainer.py
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 |
|
__init__(model, config, datasets=None, extra_args=None, use_lora=False)
¶
Initialize the DNA trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Any
|
The DNA language model to be trained |
required |
config
|
dict
|
Configuration dictionary containing task and training settings |
required |
datasets
|
Optional[DNADataset]
|
Dataset for training and evaluation |
None
|
extra_args
|
Optional[Dict]
|
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
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 |
|
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:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
A function that computes metrics for the specific task type |
Source code in dnallm/finetune/trainer.py
192 193 194 195 196 197 198 199 200 201 |
|
evaluate()
¶
Evaluate the model on the evaluation dataset.
This method runs evaluation on the configured evaluation dataset and returns task-specific metrics.
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dictionary containing evaluation metrics for the current model state |
Source code in dnallm/finetune/trainer.py
224 225 226 227 228 229 230 231 232 233 234 235 |
|
predict()
¶
Generate predictions on the test dataset.
This method generates predictions on the test dataset if available and returns both predictions and evaluation metrics.
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dictionary containing prediction results and metrics if test dataset exists, |
Dict[str, float]
|
otherwise empty dictionary |
Source code in dnallm/finetune/trainer.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
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
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 |
|
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save_tokenizer
|
bool
|
Whether to save the tokenizer along with the model, default True |
True
|
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dictionary containing training metrics including loss, learning rate, etc. |
Source code in dnallm/finetune/trainer.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|