File size: 7,321 Bytes
ce61544 403d2c6 ce61544 b09f138 ce61544 403d2c6 ce61544 b09f138 ce61544 8d25b68 ce61544 8d25b68 ce61544 8d25b68 ce61544 6a9905a ce61544 6a9905a d034c0d 6a9905a 8d25b68 6a9905a d034c0d 6a9905a d034c0d 6a9905a 8d25b68 6a9905a d034c0d 6a9905a d034c0d 6a9905a d034c0d 6a9905a 8d25b68 6a9905a 403d2c6 6a9905a ce61544 6a9905a 8d25b68 6a9905a 8d25b68 6a9905a 8d25b68 6a9905a ce61544 8d25b68 ce61544 b09f138 8d25b68 b09f138 ce61544 8d25b68 6a9905a 8d25b68 ce61544 b09f138 6a9905a b09f138 ce61544 6a9905a ce61544 8d25b68 6a9905a ce61544 8d25b68 ce61544 6a9905a 8d25b68 6a9905a ce61544 b09f138 8d25b68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
---
viewer: false
tags: [uv-script, vllm, gpu, inference, hf-jobs]
---
# vLLM Inference Scripts
Ready-to-run UV scripts for GPU-accelerated inference using [vLLM](https://github.com/vllm-project/vllm).
These scripts use [UV's inline script metadata](https://docs.astral.sh/uv/guides/scripts/) to automatically manage dependencies - just run with `uv run` and everything installs automatically!
## π Available Scripts
### vlm-classify.py
Vision Language Model (VLM) image classification with structured output constraints.
**Features:**
- πΌοΈ Process images through state-of-the-art VLMs (Qwen2-VL)
- π― Structured classification using vLLM's `GuidedDecodingParams`
- π Automatic image resizing to optimize token usage
- πΎ Memory-efficient lazy batch processing
- π·οΈ Simple CLI interface for defining classes
- π€ Direct integration with Hugging Face datasets
**Usage:**
```bash
# Basic classification
uv run vlm-classify.py \
username/input-dataset \
username/output-dataset \
--classes "document,photo,diagram,other"
# With custom prompt and image resizing
uv run vlm-classify.py \
username/input-dataset \
username/output-dataset \
--classes "index-card,manuscript,title-page,other" \
--prompt "What type of historical document is this?" \
--max-size 768
# Quick test with sample limit
uv run vlm-classify.py \
davanstrien/sloane-index-cards \
username/test-output \
--classes "index,content,other" \
--max-samples 10
```
**HF Jobs execution:**
```bash
hf jobs uv run \
--flavor a10g \
--image vllm/vllm-openai \
-s HF_TOKEN \
https://huggingface.co/datasets/uv-scripts/vllm/raw/main/vlm-classify.py \
username/input-dataset \
username/output-dataset \
--classes "title-page,content,index,other" \
--max-size 768
```
**Key Parameters:**
- `--classes`: Comma-separated list of classification categories (required)
- `--prompt`: Custom classification prompt (optional, auto-generated if not provided)
- `--max-size`: Maximum image dimension in pixels for resizing (reduces token count)
- `--model`: VLM model to use (default: Qwen/Qwen2-VL-7B-Instruct)
- `--batch-size`: Number of images to process at once (default: 8)
- `--max-samples`: Limit number of samples for testing
### classify-dataset.py
Batch text classification using BERT-style encoder models (e.g., BERT, RoBERTa, DeBERTa, ModernBERT) with vLLM's optimized inference engine.
**Note**: This script is specifically for encoder-only classification models, not generative LLMs.
**Features:**
- π High-throughput batch processing
- π·οΈ Automatic label mapping from model config
- π Confidence scores for predictions
- π€ Direct integration with Hugging Face Hub
**Usage:**
```bash
# Local execution (requires GPU)
uv run classify-dataset.py \
davanstrien/ModernBERT-base-is-new-arxiv-dataset \
username/input-dataset \
username/output-dataset \
--inference-column text \
--batch-size 10000
```
**HF Jobs execution:**
```bash
hf jobs uv run \
--flavor l4x1 \
--image vllm/vllm-openai \
https://huggingface.co/datasets/uv-scripts/vllm/resolve/main/classify-dataset.py \
davanstrien/ModernBERT-base-is-new-arxiv-dataset \
username/input-dataset \
username/output-dataset \
--inference-column text \
--batch-size 100000
```
### generate-responses.py
Generate responses for prompts using generative LLMs (e.g., Llama, Qwen, Mistral) with vLLM's high-performance inference engine.
**Features:**
- π¬ Automatic chat template application
- π Support for both chat messages and plain text prompts
- π Multi-GPU tensor parallelism support
- π Smart filtering for prompts exceeding context length
- π Comprehensive dataset cards with generation metadata
- β‘ HF Transfer enabled for fast model downloads
- ποΈ Full control over sampling parameters
- π― Sample limiting with `--max-samples` for testing
**Usage:**
```bash
# With chat-formatted messages (default)
uv run generate-responses.py \
username/input-dataset \
username/output-dataset \
--messages-column messages \
--max-tokens 1024
# With plain text prompts (NEW!)
uv run generate-responses.py \
username/input-dataset \
username/output-dataset \
--prompt-column question \
--max-tokens 1024 \
--max-samples 100
# With custom model and parameters
uv run generate-responses.py \
username/input-dataset \
username/output-dataset \
--model-id meta-llama/Llama-3.1-8B-Instruct \
--prompt-column text \
--temperature 0.9 \
--top-p 0.95 \
--max-model-len 8192
```
**HF Jobs execution (multi-GPU):**
```bash
hf jobs uv run \
--flavor l4x4 \
--image vllm/vllm-openai \
-e UV_PRERELEASE=if-necessary \
-s HF_TOKEN \
https://huggingface.co/datasets/uv-scripts/vllm/raw/main/generate-responses.py \
davanstrien/cards_with_prompts \
davanstrien/test-generated-responses \
--model-id Qwen/Qwen3-30B-A3B-Instruct-2507 \
--gpu-memory-utilization 0.9 \
--max-tokens 600 \
--max-model-len 8000
```
### Multi-GPU Tensor Parallelism
- Auto-detects available GPUs by default
- Use `--tensor-parallel-size` to manually specify
- Required for models larger than single GPU memory (e.g., 30B+ models)
### Handling Long Contexts
The generate-responses.py script includes smart prompt filtering:
- **Default behavior**: Skips prompts exceeding max_model_len
- **Use `--max-model-len`**: Limit context to reduce memory usage
- **Use `--no-skip-long-prompts`**: Fail on long prompts instead of skipping
- Skipped prompts receive empty responses and are logged
## π About vLLM
vLLM is a high-throughput inference engine optimized for:
- Fast model serving with PagedAttention
- Efficient batch processing
- Support for various model architectures
- Seamless integration with Hugging Face models
## π§ Technical Details
### UV Script Benefits
- **Zero setup**: Dependencies install automatically on first run
- **Reproducible**: Locked dependencies ensure consistent behavior
- **Self-contained**: Everything needed is in the script file
- **Direct execution**: Run from local files or URLs
### Dependencies
Scripts use UV's inline metadata for automatic dependency management:
```python
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "datasets",
# "flashinfer-python",
# "huggingface-hub[hf_transfer]",
# "torch",
# "transformers",
# "vllm",
# ]
# ///
```
For bleeding-edge features, use the `UV_PRERELEASE=if-necessary` environment variable to allow pre-release versions when needed.
### Docker Image
For HF Jobs, we recommend the official vLLM Docker image: `vllm/vllm-openai`
This image includes:
- Pre-installed CUDA libraries
- vLLM and all dependencies
- UV package manager
- Optimized for GPU inference
### Environment Variables
- `HF_TOKEN`: Your Hugging Face authentication token (auto-detected if logged in)
- `UV_PRERELEASE=if-necessary`: Allow pre-release packages when required
- `HF_HUB_ENABLE_HF_TRANSFER=1`: Automatically enabled for faster downloads
## π Resources
- [vLLM Documentation](https://docs.vllm.ai/)
- [UV Documentation](https://docs.astral.sh/uv/)
- [UV Scripts Organization](https://huggingface.co/uv-scripts)
|