# SPDX-License-Identifier: Apache-2.0
"""Debug-only PLE row dump for byte-level transport checks (off unless VLLM_PLE_ROW_DUMP=
).
Each process appends framed records to /-pid.bin:
b"PLER" | kind u8 (0 = GPU worker: rows its forward consumed; 1 = offload process: rows it published)
| rank u8 | reserved u16 | seq u32 | n u32 | row_bytes u32 | ids int64[n] | rows uint8[n * row_bytes]
"""
import os
import struct
import threading
import torch
_DIR = os.environ.get("VLLM_PLE_ROW_DUMP", "")
_LOCK = threading.Lock()
_FILES: dict = {}
MAGIC = b"PLER"
def enabled() -> bool:
return bool(_DIR)
def write(tag: str, kind: int, rank: int, seq: int, ids: torch.Tensor, rows: torch.Tensor) -> None:
ids_cpu = ids.detach().to(device="cpu", dtype=torch.int64).contiguous()
rows_cpu = rows.detach().to(device="cpu").contiguous()
n = int(ids_cpu.shape[0])
if rows_cpu.shape[0] != n:
raise RuntimeError(f"PLE row dump: {rows_cpu.shape[0]} rows for {n} ids")
raw = rows_cpu.view(torch.uint8).numpy().tobytes() if n else b""
row_bytes = len(raw) // n if n else 0
record = (MAGIC + struct.pack("