news 2026/4/23 13:35:27

【vLLM 学习】Rlhf Colocate

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【vLLM 学习】Rlhf Colocate

vLLM 是一款专为大语言模型推理加速而设计的框架,实现了 KV 缓存内存几乎零浪费,解决了内存管理瓶颈问题。

更多 vLLM 中文文档及教程可访问 →vllm.hyper.ai/

*在线运行 vLLM 入门教程:零基础分步指南

源码examples/offline_inference/rlhf_colocate.py

# SPDX-License-Identifier: Apache-2.0 """ 一个简单的演示,展示如何将 vLLM 工作进程与训练执行器(training actors) 协同部署在同一 GPU上,适用于类 RLHF 应用。 关键要点: - 通过正确设置 VLLM_RAY_PER_WORKER_GPUS 和 VLLM_RAY_BUNDLE_INDICES, 使用 Ray 控制 vLLM 工作进程的部署位置 - 使用 CUDA-IPC 传递张量,因为在同一 GPU 上存在多个进程时 NCCL 无法正常工作 """ import os import ray import torch from ray.util.placement_group import placement_group from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy from vllm import LLM class MyLLM(LLM): def __init__(self, *args, bundle_indices: list, **kwargs): # 临时方案使脚本能运行 # 阻止Ray在顶层操作CUDA_VISIBLE_DEVICES os.environ.pop("CUDA_VISIBLE_DEVICES", None) # 每个工作进程将使用 0.4 个 GPU,这样我们可以在同一 GPU 上调度 2 个实例 os.environ["VLLM_RAY_PER_WORKER_GPUS"] = "0.4" os.environ["VLLM_RAY_BUNDLE_INDICES"] = ",".join( map(str, bundle_indices)) print(f"creating LLM with bundle_indices={bundle_indices}") super().__init__(*args, **kwargs) class RayTrainingActor: def __init__(self): # ray 将 CUDA_VISIBLE_DEVICES 设置为分配的 GPU from transformers import AutoModelForCausalLM self.model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m") self.model.to("cuda:0") for name, p in self.model.named_parameters(): p.data.zero_() torch.cuda.synchronize() # get_device_uuid 的参数是 # 可见设备中 GPU 的索引 from vllm.platforms import current_platform self.device_uuid = current_platform.get_device_uuid(0) def report_device_id(self) -> str: return self.device_uuid def get_weight_ipc_handles(self): from torch.multiprocessing.reductions import reduce_tensor data = {} for name, p in self.model.named_parameters(): # 训练执行器(training actor)可能只拥有部分权重, # 需要从所有执行器进行 all-gather 操作获取完整权重。 # 出于演示目的,此处我们假设所有训练执行器都拥有完整权重。 data[name] = reduce_tensor(p.detach()) return {self.device_uuid: data} # ray 管理4 GPU os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3" ray.init() # 我们需要将 vLLM 实例和训练执行器(training actor)协同部署在同一组 GPU 上 # 具体部署方案如下: # GPU 0 和 1:训练执行器 0、1 和 vLLM 实例 0(TP=2) # GPU 2 和 3:训练执行器 2、3 和 vLLM 实例 1(TP=2) pg = placement_group([{"GPU": 1, "CPU": 0}] * 4) ray.get(pg.ready()) print(f"placement group has bundles {pg.bundle_specs=}") training_actors = [] training_actor_device_ids = [] inference_engines = [] inference_engine_device_ids = [] for bundle_index in [0, 1, 2, 3]: training_actor = ray.remote( num_cpus=0, num_gpus=0.4, scheduling_strategy=PlacementGroupSchedulingStrategy( placement_group=pg, placement_group_capture_child_tasks=True, placement_group_bundle_index=bundle_index, ), )(RayTrainingActor).remote() training_actors.append(training_actor) for bundle_index, training_actor in enumerate(training_actors): device_id = ray.get(training_actor.report_device_id.remote()) print(f"training actor {bundle_index} is on {device_id}") training_actor_device_ids.append(device_id) for (i, bundle_indices) in enumerate([[0, 1], [2, 3]]): # and cause unexpected behaviors. # 重要:创建 vLLM 实例时,我们需要 # 确保目标 GPU 上没有 GPU 活动, # 否则,它们将干扰 vLLM 内存分析, # 并引起意外的行为。 llm = ray.remote( num_cpus=0, num_gpus=0, scheduling_strategy=PlacementGroupSchedulingStrategy( placement_group=pg, placement_group_capture_child_tasks=True, ), )(MyLLM).remote( model="facebook/opt-125m", enforce_eager=True, worker_extension_cls="rlhf_utils.ColocateWorkerExtension", tensor_parallel_size=2, distributed_executor_backend="ray", gpu_memory_utilization=0.4, bundle_indices=bundle_indices, ) inference_engines.append(llm) # don't call any method on the inference engine here, # otherwise it will block until the vLLM instance is created. # 在此处的推理引擎上不要调用任何方法, # 否则,它将锁定直到创建 vLLM 实例。 for i, llm in enumerate(inference_engines): inference_engine_device_ids.append( ray.get(llm.collective_rpc.remote("report_device_id", args=tuple()))) print(f"inference engine {i} is on {inference_engine_device_ids[-1]}") # 检查部署情况 # 前两个训练执行器(training actors)应当 # 与第一个推理引擎(inference engine)部署在同一GPU上 assert training_actor_device_ids[:2] == inference_engine_device_ids[0] # 最后两个训练执行器(training actors)应当 # 与第二个推理引擎(inference engine)部署在同一GPU上 assert training_actor_device_ids[2:] == inference_engine_device_ids[1] print("gather all the IPC handles from the training actors") ipc_handles = {} for actor in training_actors: ipc_handles.update(ray.get(actor.get_weight_ipc_handles.remote())) print("update the weights of the inference engines") for llm in inference_engines: ray.get( llm.collective_rpc.remote("update_weights_from_ipc_handles", args=(ipc_handles, ))) print("check if the weights are updated") for llm in inference_engines: assert ray.get( llm.collective_rpc.remote("check_weights_changed", args=tuple()))
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:25:17

