Stop and go
StopAndGoHarness
Bases: ABC
Abstract base class for testing consistency between interrupted and continuous training.
Users should override cls.setup_model and update cls.setup_class to customize the downstream test cases. Metadata are collected through callbacks and users can add new unit tests by comparing the metadata for the interrupted and continuous cases.
By default, learning rate, global step, optimizer state, consumed samples, input and output tensors, and loss are
compared. Users can add additional metrics by adding new callbacks to cls.callbacks
and associated test functions.
Stop and go tests act as follows
- setup a clean model for a brief training run, set callbacks to track.
- interrupt training via the StopAndGoException in the callback Raise.
- train the model resumed from the checkpoint with the same set of callbacks.
- train the model continuously without interruption with a new set of the same callbacks.
- compare each pair of interrupted and continuous callbacks to check for equality.
Considerations when implementing this class
- The derived test name should start with
Test
, and test methods should start withtest_
to enable pytest discovery. - 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. By default, we are testing only on single device without parallelism.
- '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.
- changing callbacks to test metadata integrity (core feature of stop-and-go tests).
- 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(), resume(), continuous() or collectively run_stop_and_go() 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_dir |
The root directory. |
|
val_check_interval |
int
|
The validation check interval. Stored as an attribute to ensure consistency. |
exp_name |
str
|
The experiment name. |
extra_metrics_dict |
str
|
A dictionary of metrics and their corresponding functions. |
See Also: bionemo.testing.callbacks.
Source code in bionemo/testing/harnesses/stop_and_go.py
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
|
continuous()
classmethod
Trains the model in one continuous path without stopping.
Source code in bionemo/testing/harnesses/stop_and_go.py
300 301 302 303 304 305 306 307 308 |
|
get_default_callbacks()
classmethod
Returns a list of callbacks based on the specified mode. Base implementation 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()
callbacks[mode]["MyCustomCallback"] = MyCustomCallback()
return callbacks
Returns:
Type | Description |
---|---|
CallbackDict
|
A dictionary of callbacks based on the specified mode, each of which maps a callback name to a callback |
CallbackDict
|
object. |
Source code in bionemo/testing/harnesses/stop_and_go.py
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 |
|
resume()
classmethod
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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
run_stop_and_go()
classmethod
Executes training both continuously and with a checkpoint interruption.
Source code in bionemo/testing/harnesses/stop_and_go.py
310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
setup_class()
classmethod
Sets up the class by creating a temporary directory, metadata_dir, exp_name and callbacks.
Source code in bionemo/testing/harnesses/stop_and_go.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
setup_model(mode)
abstractmethod
classmethod
Constructs the model, data, and optimizer for the test harness.
Optionally supports separate code paths for 'stop'/'resume'/'continuous', although implementors are encouraged to use the same code path for both.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Mode
|
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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
setup_trainer(mode)
classmethod
Setup trainer by passing stop, resume, or continuous callbacks according to mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Mode
|
The mode indicating whether to stop, resume, or train continuously. |
required |
Returns:
Type | Description |
---|---|
Trainer
|
NeMo Lightning trainer object. |
Source code in bionemo/testing/harnesses/stop_and_go.py
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 |
|
stop()
classmethod
Runs pre-training and 'stops' after the first checkpoint is saved.
This method sets up the model, data, and optimizer for the Mode.STOP mode.
It then sets up the trainer and strategy for the Mode.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
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 |
|
teardown_class()
classmethod
Tears down the class by cleaning up the temporary directory.
Source code in bionemo/testing/harnesses/stop_and_go.py
138 139 140 141 |
|
test_stop_and_go_consistency(callback_type)
Tests the consistency of the callback data between the interrupted and continuous checks.
Source code in bionemo/testing/harnesses/stop_and_go.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
test_train_val_init_consumed_samples()
Tests the initial consumed samples in stop-and-go scenario.
Source code in bionemo/testing/harnesses/stop_and_go.py
357 358 359 360 361 362 363 364 365 366 367 368 369 |
|
get_callback(callbacks, mode, callback_type)
Returns the callback with the given name and mode.
Convenience function to make type hinting easier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callbacks
|
CallbackDict
|
The dictionary of callbacks. |
required |
mode
|
Mode
|
The mode indicating whether to stop or go. |
required |
callback_type
|
Type[Callback]
|
The type of the callback. |
required |
Returns:
Type | Description |
---|---|
Callback
|
pl.Callback: The callback with the given name and mode. |
Source code in bionemo/testing/harnesses/stop_and_go.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|