MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / save_state_dict

Method save_state_dict

deepspeed/runtime/pipe/module.py:602–638  ·  view source on GitHub ↗
(self, save_dir, checkpoint_engine, exclude_frozen_params=False)

Source from the content-addressed store, hash-verified

600 return ckpt_files
601
602 def save_state_dict(self, save_dir, checkpoint_engine, exclude_frozen_params=False):
603 # TODO: Need to validate interaction of checkpoint_parallel_write_pipeline and fastwriter
604
605 # Processes having the same model parallel rank on different data parallel instances
606 # have identical layer weights. We can distribute the task of saving the layer weights
607 # among the data parallel ranks. For example, if a pipeline stage has 9 layers and
608 # if there are 2 data parallel instances, rank 0 will save the first 5 layers and
609 # rank 1 will save the last 4.
610 dp_rank = self._grid.data_parallel_id
611 dp_size = self._grid.data_parallel_size
612 num_layers = len(self.forward_funcs)
613 if self.checkpoint_parallel_write_pipeline:
614 # spread layers evenly across data parallel ranks
615 offsets = ds_utils.partition_uniform(num_layers, dp_size)
616 start, end = offsets[dp_rank], offsets[dp_rank + 1]
617 else:
618 # data parallel rank 0 writes all layers
619 if dp_rank != 0:
620 return
621 start, end = 0, num_layers
622 layer_list = self.forward_funcs[start:end]
623
624 checkpoint_engine.makedirs(save_dir, exist_ok=True)
625 should_clone = checkpoint_engine.preserves_storage_sharing()
626 for idx, layer in enumerate(layer_list):
627 model_ckpt_path = self.ckpt_layer_path(save_dir, start + idx)
628 if not hasattr(layer, 'state_dict'):
629 continue
630
631 orig_state_dict = layer.state_dict()
632 if exclude_frozen_params:
633 for n in self._get_frozen_parameter_names(layer):
634 del orig_state_dict[n]
635 final_state_dict = orig_state_dict
636 if should_clone:
637 final_state_dict = clone_tensors_for_torch_save(orig_state_dict)
638 checkpoint_engine.save(state_dict=final_state_dict, path=model_ckpt_path)
639
640 def load_state_dir(self, load_dir, checkpoint_engine, strict=True):
641 for idx, layer in enumerate(self.forward_funcs):

Callers 2

module_state_dictMethod · 0.45

Calls 7

ckpt_layer_pathMethod · 0.95
makedirsMethod · 0.80
state_dictMethod · 0.45
saveMethod · 0.45

Tested by 1