MCPcopy Index your code
hub / github.com/zai-org/CodeGeeX / load_checkpoint

Function load_checkpoint

codegeex/megatron/checkpointing.py:301–505  ·  view source on GitHub ↗

Load a model checkpoint and return the iteration. strict (bool): whether to strictly enforce that the keys in :attr:`state_dict` of the checkpoint match the names of parameters and buffers in model.

(
    model, 
    optimizer, 
    lr_scheduler, 
    load_arg="load", 
    strict=True,
)

Source from the content-addressed store, hash-verified

299
300
301def load_checkpoint(
302 model,
303 optimizer,
304 lr_scheduler,
305 load_arg="load",
306 strict=True,
307):
308 """Load a model checkpoint and return the iteration.
309 strict (bool): whether to strictly enforce that the keys in
310 :attr:`state_dict` of the checkpoint match the names of
311 parameters and buffers in model.
312 """
313 args = get_args()
314 load_dir = getattr(args, load_arg)
315
316 if args.deepspeed:
317 loaded_dir, state_dict = model[0].load_checkpoint(load_dir)
318 if loaded_dir is None:
319 print_rank_0(
320 "WARNING: could not find the metadata file {} ".format(load_dir)
321 )
322 print_rank_0(
323 " will not load any checkpoints and will start from " "random"
324 )
325 return 0
326 release = False
327 else:
328 model = utils.unwrap_model(model)
329
330 if load_dir.endswith(".pt"):
331 checkpoint_name = load_dir
332 release = True
333 else:
334 # Read the tracker file and set the iteration.
335 tracker_filename = get_checkpoint_tracker_filename(load_dir)
336
337 # If no tracker file, return iretation zero.
338 if not os.path.isfile(tracker_filename):
339 print_rank_0(
340 "WARNING: could not find the metadata file {} ".format(tracker_filename)
341 )
342 iteration = 0
343 release = True
344 checkpoint_name = get_checkpoint_name(load_dir, iteration, release)
345 if not os.path.isfile(checkpoint_name):
346 print_rank_0(
347 " will not load any checkpoints and will start from random"
348 )
349 return 0
350 else:
351 # Otherwise, read the tracker file and either set the iteration or
352 # mark it as a release checkpoint.
353 iteration = 0
354 release = False
355 with open(tracker_filename, "r") as f:
356 metastring = f.read().strip()
357 try:
358 iteration = int(metastring)

Callers 15

mainFunction · 0.90
run_trainFunction · 0.85
restore_checkpointFunction · 0.85
load_modelFunction · 0.85
transform_opt_shardFunction · 0.85
load_modelFunction · 0.85
run_trainFunction · 0.85
load_modelFunction · 0.85
transform_model_parallelFunction · 0.85
load_modelFunction · 0.85

Calls 13

get_argsFunction · 0.90
print_rank_0Function · 0.90
update_num_microbatchesFunction · 0.90
get_checkpoint_nameFunction · 0.85
set_checkpoint_versionFunction · 0.85
check_checkpoint_argsFunction · 0.85
get_checkpoint_versionFunction · 0.85
readMethod · 0.80
set_statesMethod · 0.80
getMethod · 0.45

Tested by 1

mainFunction · 0.72