Stop and go
MetricsFn = Callable[[pl.Trainer, pl.LightningModule], Any]
module-attribute
A metrics producing function.
MetricsDict
Bases: TypedDict
Default metrics dict.
Source code in bionemo/testing/harnesses/stop_and_go.py
44 45 46 47 48 |
|
StopAndGoHarness
Bases: ABC
Abstract base class for a stop-and-go harness.
Stop and go tests act as follows
- setup a clean model for a brief training run, select metrics to track.
- interrupt training via the StopAndGoException in the callback InterruptAfterMetadataCallback.
- setup a model to be resumed from the checkpoint, with the same metrics.
- Restore training and check that metadta matches the stored metrics in the callback CheckpointIntegrityCallback.
Useful metrics to check are things like learning rate, global step, validation loss, training loss, and anything else that is important to the training process. If there is an unavailable metrics, a method for fetching the metric should be provided in the bionemo.testing.callbacks module.
Considerations when implementing this class
- devices, pipeline_model_parallel, and tensor_model_parallel may impact the setup of DataModule. Certain datasets expect a known global batch size, which depends on the number of devices and conditional tensor model parallel/ pipeline model parallel settings.
- 'mode' is useful in some cases, but not in all cases. Implement conditions based on these when useful. As an
example, it may be useful to implement a test that stops and resumes with different parallelism settings.
- changing callbacks to test metadata integrity (core feature of stop-and-go tests).
- changing trainer behavior to use multiple GPUs
- changing the model construction to use different hyperparameters.
- ... etc Each of the above tests cases may be useful for automated testing of various expected behavior.
- stop(), go(), and run_test() are provided methods which execute the actual tests, leveraging the conditions in the various setup methods, respecting 'mode' where necessary.
Attributes:
Name | Type | Description |
---|---|---|
root_di |
The root directory. |
|
val_check_interval |
The validation check interval. Stored as an attribute to ensure consistency. |
|
exp_name |
The experiment name. |
|
extra_metrics_dict |
A dictionary of metrics and their corresponding functions. |
See Also: bionemo.testing.callbacks.
Source code in bionemo/testing/harnesses/stop_and_go.py
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 |
|
__init__(root_dir=BIONEMO_CACHE_DIR, val_check_interval=2, exp_name='stop_and_go_harness', extra_metrics_dict=None)
Initializes the StopAndGoHarness object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_dir
|
Path | str
|
The root directory. Defaults to Path("./"). |
BIONEMO_CACHE_DIR
|
val_check_interval
|
int
|
The validation check interval. Defaults to 2. |
2
|
exp_name
|
str
|
The experiment name. Defaults to "stop_and_go_harness". |
'stop_and_go_harness'
|
extra_metrics_dict
|
dict[str, MetricsFn] | None
|
A dictionary that maps keys to 'functions capable of computing metrics in a callback.' Callbacks typically have an interface where both the Trainer and LightningModule are available, meaning any metric that can be computed using these are viable functions to pass in to this dictionary. By default 'global_step' and 'learning_rate' are available. |
None
|
Source code in bionemo/testing/harnesses/stop_and_go.py
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 |
|
get_callbacks(mode)
Returns a list of callbacks based on the specified mode. Base implemention provides reasonable defaults.
To extend this method, call the super and append to the callbacks, depending on which mode you are in:
callbacks = super().get_callbacks(mode, metrics)
callbacks.append(MyCustomCallback())
return callbacks
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Literal['stop', 'go']
|
The mode indicating whether to stop or go. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[Callback]
|
A list of callbacks based on the specified mode. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mode is neither 'stop' nor 'go'. |
Source code in bionemo/testing/harnesses/stop_and_go.py
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 |
|
get_default_metrics_dict()
Returns a dictionary of default metrics that can be used in the StopAndGoHarness.
Returns:
Name | Type | Description |
---|---|---|
dict |
MetricsDict
|
A dictionary of default metrics that can be used in the StopAndGoHarness. |
Source code in bionemo/testing/harnesses/stop_and_go.py
177 178 179 180 181 182 183 |
|
go()
Resumes the model from the checkpoint saved at the end of stop()
and verifies the metadata integrity.
Source code in bionemo/testing/harnesses/stop_and_go.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
run_test()
Executes the stop => go process.
Source code in bionemo/testing/harnesses/stop_and_go.py
289 290 291 292 |
|
setup_model(mode)
abstractmethod
Constructs the model, data, and optimizer for the test harness.
Optionally supports separate code paths for 'stop'/'go', although implementors are encouraged to use the same code path for both.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Literal['stop', 'go']
|
The mode indicating whether to stop or go. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[LightningModule, LightningDataModule, MegatronOptimizerModule]
|
A tuple containing the model, data, and optimizer. |
Source code in bionemo/testing/harnesses/stop_and_go.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
setup_trainer_and_strategy(mode, metrics_getter)
abstractmethod
Constructs the trainer object for the stop and go test.
This method invokes the get_callbacks method to get the appropriate callbacks for the mode and passes it to the trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Literal['stop', 'go']
|
The mode indicating whether to stop or go. |
required |
metrics_getter
|
dict[str, Callable[[Trainer, LightningModule], Any]]
|
A dictionary of functions that computes the metrics. |
required |
Source code in bionemo/testing/harnesses/stop_and_go.py
163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
stop()
Runs pre-training and 'stops' after the first checkpoint is saved.
This method sets up the model, data, and optimizer for the "stop" mode.
It then sets up the trainer and strategy for the "stop" mode with the given metrics.
The training process is executed using the llm.train
function, passing the model, data, trainer, logger, optimizer, and resume options.
If a testing_callbacks.StopAndGoException
is raised during training, it is caught and no action is taken.
Raises:
Type | Description |
---|---|
StopAndGoException
|
If a stop and go exception occurs during training. |
Source code in bionemo/testing/harnesses/stop_and_go.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 |
|
get_global_step(trainer, model)
Returns the global step of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The PyTorch Lightning trainer. |
required |
model
|
LightningModule
|
The PyTorch Lightning model. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The global step of the model. |
Source code in bionemo/testing/harnesses/stop_and_go.py
64 65 66 67 68 69 70 71 72 73 74 |
|
get_learning_rate(trainer, model)
Returns the learning rate of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The PyTorch Lightning trainer. |
required |
model
|
LightningModule
|
The PyTorch Lightning model. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The learning rate of the model. |
Source code in bionemo/testing/harnesses/stop_and_go.py
51 52 53 54 55 56 57 58 59 60 61 |
|