LeRobot 文件

即時分塊 (RTC)

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

即時分塊 (Real-Time Chunking, RTC)

即時分塊 (RTC) 是一種推理時 (inference-time) 的方法,它允許基於流匹配 (flow-matching) 的大型機器人策略(例如 Pi0Pi0.5SmolVLA),在擁有高推理延遲的情況下,仍能產生平滑、連續且具反應性的動作。

這些策略產生的是未來動作的「分塊」(例如一次 50 個步驟),而非單一動作。由於模型較大,產生每個分塊所需的時間比機器人執行該分塊的時間更長。若單純地執行這些分塊,當下一個分塊遲到或與先前執行的動作不一致時,會導致停頓、動作生硬或策略突然變更等問題。

RTC 解決了這個問題:在機器人執行當前分塊的同時,非同步地生成下一個分塊,並引導新分塊與已執行之舊分塊的重疊部分平滑對接。

RTC 運作原理(簡化版)

RTC 讓機器人在移動的同時進行預判。當機器人執行一個動作分塊時,RTC 會提前開始創建下一個分塊。但由於新分塊準備好時機器人已經移動了一段距離,RTC 必須確保新分塊仍能與機器人當前的動作平滑對接。

為達成此目的,RTC 將新分塊的起始部分視為一種「修補 (inpainting)」或「填補間隙」問題:它會微調新分塊的前段,使其自然地融入機器人正在進行的動作中。結果就是:沒有停頓,也不會出現突兀的跳躍。

從技術層面來說,RTC 在流匹配去噪過程中加入了一個引導項 (guidance term),透過軟過渡遮罩 (soft transition mask),強迫新分塊中與舊分塊重疊的時間步長與已執行的部分保持接近。

快速入門

安裝

RTC 已內建於 LeRobot 中。只需安裝您所需的策略依賴項即可。

# For Pi0 or Pi0.5
pip install -e ".[pi]"

# For SmolVLA
pip install -e ".[smolvla]"

將 RTC 用於 Pi0

您可以在 eval_with_real_robot.py 中找到完整的參考實作。下方的程式碼片段提供了一個簡單的偽代碼範例,說明 RTC 如何在您的管道中與 Pi0 運作。

from lerobot.policies.pi0 import PI0Policy, PI0Config
from lerobot.configs.types import RTCAttentionSchedule
from lerobot.policies.rtc.configuration_rtc import RTCConfig
from lerobot.policies.rtc.action_queue import ActionQueue

# Load Pi0 with RTC enabled
policy_cfg = PI0Config()

# Enable RTC
policy_cfg.rtc_config = RTCConfig(
    enabled=True,
    execution_horizon=10,  # How many steps to blend with previous chunk
    max_guidance_weight=10.0,  # How strongly to enforce consistency
    prefix_attention_schedule=RTCAttentionSchedule.EXP,  # Exponential blend
)

# Load the policy
policy = PI0Policy.from_pretrained("lerobot/pi0_base", policy_cfg=policy_cfg, device="cuda")

# Now use predict_action_chunk with RTC parameters
inference_delay = 4  # How many steps of inference latency, this values should be calculated based on the inference latency of the policy

# Initialize the action queue
action_queue = ActionQueue(policy_cfg.rtc_config)

# Start in a separate thread with the following function
def get_actions():
  while True:
    if should_get_actions:

      prev_actions = action_queue.get_left_over()
      obs = get_robot_observations(robot)

      # Generate actions WITH RTC
      actions = policy.predict_action_chunk(
          obs,
          inference_delay=inference_delay,
          prev_chunk_left_over=prev_actions,
      )

      action_queue.merge(
          actions, actions, inference_delay
      )

for step in range(num_steps):
    action = action_queue.get()

    # Execute the first N actions
    execute_actions(action)

關鍵參數

RTCConfig 具有以下可調整的參數:

execution_horizon(執行範疇):需與先前分塊保持一致的時間步長數。數值越大,過渡越平滑,但反應性可能會降低。

建議值:8-12 步

RTCConfig(execution_horizon=10)

max_guidance_weight(最大引導權重):強制與先前分塊保持一致的強度。這是一個超參數,可調整以平衡過渡平滑度與策略的反應性。對於 10 步流匹配 (SmolVLA, Pi0, Pi0.5),10.0 是最佳值。

prefix_attention_schedule(前綴注意力排程):如何在重疊區域分配一致性的權重。

  • LINEAR:從 inference_delay 到 execution_horizon 的線性衰減
  • EXP:指數衰減(建議入門使用)
  • ONES:在整個 execution_horizon 內維持全權重
  • ZEROS:二元模式(inference_delay 前為全權重,之後為零)

inference_delay(推理延遲):您的系統具有多少時間步長的推理延遲。此參數直接傳遞給 predict_action_chunk() 而非設定檔中,因為它可能在運行時發生變化。

離線測試 RTC

在實際機器人上運行之前,請使用資料集樣本測試 RTC 以視覺化其運作方式。

python examples/rtc/eval_dataset.py \
    --policy.path=lerobot/pi0_libero_finetuned \
    --dataset.repo_id=HuggingFaceVLA/libero \
    --rtc.execution_horizon=10 \
    --rtc.max_guidance_weight=10.0 \
    --device=cuda

該腳本會生成去噪過程的視覺化圖,比較標準生成(左)與 RTC(右)。在 RTC 圖表中,您可以看到前幾個步驟(藍色/紫色線條)是如何被引導至符合紅色地面實況軌跡(前一個分塊的尾端),從而確保分塊之間實現平滑過渡。

Denoising steps with and without RTC

實際機器人測試 RTC

python examples/rtc/eval_with_real_robot.py \
    --policy.path=${HF_USERNAME}/policy_repo_id \
    --robot.type=so100_follower \
    --robot.port=/dev/tty.usbmodem58FA0834591 \
    --robot.cameras="{ gripper: {type: opencv, index_or_path: 1, width: 640, height: 480, fps: 30}, front: {type: opencv, index_or_path: 0, width: 640, height: 480, fps: 30}}" \
    --task="Move green small object into the purple platform" \
    --duration=120 \
    --device=cuda

它與 LeRobot 中的非同步推理有何不同

RTC 和 非同步推理 (async inference) 皆旨在改善即時機器人控制,但它們解決的是不同的問題。

面向 非同步推理 RTC
問題 處理等待推理時的閒置訊框 解決動作分塊間的不連續性
解決方案 將預測與執行解耦 引導新分塊以從舊分塊平滑延續
優點 無需等待,動作連續 平滑過渡,動作自然
最佳使用場景 非同步推理最適合具有高推理延遲的大型模型 基於流匹配的策略

兩者搭配使用可獲得極致的平滑度與反應性!

進階:偵錯追蹤

RTC 內建偵錯追蹤功能,協助您理解推理過程中發生的情況。

# Enable debug tracking
policy_cfg.rtc_config.debug = True
policy_cfg.rtc_config.debug_maxlen = 100

# After inference, access debug data
debug_data = policy.rtc_processor.get_debug_data()

# Visualize denoising steps, corrections, etc.
from lerobot.policies.rtc.debug_visualizer import RTCDebugVisualizer
visualizer = RTCDebugVisualizer()
# ... create plots

請參閱 examples/rtc/eval_dataset.py 以取得視覺化的完整範例。

參考資料

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.