MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / save_checkpoint

Function save_checkpoint

SwissArmyTransformer/sat/training/model_io.py:162–195  ·  view source on GitHub ↗

Save a model checkpoint.

(iteration, model, optimizer,
                    lr_scheduler, args)

Source from the content-addressed store, hash-verified

160 allgather_bucket_size=optimizer.allgather_bucket_size)
161
162def save_checkpoint(iteration, model, optimizer,
163 lr_scheduler, args):
164 """Save a model checkpoint."""
165 if hasattr(args, 'deepspeed') and args.deepspeed:
166 if mpu.get_data_parallel_rank() == 0:
167 print_rank0('Saving Model...')
168 save_ds_checkpoint(iteration, model, lr_scheduler, args)
169 if optimizer is not None and optimizer.optimizer.__class__.__name__ == "FusedEmaAdam" :
170 update_ema_parameters_to_model(optimizer)
171 if mpu.get_data_parallel_rank() == 0:
172 print_rank0('Saving Ema Model...')
173 save_ds_checkpoint(iteration, model, lr_scheduler, args, True)
174 restore_ema_parameters_back(optimizer)
175
176 elif args.mode == 'inference':
177 os.makedirs(os.path.join(args.save, str(iteration)), exist_ok=True)
178 if torch.distributed.get_rank() < args.model_parallel_size:
179 torch.save({'module': model.state_dict()}, os.path.join(args.save, str(iteration), 'mp_rank_{:02d}_model_states.pt'.format(torch.distributed.get_rank())))
180 else:
181 raise ValueError("training without deepspeed is not supported.")
182 # Wait so everyone is done (necessary)
183 torch.distributed.barrier()
184 # And update the latest iteration
185 if torch.distributed.get_rank() == 0:
186 tracker_filename = get_checkpoint_tracker_filename(args.save)
187 with open(tracker_filename, 'w') as f:
188 f.write(str(iteration))
189 # save model_config.json for from_pretrained().
190 with open(os.path.join(args.save, 'model_config.json'), 'w') as f:
191 to_dump = extract_model_specific_args_to_dump(args, model)
192 json.dump(to_dump, f, indent=4)
193
194 # Wait so everyone is done (not necessary)
195 torch.distributed.barrier()
196
197
198def save_ds_checkpoint(iteration, model, lr_scheduler, args, use_ema = False):

Callers 14

test_save_and_loadFunction · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
transform_param.pyFile · 0.90
save_on_exitFunction · 0.85

Calls 8

print_rank0Function · 0.90
save_ds_checkpointFunction · 0.85
saveMethod · 0.80
state_dictMethod · 0.45

Tested by 1

test_save_and_loadFunction · 0.72