Instructions to use kyutai/CASA-Helium1-VL-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kyutai/CASA-Helium1-VL-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="kyutai/CASA-Helium1-VL-2B", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("kyutai/CASA-Helium1-VL-2B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use kyutai/CASA-Helium1-VL-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "kyutai/CASA-Helium1-VL-2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "kyutai/CASA-Helium1-VL-2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/kyutai/CASA-Helium1-VL-2B
- SGLang
How to use kyutai/CASA-Helium1-VL-2B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "kyutai/CASA-Helium1-VL-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "kyutai/CASA-Helium1-VL-2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "kyutai/CASA-Helium1-VL-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "kyutai/CASA-Helium1-VL-2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use kyutai/CASA-Helium1-VL-2B with Docker Model Runner:
docker model run hf.co/kyutai/CASA-Helium1-VL-2B
Download image_encoder.py from kyutai/CASA-Helium1-VL-2B: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/kyutai/CASA-Helium1-VL-2B/resolve/main/image_encoder.py
- Command line
-
hf download hf://kyutai/CASA-Helium1-VL-2B/image_encoder.py
-
curl -L -o image_encoder.py https://huggingface.co/kyutai/CASA-Helium1-VL-2B/resolve/main/image_encoder.py
3.55 kB
| """Qwen2.5VL encoder with delayed normalization""" | |
| import torch | |
| from einops import rearrange | |
| from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import ( | |
| Qwen2_5_VisionTransformerPretrainedModel, | |
| ) | |
| class _LinearPatchEmbedProj(torch.nn.Module): | |
| """Replaces Conv3d in Qwen's patch embed after collapsing the temporal dim. | |
| The training codebase replaces the Conv3d with a plain Linear for efficiency | |
| when temporal_patch_size=1. The checkpoint therefore stores the weight under | |
| ``patch_embed.proj.linear.weight`` rather than ``patch_embed.proj.weight``, | |
| so this wrapper is required for state-dict key compatibility. | |
| """ | |
| def __init__(self, in_features: int, out_features: int) -> None: | |
| super().__init__() | |
| self.linear = torch.nn.Linear(in_features, out_features, bias=False) | |
| def weight(self) -> torch.Tensor: | |
| return self.linear.weight | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| n = x.shape[0] | |
| return self.linear(x.reshape(n, -1)).view(n, -1, 1, 1, 1) | |
| def _replace_patch_embed_proj(visual: "Qwen2_5_VisionTransformerPretrainedModel") -> None: | |
| """Swap the Conv3d patch-embed proj to a Linear so parameter names match the checkpoint.""" | |
| proj = visual.patch_embed.proj | |
| if not isinstance(proj, torch.nn.Conv3d): | |
| return | |
| _, in_ch, _t, p, _ = proj.weight.shape | |
| visual.patch_embed.temporal_patch_size = 1 | |
| visual.patch_embed.proj = _LinearPatchEmbedProj(in_ch * p * p, proj.out_channels) # type: ignore[assignment] | |
| def prepare_for_qwen_encoder( | |
| x: torch.Tensor | list[torch.Tensor], mean: torch.Tensor, std: torch.Tensor | |
| ) -> tuple[torch.Tensor, torch.Tensor]: | |
| """ | |
| Preprocessing for Qwen encoder | |
| Image mean and std come from processor.image_processor.image_mean and image_std | |
| """ | |
| grid_thw = torch.Tensor([[1, img.shape[0], img.shape[1]] for img in x]).to(x[0].device) | |
| hws_flatten_shape = torch.prod(grid_thw, dim=-1) | |
| x = torch.cat( | |
| [img.reshape((int(hws_flatten_shape[idx].item()), -1)) for idx, img in enumerate(x)], | |
| dim=0, | |
| ) | |
| assert x.min() >= 0.0 and x.max() <= 1.0 | |
| og_shape = x.shape | |
| x = rearrange(x, "L (c d) -> L c d", c=3) | |
| x = (x - mean) / std | |
| x = x.view(og_shape).to(torch.bfloat16) | |
| return x, grid_thw | |
| class Qwen25VLEncoder(torch.nn.Module): | |
| """Qwen2.5 VL encoder with pre/post processing compatible with our CA implementation""" | |
| def __init__( | |
| self, | |
| visual: "Qwen2_5_VisionTransformerPretrainedModel", | |
| ): | |
| super().__init__() | |
| self.visual = visual | |
| # Match training checkpoint: Conv3d is replaced by a Linear for t=1 images | |
| _replace_patch_embed_proj(self.visual) | |
| self.image_mean = torch.tensor(self.visual.config.image_mean).view(1, 3, 1) | |
| self.image_std = torch.tensor(self.visual.config.image_std).view(1, 3, 1) | |
| def forward( | |
| self, x: torch.Tensor | list[torch.Tensor] | |
| ) -> dict[str, torch.Tensor | list[torch.Tensor]]: | |
| x, grid_thw = prepare_for_qwen_encoder( | |
| x, mean=self.image_mean.to(x[0].device), std=self.image_std.to(x[0].device) | |
| ) | |
| grid_thw = grid_thw.type(torch.int) | |
| assert len(x) == grid_thw.prod(dim=1).sum() | |
| out = self.visual(x, grid_thw=grid_thw) | |
| split_sizes = (grid_thw.prod(dim=-1) // self.visual.spatial_merge_size**2).tolist() | |
| embeds = list(torch.split(out, split_sizes, dim=0)) # Ni * (seq, C) | |
| return {"image_embeds": embeds, "grid_thw": grid_thw} | |