LMDeploy 奖励模型(Reward Model)推理指南:离线 Pipeline 与在线 /pooling API 实战
发布时间:2026/9/27 21:23:05来源:尧图网络
人工智能大模型模型推理服务推理引擎本地部署模型量化【免费下载链接】lmdeployLMDeploy is a toolkit for compressing, deploying, and serving LLMs.项目地址https://gitcode.com/gh_mirrors/lm/lmdeploy点击查看免费下载LMDeploy 原生支持 Reward Model奖励模型的推理与部署可用于 RLHF/RL 训练中的偏好打分、数据筛选与对话质量评估等场景。本文以internlm/internlm2-1_8b-reward为例完整讲解支持矩阵、离线 Pipeline 打分流程、在线/poolingAPI 用法及底层实现原理帮助你在当前项目中快速落地奖励模型服务。支持的奖励模型LMDeploy 当前支持以下奖励模型其模型结构与打分方式各不相同模型参数量推理引擎Qwen2.5-Math-RM72BPyTorchInternLM2-Reward1.8B / 7B / 20BPyTorchPOLAR1.8B / 7BPyTorch几点重要事实以当前仓库源码为准所有奖励模型均由PyTorch 后端支撑不支持 TurboMind 引擎从引擎侧校验逻辑看真正被get_reward_score//pooling入口认可的实现类为InternLM2ForRewardModel与Qwen2ForRewardModel见 async_engine.py 与 pipeline.py。Qwen2.5-Math-RM 通过Qwen2ForRewardModel加载InternLM2-Reward / POLAR 通过InternLM2ForRewardModel加载POLAR 的权重基座与 InternLM2 同构因此可复用同一实现类两类实现类分别注册在 module_map.py供模型架构名自动路由。离线推理通过 Pipeline 获取奖励分数离线场景下Pipeline 提供get_reward_score(input_ids)接口输入 token id 序列返回list[float]奖励分数。官方示例以internlm/internlm2-1_8b-reward为例原文示例已完整保留from transformers import AutoTokenizer from lmdeploy import pipeline, PytorchEngineConfig model_path internlm/internlm2-1_8b-reward chat [ {role: system, content: Please reason step by step, and put your final answer within \\boxed{}.}, {role: user, content: Janet’s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers market?}, {role: assistant, content: To determine how much Janet makes from selling the duck eggs at the farmers market, we need to follow these steps:\n\n1. Calculate the total number of eggs laid by the ducks each day.\n2. Determine how many eggs Janet eats and bakes for herself each day.\n3. Find out how many eggs are left to be sold.\n4. Calculate the revenue from selling the remaining eggs at $2 per egg.\n\nLets start with the first step:\n\n1. Janets ducks lay 16 eggs per day.\n\nNext, we calculate how many eggs Janet eats and bakes for herself each day:\n\n2. Janet eats 3 eggs for breakfast every morning.\n3. Janet bakes 4 eggs for her friends every day.\n\nSo, the total number of eggs Janet eats and bakes for herself each day is:\n\\[ 3 4 7 \\text{ eggs} \\]\n\nNow, we find out how many eggs are left to be sold:\n\\[ 16 - 7 9 \\text{ eggs} \\]\n\nFinally, we calculate the revenue from selling the remaining eggs at $2 per egg:\n\\[ 9 \\times 2 18 \\text{ dollars} \\]\n\nTherefore, Janet makes 18 dollars every day at the farmers market.} ] tokenizer AutoTokenizer.from_pretrained(model_path, trust_remote_codeTrue) conversation_str tokenizer.apply_chat_template( chat, tokenizeFalse, add_generation_promptFalse ) input_ids tokenizer.encode( conversation_str, add_special_tokensFalse ) if __name__ __main__: engine_config PytorchEngineConfig(tptp) with pipeline(model_path, backend_configengine_config) as pipe: score pipe.get_reward_score(input_ids) print(fscore: {score})关键步骤拆解组装对话按system / user / assistant轮次构造对话列表其中assistant的回复即待打分内容如上面的数学推理题解套用 Chat Template调用tokenizer.apply_chat_template(..., add_generation_promptFalse)生成完整对话字符串。注意此处不加生成提示符因为打分对象是完整对话而非生成任务编码为 token idadd_special_tokensFalse避免重复插入特殊 token创建 Pipeline 并打分pipeline(model_path, backend_configengine_config)以上下文管理器方式运行engine_config中的tp张量并行度可按显存与模型规模调整get_reward_score接受“单条 token 列表、token 列表的列表或 token tensor”返回始终为list[float]单条输入时长度为 1。底层打分原理从 pipeline.py 可以看到完整调用链先校验当前引擎架构是否在[InternLM2ForRewardModel, Qwen2ForRewardModel]白名单内否则直接抛出 ValueError将输入规整为「list of token-id list」的批次形式调用async_get_logits拿到每个序列的 logits然后squeeze后取每条序列最后一个 token 位置的 logit 值作为奖励分数。对于 InternLM2 系奖励模型internlm2_reward.py其结构为「InternLM2 主干 v_headhidden_size → 1的无 bias 线性层」get_logits直接输出v_head(hidden_states)即标量 logit。对于 Qwen2 系qwen2_reward.py结构为「Qwen2 主干 两层 MLP score 头hidden_size → hidden_sizeReLUhidden_size → 1」。另外两个实现细节值得注意推理时get_logits分支使用top_k1的生成配置以避免 PyTorch 引擎在采样阶段对奖励模型崩溃见 async_engine.py奖励模型只支持纯文本InternLM2ForRewardModel.prepare_inputs_for_generation会显式拒绝多模态vision_embeddings输入见 internlm2_reward.py。在线推理部署 API 服务并通过 /pooling 打分启动服务lmdeploy serve api_server internlm/internlm2-1_8b-reward --backend pytorch--backend pytorch必须显式指定奖励模型仅由 PyTorch 引擎支持服务默认监听0.0.0.0:23333如需调整端口/并发等参数可追加--server-port、--max-concurrent-requests等 CLI 选项。调用 /pooling 接口curl http://0.0.0.0:23333/pooling \ -H Content-Type: application/json \ -d { model: internlm/internlm2-1_8b-reward, input: Who are you? }返回体结构由 protocol.py 的PoolingResponse定义{ id: pool-xxxxxxxx, object: list, created: 1700000000, model: internlm/internlm2-1_8b-reward, data: [{index: 0, object: pooling, data: 0.123}], usage: {prompt_tokens: 3, completion_tokens: 0, cached_tokens: 0} }其中data[i].data即第i条输入的奖励分数。请求入参支持格式/pooling请求体遵循PoolingRequest见 protocol.pyinput字段支持四种形态服务端在 auxiliary.py 中统一归一化为批次的 token id 列表input类型示例归一化结果strWho are you?用tokenizer.encode编码为单条 token 列表list[str][Who are you?, Hi]逐条编码得到批次的 token 列表list[int][812, 1268, ...]视为单条 token 列表list[list[int]][[812, ...], [102, ...]]直接作为批次归一化后由async_engine.async_get_reward_score(input_ids)批量打分见 async_engine.pyusage.prompt_tokens统计全部 prompt token 总数。若input为空或包含非法元素类型如list[dict]接口会返回 400 错误。/pooling路由注册于 router.py并随 OpenAI 兼容服务一起挂载在 api_server.py。奖励模型打分常见问题引擎选择务必使用--backend pytorchTurboMind 不支持奖励模型推理。架构不在白名单若加载的模型不属于InternLM2ForRewardModel/Qwen2ForRewardModel如普通对话模型get_reward_score与/pooling会抛出ValueError: ... is not in reward model list需确认模型确实是受支持的奖励模型。输入格式离线接口要求input_ids为 int 列表或列表的列表在线接口还需注意空列表会直接报 400。单条输入的返回离线get_reward_score即使传入单条 token 列表返回值也是长度 1 的list[float]不要误读为裸浮点数。多模态输入奖励模型仅支持文本传入视觉 embedding 会被显式拒绝。进一步探索中英文对照文档docs/zh_cn/supported_models/reward_models.md奖励模型实现internlm2_reward.py、qwen2_reward.py打分 API 实现pipeline.py、auxiliary.py相关模型注册module_map.py按本文步骤你即可用 LMDeploy 对 InternLM2-Reward / Qwen2.5-Math-RM / POLAR 等奖励模型完成离线与在线打分并将其无缝嵌入 RLHF 数据管线或质量评估流程。赞分享人工智能大模型模型推理服务推理引擎本地部署模型量化【免费下载链接】lmdeployLMDeploy is a toolkit for compressing, deploying, and serving LLMs.项目地址https://gitcode.com/gh_mirrors/lm/lmdeploy点击查看免费下载相关推荐InternLM/lmdeploy 大语言模型离线推理 Pipeline 使用指南InternLM/lmdeploy 大语言模型离线推理 Pipeline 使用指南 前言 在现代人工智能应用中大语言模型 LLM 的推理部署是一个关键环节。I人工智能大模型模型推理服务推理引擎本地部署模型量化vLLM Pooling Models 完全指南非生成式模型Embedding / 分类 / 评分 / Reward的任务体系、配置解析与离线在线 API 实战vLLM Pooling Models 完全指南非生成式模型Embedding / 分类 / 评分 / Reward的任务体系、配置解析与离线在线 API人工智能大模型模型推理服务推理引擎本地部署LeRobot 奖励模型Reward Model模型卡指南从训练发布到 Hub 加载推理的完整实践LeRobot 奖励模型Reward Model模型卡指南从训练发布到 Hub 加载推理的完整实践 LeRobot 将「奖励模型」作为一类与策略网络并列的人工智能机器学习深度学习机器人具身智能强化学习上一篇highlight.io Rage Clicks 完整指南狂点检测原理、灵敏度配置与 Slack/邮件告警下一篇Phabricator数据库连接池监控性能指标与优化建议创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网