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

Method load_moe_state_dict

deepspeed/runtime/engine.py:2414–2467  ·  view source on GitHub ↗
(checkpoint_path,
                            tag,
                            state_dict,
                            old_moe_load,
                            model=None,
                            mpu=None,
                            num_experts=1,
                            checkpoint_engine=TorchCheckpointEngine())

Source from the content-addressed store, hash-verified

2412
2413 @staticmethod
2414 def load_moe_state_dict(checkpoint_path,
2415 tag,
2416 state_dict,
2417 old_moe_load,
2418 model=None,
2419 mpu=None,
2420 num_experts=1,
2421 checkpoint_engine=TorchCheckpointEngine()):
2422 if old_moe_load:
2423 expp_rank = groups._get_expert_data_parallel_rank(groups._get_max_expert_size_name())
2424
2425 num_local_experts = max(num_experts) // groups._get_expert_parallel_world_size(
2426 groups._get_max_expert_size_name())
2427 for local_expert_id in range(num_local_experts):
2428 global_expert_id = expp_rank * num_local_experts + local_expert_id
2429 expert_state_dict = checkpoint_engine.load(
2430 DeepSpeedEngine._get_expert_ckpt_name(
2431 checkpoint_path,
2432 -1, # -1 means ignore layer_id
2433 global_expert_id,
2434 tag,
2435 mpu),
2436 map_location=torch.device('cpu'))
2437
2438 # Updating global -> local expert ids
2439 moe_str_prefix = '.deepspeed_moe.experts.deepspeed_experts.'
2440 for key in list(expert_state_dict.keys()):
2441 local_key = key.replace(f'{moe_str_prefix}{global_expert_id}',
2442 f'{moe_str_prefix}{local_expert_id}')
2443 expert_state_dict[local_key] = expert_state_dict.pop(key)
2444 state_dict.update(expert_state_dict)
2445
2446 else:
2447 moe_layer_id = 0
2448 for n_module, module in model.named_modules():
2449 if isinstance(module, MoE): # and deepspeed.comm.get_rank() == 0:
2450 group_name = module.expert_group_name
2451 num_local_experts = module.num_local_experts
2452 expp_rank = groups._get_expert_parallel_rank(group_name)
2453 # loop all local_experts
2454 for local_expert_id in range(num_local_experts):
2455 global_expert_id = expp_rank * num_local_experts + local_expert_id
2456 expert_state_dict = checkpoint_engine.load(DeepSpeedEngine._get_expert_ckpt_name(
2457 checkpoint_path, moe_layer_id, global_expert_id, tag, mpu),
2458 map_location=torch.device('cpu'))
2459 # print(expert_state_dict.keys())
2460 # Updating global -> local expert ids
2461 moe_str_prefix = '.deepspeed_moe.experts.deepspeed_experts.'
2462 for key in list(expert_state_dict.keys()):
2463 local_key = key.replace(f'{moe_str_prefix}{global_expert_id}',
2464 f'{moe_str_prefix}{local_expert_id}')
2465 expert_state_dict[local_key] = expert_state_dict.pop(key)
2466 state_dict.update(expert_state_dict)
2467 moe_layer_id += 1
2468
2469 def load_module_state_dict(self, checkpoint, strict=True, custom_load_fn=None):
2470 module_state_dict = checkpoint['module']

Callers 2

_load_checkpointMethod · 0.80
_load_checkpointMethod · 0.80

Calls 5

_get_expert_ckpt_nameMethod · 0.80
loadMethod · 0.45
deviceMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected