-
Notifications
You must be signed in to change notification settings - Fork 905
[megatron] Support qwen3-vl/qwen3-omni cp #5952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -64,42 +64,24 @@ def get_packed_seq_params(position_ids: torch.Tensor) -> PackedSeqParams: | |||||||||||
qkv_format='thd') | ||||||||||||
|
||||||||||||
|
||||||||||||
def _split_tokens(tokens, cu_seqlens): | ||||||||||||
assert tokens.shape[-2] == 1, f'tokens.shape: {tokens.shape}' # [..., 1, L] | ||||||||||||
new_tokens = [] | ||||||||||||
def split_cp_inputs(inputs: torch.Tensor, cu_seqlens: torch.Tensor, dim: int): | ||||||||||||
if dim < 0: | ||||||||||||
dim = (dim + inputs.ndim) % inputs.ndim | ||||||||||||
new_inputs = [] | ||||||||||||
cp_size = mpu.get_context_parallel_world_size() | ||||||||||||
cp_rank = mpu.get_context_parallel_rank() | ||||||||||||
for i in range(cu_seqlens.shape[0] - 1): | ||||||||||||
val = tokens[..., cu_seqlens[i]:cu_seqlens[i + 1]] | ||||||||||||
val = val.view( | ||||||||||||
*tokens.shape[:-1], | ||||||||||||
2 * cp_size, | ||||||||||||
val.shape[-1] // (2 * cp_size), | ||||||||||||
) | ||||||||||||
slices = [slice(None)] * inputs.ndim | ||||||||||||
slices[dim] = slice(cu_seqlens[i], cu_seqlens[i + 1]) | ||||||||||||
val = inputs[slices] | ||||||||||||
view_shape = (*inputs.shape[:dim], 2 * cp_size, val.shape[dim] // (2 * cp_size), *inputs.shape[dim + 1:]) | ||||||||||||
val = val.view(view_shape) | ||||||||||||
index = torch.tensor([cp_rank, (2 * cp_size - cp_rank - 1)], device='cpu', | ||||||||||||
pin_memory=True).cuda(non_blocking=True) | ||||||||||||
val = val.index_select(-2, index) | ||||||||||||
new_tokens.append(val.view(*tokens.shape[:-1], -1)) | ||||||||||||
return torch.cat(new_tokens, dim=-1) | ||||||||||||
|
||||||||||||
|
||||||||||||
def _split_tokens_decoder_input(tokens, cu_seqlens): | ||||||||||||
assert tokens.shape[1] == 1, f'tokens.shape: {tokens.shape}' # [L, 1, E] | ||||||||||||
new_tokens = [] | ||||||||||||
cp_size = mpu.get_context_parallel_world_size() | ||||||||||||
cp_rank = mpu.get_context_parallel_rank() | ||||||||||||
for i in range(cu_seqlens.shape[0] - 1): | ||||||||||||
val = tokens[cu_seqlens[i]:cu_seqlens[i + 1], ...] | ||||||||||||
val = val.view( | ||||||||||||
2 * cp_size, | ||||||||||||
val.shape[0] // (2 * cp_size), | ||||||||||||
*tokens.shape[1:], | ||||||||||||
) | ||||||||||||
index = torch.tensor([cp_rank, (2 * cp_size - cp_rank - 1)], device='cpu', | ||||||||||||
pin_memory=True).cuda(non_blocking=True) | ||||||||||||
val = val.index_select(0, index) | ||||||||||||
new_tokens.append(val.view(-1, *tokens.shape[1:])) | ||||||||||||
return torch.cat(new_tokens, dim=0) | ||||||||||||
val = val.index_select(dim, index) | ||||||||||||
view_shape = (*inputs.shape[:dim], -1, *inputs.shape[dim + 1:]) | ||||||||||||
new_inputs.append(val.view(view_shape)) | ||||||||||||
return torch.cat(new_inputs, dim=dim) | ||||||||||||
|
||||||||||||
|
||||||||||||
def get_batch_on_this_cp_rank(batch: Dict[str, Any]): | ||||||||||||
|
@@ -130,10 +112,7 @@ def get_batch_on_this_cp_rank(batch: Dict[str, Any]): | |||||||||||
if args.task_type == 'seq_cls' and key == 'labels': | ||||||||||||
continue | ||||||||||||
if val is not None: | ||||||||||||
if key == 'decoder_input': | ||||||||||||
batch[key] = _split_tokens_decoder_input(val, packed_seq_params.cu_seqlens_q) | ||||||||||||
else: | ||||||||||||
batch[key] = _split_tokens(val, packed_seq_params.cu_seqlens_q) | ||||||||||||
batch[key] = split_cp_inputs(val, packed_seq_params.cu_seqlens_q, -1) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This simplification introduces a potential issue. The previous code had special handling for While To fix this, please either restore the special handling for
Suggested change
|
||||||||||||
|
||||||||||||
return batch | ||||||||||||
|
||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better code organization and to avoid potential circular import issues, it's recommended to move imports to the top of the file. Please move
from ...trainers.utils import split_cp_inputs
to the top-level imports.