fix: preserve Torch seeded sampling on ROCm (#8341)
Signed-off-by: andyluo7 <andy.luo@amd.com>
This commit is contained in:
1 parent
c150600896
commit
09d704907f
2 files changed
+136
-5
No files matched your search
@@ -12,6 +12,7 @@ every value and every generator state afterwards matches the per-request loop.
|
||||
"""
|
||||
|
||||
import torch
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
|
||||
|
||||
@@ -73,10 +74,18 @@ def torch_exponential_policy(numel: int, device: torch.device) -> tuple[int, int
|
||||
def fill_exponential_rows(out: torch.Tensor, generators: list, rows: list[int] | None = None) -> torch.Tensor:
|
||||
"""Draw ``out[rows[i]].exponential_(generator=generators[i])`` for all ``i`` in one launch.
|
||||
|
||||
``out`` is a contiguous float32 CUDA matrix; ``rows`` defaults to
|
||||
``range(len(generators))``. ``None`` generators use the default CUDA
|
||||
generator in order. Each generator is advanced exactly as torch would.
|
||||
``out`` is a contiguous float32 matrix. Without ``rows``, each generator
|
||||
owns an equal contiguous slice of the flattened output (which may span
|
||||
several codebook rows). ``None`` generators use the default generator in
|
||||
order. NVIDIA uses the batched CUDA kernel; other platforms retain
|
||||
ordered Torch draws, including direct Higgs Audio callers.
|
||||
"""
|
||||
if not current_platform.is_cuda():
|
||||
targets = out.reshape(len(generators), -1) if rows is None else [out[row] for row in rows]
|
||||
for target, generator in zip(targets, generators):
|
||||
target.exponential_(generator=generator)
|
||||
return out
|
||||
|
||||
numel = int(out.shape[-1]) if rows is not None else int(out.numel()) // len(generators)
|
||||
device = out.device
|
||||
threads, increment = torch_exponential_policy(numel, device)
|
||||
@@ -109,9 +118,15 @@ def fill_exponential_rows(out: torch.Tensor, generators: list, rows: list[int] |
|
||||
|
||||
|
||||
def batched_seeded_exponential_supported(q: torch.Tensor, generators: dict) -> bool:
|
||||
"""Whether ``fill_exponential_rows`` reproduces the per-row loop for ``q``."""
|
||||
"""Whether the NVIDIA CUDA kernel reproduces the per-row loop for ``q``.
|
||||
|
||||
ROCm tensors also report ``is_cuda``, but HIP uses a different distribution
|
||||
implementation and cannot compile the CUDA libdevice transform above.
|
||||
Keep those draws on Torch's original per-row path, including RNG state.
|
||||
"""
|
||||
return (
|
||||
len(generators) > 1
|
||||
current_platform.is_cuda()
|
||||
and len(generators) > 1
|
||||
and q.is_cuda
|
||||
and q.dtype == torch.float32
|
||||
and q.dim() == 2
|
||||
|
||||
Reference in new issue
Block a user