File size: 10,123 Bytes
ce61544
 
 
611f9d7
ce61544
8cba5fe
ce61544
 
 
611f9d7
 
9c2126e
 
 
 
 
611f9d7
 
 
 
 
 
ce61544
 
 
 
 
 
 
 
 
9c2126e
 
 
ce61544
 
 
 
 
 
9c2126e
 
ce61544
 
 
 
 
 
 
 
 
 
 
 
 
9c2126e
 
 
 
ce61544
eb5a4ea
 
 
ce61544
 
 
 
 
9c2126e
 
 
ce61544
 
eb5a4ea
 
 
ce61544
eb5a4ea
ce61544
 
 
 
 
 
 
 
 
 
eb5a4ea
ce61544
 
 
 
eb5a4ea
ce61544
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
 
eb5a4ea
ce61544
 
 
 
 
 
 
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
9c2126e
 
 
ce61544
 
 
 
eb5a4ea
ce61544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb5a4ea
ce61544
 
 
 
 
 
 
 
9c2126e
 
ce61544
 
eb5a4ea
ce61544
 
 
 
 
eb5a4ea
 
 
ce61544
eb5a4ea
8cba5fe
 
ce61544
8cba5fe
eb5a4ea
ce61544
 
eb5a4ea
ce61544
 
 
 
 
eb5a4ea
ce61544
 
eb5a4ea
 
 
ce61544
eb5a4ea
ce61544
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
 
 
 
 
 
 
 
 
 
 
 
9c2126e
 
ce61544
9c2126e
ce61544
 
9c2126e
ce61544
 
 
 
9c2126e
eb5a4ea
ce61544
eb5a4ea
ce61544
 
eb5a4ea
ce61544
 
 
eb5a4ea
ce61544
 
eb5a4ea
ce61544
 
 
 
 
eb5a4ea
ce61544
 
 
 
 
eb5a4ea
ce61544
 
 
 
eb5a4ea
ce61544
eb5a4ea
ce61544
eb5a4ea
ce61544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c2126e
 
 
 
 
 
 
 
eb5a4ea
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "datasets>=4.0.0",
#     "httpx",
#     "huggingface-hub",
#     "setuptools",
#     "toolz",
# ]
#
# [tool.hf-jobs]
# # vllm, torch and transformers come from the image (PYTHONPATH). Listing them above makes uv
# # install newer PyPI copies plus their deps. PYTHONPATH shadows the copies, but the current
# # flashinfer-python wheel (0.7.2.post1) also ships a top-level b12x module that the image lacks.
# # The image's vllm imports it and crashes (missing flashinfer/experimental/b12x/_api.py).
# # Seen on v0.32.0 (2026-11-05); re-test before moving the tag. Local GPU: uv run --with vllm.
# image = "vllm/vllm-openai:v0.32.0"
# python = "/usr/bin/python3"
# env = { PYTHONPATH = "/usr/local/lib/python3.12/dist-packages" }
# flavor = "l4x1"
# secrets = ["HF_TOKEN"]
# timeout = "1h"
# ///
"""
Batch text classification using vLLM for efficient GPU inference.

This script loads a dataset from Hugging Face Hub, performs classification using
a BERT-style model via vLLM, and saves the results back to the Hub with predicted
labels and confidence scores.

Example usage:
    # Local execution (Linux + CUDA). The header leaves vllm, torch and
    # transformers to the HF Jobs image, so add vllm with --with.
    uv run --with vllm 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 (the header sets the vLLM image, flavor and HF_TOKEN secret)
    hf jobs uv run classify-dataset.py model/name input-dataset output-dataset
"""

import argparse
import logging
import os
import sys
from typing import Optional

import httpx
from datasets import load_dataset
from huggingface_hub import hf_hub_url, login
from toolz import concat, keymap, partition_all
from tqdm.auto import tqdm

# torch and vllm are imported inside the functions that use them, so
# `uv run classify-dataset.py --help` works without them. On HF Jobs they come from
# the vLLM image (see the header).

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


def check_gpu_availability():
    """Check if CUDA is available and log GPU information."""
    import torch
    import vllm

    if not torch.cuda.is_available():
        logger.error("CUDA is not available. This script requires a GPU.")
        logger.error(
            "Please run on a machine with NVIDIA GPU or use HF Jobs with GPU flavor."
        )
        sys.exit(1)

    gpu_name = torch.cuda.get_device_name(0)
    gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3
    logger.info(f"GPU detected: {gpu_name} with {gpu_memory:.1f} GB memory")
    logger.info(f"vLLM version: {vllm.__version__}")


def get_model_id2label(hub_model_id: str) -> Optional[dict[int, str]]:
    """Extract label mapping from model's config.json on Hugging Face Hub."""
    try:
        response = httpx.get(
            hf_hub_url(hub_model_id, filename="config.json"), follow_redirects=True
        )
        if response.status_code != 200:
            logger.warning(f"Could not fetch config.json for {hub_model_id}")
            return None

        data = response.json()
        id2label = data.get("id2label")

        if id2label is None:
            logger.info("No id2label mapping found in config.json")
            return None

        # Convert string keys to integers
        label_map = keymap(int, id2label)
        logger.info(f"Found label mapping: {label_map}")
        return label_map

    except Exception as e:
        logger.warning(f"Failed to parse config.json: {e}")
        return None


