This is a useful wrapper for checkpointing the Application State. Since this object is compliant with the Stateful protocol, DCP will automatically call state_dict/load_stat_dict as needed in the dcp.save/load APIs. Note: We take advantage of this wrapper to handle calling distributed s
| 313 | f.write(txt) |
| 314 | |
| 315 | class AppState(Stateful): |
| 316 | """This is a useful wrapper for checkpointing the Application State. Since |
| 317 | this object is compliant with the Stateful protocol, DCP will automatically |
| 318 | call state_dict/load_stat_dict as needed in the dcp.save/load APIs. |
| 319 | |
| 320 | Note: We take advantage of this wrapper to handle calling distributed state dict methods on the model |
| 321 | and optimizer. |
| 322 | """ |
| 323 | |
| 324 | def __init__(self, model, optimizer=None): |
| 325 | self.model = model |
| 326 | self.optimizer = optimizer |
| 327 | |
| 328 | def state_dict(self): |
| 329 | # this line automatically manages FSDP FQN's, as well as sets the default state dict type to FSDP.SHARDED_STATE_DICT |
| 330 | model_state_dict, optimizer_state_dict = get_state_dict( |
| 331 | self.model, self.optimizer) |
| 332 | state_dict = { |
| 333 | 'model': model_state_dict, |
| 334 | 'optim': optimizer_state_dict, |
| 335 | } |
| 336 | if self.ema is not None: |
| 337 | ema_state_dict, _ = get_state_dict(self.ema, self.optimizer) |
| 338 | state_dict['ema'] = ema_state_dict |
| 339 | return state_dict |
| 340 | |
| 341 | def load_state_dict(self, state_dict): |
| 342 | # sets our state dicts on the model and optimizer, now that we've loaded |
| 343 | set_state_dict( |
| 344 | self.model, |
| 345 | self.optimizer, |
| 346 | model_state_dict=state_dict['model'], |
| 347 | optim_state_dict=state_dict['optim'], |
| 348 | ) |
| 349 | if self.ema is not None: |
| 350 | set_state_dict( |
| 351 | self.ema, |
| 352 | model_state_dict=state_dict['ema'], |
| 353 | ) |
| 354 | |
| 355 | |
| 356 | class Timer: |
nothing calls this directly
no outgoing calls
no test coverage detected