diff --git a/src/lerobot/policies/pi0_fast/modeling_pi0_fast.py b/src/lerobot/policies/pi0_fast/modeling_pi0_fast.py index 494d25644..475563d7c 100644 --- a/src/lerobot/policies/pi0_fast/modeling_pi0_fast.py +++ b/src/lerobot/policies/pi0_fast/modeling_pi0_fast.py @@ -612,6 +612,11 @@ class PI0FastPytorch(nn.Module): # see openpi `PI0Pytorch` device = tokens.device lm_head = self.paligemma_with_expert.paligemma.lm_head + # detokenize_actions() cuts at the first "|", so anything generated past it is + # discarded anyway. + pipe_id = self._paligemma_tokenizer.convert_tokens_to_ids("|") + finished = torch.zeros(bsize, dtype=torch.bool, device=device) + # --- 1. PREFILL PHASE --- # Process Images + Text Prompt + BOS token once to populate the KV cache. @@ -663,6 +668,7 @@ class PI0FastPytorch(nn.Module): # see openpi `PI0Pytorch` # Initialize storage for generated tokens generated_action_tokens = torch.zeros((bsize, max_decoding_steps), dtype=torch.long, device=device) generated_action_tokens[:, 0] = next_token.squeeze(-1) + finished |= next_token.squeeze(-1) == pipe_id # Track valid tokens mask (0 for pad, 1 for valid) # We need this to tell the new token what it can attend to (images + text + past actions) @@ -713,6 +719,10 @@ class PI0FastPytorch(nn.Module): # see openpi `PI0Pytorch` generated_action_tokens[:, t] = next_token.squeeze(-1) + finished |= next_token.squeeze(-1) == pipe_id + if bool(finished.all()): + break + return generated_action_tokens