File size: 9,307 Bytes
ce61544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "datasets",
#     "flashinfer-python",
#     "httpx",
#     "huggingface-hub[hf_transfer]",
#     "setuptools",
#     "toolz",
#     "torch",
#     "transformers",
#     "vllm",
# ]
#
# [[tool.uv.index]]
# url = "https://flashinfer.ai/whl/cu126/torch2.6"
#
# [[tool.uv.index]]
# url = "https://wheels.vllm.ai/nightly"
# ///
"""
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
    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 (see script output for full command)
    hfjobs run --flavor l4x1 ...
"""

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

import httpx
import torch
import torch.nn.functional as F
import vllm
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
from vllm import LLM

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."""
    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)
    """
    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
    """
    # 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)
    
    # Initialize vLLM with classification task
    logger.info(f"Loading model: {hub_model_id}")
    llm = LLM(model=hub_model_id, task="classify")
    
    # 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:
  # Basic usage
  uv run classify-dataset.py model/name input-dataset output-dataset
  
  # With custom column and batch size
  uv run 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 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:
    hfjobs run \\
        --flavor l4x1 \\
        --secret HF_TOKEN=\$(python -c "from huggingface_hub import HfFolder; print(HfFolder.get_token())") \\
        vllm/vllm-openai:latest \\
        /bin/bash -c '
            uv run 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
        ' \\
        --project vllm-classify \\
        --name my-classification-job
        """)