UniVLA icon indicating copy to clipboard operation
UniVLA copied to clipboard

Online inference script of LAM model

Open lucasjinreal opened this issue 1 year ago • 7 comments

Hi, I'm wondering:

  1. is there a script to infer the LAM model only with real-world videos? I want to see how it behaves for a simple task like navigation;
  2. How to use the existing LAM to easily train a Qwen2.5 VL based two - system model? Currently it heavily relies on primisma which is a little complicated compared with SOTA vlms.

ps: tried not work:

ckpt_path = "checkpoints/lam-stage-2.ckpt"
    lam = LAM(ckpt_path=ckpt_path, stage="stage-2")

    x = torch.randn(1, 16, 3, 224, 224)
    out = lam({"videos": x})
    print(out)

  padded_patches = torch.cat([action_pad, dion_features], dim=2)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 1 but got size 8 for tensor number 1 in the list.

lucasjinreal avatar May 25 '25 09:05 lucasjinreal

1. You can try the following script:

import torchvision
from latent_action_model.genie.modules.lam import ControllableDINOLatentActionModel

# Load model and checkpoint
latent_action_model = ControllableDINOLatentActionModel(
    in_dim=3,
    model_dim=cfg.lam_model_dim,
    latent_dim=cfg.lam_latent_dim,
    num_latents=cfg.codebook_size,
    patch_size=cfg.lam_patch_size,
    enc_blocks=cfg.lam_enc_blocks,
    dec_blocks=cfg.lam_dec_blocks,
    num_heads=cfg.lam_num_heads,
    dropout=0.,
)

# Process and load checkpoint
lam_ckpt = torch.load(cfg.lam_path)['state_dict']
latent_action_model.load_state_dict({k.replace("lam.", ""): v for k, v in lam_ckpt.items()}, strict=True)
latent_action_model = latent_action_model.to(device_id).eval()

# Process images and get latent action
image_transform_lam = torchvision.transforms.ToTensor()  # DINO normalization handled by model
video = torch.stack([
    image_transform_lam(img),    # current observation
    image_transform_lam(img_k)   # 1-sec after observation
], dim=0).unsqueeze(0).to(latent_action_model.device)

action_idx = latent_action_model.vq_encode(video)['indices'].squeeze()

2. This approach can be integrated with any VLM. In UniVLA, we simply add specialized tokens to the LLaMA tokenizer:

special_tokens_dict = {'additional_special_tokens': [f'<ACT_{i}>' for i in range(cfg.codebook_size)]}
num_added_toks = action_tokenizer.add_special_tokens(special_tokens_dict)

During policy training for next-token prediction, map the latent action indices to their corresponding tokens:

action_vocab = [f'<ACT_{i.item()}>' for i in action_idx]

The LLM tokenizer can then process these mapped latent action tokens normally.

retsuh-bqw avatar May 27 '25 06:05 retsuh-bqw

thanks for the example code provided. Still have 2 questions:

  1. the input video shape, it looks like [1, 2, 3, 224, 224], does it only need 2 image as input? Why there need image from future 1-sec after?
  2. Does LLM direclty genreated <ACT_i> tokens? How does it mapping to LAM for higher frequency prediction?

lucasjinreal avatar May 27 '25 13:05 lucasjinreal

  1. The Latent Action Model extracts dynamics (e.g., latent actions / motion ...) between pairs of frames. While the current implementation uses two consecutive frames (can be deemed as a minimal design), it can be extended to multiple frames, allowing the model to predict latent actions for any frame pair in the sequence.

    The 1-second interval is a heuristic choice—we haven’t exhaustively tested the optimal frame interval. However, we can expect that:

    • A shorter interval may result in minimal visual changes between frames.
    • A longer interval could make the dynamics too complex for the model to learn effectively.
  2. I’m not entirely sure I follow your question. Could you clarify the connection you’re making between the Latent Action Model (LAM) and higher-frequency prediction? Regarding the generation of <ACT_i> tokens as labels: We simply map the indices from the LAM codebook to special tokens in the LLaMA tokenizer.

retsuh-bqw avatar Jun 03 '25 08:06 retsuh-bqw

Hi, in terms of the second question, what I mean is that when VLM outputs <ACT_i> tokens, how are these tokens resolved by LAM, how does LAM know how to interpolate these tokens. I'm still puzzled by the connection between them.

lucasjinreal avatar Jun 03 '25 12:06 lucasjinreal

LAM does not need to resolve or interpolate them. LAM is only to generate pseudo labels for VLM pretraining. I believe your are referring to the action deocder, which is built upon a multi-head attention pooling module and a MLP to directly decode latent action embeddings (aka, the VLM's last-layer faeture corresponding to latent action tokens, not the decoded <ACT_i> tokens in the form of text) into low-level action chunks with the L1 regression loss. So LAM is NOT used to decode the latent actions and it is completly deprecated during inference time.

retsuh-bqw avatar Jun 03 '25 13:06 retsuh-bqw

hi, does the navigation task is accurate and robust? what if target is missing a long time, will it resume instructions?

lucasjinreal avatar Jun 03 '25 13:06 lucasjinreal

1. You can try the following script:

import torchvision from latent_action_model.genie.modules.lam import ControllableDINOLatentActionModel

Load model and checkpoint

latent_action_model = ControllableDINOLatentActionModel( in_dim=3, model_dim=cfg.lam_model_dim, latent_dim=cfg.lam_latent_dim, num_latents=cfg.codebook_size, patch_size=cfg.lam_patch_size, enc_blocks=cfg.lam_enc_blocks, dec_blocks=cfg.lam_dec_blocks, num_heads=cfg.lam_num_heads, dropout=0., )

Process and load checkpoint

lam_ckpt = torch.load(cfg.lam_path)['state_dict'] latent_action_model.load_state_dict({k.replace("lam.", ""): v for k, v in lam_ckpt.items()}, strict=True) latent_action_model = latent_action_model.to(device_id).eval()

Process images and get latent action

image_transform_lam = torchvision.transforms.ToTensor() # DINO normalization handled by model video = torch.stack([ image_transform_lam(img), # current observation image_transform_lam(img_k) # 1-sec after observation ], dim=0).unsqueeze(0).to(latent_action_model.device)

action_idx = latent_action_model.vq_encode(video)['indices'].squeeze()

2. This approach can be integrated with any VLM. In UniVLA, we simply add specialized tokens to the LLaMA tokenizer:

special_tokens_dict = {'additional_special_tokens': [f'<ACT_{i}>' for i in range(cfg.codebook_size)]} num_added_toks = action_tokenizer.add_special_tokens(special_tokens_dict) During policy training for next-token prediction, map the latent action indices to their corresponding tokens:

action_vocab = [f'<ACT_{i.item()}>' for i in action_idx] The LLM tokenizer can then process these mapped latent action tokens normally.

Dear author,

Thanks for your wonderful work! I just want to quickly check is there a resolution limit for RGB images input into the LAM? Or it can deal with any regular image resolution (e.g., 200 * 200, 224 * 224, 256 * 256, etc.).

Thanks for your precious time.

Best regards

Trav1slaflame avatar Dec 25 '25 15:12 Trav1slaflame