多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

如何用 LeRobot 的 PolicyServer 与 RobotClient 运行异步推理?

如何用 LeRobot 的 PolicyServer 与 RobotClient 运行异步推理? 如何用 LeRobot 的 PolicyServer 与 RobotClient 运行异步推理【免费下载链接】lerobot LeRobot: Making AI for Robotics more accessible with end-to-end learning项目地址: https://gitcode.com/GitHub_Trending/le/lerobot在 LeRobot 中跑真机推理时同步推理会让机器人在策略计算下一个动作块action chunk期间停住等待。异步推理async inference把动作预测与动作执行解耦RobotClient在本地机器人上持续执行动作队列同时PolicyServer在算力更强的机器上提前算好下一个动作块消除等待推理产生的空闲帧。这条路径适用于 LeRobot 支持的所有策略类型前提是机器人在本地、推理可以在本机或另一台机器上进行。下面按「安装依赖 → 启动 PolicyServer → 启动 RobotClient → 验证运行 → 调参」的顺序走一遍。安装 async 依赖异步推理需要额外的依赖gRPC 与绘图安装时指定asynctagpip install -e .[async]启动 PolicyServerPolicyServer 是一个围绕PreTrainedPolicy的 gRPC 服务。启动时它是空容器运行哪个策略、用什么参数都在客户端首次握手时指定所以启动服务器只需要 host 和端口python -m lerobot.async_inference.policy_server \ --host127.0.0.1 \ --port8080启动后服务监听127.0.0.1:8080等待RobotClient连接。如果在同一台机器上运行客户端host 用localhost即可服务器放在另一台机器时改成对应地址。服务器端还有--fps、--inference_latency、obs_queue_timeout三个可选参数见 policy_server.py 头部注释中的示例默认值定义在 constants.pyDEFAULT_FPS 30、DEFAULT_INFERENCE_LATENCY 1 / 30、DEFAULT_OBS_QUEUE_TIMEOUT 2主路径中不必改动。也可以用 Python API 方式启动等价于上面的命令async.mdx 给出的示例from lerobot.async_inference.configs import PolicyServerConfig from lerobot.async_inference.policy_server import serve config PolicyServerConfig( hostlocalhost, port8080, ) serve(config)启动 RobotClientRobotClient包装一个Robot实例并连接到可能在远程的PolicyServer它向服务器流式发送观测接收推理得到的动作块在本地执行。文档给出的主路径命令如下其中带「你的」字样的值需要按实际环境替换python -m lerobot.async_inference.robot_client \ --server_address127.0.0.1:8080 \ --robot.typeso100_follower \ --robot.port/dev/tty.usbmodem585A0076841 \ --robot.idfollower_so100 \ --robot.cameras{ laptop: {type: opencv, index_or_path: 0, width: 1920, height: 1080, fps: 30}, phone: {type: opencv, index_or_path: 0, width: 1920, height: 1080, fps: 30}} \ --taskdummy \ --policy_typesmolvla \ --pretrained_name_or_pathlerobot/smolvla_base \ --policy_devicemps \ --actions_per_chunk50 \ --chunk_size_threshold0.5 \ --aggregate_fn_nameweighted_average \ --debug_visualize_queue_sizeTrue各参数分四组需要替换或确认的是SERVERserver_address是服务器主机地址和端口需与启动 PolicyServer 时一致。ROBOTrobot.type文档示例为so100_follower换成你的机器人类型、robot.port你的机器人串口、robot.id本地机器人 id用于加载校准文件。POLICYpolicy_type是策略类型文档示例写your_policy_type需替换为实际类型如smolvla、actpretrained_name_or_path是服务器上要加载的 checkpoint 名称或路径文档示例user/model如lerobot/smolvla_basepolicy_device是服务器上运行策略的设备cuda、mps、xpu、cputask是任务指令并非所有策略都需要文档注明act等策略不一定定义 task。CLIENTactions_per_chunk、chunk_size_threshold、aggregate_fn_name、debug_visualize_queue_size见下节。robot.cameras里相机的键名必须与策略期望的观测键匹配文档提示可查看 Hub 上策略的config.json确认可用 lerobot_find_cameras.py 查看当前可用的相机。也可以用 Python API 方式构造客户端关键部分是来自 async.mdx 的示例from lerobot.robots.so_follower import SO100FollowerConfig from lerobot.cameras.opencv import OpenCVCameraConfig from lerobot.async_inference.configs import RobotClientConfig from lerobot.async_inference.robot_client import RobotClient from lerobot.async_inference.helpers import visualize_action_queue_size camera_cfg { top: OpenCVCameraConfig(index_or_path0, width640, height480, fps30), side: OpenCVCameraConfig(index_or_path1, width640, height480, fps30) } robot_cfg SO100FollowerConfig( port/dev/tty.usbmodem585A0076841, idfollower_so100, camerascamera_cfg ) client_cfg RobotClientConfig( robotrobot_cfg, server_addresslocalhost:8080, policy_devicemps, client_devicecpu, policy_typesmolvla, pretrained_name_or_pathuser/smolvla_async, chunk_size_threshold0.5, actions_per_chunk50, # make sure this is less than the max actions of the policy ) client RobotClient(client_cfg) task Dont do anything, stay still if client.start(): # 启动动作接收线程 action_receiver_thread threading.Thread(targetclient.receive_actions, daemonTrue) action_receiver_thread.start() try: client.control_loop(task) except KeyboardInterrupt: client.stop() action_receiver_thread.join() visualize_action_queue_size(client.action_queue_size)其中user/smolvla_async是文档示例里的模型路径占位符替换为你自己的 checkpoint 路径。如何判断连接建立、推理在跑客户端start()会依次完成Ready握手和SendPolicyInstructions把策略类型、checkpoint、actions_per_chunk、设备发给服务器服务器据此加载策略。运行中可依据以下源码中的日志判断各环节是否正常服务器侧PolicyServer started on {host}:{port}policy_server.py、每次推理输出Action chunk #{timestep} generated | Total time: ...mspolicy_server.py。客户端侧Robot connected and ready表示机器人已连接robot_client.py、Sending policy instructions to policy server、Control loop thread starting。客户端连接失败会记录Failed to connect to policy server: {e}robot_client.py此时先确认服务器已启动、server_address与--host/--port一致。最终结果是文档给出的机器人开始按策略动作运动You should see your robot moving around by now。调两个关键参数文档明确actions_per_chunk与chunk_size_threshold是每个部署都需要调的两个参数参数文档默认值作用actions_per_chunk50策略一次输出多少个动作典型值 10–50且不得超过策略的 max actionschunk_size_threshold0.7动作队列低于该比例时客户端发送新观测取值 [0, 1]注意一处文档与源码的差异async.mdx 的参数表把chunk_size_threshold的默认值写成 0.7而 configs.py 中RobotClientConfig的默认值是 0.5文档自己的命令示例也使用 0.5。按源码以 0.5 为准。两者的取舍增大actions_per_chunk新块算出来时手头可执行的更多降低「队列耗尽」的概率但预测跨度更长累积误差可能让动作精度下降。增大chunk_size_threshold更频繁地把观测发给服务器动作块重叠更多、适应性更高但推理压力增大取接近 0.0 则退化为同步推理的边缘情况当前块耗尽才发新观测。文档建议从 0.5–0.6 附近开始试。调参的具体做法来自文档的 Tuning 章节先选算力。文档给出的参考值PI0 推理时约占 14GB 显存SmolVLA 约 2GB。策略与设备CPU、MPS、NVIDIA GPU 的 CUDA 核心数的组合直接决定平均推理延迟。根据推理延迟调fps。服务器算新块期间客户端在消耗当前队列两边速度差太大时客户端可能耗尽队列若频繁出现动作队列耗尽就调低 fps。调chunk_size_threshold时把客户端的--debug_visualize_queue_size设为True运行结束时 helpers.py 中的visualize_action_queue_size会画出动作队列大小的演变曲线用它对比不同取值下队列是否稳定、何时见底。aggregate_fn_name指定重叠部分动作的聚合函数内置注册表configs.py提供weighted_average、latest_only、average、conservative四个选项也可以按文档说明在 robot_client.py 中自行添加。边界与限制异步推理当前支持的动作块策略为 constants.py 中的SUPPORTED_POLICIESact、smolvla、diffusion、tdmpc、vqbet、pi0、pi05、groot。客户端发送的policy_type不在此列时服务器会抛出Policy type ... not supportedpolicy_server.py。客户端支持构造的机器人类型包括so100_follower、so101_follower、bi_so_follower、omx_followerSUPPORTED_ROBOTS另可通过 robot_client.py 中导入的koch_follower、unitree_g1及第三方插件扩展。chunk_size_threshold必须位于 [0, 1]、actions_per_chunk必须为正否则客户端配置校验会直接报错configs.py。完成一轮运行后如果队列频繁见底或动作响应不够及时回到上一节按「选算力 → 调 fps → 用队列曲线调chunk_size_threshold」的顺序迭代即可。【免费下载链接】lerobot LeRobot: Making AI for Robotics more accessible with end-to-end learning项目地址: https://gitcode.com/GitHub_Trending/le/lerobot创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表