MCPcopy Create free account
hub / github.com/AIS-SNU/Smart-Infinity / _load_checkpoint

Method _load_checkpoint

deepspeed/inference/engine.py:449–506  ·  view source on GitHub ↗
(self, load_dir, load_module_strict=True, tag=None)

Source from the content-addressed store, hash-verified

447 return ckpt_name
448
449 def _load_checkpoint(self, load_dir, load_module_strict=True, tag=None):
450 is_pipe_parallel = isinstance(self.module, PipelineModule)
451 if is_pipe_parallel:
452 raise RuntimeError('pipeline parallelism is currently not supported in inference.')
453 if not isinstance(load_dir, dict) and os.path.isdir(load_dir):
454 if tag is None:
455 latest_path = os.path.join(load_dir, "latest")
456 if os.path.isfile(latest_path):
457 with open(latest_path, "r") as fd:
458 tag = fd.read().strip()
459
460 ckpt_list = self._get_all_ckpt_names(load_dir, tag)
461 sd_loader = SDLoaderFactory.get_sd_loader(ckpt_list, self.checkpoint_engine)
462 else:
463 sd_loader = SDLoaderFactory.get_sd_loader_json(load_dir, self.checkpoint_engine)
464
465 checkpoint = sd_loader['checkpoints']
466
467 if type(checkpoint) is list:
468 self.sd = torch.load(checkpoint[0], map_location='cpu')
469 self.key_list = list(self.sd.keys())
470
471 self.load_model_with_checkpoint(self.module)
472
473 for i in range(1, len(checkpoint)):
474 if not dist.is_initialized() or dist.get_rank() == 0:
475 print(f"loading checkpoint ({i})")
476 self.sd = torch.load(checkpoint[i], map_location=get_accelerator().device_name())
477 self.key_list = list(self.sd.keys())
478 self.load_model_with_checkpoint(self.module)
479 else:
480 mp_rank = 0 if self.mpu is None else self.mpu.get_model_parallel_rank()
481
482 load_path, checkpoint, quantize_config = sd_loader.load(self._config.tensor_parallel.tp_size,
483 mp_rank,
484 is_pipe_parallel=is_pipe_parallel,
485 quantize=(self._config.dtype is torch.int8),
486 quantize_groups=self.quantize_groups,
487 mlp_extra_grouping=self.mlp_extra_grouping)
488
489 self.quantization_scales, self.quantize_merge_count = quantize_config
490
491 moe, _ = has_moe_layers(self.module)
492 if moe:
493 from deepspeed.runtime.engine import DeepSpeedEngine
494 old_moe_load = False
495 if not isinstance(checkpoint['num_experts'], list):
496 old_moe_load = True
497 DeepSpeedEngine.load_moe_state_dict(load_dir,
498 tag,
499 state_dict=checkpoint[self._choose_module_key(checkpoint)],
500 old_moe_load=old_moe_load,
501 model=self.module,
502 mpu=self.mpu,
503 checkpoint_engine=self.checkpoint_engine)
504
505 self.module.load_state_dict(state_dict=checkpoint[self._choose_module_key(checkpoint)],
506 strict=load_module_strict)

Callers

nothing calls this directly

Calls 15

_get_all_ckpt_namesMethod · 0.95
_choose_module_keyMethod · 0.95
get_acceleratorFunction · 0.90
has_moe_layersFunction · 0.85
readMethod · 0.80
get_sd_loaderMethod · 0.80
get_sd_loader_jsonMethod · 0.80
load_moe_state_dictMethod · 0.80
loadMethod · 0.45
is_initializedMethod · 0.45

Tested by

no test coverage detected