def get_top_label(output, label_map: Optional[dict[int, str]] = None):
    """
    Extract the top predicted label and confidence score from vLLM output.

    Args:
        output: vLLM ClassificationRequestOutput
        label_map: Optional mapping from label indices to label names

    Returns:
        Tuple of (label, confidence_score)
    """
    import torch
    import torch.nn.functional as F

    logits = torch.tensor(output.outputs.probs)
    probs = F.softmax(logits, dim=0)
    top_idx = torch.argmax(probs).item()
    top_prob = probs[top_idx].item()

    # Use label name if mapping available, otherwise use index
    label = label_map.get(top_idx, str(top_idx)) if label_map else str(top_idx)
    return label, top_prob


def main(
    hub_model_id: str,
    src_dataset_hub_id: str,
    output_dataset_hub_id: str,
    inference_column: str = "text",
    batch_size: int = 10_000,
    hf_token: Optional[str] = None,
):
    """
    Main classification pipeline.

    Args:
        hub_model_id: Hugging Face model ID for classification
        src_dataset_hub_id: Input dataset on Hugging Face Hub
        output_dataset_hub_id: Where to save results on Hugging Face Hub
        inference_column: Column name containing text to classify
        batch_size: Number of examples to process at once
        hf_token: Hugging Face authentication token
    """
    from vllm import LLM

    # GPU check
    check_gpu_availability()

    # Authentication
    HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
    if HF_TOKEN:
        login(token=HF_TOKEN)
    else:
        logger.error(
            "HF_TOKEN is required. Set via --hf-token or HF_TOKEN environment variable."
        )
        sys.exit(1)

    # vLLM auto-detects the sequence-classification runner from the model
    # architecture (*ForSequenceClassification); the task= arg was removed in 0.12.0.
    logger.info(f"Loading model: {hub_model_id}")
    llm = LLM(model=hub_model_id)

    # Get label mapping if available
    id2label = get_model_id2label(hub_model_id)

    # Load dataset
    logger.info(f"Loading dataset: {src_dataset_hub_id}")
    dataset = load_dataset(src_dataset_hub_id, split="train")
    total_examples = len(dataset)
    logger.info(f"Dataset loaded with {total_examples:,} examples")

    # Extract text column
    if inference_column not in dataset.column_names:
        logger.error(
            f"Column '{inference_column}' not found. Available columns: {dataset.column_names}"
        )
        sys.exit(1)

    prompts = dataset[inference_column]

    # Process in batches
    logger.info(f"Starting classification with batch size {batch_size:,}")
    all_results = []

    for batch in tqdm(
        list(partition_all(batch_size, prompts)),
        desc="Processing batches",
        unit="batch",
    ):
        batch_results = llm.classify(batch)
        all_results.append(batch_results)

    # Flatten results
    outputs = list(concat(all_results))

    # Extract labels and probabilities
    logger.info("Extracting predictions...")
    labels_and_probs = [get_top_label(output, id2label) for output in outputs]

    # Add results to dataset
    dataset = dataset.add_column("label", [label for label, _ in labels_and_probs])
    dataset = dataset.add_column("prob", [prob for _, prob in labels_and_probs])

    # Push to hub
    logger.info(f"Pushing results to: {output_dataset_hub_id}")
    dataset.push_to_hub(output_dataset_hub_id, token=HF_TOKEN)
    logger.info("✅ Classification complete!")


if __name__ == "__main__":
    if len(sys.argv) > 1:
        parser = argparse.ArgumentParser(
            description="Classify text data using vLLM and save results to Hugging Face Hub",
            formatter_class=argparse.RawDescriptionHelpFormatter,
            epilog="""
Examples:
  # Local GPU (Linux + CUDA): the header leaves vllm/torch/transformers to
  # the HF Jobs image, so add vllm with --with.
  # Basic usage
  uv run --with vllm classify-dataset.py model/name input-dataset output-dataset
  
  # With custom column and batch size
  uv run --with vllm classify-dataset.py model/name input-dataset output-dataset \\
    --inference-column prompt \\
    --batch-size 50000
  
  # Using environment variable for token
  HF_TOKEN=hf_xxx uv run --with vllm classify-dataset.py model/name input-dataset output-dataset
            """,
        )

        parser.add_argument(
            "hub_model_id",
            help="Hugging Face model ID for classification (e.g., bert-base-uncased)",
        )
        parser.add_argument(
            "src_dataset_hub_id",
            help="Input dataset on Hugging Face Hub (e.g., username/dataset-name)",
        )
        parser.add_argument(
            "output_dataset_hub_id", help="Output dataset name on Hugging Face Hub"
        )
        parser.add_argument(
            "--inference-column",
            type=str,
            default="text",
            help="Column containing text to classify (default: text)",
        )
        parser.add_argument(
            "--batch-size",
            type=int,
            default=10_000,
            help="Batch size for inference (default: 10,000)",
        )
        parser.add_argument(
            "--hf-token",
            type=str,
            help="Hugging Face token (can also use HF_TOKEN env var)",
        )

        args = parser.parse_args()

        main(
            hub_model_id=args.hub_model_id,
            src_dataset_hub_id=args.src_dataset_hub_id,
            output_dataset_hub_id=args.output_dataset_hub_id,
            inference_column=args.inference_column,
            batch_size=args.batch_size,
            hf_token=args.hf_token,
        )
    else:
        # Show HF Jobs example when run without arguments
        print("""
vLLM Classification Script
=========================

This script requires arguments. For usage information:
    uv run classify-dataset.py --help

Example HF Jobs command (the header sets the vLLM image, l4x1 and the HF_TOKEN secret):
    hf jobs uv run \\
        https://huggingface.co/datasets/uv-scripts/vllm/raw/main/classify-dataset.py \\
        davanstrien/ModernBERT-base-is-new-arxiv-dataset \\
        username/input-dataset \\
        username/output-dataset \\
        --inference-column text \\
        --batch-size 100000
        """)