2025-11-28 14:22:11 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
2026-01-28 22:12:52 +08:00
|
|
|
from vllm_omni.platforms import current_omni_platform
|
2025-11-30 20:39:06 +08:00
|
|
|
|
2025-11-28 14:22:11 +08:00
|
|
|
|
|
|
|
|
def get_local_device() -> torch.device:
|
2025-11-30 20:39:06 +08:00
|
|
|
"""Return the torch device for the current rank based on detected device type."""
|
2026-05-22 10:17:59 +08:00
|
|
|
if current_omni_platform.is_initialized():
|
|
|
|
|
return current_omni_platform.get_torch_device(current_omni_platform.current_device())
|
2026-01-28 22:12:52 +08:00
|
|
|
local_rank = int(os.environ.get("LOCAL_RANK", 0))
|
|
|
|
|
return current_omni_platform.get_torch_device(local_rank)
|
2026-06-25 23:15:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_local_sp_padding_mask(
|
|
|
|
|
batch_size: int,
|
|
|
|
|
local_seq_len: int,
|
|
|
|
|
sp_original_seq_len: int | None,
|
|
|
|
|
sp_padding_size: int,
|
|
|
|
|
sequence_parallel_rank: int,
|
|
|
|
|
device,
|
|
|
|
|
):
|
|
|
|
|
"""Build a per-rank SP padding mask that matches the local shard shape."""
|
|
|
|
|
if sp_original_seq_len is None or sp_padding_size <= 0:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
shard_start = sequence_parallel_rank * local_seq_len
|
|
|
|
|
shard_end = shard_start + local_seq_len
|
|
|
|
|
valid_tokens = max(0, min(sp_original_seq_len, shard_end) - shard_start)
|
|
|
|
|
|
|
|
|
|
if valid_tokens >= local_seq_len:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
token_mask = torch.arange(local_seq_len, device=device) < valid_tokens
|
|
|
|
|
return token_mask.unsqueeze(0).expand(batch_size, -1)
|