对比传统开发:QWEN CLI如何提升10倍AI项目效率

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 编写一个效率对比测试脚本,分别使用:1) 传统手工方式;2) QWEN CLI工具完成相同的AI模型开发任务。记录各阶段耗时(环境配置、数据处理、训练调参…

作者头像 李华
网站建设 2026/4/23 12:24:13

用Redis快速构建实时在线人数统计

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 构建一个实时在线用户统计系统原型,要求:1) 使用Redis HyperLogLog统计UV 2) Bitmap记录活跃用户 3) 可视化仪表盘 4) 自动过期机制。系统应能在5分钟内完成…

作者头像 李华
网站建设 2026/4/23 12:25:57

5分钟玩转AI艺术:印象派工坊一键生成4种画风

5分钟玩转AI艺术:印象派工坊一键生成4种画风 关键词:OpenCV、非真实感渲染、图像风格迁移、素描生成、油画滤镜、水彩效果、彩铅画算法、WebUI画廊 摘要:本文将深入解析基于 OpenCV 计算摄影学算法构建的「AI 印象派艺术工坊」镜像技术原理。…

作者头像 李华
网站建设 2026/4/23 12:25:32

AnimeGANv2本地部署安全吗?数据隐私保护实战解析

AnimeGANv2本地部署安全吗?数据隐私保护实战解析 1. 引言:AI二次元转换的技术热潮与隐私隐忧 近年来,AI驱动的图像风格迁移技术迅速普及,其中AnimeGANv2因其出色的动漫化效果成为热门模型之一。它能够将普通照片转化为具有宫崎骏…

作者头像 李华
网站建设 2026/4/23 12:24:45

TOMCAT安装效率提升300%的秘籍

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 请开发一个TOMCAT安装效率对比工具,功能包括:1.传统手动安装步骤记录器 2.自动化安装脚本生成器 3.安装耗时统计对比模块 4.配置一致性校验工具 5.生成可视…

作者头像 李华
网站建设 2026/4/23 10:19:39

零基础5分钟创建PPK登录页面

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个极简的PPK登录页面教学demo,要求:1.分步骤指导 2.每个操作都有可视化示例 3.最终生成可运行的登录页面代码 4.包含点击查看效果按钮 5.提供常见问题…

作者头像 李华