@@ -2532,11 +2532,13 @@ def hf_to_megatron(
25322532 d_tot_ssm = config .mamba_state_dim * config .mamba_num_groups
25332533
25342534 # Define component indices in the concatenated tensor
2535- z_shard_idx = torch .arange (d_inner )
2536- x_shard_idx = torch .arange (d_inner , 2 * d_inner )
2537- B_shard_idx = torch .arange (2 * d_inner , 2 * d_inner + d_tot_ssm )
2538- C_shard_idx = torch .arange (2 * d_inner + d_tot_ssm , 2 * d_inner + 2 * d_tot_ssm )
2539- dt_shard_idx = torch .arange (2 * (d_inner + d_tot_ssm ), 2 * (d_inner + d_tot_ssm ) + config .mamba_num_heads )
2535+ z_shard_idx = torch .arange (d_inner , device = hf_weights .device )
2536+ x_shard_idx = torch .arange (d_inner , 2 * d_inner , device = hf_weights .device )
2537+ B_shard_idx = torch .arange (2 * d_inner , 2 * d_inner + d_tot_ssm , device = hf_weights .device )
2538+ C_shard_idx = torch .arange (2 * d_inner + d_tot_ssm , 2 * d_inner + 2 * d_tot_ssm , device = hf_weights .device )
2539+ dt_shard_idx = torch .arange (
2540+ 2 * (d_inner + d_tot_ssm ), 2 * (d_inner + d_tot_ssm ) + config .mamba_num_heads , device = hf_weights .device
2541+ )
25402542
25412543 # Reshape for tensor parallel distribution
25422544 target_shape = (self .tp_size , - 1 , config .hidden_size )
@@ -2582,11 +2584,15 @@ def megatron_to_hf(
25822584 n_heads_local = config .mamba_num_heads // self .tp_size
25832585
25842586 # Extract local components
2585- z_shard_idx = torch .arange (d_inner_local )
2586- x_shard_idx = torch .arange (d_inner_local ) + d_inner_local
2587- B_shard_idx = torch .arange (d_tot_ssm_local ) + 2 * d_inner_local
2588- C_shard_idx = torch .arange (d_tot_ssm_local ) + 2 * d_inner_local + d_tot_ssm_local
2589- dt_shard_idx = torch .arange (n_heads_local ) + 2 * (d_inner_local + d_tot_ssm_local )
2587+ z_shard_idx = torch .arange (d_inner_local , device = megatron_weights .device )
2588+ x_shard_idx = torch .arange (d_inner_local , device = megatron_weights .device ) + d_inner_local
2589+ B_shard_idx = torch .arange (d_tot_ssm_local , device = megatron_weights .device ) + 2 * d_inner_local
2590+ C_shard_idx = (
2591+ torch .arange (d_tot_ssm_local , device = megatron_weights .device ) + 2 * d_inner_local + d_tot_ssm_local
2592+ )
2593+ dt_shard_idx = torch .arange (n_heads_local , device = megatron_weights .device ) + 2 * (
2594+ d_inner_local + d_tot_ssm_local
2595+ )
25902596
25912597 local_components = [
25922598 megatron_weights [z_shard_idx ],
@@ -2625,7 +2631,9 @@ def __init__(self, megatron_param: str, hf_param: str):
26252631 self ._tp_mapping = ColumnParallelMapping (megatron_param , megatron_param )
26262632
26272633 @abstractmethod
2628- def get_shard_idx (self , config : TransformerConfig , local_tp : bool ) -> List [int ]:
2634+ def get_shard_idx (
2635+ self , config : TransformerConfig , local_tp : bool , device : torch .device | None = None
2636+ ) -> List [int ]:
26292637 """Get shard indices for the given config."""
26302638 ...
26312639
@@ -2645,7 +2653,7 @@ def hf_to_megatron(
26452653 assert "bias" in self .megatron_param , "Only bias and weight are supported for conv1d"
26462654 target_shape = (self .tp_size , - 1 )
26472655
2648- shard_idx = self .get_shard_idx (config , local_tp = False )
2656+ shard_idx = self .get_shard_idx (config , local_tp = False , device = hf_weights . device )
26492657
26502658 # Extract and reshape components
26512659 sharded_weights = [hf_weights [idx ].reshape (target_shape ) for idx in shard_idx ]
@@ -2680,7 +2688,7 @@ def megatron_to_hf(
26802688 config = remove_non_pickleables (config , max_depth = 3 )
26812689 config = self .broadcast_obj_from_pp_rank (config , cache_key = "config" )
26822690
2683- shard_idx = self .get_shard_idx (config , local_tp = True )
2691+ shard_idx = self .get_shard_idx (config , local_tp = True , device = megatron_weights . device )
26842692
26852693 local_components = [megatron_weights [idx ] for idx in shard_idx ]
26862694
@@ -2704,17 +2712,19 @@ class GDNConv1dMapping(ChunkedMapping):
27042712 tensor-parallel distributed format for GDN SSM layers.
27052713 """
27062714
2707- def get_shard_idx (self , config : TransformerConfig , local_tp : bool ) -> List [int ]:
2715+ def get_shard_idx (
2716+ self , config : TransformerConfig , local_tp : bool , device : torch .device | None = None
2717+ ) -> List [int ]:
27082718 """Get shard indices for the given config."""
27092719 qk_dim = config .linear_key_head_dim * config .linear_num_key_heads
27102720 v_dim = config .linear_value_head_dim * config .linear_num_value_heads
27112721 if local_tp :
27122722 qk_dim = qk_dim // self .tp_size
27132723 v_dim = v_dim // self .tp_size
27142724
2715- q_shard_idx = torch .arange (qk_dim )
2716- k_shard_idx = torch .arange (qk_dim ) + qk_dim
2717- v_shard_idx = torch .arange (v_dim ) + qk_dim * 2
2725+ q_shard_idx = torch .arange (qk_dim , device = device )
2726+ k_shard_idx = torch .arange (qk_dim , device = device ) + qk_dim
2727+ v_shard_idx = torch .arange (v_dim , device = device ) + qk_dim * 2
27182728
27192729 return [q_shard_idx , k_shard_idx , v_shard_idx ]
27202730
@@ -2726,7 +2736,9 @@ class MambaConv1dMapping(ChunkedMapping):
27262736 tensor-parallel distributed format for Mamba SSM layers.
27272737 """
27282738
2729- def get_shard_idx (self , config : TransformerConfig , local_tp : bool ) -> List [int ]:
2739+ def get_shard_idx (
2740+ self , config : TransformerConfig , local_tp : bool , device : torch .device | None = None
2741+ ) -> List [int ]:
27302742 """Get shard indices for the given config."""
27312743 d_inner = config .mamba_num_heads * config .mamba_head_dim
27322744 d_tot_ssm = config .mamba_state_dim * config .mamba_num_groups
@@ -2735,9 +2747,9 @@ def get_shard_idx(self, config: TransformerConfig, local_tp: bool) -> List[int]:
27352747 d_tot_ssm = d_tot_ssm // self .tp_size
27362748
27372749 # Extract local components
2738- x_shard_idx = torch .arange (d_inner )
2739- B_shard_idx = torch .arange (d_tot_ssm ) + d_inner
2740- C_shard_idx = torch .arange (d_tot_ssm ) + d_inner + d_tot_ssm
2750+ x_shard_idx = torch .arange (d_inner , device = device )
2751+ B_shard_idx = torch .arange (d_tot_ssm , device = device ) + d_inner
2752+ C_shard_idx = torch .arange (d_tot_ssm , device = device ) + d_inner + d_tot_ssm
27412753
27422754 return [x_shard_idx , B_shard_idx , C_shard_idx ]
27432755
0 commit comments