Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions cookbooks/cosmos3/reasoner/run_with_transformers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@
"echo \"Next: switch this notebook kernel to: Cosmos3 Transformers (Python 3.13)\""
]
},
{
"cell_type": "markdown",
"id": "72c1bd2d",
"metadata": {},
"source": [
"### 3a. Optional: Install ModelOpt for FP8\n",
"\n",
"Skip this subsection for standard BF16 inference. To load the `fp8` model\n",
"revision, run the cell below before switching kernels. `requests` is installed\n",
"explicitly because ModelOpt `0.44.0` imports it but does not declare it as a dependency."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96eb0e73",
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"set -euo pipefail\n",
"\n",
"source \"$COSMOS3_TRANSFORMERS_VENV/bin/activate\"\n",
"uv pip install \\\n",
" \"nvidia-modelopt==0.44.0\" \\\n",
" requests"
]
},
{
"cell_type": "markdown",
"id": "1149a05a",
Expand Down Expand Up @@ -285,6 +313,10 @@
"## 6. Load the Reasoner\n",
"\n",
"Load the processor and model once, then reuse them for every request below.\n",
"The default is the public BF16 Nano checkpoint. To use its FP8 weights, set\n",
"`USE_MODELOPT_FP8 = True`. The model ID stays the same and Transformers downloads\n",
"the `fp8` revision. ModelOpt restores that revision's calibrated E4M3 weights and\n",
"static scales; this is not runtime quantization.\n",
"\n",
"`device_map=\"auto\"` places the model on the available GPU(s) and can shard\n",
"`Cosmos3-Super` across multiple GPUs when Accelerate is installed.\n",
Expand All @@ -302,15 +334,30 @@
"outputs": [],
"source": [
"import torch\n",
"from transformers import AutoProcessor, Cosmos3OmniForConditionalGeneration\n",
"\n",
"USE_MODELOPT_FP8 = False\n",
"model_id = \"nvidia/Cosmos3-Nano\" # or \"nvidia/Cosmos3-Super\"\n",
"FP8_REVISION = \"fp8\"\n",
"\n",
"if USE_MODELOPT_FP8:\n",
" # Enable ModelOpt restoration before importing the Transformers model class.\n",
" # Importing this backend registers the real per-tensor FP8 GEMM implementation.\n",
" from modelopt.torch import opt as modelopt_opt\n",
" from modelopt.torch.quantization.backends import fp8_per_tensor_gemm\n",
"\n",
" modelopt_opt.enable_huggingface_checkpointing()\n",
" revision_kwargs = {\"revision\": FP8_REVISION}\n",
"else:\n",
" revision_kwargs = {}\n",
"\n",
"from transformers import AutoProcessor, Cosmos3OmniForConditionalGeneration\n",
"\n",
"processor = AutoProcessor.from_pretrained(model_id)\n",
"processor = AutoProcessor.from_pretrained(model_id, **revision_kwargs)\n",
"model = Cosmos3OmniForConditionalGeneration.from_pretrained(\n",
" model_id,\n",
" dtype=torch.bfloat16,\n",
" device_map=\"auto\",\n",
" **revision_kwargs,\n",
")\n",
"\n",
"\n",
Expand Down Expand Up @@ -416,6 +463,8 @@
"\n",
"- Run **Cosmos3-Super**: set `model_id = \"nvidia/Cosmos3-Super\"` in step 6 and\n",
" re-run from there. `device_map=\"auto\"` shards it across multiple GPUs.\n",
"- Run **ModelOpt FP8**: set `USE_MODELOPT_FP8 = True` and re-run from step 6.\n",
" The same `model_id` is loaded from its `fp8` revision.\n",
"- Try other Reasoner tasks (temporal localization, grounding, embodied reasoning)\n",
" by changing the prompt and asset — see the\n",
" [Reasoner Prompt Guide](./reasoner_prompt_guide.md).\n",
Expand Down