[Bug] TorchDist.allreduce / tp_allreduce convert Python int vs float scalars to int64 vs float32 on CPU fallback, crashing or hanging on mixed scalar types

Aperta
#23,030 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@DaDerekkk ci sta già lavorando.

Dal 3/12/2026.

  • #23323 di @DaDerekkk — aperta
  • #23329 di @DhineshPonnarasan — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Read tensorrt_llm/_torch/distributed/communicator.py around TorchDist.allreduce and TorchDist.tp_allreduce, then check how _cuda_allreduce_scalar handles scalar dtypes. Reproduce the mixed int and float case with the two-rank Gloo example in the issue. Done means both methods handle mixed scalar types without rank failures; add the two-rank Gloo tests described in the issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Pytorch
Summary

In tensorrt_llm/_torch/distributed/communicator.py (commit 1aa0192e62d219599930f125ada7067081fb44b7, present at HEAD), TorchDist.allreduce and TorchDist.tp_allreduce support obj: int | float | torch.Tensor:

# tensorrt_llm/_torch/distributed/communicator.py:1657-1696
@log_op
def allreduce(
    self,
    obj: int | float | torch.Tensor,
    op: ReduceOp = ReduceOp.SUM,
):
    is_base_type = isinstance(obj, int) or isinstance(obj, float)
    if is_base_type:
        vote_pg = self._cuda_obj_pg("world")
        if op in self._CUDA_SCALAR_REDUCE_OPS and vote_pg is not None:
            return self._cuda_allreduce_scalar(obj, op, vote_pg)
        obj = torch.tensor(obj)

    dist.all_reduce(obj, op=reduce_op_to_torch(op))

    if is_base_type:
        obj = obj.item()

    return obj

While _cuda_allreduce_scalar explicitly promotes scalars to dtype=torch.float64 (line 1650), the CPU/Gloo fallback path (vote_pg is None or op not in _CUDA_SCALAR_REDUCE_OPS) calls obj = torch.tensor(obj) without specifying a unified dtype.

In PyTorch:

  • torch.tensor(int) produces torch.int64 (8 bytes)
  • torch.tensor(float) produces torch.float32 (4 bytes)

If one rank passes an int and another passes a float (for example in KV-cache sizing or step counters where one rank holds a literal/config int and another holds a computed float), dist.all_reduce(obj, ...) executes with mismatched tensor dtypes and element byte sizes across ranks. On Gloo, this triggers a fatal C++ gloo::EnforceNotMet (op.nread == op.preamble.nbytes) SIGABRT on the sender/receiver and Timed out waiting ... for recv operation to complete on peer ranks.

Affected Location
  • Repository: NVIDIA/TensorRT-LLM (1aa0192e62d219599930f125ada7067081fb44b7, present at HEAD)
  • File & lines: tensorrt_llm/_torch/distributed/communicator.py:1657-1696 (TorchDist.allreduce, TorchDist.tp_allreduce)
Minimal Standalone Reproduction
import datetime, os, socket
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from tensorrt_llm._torch.distributed.communicator import TorchDist

def free_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("127.0.0.1", 0))
        return s.getsockname()[1]

def worker(rank, port):
    dist.init_process_group(
        "gloo", init_method=f"tcp://127.0.0.1:{port}", rank=rank, world_size=2,
        timeout=datetime.timedelta(seconds=3),
    )
    td = TorchDist.__new__(TorchDist)
    td.mapping = None
    td._cuda_obj_pg = lambda dim: None
    val = 1024 if rank == 0 else 512.0  # int (int64, 8B) vs float (float32, 4B)
    print(rank, td.allreduce(val, op=dist.ReduceOp.MIN))
    dist.destroy_process_group()

if __name__ == "__main__":
    port = free_port()
    mp.spawn(worker, args=(port,), nprocs=2, join=True)

Observed behavior (2-process Gloo execution against allreduce):

  • Rank 0 aborts with SIGABRT (exitcode=-6): gloo::EnforceNotMet: [enforce fail at .../gloo/transport/uv/pair.cc:253] op.nread == op.preamble.nbytes.
  • Rank 1 raises RuntimeError: ... Timed out waiting 3000ms for recv operation to complete.
Suggested Fix
  1. In both TorchDist.allreduce and TorchDist.tp_allreduce, normalize scalar inputs to a common floating-point dtype before dist.all_reduce, matching _cuda_allreduce_scalar:
    if is_base_type:
        ...
        obj = torch.tensor(obj, dtype=torch.float64)
    
    (Or preserve int only if all callers are guaranteed integer-only, or convert via float(obj) / dtype=torch.float64 consistently with line 1650.)
  2. Add a 2-rank Gloo unit test exercising allreduce and tp_allreduce when rank 0 passes int and rank 1 passes float.
Lingua principale
Python
Stelle
15.2k
Fork
3k
Merge medio
2g 21h
PR unite (30g)
477

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NVIDIA/TensorRT-LLM

Tutte le issue di NVIDIA/TensorRT-LLM

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.