NVIDIA Cosmos Reason2

repository·main·Indexed 19 days ago

https://github.com/nvidia-cosmos/cosmos-reason2

An open, customizable reasoning vision language model (VLM) based on the Qwen3-VL architecture, designed for Physical AI and robotics. It utilizes chain-of-thought reasoning to understand spatial-temporal dynamics and physics. The package includes Cosmos-RL, an async post-training framework for Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL), and supports inference via Transformers and vLLM on Hopper and Blackwell architectures.

Tokens
9.1K
Snippets
30
Records
43
Agent score
65%

What's inside cosmos-reason2

  1. Available Task-Specific Prompt Templates

    main

    The repository provides several task-specific prompt templates organized into two main categories: Captioning and Embodied Reasoning. These templates can be found as .yaml files within the prompts/ directory.

    ### Captioning Templates
    * Caption (`caption.yaml`)
    * Temporal Localization (`temporal_localization.yaml`)
    * Describe Anything (`describe_anything.yaml`)
    * 2D Grounding (`2d_grounding.yaml`)
    
    ### Embodied Reasoning Templates
    * Embodied Reasoning (`embodied_reasoning.yaml`)
    * Robot COT (`robot_cot.yaml`)
    * AV COT (`av_cot.yaml`)
    * MVP Bench (`mvp_bench.yaml`)
    * Causal VQA (`causal_vqa.yaml`)
  2. Migrate configuration from Reason 1 to Reason 2

    main

    When migrating from Cosmos Reason 1 to Reason 2, you must update your vision processor configuration. The parameter names and semantics have changed to follow the Qwen3-VL Pixel Control specification.

    Important: You cannot simply swap the old keys for the new ones with the same values. shortest_edge and longest_edge control resizing differently than the deprecated min_pixels and max_pixels. You must recalculate appropriate values for your desired frame sizes.

    Reason 2 format (Current):

    "mm_processor_kwargs": {
      "size": {
        "shortest_edge": 1568,
        "longest_edge": 374544
      }
    }

    Reason 1 format (Deprecated):

    "mm_processor_kwargs": {
      "videos_kwargs": {
        "min_pixels": 1568,
        "max_pixels": 374544
      }
    }

    Other Migration Notes:

    • Video timestamps: Timestamps are no longer overlaid on videos; they are now automatically included in the model embedding.
  3. Access additional Cosmos-Reason2 resources

    main

    For deeper technical guidance and specific use cases, refer to the following resources:

    Cosmos-Reason2 is built upon the Qwen3-VL architecture. You may find relevant architectural details and implementation guides in the Qwen3-VL Repository, Qwen3-VL vLLM documentation, or the official Qwen3 Documentation.

  4. Prepare LLaVA format datasets for Cosmos-RL

    main

    Cosmos-RL uses a TOML configuration for training. For LLaVA-style datasets, you must configure the annotation_path and media_path in your config file (e.g., configs/llava_sft.toml). If your annotations use absolute paths, media_path can be left as an empty string ("").

    To download the Llava-Instruct-150K dataset and COCO images for testing, use the following commands:

    DATASET_DIR="/tmp/cosmos_reason2/cosmos_rl/data/llava_sft"
    mkdir -p $DATASET_DIR
    wget https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K/resolve/main/detail_23k.json -O $DATASET_DIR/annotations.json
    wget http://images.cocodataset.org/zips/train2017.zip -O $DATASET_DIR/media.zip && unzip -q $DATASET_DIR/media.zip -d $DATASET_DIR
    DATASET_DIR="/tmp/cosmos_reason2/cosmos_rl/data/llava_sft"
    mkdir -p $DATASET_DIR
    wget https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K/resolve/main/detail_23k.json -O $DATASET_DIR/annotations.json
    wget http://images.cocodataset.org/zips/train2017.zip -O $DATASET_DIR/media.zip && unzip -q $DATASET_DIR/media.zip -d $DATASET_DIR
  5. Run Supervised Fine-Tuning (SFT)

    main

    Supervised Fine-Tuning (SFT) improves model capability on specific task distributions.

    Minimum Requirements:

    • 4 GPUs with 80GB of memory each.

    Configuration: Adjust the dp_shard_size in the [policy.parallelism] section of your TOML config based on your GPU count:

    • For 4 GPUs: dp_shard_size = 4
    • For 8 GPUs: dp_shard_size = 8

    Execution Command: Run the SFT script using uv run and the cosmos-rl CLI:

    uv run cosmos-rl --config configs/llava_sft.toml --log-dir outputs/llava_sft scripts/llava_sft.py
    uv run cosmos-rl --config configs/llava_sft.toml --log-dir outputs/llava_sft scripts/llava_sft.py
  6. Setup Cosmos-Reason2 via Docker Container

    main

    Use Docker to run Cosmos-Reason2. Ensure you have the NVIDIA Container Toolkit installed.

    1. Build the container: Specify the CUDA version using --build-arg. For DGX Spark and Jetson AGX, use 13.0.0.
    image_tag=$(docker build -f Dockerfile --build-arg=CUDA_VERSION=12.8.1 -q .)
    1. Run the container:
    docker run -it --gpus all --ipc=host --rm -v .:/workspace -v /workspace/.venv -v /workspace/examples/cosmos_rl/.venv -v /root/.cache:/root/.cache -e HF_TOKEN="$HF_TOKEN" $image_tag

    Optional Arguments:

    • --ipc=host: Uses host shared memory (recommended for parallel torchrun).
    • -v /root/.cache:/root/.cache: Mounts host cache to avoid re-downloading models.
    • -e HF_TOKEN="$HF_TOKEN": Passes your Hugging Face token for authentication.
    # Build
    image_tag=$(docker build -f Dockerfile --build-arg=CUDA_VERSION=12.8.1 -q .)
    
    # Run
    docker run -it --gpus all --ipc=host --rm -v .:/workspace -v /workspace/.venv -v /workspace/examples/cosmos_rl/.venv -v /root/.cache:/root/.cache -e HF_TOKEN="$HF_TOKEN" $image_tag
  7. Install and Setup Cosmos-RL

    main

    Cosmos-RL is an async post-training framework for Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL). To set up the example environment, follow these steps:

    1. Install Redis: Install the redis-server system dependency using conda:
      conda install -c conda-forge redis-server
    2. Install the Example: Navigate to the example directory and use uv to sync the environment:
      cd examples/cosmos_rl
      uv sync
    3. Monitor with Weights & Biases (Optional): To monitor training, install and login to wandb:
      uv tool install -U wandb
      wandb login
      During training, the controller log will provide a direct link to your wandb run.
    cd examples/cosmos_rl
    uv sync
  8. Run Cosmos-Reason2 notebooks locally

    main

    To run the provided Jupyter notebooks in this directory on your local machine, follow these steps:

    1. Prerequisites: Ensure you have completed the initial project setup as described in the main README.
    2. Environment Setup: Use uv to synchronize the environment and activate the virtual environment:
      cd examples/notebooks
      uv sync
      source .venv/bin/activate
    3. Execution:
      • You can run the notebooks as standard Python scripts using python <notebook>.py.
      • Alternatively, you can use VS Code to run them as interactive Jupyter notebooks.
    cd examples/notebooks
    uv sync
    source .venv/bin/activate
  9. Deploy Cosmos-Reason2 with vLLM Online Serving

    main

    For production deployment, use vllm>=0.11.0.

    1. Start the vLLM server:
    vllm serve nvidia/Cosmos-Reason2-2B \
      --allowed-local-media-path "$(pwd)" \
      --max-model-len 16384 \
      --media-io-kwargs '{"video": {"num_frames": -1}}' \
      --reasoning-parser qwen3 \
      --port 8000

    Server Arguments:

    • --max-model-len 16384: Maximum model length (recommended: 8192 - 16384).
    • --media-io-kwargs '{"video": {"num_frames": -1}}': Allows overriding FPS per sample.
    • --reasoning-parser qwen3: Enables parsing of the reasoning trace.
    • --port 8000: The server port.
    1. Perform Inference (Client Side):

    Caption a video:

    cosmos-reason2-inference online --port 8000 -i prompts/caption.yaml --reasoning --videos assets/sample.mp4 --fps 4

    Embodied reasoning (verbose):

    cosmos-reason2-inference online -v --port 8000 -i prompts/embodied_reasoning.yaml --reasoning --images assets/sample.png
  10. Use vLLM with Cosmos-Reason2

    main

    Since Cosmos-Reason2 is based on the Qwen3-VL architecture, you can leverage vLLM for high-performance serving and inference. Key vLLM capabilities applicable to this model include:

    • Online Serving: Deploying an OpenAI-compatible server.
    • Offline Inference: Running batch inference locally.
    • Multimodal Inputs: Handling the vision-language capabilities of the model.
    • LoRA: Applying Low-Rank Adaptation for efficient fine-tuning.