@@ -871,46 +871,43 @@ def reorder_sequence(tensor, cp_size: int, seq_dim: int = 1, to_contiguous: bool
871871 if seq_len % (cp_size * 2 ) != 0 :
872872 raise ValueError (f"{ tensor .shape = } is not a multiple of { cp_size * 2 = } " )
873873
874- # [B, S, H, D]: [B, 2*cp_size, S/2*cp_size, H, D] -> [B, 2, S/2*cp_size, H, D]
875- # [S, B, H, D]: [2*cp_size, S/2*cp_size, B, H, D] -> [2, S/2*cp_size, B, H, D]
874+ seq_dim = seq_dim % tensor .ndim
876875 ori_tensor_shape = tensor .shape
876+
877+ # Generic transformation: Isolates the target sequence dimension into `2 * cp_size` discrete chunks.
878+ # Note: The shape walkthrough below uses [b, s, h, d] with seq_dim=1 as an illustrative example,
879+ # but actual dimensions depend on the input tensor (e.g., [s, b, h, d], [b, t, d], etc.):
880+ # [b, s, h, d] -> [b, 2*cp_size, group_size, h, d]
877881 reshaped = tensor .reshape (
878882 * ori_tensor_shape [:seq_dim ],
879883 2 * cp_size ,
880884 group_size ,
881885 * ori_tensor_shape [seq_dim + 1 :],
882886 )
883887
884- if not to_contiguous :
885- # Create first and second halves
886- first_half = jnp .arange (cp_size )
887- second_half = jnp .arange (2 * cp_size - 1 , cp_size - 1 , - 1 )
888-
889- # Stack and reshape to interleave
890- src_indices = jnp .stack ([first_half , second_half ], axis = 1 ).reshape (- 1 )
888+ # Swap target seq_dim with axis 0 to perform slicing/concat easily:
889+ # e.g., [b, 2*cp_size, group_size, h, d] -> [2*cp_size, b, group_size, h, d]
890+ swapped = jnp .swapaxes (reshaped , 0 , seq_dim )
891891
892+ if not to_contiguous :
893+ # Split along axis 0 into halves: each [cp_size, b, group_size, h, d]
894+ first_half , second_half = jnp .split (swapped , 2 , axis = 0 )
895+ second_half_reversed = second_half [::- 1 , ...]
896+ # Stack along axis 1 to interleave: [cp_size, 2, b, group_size, h, d]
897+ stacked = jnp .stack ([first_half , second_half_reversed ], axis = 1 )
898+ # Reshape squashes the [cp_size, 2] pair dimensions back into [2*cp_size]: [2*cp_size, b, group_size, h, d]
899+ permuted = stacked .reshape (2 * cp_size , * swapped .shape [1 :])
892900 else :
893-
894- half = cp_size // 2
895-
896- # Build the 1st and 2nd groups of contiguous‑pair indices:
897- first_pair = [4 * r for r in range (half )] # [0, 4, 8, …]
898- second_pair = [4 * r + 2 for r in range (half )] # [2, 6, 10, …]
899- third_pair = [2 * cp_size - 1 - 4 * r for r in range (half )] # [2*cp_size-1, 2*cp_size-5, …]
900- fourth_pair = [i - 2 for i in third_pair ] # [2*cp_size-3, 2*cp_size-7, …]
901-
902- # Concatenate so each rank’s two indices sit next to each other:
903- # e.g. [0,2, 4,6, …, (2cp‑1),(2cp‑3), …]
904- first_block = first_pair + third_pair
905- second_block = second_pair + fourth_pair
906-
907- # Stack into shape (2*cp_size//2, 2) → then flatten → length=2*cp_size
908- src_indices = jnp .stack ([jnp .array (first_block ), jnp .array (second_block )], axis = 1 ).reshape (- 1 )
909-
910- # One gather and one reshape
911- reordered = jnp .take (reshaped , src_indices , axis = seq_dim )
912-
913- # Reshape back to original dimensions
901+ # Strided slice extracts every other chunk natively: each [cp_size, b, group_size, h, d]
902+ first_half = swapped [0 ::2 , ...]
903+ second_half_reversed = swapped [1 ::2 , ...]
904+ second_half = second_half_reversed [::- 1 , ...]
905+ # Concatenate along axis 0: [2*cp_size, b, group_size, h, d]
906+ permuted = jnp .concatenate ([first_half , second_half ], axis = 0 )
907+
908+ # Swap axis 0 back to seq_dim: e.g., [b, 2*cp_size, group_size, h, d]
909+ reordered = jnp .swapaxes (permuted , 0 , seq_dim )
910+ # Restore original tensor shape: e.g., [b, s, h, d]
914911 return reordered .reshape (ori_tensor_shape )
915912
916913
@@ -1245,5 +1242,9 @@ def maybe_pad(inputs, tile_size):
12451242 padding_amount = 0
12461243 if inputs_dim % tile_size :
12471244 padding_amount = tile_size - inputs_dim % tile_size
1248- inputs = jax .lax .pad (inputs , jnp .array (0.0 , dtype = inputs .dtype ), [(0 , padding_amount , 0 ), (0 , 0 , 0 )])
1245+ inputs = jax .lax .pad (
1246+ inputs ,
1247+ jnp .array (0.0 , dtype = inputs .dtype ),
1248+ [(0 , padding_amount , 0 ), (0 , 0 , 0 )],
1249+ )
12491250 return inputs , padding_amount
0 commit comments