[Bug] TorchDist.recv allocates a temporary tensor instead of writing into the caller's NumPy buffer in-place

Đang mở
#23,031 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@bojiang-li đang làm issue này rồi.

Từ ngày 29/11/2026.

  • #23032 của @bojiang-li — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
numpy, python, pytorch
Lĩnh vực
distributed-systems

Hướng nghiên cứu

Read tensorrt_llm/_torch/distributed/communicator.py around TorchDist.recv and check any tests covering NumPy buffer receives. The issue includes a two process reproduction using Gloo; run it to confirm whether the caller's buffer changes. Done means received data is visible in the supplied buffer, with any relevant receive tests passing; a linked pull request is already open for this issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Pytorch
Summary

In tensorrt_llm/_torch/distributed/communicator.py (commit 1aa0192e62d219599930f125ada7067081fb44b7, present at HEAD), TorchDist.recv is annotated # in-place recv numpy buffer, yet allocates a disconnected temporary tensor with torch.empty_like(torch.from_numpy(buf)) instead of receiving into torch.from_numpy(buf):

# tensorrt_llm/_torch/distributed/communicator.py:1577-1593
@log_op
def isend(self, buf: np.ndarray, dest, tag=0):
    # non-blocking send numpy buffer
    tensor = torch.from_numpy(buf)
    return dist.isend(tensor, dst=dest, tag=tag)

@log_op
def recv(self, buf: np.ndarray, src, tag=0):
    # in-place recv numpy buffer
    tensor = torch.empty_like(torch.from_numpy(buf))
    dist.recv(tensor, src=src, tag=tag)
    return tensor.numpy()

Because torch.empty_like(...) allocates a fresh tensor that does not share memory with buf, dist.recv(tensor, ...) writes received bytes into the temporary tensor and leaves the caller's buf untouched (e.g., all zeros). Any caller that discards the return value and expects buf to be populated in-place (matching # in-place recv numpy buffer and MPI Recv(buf) semantics) silently operates on stale/zeroed data without error.

Affected Location
  • Repository: NVIDIA/TensorRT-LLM (1aa0192e62d219599930f125ada7067081fb44b7, present at HEAD)
  • File & lines: tensorrt_llm/_torch/distributed/communicator.py:1587-1593 (TorchDist.recv)
Minimal Standalone Reproduction
import datetime, socket
import numpy as np
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=5),
    )
    td = TorchDist.__new__(TorchDist)
    td.mapping = None
    if rank == 0:
        td.isend(np.array([11, 22, 33, 44], dtype=np.int64), dest=1, tag=7).wait()
    else:
        recv_buf = np.zeros(4, dtype=np.int64)
        ret = td.recv(recv_buf, src=0, tag=7)
        print("caller recv_buf:", recv_buf.tolist(), "returned:", ret.tolist())
        assert np.array_equal(recv_buf, ret), f"Expected in-place update, got {recv_buf}"
    dist.destroy_process_group()

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

Observed output:

caller recv_buf: [0, 0, 0, 0] returned: [11, 22, 33, 44]
AssertionError: Expected in-place update, got [0 0 0 0]
Suggested Fix

Receive directly into the backing storage of buf via torch.from_numpy(buf) (if contiguous, or copy back into buf if non-contiguous):

@log_op
def recv(self, buf: np.ndarray, src, tag=0):
    # in-place recv numpy buffer
    tensor = torch.from_numpy(buf)
    dist.recv(tensor, src=src, tag=tag)
    return buf

(If buf may be non-contiguous where torch.from_numpy or dist.recv requires contiguous storage, copy tensor.numpy() back into buf[:] before returning buf.)

Ngôn ngữ chính
Python
Star
15.2k
Fork
3k
Merge trung bình
2 ngày 21 giờ
Pull request đã merge (30 ngày)
477

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của NVIDIA/TensorRT-LLM

Tất cả issue của NVIDIA/TensorRT-LLM

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.