华为昇腾 NPU 适配第 2 期:ACT (Action Chunking Transformer) 机器人策略模型,用于 AlohaTransferCube 双臂操作任务。
本模型是 LeRobot 框架下训练的 ACT (Action Chunking Transformer) 机器人策略模型,基于 Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware (https://huggingface.co/papers/2304.13705) 论文实现。模型在 aloha_sim_transfer_cube_human 数据集上训练 80k 步,用于 AlohaTransferCube 仿真环境中的双臂协作搬砖任务。
模型架构:
推理流程:
适配说明:本模型为机器人策略模型,非 LLM 架构,vLLM-Ascend 不适用,采用 torch_npu 直接部署为 HTTP 服务。
# 从 ModelScope 下载(推荐)
python3 -c "
from modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('lerobot/act_aloha_sim_transfer_cube_human', cache_dir='./models')
"
# 或从 HuggingFace 下载
# git lfs clone https://huggingface.co/lerobot/act_aloha_sim_transfer_cube_human# 启动推理服务(端口 8008)
python3 serve.py --host 0.0.0.0 --port 8008 --model-dir ./models --device npu:0curl http://localhost:8008/health
# 预期输出:{"status": "ok"}import base64, json, urllib.request
# 读取图像
with open('test_image.png', 'rb') as f:
img_b64 = base64.b64encode(f.read()).decode()
# 构造请求(14 维机器人关节状态)
state = [0.0, -0.5, 1.0, 0.0, -0.4, 0.1, 0.3, 0.0, -0.2, 0.7, -0.1, -0.2, -0.3, 0.6]
payload = json.dumps({'image_base64': img_b64, 'state': state}).encode()
req = urllib.request.Request('http://localhost:8008/infer', data=payload,
headers={'Content-Type': 'application/json'})
with urllib.request.urlopen(req, timeout=60) as resp:
result = json.loads(resp.read())
print(result['action_chunk_shape']) # [1, 100, 14]
print(result['first_action']) # 14-dim action vectorpython3 inference.py --smoke-test使用随机生成的 dummy 图像 (3x480x640) 和状态向量 (14-dim) 进行推理,验证模型加载和推理流程正常。
# 1. 健康检查
curl -s http://localhost:8008/health
# 2. 推理请求(发送测试图像)
python3 -c "
import base64, json, urllib.request
with open('/tmp/test_aloha_input.png', 'rb') as f:
img_b64 = base64.b64encode(f.read()).decode()
payload = json.dumps({'image_base64': img_b64, 'state': [0.0]*14}).encode()
req = urllib.request.Request('http://localhost:8008/infer', data=payload,
headers={'Content-Type': 'application/json'})
with urllib.request.urlopen(req, timeout=60) as resp:
result = json.loads(resp.read())
print('Status:', result['status'])
print('Latency:', result['latency_ms'], 'ms')
print('Action chunk shape:', result['action_chunk_shape'])
"| 指标 | 数值 |
|---|---|
| 模型参数 | 51.61M |
| 权重文件大小 | 197 MB |
| NPU 内存占用(服务进程) | ~462 MB |
| NPU HBM 总使用 | ~4.9 GB / 64 GB |
| 首轮推理延迟(含编译) | ~227 ms |
| 稳定推理延迟(后 9 轮) | ~20.6 ms (模型端) / ~33.4 ms (HTTP 端到端) |
| AICore 利用率 | ~1-3%(较低,模型较小) |
| 批量大小 | 1 |
| 动作步数 | 100 步 / 推理 |
| 轮次 | 模型延迟 (ms) | HTTP 端到端 (ms) |
|---|---|---|
| 第 1 轮 | 227 | 35.5 |
| 第 2 轮 | 20.8 | 33.4 |
| 第 3 轮 | 20.6 | 33.1 |
| 第 4 轮 | 20.8 | 33.2 |
| 第 5 轮 | 20.9 | 33.4 |
| 第 6 轮 | 20.8 | 33.2 |
| 第 7 轮 | 20.6 | 33.0 |
| 第 8 轮 | 20.8 | 33.4 |
| 第 9 轮 | 20.6 | 33.0 |
| 第 10 轮 | 20.6 | 32.9 |
注:首轮延迟较高是由于 NPU 图编译和算子缓存。模型端延迟与 HTTP 端到端延迟的差异来自图像 base64 编解码和网络传输。
模型权重中内置了训练数据集的归一化统计信息,用于输入/输出归一化:
| 特征 | 归一化方式 | mean | std |
|---|---|---|---|
| observation.images.top | ImageNet MEAN_STD | [0.485, 0.456, 0.406] | [0.229, 0.224, 0.225] |
| observation.state | MEAN_STD | 14-dim | 14-dim |
| action | MEAN_STD | 14-dim | 14-dim |
使用随机 dummy 图像和状态测试,模型输出动作序列的典型范围:
vLLM-Ascend 不适用:本模型为 ACT 机器人策略模型,非 LLM/VLM 架构,vLLM-Ascend 不支持。采用 torch_npu 直接部署推理。
归一化移出:当前 lerobot 0.4.4 的 ACTPolicy 类不包含内置 normalize_inputs/unnormalize_outputs 层。这些层在 0.4.4 版本中被迁移为独立的 ProcessorPipeline。本适配从 safetensors 权重文件中提取归一化统计,在 inference.py 中手动处理。
权重加载:使用 strict=False 加载模型权重,忽略 8 个归一化 buffer 键(normalize_inputs.buffer_* 等),这些由 ACTNormalizer 分类处理。
NPU 内存:模型较小(51.6M 参数),NPU 内存占用约 462MB,HBM 总使用约 4.9GB。如有多卡,可指定其他 NPU 设备。
CANN 日志:CANN 可能因权限问题无法创建日志目录(/home/atomgit/ascend/log),不影响推理功能。
生产部署建议:对于生产环境,建议使用 FastAPI 或 gRPC 替代 http.server 以支持并发和高吞吐;或使用 lerobot 框架的 async_inference 模块。
模型来源:权重从 ModelScope (lerobot/act_aloha_sim_transfer_cube_human) 下载,原始来源为 HuggingFace (lerobot/act_aloha_sim_transfer_cube_human)。训练配置见 train_config.json,训练 80k 步,A100 耗时约 1h45min。
环境依赖:需安装 CANN 8.5.1 及配套驱动,配置 torch_npu 2.9.0.post1。详细依赖见 requirements.txt。