Prhub

#46868 [Loader] Improve InstantTensor loading

原始 PR 作者 mgoin 合并时间 2026-07-18 04:30 文件变更 9 提交数 3 评论 1 代码增减 +27 / -14

执行摘要

改进 InstantTensor 加载:显示吞吐量并移除冗余克隆

保持 InstantTensor 作为可选加载方式(load_format='instanttensor'),但吸收 #37309 的优点:进度条报告加载吞吐量,升级依赖并利用 copy=True 避免冗余克隆,提升加载体验和内存效率。具体引用 PR body:'Keeps InstantTensor opt-in but takes the good parts from #37309: the progress bar now reports load throughput in GB/s. We also bump to instanttensor>=0.1.9 and use copy=True so tensors own their memory, dropping the redundant vLLM-side .clone().'

建议合并。该 PR 是轻量级改进,提升了 InstantTensor 加载器的可用性和可观测性,同时简化了代码。值得关注的设计决策是使用 copy=True 来让底层库管理内存所有权,这避免了 vLLM 侧的冗余拷贝,是一个良好的协作模式。

讨论亮点

该 PR 没有实质性代码讨论。唯一的评论来自 mergify 机器人提示存在合并冲突,已通过合并 main 解决。reviewer tlrmchlsmth 批准了 PR。

实现拆解

  1. 修改 vllm/model_executor/model_loader/weight_utils.py 中的 instanttensor_weights_iterator 函数:添加 copy=True 参数,使用 f.total_tensor_size 设置进度条总量,并在遍历张量时动态更新已加载字节数,从而显示吞吐量。
  2. setup.py 中将 instanttensor 可选依赖从 0.1.5 提升到 0.1.9,并在多个 requirements 文件中同步更新版本(cuda.in, rocm.in, cuda.txt, rocm.txt, cpu.txt, nightly-torch.txt)。
  3. 更新错误提示信息,引导用户使用 pip install vllm[instanttensor] 安装,而非直接 pip install instanttensor
  4. 在测试文件 test_weight_utils.py 中移除一条关于需要立即拷贝张量的注释,因为 copy=True 已经保证了所有权。
    这些改动均为向后兼容,仅影响已启用 instanttensor 加载的用户。
文件 模块 状态 重要度
vllm/model_executor/model_loader/weight_utils.py 加载器 modified 6.71
setup.py 安装配置 modified 4.09
tests/model_executor/model_loader/instanttensor_loader/test_weight_utils.py 加载测试 modified 3.06
requirements/test/cuda.in 测试依赖 modified 2.85

关键符号

instanttensor_weights_iterator

关键源码片段

vllm/model_executor/model_loader/weight_utils.py core-logic

核心修改:添加 copy=True,改进进度条显示吞吐量

def instanttensor_weights_iterator(
    hf_weights_files: list[str],
    use_tqdm_on_load: bool,
) -> Generator[tuple[str, torch.Tensor], None, None]:
    """Iterate over the weights in the model safetensor files
    using instanttensor library."""
    try:
        import instanttensor
    except ImportError as e:
        raise ImportError(
            "Please install instanttensor via `pip install vllm[instanttensor]`"
        ) from e
​
    if not current_platform.is_cuda():
        raise ValueError("InstantTensor requires NVIDIA GPUs")
​
    try:
        world_group = get_world_group()
    except AssertionError:
        # 单元测试时 world group 未初始化
        process_group = None
    else:
        process_group = world_group.device_group if world_group.world_size > 1 else None
​
    device = current_platform.current_device()
​
    # copy=True 使 yield 出的张量拥有独立内存,在 safe_open 上下文退出后仍然有效
    with instanttensor.safe_open(
        hf_weights_files,
        framework="pt",
        device=device,
        process_group=process_group,
        copy=True,
    ) as f:
        # 以字节为单位跟踪加载进度,用以显示吞吐量(GB/s)
        pbar = tqdm(
            total=f.total_tensor_size,
            desc="Loading safetensors using InstantTensor loader",
            disable=not enable_tqdm(use_tqdm_on_load),
            bar_format=_BAR_FORMAT,
            position=tqdm._get_free_pos(),
            unit="B",
            unit_scale=True,
            unit_divisor=1024,
            mininterval=1.0,
        )
        try:
            for name, tensor in f.tensors():
                pbar.update(tensor.numel() * tensor.element_size())
                yield name, tensor
        finally:
            pbar.close()

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险较低。核心变更仅影响可选的 InstantTensor 加载路径;升级 instanttensor 依赖到 0.1.9 可能引入未知兼容性问题,但由于该库专注于 safetensors 加载且接口稳定,风险可控。使用 copy=True 可避免张量在上下文退出后失效,反而提升了安全性。进度条显示的修改不影响加载逻辑的正确性。

用户影响:使用 load_format='instanttensor' 的用户会看到更详细的进度条(显示吞吐量),且不再需要手动 .clone() 来保留张量。系统影响:无。团队影响:无。影响程度低。

仅影响可选加载路径 依赖版本升级

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论