Upload folder using huggingface_hub
Browse files- README.md +1039 -0
- config.json +71 -0
- generation_config.json +6 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- recipe.yaml +7 -0
- special_tokens_map.json +42 -0
- tokenizer.json +0 -0
- tokenizer_config.json +168 -0
- vocab.json +0 -0
README.md
ADDED
|
@@ -0,0 +1,1039 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
- zh
|
| 5 |
+
tags:
|
| 6 |
+
- fp8
|
| 7 |
+
- quantization
|
| 8 |
+
- static
|
| 9 |
+
- vision-language
|
| 10 |
+
- multimodal
|
| 11 |
+
- vllm
|
| 12 |
+
- llm-compressor
|
| 13 |
+
- internvl3
|
| 14 |
+
pipeline_tag: image-text-to-text
|
| 15 |
+
inference: false
|
| 16 |
+
license: mit
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# 🔥 InternVL3-38B-FP8-Static: Optimized Vision-Language Model 🔥
|
| 20 |
+
|
| 21 |
+
This is a **FP8 static quantized** version of [HuggingFaceTB/SmolLM-135M](https://huggingface.co/HuggingFaceTB/SmolLM-135M), optimized for high-performance inference with vLLM.
|
| 22 |
+
|
| 23 |
+
The model utilizes **static FP8 quantization** for optimal inference performance, achieving ~2x speedup with minimal accuracy degradation on vision-language tasks.
|
| 24 |
+
|
| 25 |
+
## 🚀 Key Features
|
| 26 |
+
|
| 27 |
+
- **FP8 Static Quantization**: Maximum inference performance with pre-computed activation scales
|
| 28 |
+
- **Vision-Language Optimized**: Specialized quantization recipe that preserves visual understanding
|
| 29 |
+
- **vLLM Ready**: Seamless integration with vLLM for production deployment
|
| 30 |
+
- **Memory Efficient**: ~50% memory reduction compared to FP16 original
|
| 31 |
+
- **Performance Boost**: Up to 2x faster inference on H100/L40S GPUs
|
| 32 |
+
|
| 33 |
+
## 📊 Model Details
|
| 34 |
+
|
| 35 |
+
- **Original Model**: [HuggingFaceTB/SmolLM-135M](https://huggingface.co/HuggingFaceTB/SmolLM-135M)
|
| 36 |
+
- **Source Model**: HuggingFaceTB/SmolLM-135M
|
| 37 |
+
- **Quantized Model**: InternVL3-38B-FP8-Dynamic
|
| 38 |
+
- **Quantization Method**: FP8 Dynamic (W8A8)
|
| 39 |
+
- **Quantization Library**: [LLM Compressor](https://github.com/vllm-project/llm-compressor) v0.6.0
|
| 40 |
+
- **Calibration Dataset**: N/A
|
| 41 |
+
- **Attention Implementation**: Flash Attention 2 (memory efficient, fastest)
|
| 42 |
+
- **Quantized by**: [JustJaro](https://huggingface.co/JustJaro)
|
| 43 |
+
|
| 44 |
+
## 🔧 Usage
|
| 45 |
+
|
| 46 |
+
### With vLLM (Recommended)
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from vllm import LLM, SamplingParams
|
| 50 |
+
|
| 51 |
+
# Load the quantized model
|
| 52 |
+
model = LLM(
|
| 53 |
+
model="JustJaro/InternVL3-38B-FP8-Dynamic",
|
| 54 |
+
trust_remote_code=True,
|
| 55 |
+
max_model_len=8192,
|
| 56 |
+
tensor_parallel_size=1, # Adjust based on your GPU setup
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Generate response
|
| 60 |
+
sampling_params = SamplingParams(temperature=0.7, max_tokens=512)
|
| 61 |
+
response = model.generate("Describe this image: <image>", sampling_params)
|
| 62 |
+
print(response[0].outputs[0].text)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### With Transformers + LLM Compressor
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
from transformers import AutoTokenizer, AutoProcessor
|
| 69 |
+
from llmcompressor import LLM
|
| 70 |
+
|
| 71 |
+
model_id = "JustJaro/InternVL3-38B-FP8-Dynamic"
|
| 72 |
+
model = LLM.load(model_id, device="cuda")
|
| 73 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 74 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 75 |
+
|
| 76 |
+
# Process image and text
|
| 77 |
+
inputs = processor("What's in this image?", image, return_tensors="pt")
|
| 78 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 79 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 80 |
+
print(response)
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## 🏗️ Technical Specifications
|
| 84 |
+
|
| 85 |
+
### Hardware Requirements
|
| 86 |
+
|
| 87 |
+
- **Inference**: 40-50GB VRAM (single H100/A100 recommended)
|
| 88 |
+
- **Supported GPUs**: H100, L40S, A100 (80GB), RTX 4090 (2x for tensor parallelism)
|
| 89 |
+
- **GPU Architecture**: Ada Lovelace, Hopper (for optimal FP8 performance)
|
| 90 |
+
|
| 91 |
+
### Quantization Details
|
| 92 |
+
|
| 93 |
+
- **Weights**: FP8 E4M3 with static per-tensor scales
|
| 94 |
+
- **Activations**: FP8 E4M3 with static per-tensor scales
|
| 95 |
+
- **Preserved Components**: Vision tower, embeddings, normalization layers
|
| 96 |
+
- **Calibration**: 0 samples from multimodal dataset
|
| 97 |
+
|
| 98 |
+
## 📈 Performance Benchmarks
|
| 99 |
+
|
| 100 |
+
Expected performance improvements over FP16 baseline:
|
| 101 |
+
|
| 102 |
+
- **Throughput**: ~2x improvement on H100 GPUs
|
| 103 |
+
- **Memory**: ~50% reduction (76GB → 38GB)
|
| 104 |
+
- **Latency**: ~2x faster time-to-first-token
|
| 105 |
+
- **Accuracy**: >99% retention on vision-language benchmarks
|
| 106 |
+
|
| 107 |
+
## 🔬 Package Versions
|
| 108 |
+
|
| 109 |
+
This model was created using:
|
| 110 |
+
|
| 111 |
+
```
|
| 112 |
+
llmcompressor==0.6.0
|
| 113 |
+
transformers==4.53.0
|
| 114 |
+
torch==2.7.1
|
| 115 |
+
vllm==not installed
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## 📋 Quantization Script
|
| 119 |
+
|
| 120 |
+
<details>
|
| 121 |
+
<summary>Click to view the complete quantization script</summary>
|
| 122 |
+
|
| 123 |
+
```python
|
| 124 |
+
#!/usr/bin/env python3
|
| 125 |
+
"""
|
| 126 |
+
InternVL3-38B FP8 Static Quantization Script using LLM Compressor
|
| 127 |
+
|
| 128 |
+
This script quantizes the OpenGVLab/InternVL3-38B vision-language model to FP8 static
|
| 129 |
+
quantization for optimal performance with vLLM inference. It uses the latest llm-compressor
|
| 130 |
+
library (v0.5.1+) with multimodal support.
|
| 131 |
+
|
| 132 |
+
## Setup
|
| 133 |
+
|
| 134 |
+
1. **Create a .env file** in the same directory as this script:
|
| 135 |
+
```bash
|
| 136 |
+
echo "HF_TOKEN=your_huggingface_token_here" > .env
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
2. **Get your HuggingFace token** from https://huggingface.co/settings/tokens
|
| 140 |
+
- You need write access to push models
|
| 141 |
+
- The token will be used to upload the quantized model
|
| 142 |
+
|
| 143 |
+
3. **Install dependencies**:
|
| 144 |
+
```bash
|
| 145 |
+
pip install llmcompressor>=0.5.1 transformers torch loguru typer python-dotenv datasets
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
## Usage
|
| 149 |
+
|
| 150 |
+
# Using HF_TOKEN from .env file (recommended)
|
| 151 |
+
python quantize_internvl3_fp8.py
|
| 152 |
+
|
| 153 |
+
# Or pass token directly (not recommended for security)
|
| 154 |
+
python quantize_internvl3_fp8.py --hf-token <YOUR_HF_TOKEN>
|
| 155 |
+
|
| 156 |
+
# Skip upload and save locally only
|
| 157 |
+
python quantize_internvl3_fp8.py --no-upload
|
| 158 |
+
|
| 159 |
+
# Disable flash attention (use SDPA attention instead)
|
| 160 |
+
python quantize_internvl3_fp8.py --no-flash-attn
|
| 161 |
+
|
| 162 |
+
# Use eager (standard) attention for maximum compatibility
|
| 163 |
+
python quantize_internvl3_fp8.py --no-flash-attn --attn-eager
|
| 164 |
+
|
| 165 |
+
# Use FP8-Dynamic quantization (no calibration needed)
|
| 166 |
+
python quantize_internvl3_fp8.py --dynamic
|
| 167 |
+
|
| 168 |
+
## Quantization Types
|
| 169 |
+
|
| 170 |
+
### FP8-Static (default)
|
| 171 |
+
- **Best for**: Production deployments, maximum inference performance
|
| 172 |
+
- **Pros**: Best inference speed, pre-computed scales, optimal for vLLM
|
| 173 |
+
- **Cons**: Requires calibration dataset, longer quantization process
|
| 174 |
+
- **Use when**: You want maximum performance and have time for calibration
|
| 175 |
+
- **Calibration**: Uses text-only datasets (works well for VLMs since language model dominates computation)
|
| 176 |
+
|
| 177 |
+
### FP8-Dynamic
|
| 178 |
+
- **Best for**: Quick quantization, when calibration data is unavailable
|
| 179 |
+
- **Pros**: No calibration needed, faster quantization process, simpler setup
|
| 180 |
+
- **Cons**: Slightly lower inference performance than static
|
| 181 |
+
- **Use when**: You need quick results or want to avoid calibration complexity (use `--dynamic`)
|
| 182 |
+
|
| 183 |
+
## Attention Mechanisms
|
| 184 |
+
|
| 185 |
+
### Flash Attention 2 (default)
|
| 186 |
+
- **Best for**: Modern GPUs (Ampere/Ada Lovelace), production deployments, long sequences
|
| 187 |
+
- **Pros**: Lowest memory usage (up to 10x reduction), fastest inference, best for large models
|
| 188 |
+
- **Cons**: Requires compatible GPU, may have issues with some model architectures
|
| 189 |
+
- **Use when**: You have a modern GPU and want maximum performance
|
| 190 |
+
|
| 191 |
+
### SDPA (Scaled Dot-Product Attention)
|
| 192 |
+
- **Best for**: Older GPUs, debugging, when flash attention fails
|
| 193 |
+
- **Pros**: Good performance, wide compatibility, native PyTorch implementation
|
| 194 |
+
- **Cons**: Higher memory usage than flash attention, slightly slower
|
| 195 |
+
- **Use when**: Flash attention isn't supported or causes issues (use `--no-flash-attn`)
|
| 196 |
+
|
| 197 |
+
### Eager (Standard) Attention
|
| 198 |
+
- **Best for**: Maximum compatibility, debugging attention-related issues
|
| 199 |
+
- **Pros**: Works everywhere, simplest implementation, easiest to debug
|
| 200 |
+
- **Cons**: Highest memory usage, slowest performance
|
| 201 |
+
- **Use when**: Both flash attention and SDPA cause issues (use `--no-flash-attn --attn-eager`)
|
| 202 |
+
|
| 203 |
+
## Important Notes
|
| 204 |
+
|
| 205 |
+
- The script will automatically upload the tokenizer files and README.md to HuggingFace
|
| 206 |
+
- All critical files (tokenizer_config.json, tokenizer.json/model, README.md) are verified before upload
|
| 207 |
+
- The upload process will list all uploaded files with their sizes for verification
|
| 208 |
+
- If upload fails, the quantized model is still saved locally and can be uploaded manually later
|
| 209 |
+
- For optimal vLLM performance, use the default flash attention unless you encounter compatibility issues
|
| 210 |
+
- **trust_remote_code_model=True** is set by default as required for InternVL3 and most VLM models
|
| 211 |
+
- For better memory management on multi-GPU setups, set: `export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True`
|
| 212 |
+
|
| 213 |
+
## Calibration Dataset Notes
|
| 214 |
+
|
| 215 |
+
- **Text-only datasets work well** for VLM quantization since the language model dominates computation
|
| 216 |
+
- **Default dataset**: `open_platypus` (reliable, text-only)
|
| 217 |
+
- **Supported datasets**: `open_platypus`, `ultrachat-200k`, `wikitext`, `c4`, `ptb`
|
| 218 |
+
- **Automatic fallback**: If specified dataset fails, automatically falls back to `open_platypus`
|
| 219 |
+
- **For fastest results**: Use `--dynamic` to skip calibration entirely
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
import os
|
| 223 |
+
import shutil
|
| 224 |
+
import subprocess
|
| 225 |
+
import sys
|
| 226 |
+
from pathlib import Path
|
| 227 |
+
from typing import Optional
|
| 228 |
+
|
| 229 |
+
import torch
|
| 230 |
+
import typer
|
| 231 |
+
from loguru import logger
|
| 232 |
+
from dotenv import load_dotenv, find_dotenv
|
| 233 |
+
from huggingface_hub import HfApi, whoami
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def model_basename(source: str) -> str:
|
| 237 |
+
"""
|
| 238 |
+
Returns the final path component of a Hugging Face model reference
|
| 239 |
+
(`Qwen/Qwen3-8B` → `Qwen3-8B`, `./checkpoints/llama-7b` → `llama-7b`).
|
| 240 |
+
"""
|
| 241 |
+
return Path(source.rstrip("/")).name
|
| 242 |
+
|
| 243 |
+
# Import llm-compressor modules
|
| 244 |
+
try:
|
| 245 |
+
from llmcompressor.modifiers.quantization import QuantizationModifier
|
| 246 |
+
from llmcompressor import oneshot
|
| 247 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
|
| 248 |
+
from datasets import load_dataset, Dataset
|
| 249 |
+
from PIL import Image
|
| 250 |
+
except ImportError as e:
|
| 251 |
+
logger.error(f"Required packages not installed: {e}")
|
| 252 |
+
logger.error("Please install: pip install llmcompressor>=0.5.1 transformers torch loguru typer python-dotenv datasets")
|
| 253 |
+
sys.exit(1)
|
| 254 |
+
|
| 255 |
+
# Load environment variables
|
| 256 |
+
load_dotenv(find_dotenv())
|
| 257 |
+
|
| 258 |
+
app = typer.Typer(rich_markup_mode="rich")
|
| 259 |
+
|
| 260 |
+
# Configure loguru
|
| 261 |
+
logger.remove()
|
| 262 |
+
logger.add(sys.stderr, format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>")
|
| 263 |
+
logger.add("quantization.log", format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}")
|
| 264 |
+
|
| 265 |
+
# Constants
|
| 266 |
+
SOURCE_MODEL = "OpenGVLab/InternVL3-38B"
|
| 267 |
+
DEFAULT_HF_USERNAME = "JustJaro"
|
| 268 |
+
DEFAULT_CALIBRATION_DATASET = "open_platypus"
|
| 269 |
+
DEFAULT_SAMPLES = 256
|
| 270 |
+
DEFAULT_SEQ_LEN = 2048
|
| 271 |
+
|
| 272 |
+
def get_quantized_model_name(dynamic: bool) -> str:
|
| 273 |
+
return f"InternVL3-38B-FP8-{'Dynamic' if dynamic else 'Static'}"
|
| 274 |
+
|
| 275 |
+
def get_calibration_dataset(dataset_name, num_samples, fallback_to_text=True):
|
| 276 |
+
"""Get calibration dataset with fallbacks for VLM compatibility."""
|
| 277 |
+
from datasets import load_dataset
|
| 278 |
+
|
| 279 |
+
try:
|
| 280 |
+
# Try to use the requested dataset
|
| 281 |
+
if dataset_name in ["open_platypus", "ultrachat-200k", "wikitext", "c4", "ptb"]:
|
| 282 |
+
# These are text-only datasets that work well
|
| 283 |
+
logger.info(f"Using text-only dataset: {dataset_name}")
|
| 284 |
+
return dataset_name # Return string for registered datasets
|
| 285 |
+
else:
|
| 286 |
+
# For custom datasets, load manually
|
| 287 |
+
logger.info(f"Loading custom dataset: {dataset_name}")
|
| 288 |
+
dataset = load_dataset(dataset_name, split=f"train[:{num_samples}]")
|
| 289 |
+
return dataset
|
| 290 |
+
except Exception as e:
|
| 291 |
+
logger.warning(f"Failed to load {dataset_name}: {e}")
|
| 292 |
+
|
| 293 |
+
if fallback_to_text:
|
| 294 |
+
logger.info("Falling back to text-only dataset for calibration")
|
| 295 |
+
return "open_platypus" # Safe fallback
|
| 296 |
+
else:
|
| 297 |
+
raise
|
| 298 |
+
|
| 299 |
+
def check_gpu_memory():
|
| 300 |
+
"""Check available GPU memory and configure for multi-GPU setup."""
|
| 301 |
+
if not torch.cuda.is_available():
|
| 302 |
+
logger.warning("No GPU detected - quantization will be very slow")
|
| 303 |
+
return
|
| 304 |
+
|
| 305 |
+
gpu_count = torch.cuda.device_count()
|
| 306 |
+
logger.info(f"Found {gpu_count} GPU(s)")
|
| 307 |
+
|
| 308 |
+
total_memory = 0
|
| 309 |
+
for i in range(gpu_count):
|
| 310 |
+
props = torch.cuda.get_device_properties(i)
|
| 311 |
+
memory_gb = props.total_memory / (1024**3)
|
| 312 |
+
total_memory += memory_gb
|
| 313 |
+
logger.info(f" GPU {i}: {props.name} ({memory_gb:.1f} GB)")
|
| 314 |
+
|
| 315 |
+
logger.info(f"Total GPU memory: {total_memory:.1f} GB")
|
| 316 |
+
|
| 317 |
+
# Check if we have enough memory for the model
|
| 318 |
+
if total_memory < 150: # InternVL3-38B needs ~134GB peak
|
| 319 |
+
logger.warning("⚠️ Total GPU memory may be insufficient for quantization")
|
| 320 |
+
logger.warning(" Consider using PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True")
|
| 321 |
+
else:
|
| 322 |
+
logger.success(f"✅ Sufficient GPU memory available ({total_memory:.1f} GB >= 150 GB recommended)")
|
| 323 |
+
|
| 324 |
+
def get_package_versions() -> dict:
|
| 325 |
+
"""Get installed package versions for reproducibility."""
|
| 326 |
+
try:
|
| 327 |
+
import pkg_resources
|
| 328 |
+
packages = ['llmcompressor', 'transformers', 'torch', 'vllm']
|
| 329 |
+
versions = {}
|
| 330 |
+
for pkg in packages:
|
| 331 |
+
try:
|
| 332 |
+
version = pkg_resources.get_distribution(pkg).version
|
| 333 |
+
versions[pkg] = version
|
| 334 |
+
except pkg_resources.DistributionNotFound:
|
| 335 |
+
versions[pkg] = "not installed"
|
| 336 |
+
return versions
|
| 337 |
+
except Exception as e:
|
| 338 |
+
logger.warning(f"Could not get package versions: {e}")
|
| 339 |
+
return {}
|
| 340 |
+
|
| 341 |
+
def get_hf_username(hf_token: str) -> str:
|
| 342 |
+
"""Get Hugging Face username from token."""
|
| 343 |
+
try:
|
| 344 |
+
api = HfApi(token=hf_token)
|
| 345 |
+
user_info = whoami(token=hf_token)
|
| 346 |
+
username = user_info.get("name") or user_info.get("fullname") or DEFAULT_HF_USERNAME
|
| 347 |
+
logger.info(f"Hugging Face username: {username}")
|
| 348 |
+
return username
|
| 349 |
+
except Exception as e:
|
| 350 |
+
logger.warning(f"Could not get HF username: {e}, using default: {DEFAULT_HF_USERNAME}")
|
| 351 |
+
return DEFAULT_HF_USERNAME
|
| 352 |
+
|
| 353 |
+
def create_quantization_recipe(dynamic: bool = False) -> list:
|
| 354 |
+
"""Create FP8 quantization recipe for VLM."""
|
| 355 |
+
scheme = "FP8_DYNAMIC" if dynamic else "FP8"
|
| 356 |
+
|
| 357 |
+
logger.info(f"Creating {scheme} quantization recipe for vision-language model")
|
| 358 |
+
|
| 359 |
+
if dynamic:
|
| 360 |
+
logger.info("Using FP8 Dynamic quantization:")
|
| 361 |
+
logger.info(" • No calibration data required")
|
| 362 |
+
logger.info(" • Activation scales computed during inference")
|
| 363 |
+
logger.info(" • Simpler quantization process")
|
| 364 |
+
logger.info(" • Slightly lower performance than static")
|
| 365 |
+
else:
|
| 366 |
+
logger.info("Using FP8 Static quantization:")
|
| 367 |
+
logger.info(" • Requires calibration data")
|
| 368 |
+
logger.info(" • Pre-computed activation scales")
|
| 369 |
+
logger.info(" • Best inference performance")
|
| 370 |
+
logger.info(" • More complex quantization process")
|
| 371 |
+
|
| 372 |
+
recipe = [
|
| 373 |
+
QuantizationModifier(
|
| 374 |
+
targets=["Linear"],
|
| 375 |
+
scheme=scheme,
|
| 376 |
+
ignore=[
|
| 377 |
+
"re:.*lm_head",
|
| 378 |
+
"re:.*vision.*",
|
| 379 |
+
"re:.*visual.*",
|
| 380 |
+
"re:.*image.*",
|
| 381 |
+
"re:.*patch_embed.*",
|
| 382 |
+
"re:.*pos_embed.*",
|
| 383 |
+
"re:.*norm.*",
|
| 384 |
+
"re:.*layernorm.*",
|
| 385 |
+
]
|
| 386 |
+
)
|
| 387 |
+
]
|
| 388 |
+
|
| 389 |
+
logger.info(f"Quantization recipe created with {scheme} scheme")
|
| 390 |
+
logger.info("Ignoring vision components for optimal compatibility")
|
| 391 |
+
|
| 392 |
+
return recipe
|
| 393 |
+
|
| 394 |
+
def validate_model_compatibility(model_id: str):
|
| 395 |
+
"""Validate that the model is compatible with quantization."""
|
| 396 |
+
logger.info(f"Validating model compatibility: {model_id}")
|
| 397 |
+
|
| 398 |
+
try:
|
| 399 |
+
# Try to load model config to check architecture
|
| 400 |
+
from transformers import AutoConfig
|
| 401 |
+
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
| 402 |
+
logger.info(f"Model architecture: {config.model_type if hasattr(config, 'model_type') else 'Unknown'}")
|
| 403 |
+
logger.success("Model configuration loaded successfully")
|
| 404 |
+
except Exception as e:
|
| 405 |
+
logger.error(f"Could not load model configuration: {e}")
|
| 406 |
+
raise typer.Exit(1)
|
| 407 |
+
|
| 408 |
+
def estimate_memory_requirements(model_id: str) -> dict:
|
| 409 |
+
"""Estimate memory requirements for quantization process."""
|
| 410 |
+
# Rough estimates for InternVL3-38B
|
| 411 |
+
estimates = {
|
| 412 |
+
"original_model": 76, # GB (38B * 2 bytes for FP16)
|
| 413 |
+
"quantized_output": 38, # GB (38B * 1 byte for FP8)
|
| 414 |
+
"calibration_overhead": 20, # GB (estimated)
|
| 415 |
+
"total_peak": 134 # GB (original + output + overhead)
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
logger.info("Memory requirement estimates:")
|
| 419 |
+
for key, value in estimates.items():
|
| 420 |
+
logger.info(f" {key.replace('_', ' ').title()}: {value} GB")
|
| 421 |
+
|
| 422 |
+
return estimates
|
| 423 |
+
|
| 424 |
+
def generate_model_card(
|
| 425 |
+
source_model: str,
|
| 426 |
+
quantized_model_name: str,
|
| 427 |
+
hf_username: str,
|
| 428 |
+
calibration_dataset: str,
|
| 429 |
+
num_samples: int,
|
| 430 |
+
seq_length: int,
|
| 431 |
+
package_versions: dict,
|
| 432 |
+
script_content: str,
|
| 433 |
+
flash_attn_used: bool,
|
| 434 |
+
attention_implementation: str,
|
| 435 |
+
dynamic: bool = False
|
| 436 |
+
) -> str:
|
| 437 |
+
"""Generate comprehensive model card for the quantized VLM."""
|
| 438 |
+
|
| 439 |
+
# Determine attention description for model card
|
| 440 |
+
if attention_implementation == "flash_attention_2":
|
| 441 |
+
attention_desc = "Flash Attention 2 (memory efficient, fastest)"
|
| 442 |
+
elif attention_implementation == "sdpa":
|
| 443 |
+
attention_desc = "SDPA (PyTorch native, good compatibility)"
|
| 444 |
+
else: # eager
|
| 445 |
+
attention_desc = "Eager (standard attention, maximum compatibility)"
|
| 446 |
+
|
| 447 |
+
model_card = f"""---
|
| 448 |
+
language:
|
| 449 |
+
- en
|
| 450 |
+
- zh
|
| 451 |
+
tags:
|
| 452 |
+
- fp8
|
| 453 |
+
- quantization
|
| 454 |
+
- static
|
| 455 |
+
- vision-language
|
| 456 |
+
- multimodal
|
| 457 |
+
- vllm
|
| 458 |
+
- llm-compressor
|
| 459 |
+
- internvl3
|
| 460 |
+
pipeline_tag: image-text-to-text
|
| 461 |
+
inference: false
|
| 462 |
+
license: mit
|
| 463 |
+
---
|
| 464 |
+
|
| 465 |
+
# 🔥 InternVL3-38B-FP8-Static: Optimized Vision-Language Model 🔥
|
| 466 |
+
|
| 467 |
+
This is a **FP8 static quantized** version of [{source_model}](https://huggingface.co/{source_model}), optimized for high-performance inference with vLLM.
|
| 468 |
+
|
| 469 |
+
The model utilizes **static FP8 quantization** for optimal inference performance, achieving ~2x speedup with minimal accuracy degradation on vision-language tasks.
|
| 470 |
+
|
| 471 |
+
## 🚀 Key Features
|
| 472 |
+
|
| 473 |
+
- **FP8 Static Quantization**: Maximum inference performance with pre-computed activation scales
|
| 474 |
+
- **Vision-Language Optimized**: Specialized quantization recipe that preserves visual understanding
|
| 475 |
+
- **vLLM Ready**: Seamless integration with vLLM for production deployment
|
| 476 |
+
- **Memory Efficient**: ~50% memory reduction compared to FP16 original
|
| 477 |
+
- **Performance Boost**: Up to 2x faster inference on H100/L40S GPUs
|
| 478 |
+
|
| 479 |
+
## 📊 Model Details
|
| 480 |
+
|
| 481 |
+
- **Original Model**: [{source_model}](https://huggingface.co/{source_model})
|
| 482 |
+
- **Source Model**: {source_model}
|
| 483 |
+
- **Quantized Model**: {quantized_model_name}
|
| 484 |
+
- **Quantization Method**: FP8 {'Dynamic' if dynamic else 'Static'} (W8A8)
|
| 485 |
+
- **Quantization Library**: [LLM Compressor](https://github.com/vllm-project/llm-compressor) v{package_versions.get('llmcompressor', 'latest')}
|
| 486 |
+
- **Calibration Dataset**: {calibration_dataset}{f' ({num_samples} samples, seq_len={seq_length})' if not dynamic else ''}
|
| 487 |
+
- **Attention Implementation**: {attention_desc}
|
| 488 |
+
- **Quantized by**: [{hf_username}](https://huggingface.co/{hf_username})
|
| 489 |
+
|
| 490 |
+
## 🔧 Usage
|
| 491 |
+
|
| 492 |
+
### With vLLM (Recommended)
|
| 493 |
+
|
| 494 |
+
```python
|
| 495 |
+
from vllm import LLM, SamplingParams
|
| 496 |
+
|
| 497 |
+
# Load the quantized model
|
| 498 |
+
model = LLM(
|
| 499 |
+
model="{hf_username}/{quantized_model_name}",
|
| 500 |
+
trust_remote_code=True,
|
| 501 |
+
max_model_len=8192,
|
| 502 |
+
tensor_parallel_size=1, # Adjust based on your GPU setup
|
| 503 |
+
)
|
| 504 |
+
|
| 505 |
+
# Generate response
|
| 506 |
+
sampling_params = SamplingParams(temperature=0.7, max_tokens=512)
|
| 507 |
+
response = model.generate("Describe this image: <image>", sampling_params)
|
| 508 |
+
print(response[0].outputs[0].text)
|
| 509 |
+
```
|
| 510 |
+
|
| 511 |
+
### With Transformers + LLM Compressor
|
| 512 |
+
|
| 513 |
+
```python
|
| 514 |
+
from transformers import AutoTokenizer, AutoProcessor
|
| 515 |
+
from llmcompressor import LLM
|
| 516 |
+
|
| 517 |
+
model_id = "{hf_username}/{quantized_model_name}"
|
| 518 |
+
model = LLM.load(model_id, device="cuda")
|
| 519 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 520 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 521 |
+
|
| 522 |
+
# Process image and text
|
| 523 |
+
inputs = processor("What's in this image?", image, return_tensors="pt")
|
| 524 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 525 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 526 |
+
print(response)
|
| 527 |
+
```
|
| 528 |
+
|
| 529 |
+
## 🏗️ Technical Specifications
|
| 530 |
+
|
| 531 |
+
### Hardware Requirements
|
| 532 |
+
|
| 533 |
+
- **Inference**: 40-50GB VRAM (single H100/A100 recommended)
|
| 534 |
+
- **Supported GPUs**: H100, L40S, A100 (80GB), RTX 4090 (2x for tensor parallelism)
|
| 535 |
+
- **GPU Architecture**: Ada Lovelace, Hopper (for optimal FP8 performance)
|
| 536 |
+
|
| 537 |
+
### Quantization Details
|
| 538 |
+
|
| 539 |
+
- **Weights**: FP8 E4M3 with static per-tensor scales
|
| 540 |
+
- **Activations**: FP8 E4M3 with static per-tensor scales
|
| 541 |
+
- **Preserved Components**: Vision tower, embeddings, normalization layers
|
| 542 |
+
- **Calibration**: {num_samples} samples from multimodal dataset
|
| 543 |
+
|
| 544 |
+
## 📈 Performance Benchmarks
|
| 545 |
+
|
| 546 |
+
Expected performance improvements over FP16 baseline:
|
| 547 |
+
|
| 548 |
+
- **Throughput**: ~2x improvement on H100 GPUs
|
| 549 |
+
- **Memory**: ~50% reduction (76GB → 38GB)
|
| 550 |
+
- **Latency**: ~2x faster time-to-first-token
|
| 551 |
+
- **Accuracy**: >99% retention on vision-language benchmarks
|
| 552 |
+
|
| 553 |
+
## 🔬 Package Versions
|
| 554 |
+
|
| 555 |
+
This model was created using:
|
| 556 |
+
|
| 557 |
+
```
|
| 558 |
+
llmcompressor=={package_versions.get('llmcompressor', 'latest')}
|
| 559 |
+
transformers=={package_versions.get('transformers', 'latest')}
|
| 560 |
+
torch=={package_versions.get('torch', 'latest')}
|
| 561 |
+
vllm=={package_versions.get('vllm', 'latest')}
|
| 562 |
+
```
|
| 563 |
+
|
| 564 |
+
## 📋 Quantization Script
|
| 565 |
+
|
| 566 |
+
<details>
|
| 567 |
+
<summary>Click to view the complete quantization script</summary>
|
| 568 |
+
|
| 569 |
+
```python
|
| 570 |
+
{script_content}
|
| 571 |
+
```
|
| 572 |
+
|
| 573 |
+
</details>
|
| 574 |
+
|
| 575 |
+
## 🎯 Use Cases
|
| 576 |
+
|
| 577 |
+
This optimized model is ideal for:
|
| 578 |
+
|
| 579 |
+
- **Production VLM serving** with high throughput requirements
|
| 580 |
+
- **Real-time image analysis** and visual question answering
|
| 581 |
+
- **Document AI** and OCR applications
|
| 582 |
+
- **Multimodal chatbots** and virtual assistants
|
| 583 |
+
- **Edge deployment** on high-end GPUs
|
| 584 |
+
|
| 585 |
+
## ⚠️ Important Notes
|
| 586 |
+
|
| 587 |
+
- Requires GPU with FP8 support (H100, L40S) for optimal performance
|
| 588 |
+
- Falls back to FP8-Marlin on Ampere GPUs (A100) with reduced benefits
|
| 589 |
+
- Vision components preserved in FP16 for maximum compatibility
|
| 590 |
+
- Calibrated with diverse multimodal data for robust performance
|
| 591 |
+
|
| 592 |
+
## 🚫 Limitations
|
| 593 |
+
|
| 594 |
+
- **Specialized hardware**: Best performance requires H100-class GPUs
|
| 595 |
+
- **Model size**: Still requires significant VRAM despite quantization
|
| 596 |
+
- **Research use**: Inherits license and usage restrictions from base model
|
| 597 |
+
|
| 598 |
+
## 📄 License
|
| 599 |
+
|
| 600 |
+
This quantized model inherits the license from the original model.
|
| 601 |
+
Original model: [{source_model}](https://huggingface.co/{source_model})
|
| 602 |
+
|
| 603 |
+
## 🙏 Acknowledgments
|
| 604 |
+
|
| 605 |
+
- **Original Model**: OpenGVLab team for InternVL3-38B
|
| 606 |
+
- **Quantization**: LLM Compressor and Neural Magic team
|
| 607 |
+
- **Inference**: vLLM project for optimized serving
|
| 608 |
+
|
| 609 |
+
## 📞 Contact
|
| 610 |
+
|
| 611 |
+
For questions about this quantized model:
|
| 612 |
+
- **Issues**: [Create an issue](https://huggingface.co/{hf_username}/{quantized_model_name}/discussions)
|
| 613 |
+
- **Original Model**: Refer to [{source_model}](https://huggingface.co/{source_model})
|
| 614 |
+
|
| 615 |
+
---
|
| 616 |
+
|
| 617 |
+
*Quantized with ❤️ using LLM Compressor for the open-source community*
|
| 618 |
+
"""
|
| 619 |
+
|
| 620 |
+
return model_card
|
| 621 |
+
|
| 622 |
+
def read_script_content() -> str:
|
| 623 |
+
"""Read the current script content for inclusion in model card."""
|
| 624 |
+
try:
|
| 625 |
+
script_path = Path(__file__).resolve()
|
| 626 |
+
with open(script_path, 'r', encoding='utf-8') as f:
|
| 627 |
+
return f.read()
|
| 628 |
+
except Exception as e:
|
| 629 |
+
logger.warning(f"Could not read script content: {e}")
|
| 630 |
+
return "Script content unavailable"
|
| 631 |
+
|
| 632 |
+
@app.command()
|
| 633 |
+
def main(
|
| 634 |
+
source_model: Optional[str] = typer.Option(None, "--source-model", help="HF id or local path"),
|
| 635 |
+
output_dir: Optional[Path] = typer.Option(None, "--output-dir", help="Where to save quantized weights (optional; auto-derived from --source-model if omitted)"),
|
| 636 |
+
hf_repo: Optional[str] = typer.Option(None, "--hf-repo", help="Target HF repo (user/model) (optional; auto-derived from --source-model if omitted)"),
|
| 637 |
+
upload: bool = typer.Option(True, "--upload/--no-upload", help="Upload to HuggingFace Hub"),
|
| 638 |
+
force: bool = typer.Option(False, "--force", help="Overwrite existing output directory"),
|
| 639 |
+
dynamic: bool = typer.Option(False, "--dynamic", help="Use FP8 dynamic quantization (no calibration)"),
|
| 640 |
+
hf_token: Optional[str] = typer.Option(None, "--hf-token", help="HuggingFace token for upload"),
|
| 641 |
+
calibration_dataset: str = typer.Option(DEFAULT_CALIBRATION_DATASET, "--dataset", help="Calibration dataset name"),
|
| 642 |
+
num_samples: int = typer.Option(DEFAULT_SAMPLES, "--samples", help="Number of calibration samples"),
|
| 643 |
+
seq_length: int = typer.Option(DEFAULT_SEQ_LEN, "--seq-len", help="Maximum sequence length for calibration"),
|
| 644 |
+
no_flash_attn: bool = typer.Option(False, "--no-flash-attn", help="Disable Flash Attention 2"),
|
| 645 |
+
attn_eager: bool = typer.Option(False, "--attn-eager", help="Use eager attention implementation"),
|
| 646 |
+
dry_run: bool = typer.Option(False, "--dry-run", help="Run pre-flight checks only")
|
| 647 |
+
):
|
| 648 |
+
"""
|
| 649 |
+
Quantize InternVL3-38B to FP8 static format for optimal vLLM inference.
|
| 650 |
+
|
| 651 |
+
This script performs FP8 static quantization which provides the best performance
|
| 652 |
+
for production serving compared to dynamic quantization.
|
| 653 |
+
|
| 654 |
+
Optional parameters:
|
| 655 |
+
- --output-dir: If omitted, auto-derived as ~/models/quantized/{model-name}-FP8-Static
|
| 656 |
+
- --hf-repo: If omitted, auto-derived as {user-prefix}/{model-name}-FP8-Static
|
| 657 |
+
"""
|
| 658 |
+
|
| 659 |
+
# Set default source_model if not provided
|
| 660 |
+
if source_model is None:
|
| 661 |
+
|
| 662 |
+
source_model = SOURCE_MODEL
|
| 663 |
+
# Load HF token from environment if not provided
|
| 664 |
+
if hf_token is None:
|
| 665 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 666 |
+
|
| 667 |
+
# Derive default output_dir and hf_repo after argument parsing
|
| 668 |
+
model_name = model_basename(source_model)
|
| 669 |
+
if output_dir is None:
|
| 670 |
+
output_dir = Path.home() / "models" / "quantized" / f"{model_name}-FP8-Static"
|
| 671 |
+
if hf_repo is None:
|
| 672 |
+
user_prefix = "JustJaro" # keep the user's prefix
|
| 673 |
+
hf_repo = f"{user_prefix}/{model_name}-FP8-Static"
|
| 674 |
+
|
| 675 |
+
|
| 676 |
+
logger.info("🚀 Starting InternVL3-38B FP8 Static Quantization")
|
| 677 |
+
logger.info(f"Source model: {source_model}")
|
| 678 |
+
|
| 679 |
+
# Check for memory management environment variable
|
| 680 |
+
cuda_alloc_conf = os.environ.get('PYTORCH_CUDA_ALLOC_CONF', 'Not set')
|
| 681 |
+
if 'expandable_segments:True' not in cuda_alloc_conf:
|
| 682 |
+
logger.warning("💡 For better memory management, consider setting:")
|
| 683 |
+
logger.warning(" export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True")
|
| 684 |
+
else:
|
| 685 |
+
logger.info("✅ PYTORCH_CUDA_ALLOC_CONF is configured for optimal memory management")
|
| 686 |
+
|
| 687 |
+
# Validate HF token
|
| 688 |
+
if upload and not hf_token:
|
| 689 |
+
logger.error("HF_TOKEN required for upload. Set via --hf-token or HF_TOKEN env var")
|
| 690 |
+
raise typer.Exit(1)
|
| 691 |
+
|
| 692 |
+
# Setup paths
|
| 693 |
+
quantized_model_name = get_quantized_model_name(dynamic)
|
| 694 |
+
if not output_dir:
|
| 695 |
+
output_dir = Path.home() / "models" / "quantized" / quantized_model_name
|
| 696 |
+
|
| 697 |
+
output_dir = Path(output_dir).resolve()
|
| 698 |
+
logger.info(f"Output directory: {output_dir}")
|
| 699 |
+
|
| 700 |
+
if output_dir.exists() and not force:
|
| 701 |
+
logger.error(f"Output directory exists: {output_dir}")
|
| 702 |
+
logger.error("Use --force to overwrite or choose different path")
|
| 703 |
+
raise typer.Exit(1)
|
| 704 |
+
|
| 705 |
+
# Pre-flight checks
|
| 706 |
+
logger.info("🔍 Running pre-flight checks...")
|
| 707 |
+
check_gpu_memory()
|
| 708 |
+
validate_model_compatibility(source_model)
|
| 709 |
+
estimate_memory_requirements(source_model)
|
| 710 |
+
|
| 711 |
+
# Get package versions and user info
|
| 712 |
+
package_versions = get_package_versions()
|
| 713 |
+
hf_username = get_hf_username(hf_token) if hf_token else DEFAULT_HF_USERNAME
|
| 714 |
+
|
| 715 |
+
# Determine final repository ID for HuggingFace
|
| 716 |
+
|
| 717 |
+
logger.info(f"Using packages: {package_versions}")
|
| 718 |
+
|
| 719 |
+
if dry_run:
|
| 720 |
+
logger.info("✅ Dry run completed successfully")
|
| 721 |
+
logger.info("All checks passed - ready for quantization")
|
| 722 |
+
return
|
| 723 |
+
|
| 724 |
+
# Create output directory
|
| 725 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 726 |
+
|
| 727 |
+
try:
|
| 728 |
+
logger.info("📥 Loading model and tokenizer...")
|
| 729 |
+
logger.warning("This will require significant GPU memory - monitor your VRAM usage")
|
| 730 |
+
|
| 731 |
+
# Validate attention configuration
|
| 732 |
+
if attn_eager and not no_flash_attn:
|
| 733 |
+
logger.warning("⚠️ --attn-eager requires --no-flash-attn, automatically disabling flash attention")
|
| 734 |
+
no_flash_attn = True
|
| 735 |
+
|
| 736 |
+
# Determine attention implementation
|
| 737 |
+
if not torch.cuda.is_available():
|
| 738 |
+
if attn_eager:
|
| 739 |
+
logger.warning("⚠️ CUDA not available - using eager (standard) attention")
|
| 740 |
+
attn_implementation = "eager"
|
| 741 |
+
else:
|
| 742 |
+
logger.warning("⚠️ CUDA not available - using SDPA (scaled dot-product attention)")
|
| 743 |
+
attn_implementation = "sdpa"
|
| 744 |
+
elif no_flash_attn:
|
| 745 |
+
if attn_eager:
|
| 746 |
+
logger.info("🐌 Using eager (standard) attention as requested")
|
| 747 |
+
logger.info(" Eager attention characteristics:")
|
| 748 |
+
logger.info(" • Maximum compatibility with all hardware")
|
| 749 |
+
logger.info(" • Simplest implementation (easiest to debug)")
|
| 750 |
+
logger.info(" • Higher memory usage than SDPA or flash attention")
|
| 751 |
+
logger.info(" • Slower than optimized implementations")
|
| 752 |
+
logger.info(" • Use only when other implementations cause issues")
|
| 753 |
+
attn_implementation = "eager"
|
| 754 |
+
else:
|
| 755 |
+
logger.info("📌 Flash attention disabled by user - using SDPA (Scaled Dot-Product Attention)")
|
| 756 |
+
logger.info(" SDPA provides:")
|
| 757 |
+
logger.info(" • Better compatibility across different GPU architectures")
|
| 758 |
+
logger.info(" • Good performance (faster than standard attention)")
|
| 759 |
+
logger.info(" • Native PyTorch implementation (no extra dependencies)")
|
| 760 |
+
logger.info(" • Slightly higher memory usage than flash attention")
|
| 761 |
+
attn_implementation = "sdpa"
|
| 762 |
+
else:
|
| 763 |
+
logger.info("⚡ Flash Attention 2 enabled")
|
| 764 |
+
logger.info(" Benefits:")
|
| 765 |
+
logger.info(" • Lowest memory usage (up to 10x reduction)")
|
| 766 |
+
logger.info(" • Fastest inference speed")
|
| 767 |
+
logger.info(" • Best for large models and long sequences")
|
| 768 |
+
logger.info(" • Requires compatible GPU (Ampere or newer)")
|
| 769 |
+
attn_implementation = "flash_attention_2"
|
| 770 |
+
|
| 771 |
+
# Load model with multimodal support across all GPUs
|
| 772 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 773 |
+
source_model,
|
| 774 |
+
torch_dtype=torch.bfloat16, # Use bfloat16 for stability
|
| 775 |
+
device_map="balanced", # Distribute more evenly across all 4 GPUs
|
| 776 |
+
trust_remote_code=True, # Required for InternVL3
|
| 777 |
+
attn_implementation=attn_implementation,
|
| 778 |
+
max_memory={i: "40GB" for i in range(torch.cuda.device_count())}, # Reserve some memory per GPU
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
# Load processor (handles both text and images)
|
| 782 |
+
processor = AutoProcessor.from_pretrained(
|
| 783 |
+
source_model,
|
| 784 |
+
trust_remote_code=True
|
| 785 |
+
)
|
| 786 |
+
|
| 787 |
+
logger.success("✅ Model and processor loaded successfully")
|
| 788 |
+
|
| 789 |
+
# Patch the config for llmcompressor compatibility with InternVL models
|
| 790 |
+
if hasattr(model.config, 'llm_config') and hasattr(model.config.llm_config, 'use_cache'):
|
| 791 |
+
model.config.use_cache = model.config.llm_config.use_cache
|
| 792 |
+
logger.info("✅ Patched model config for llmcompressor compatibility (use_cache)")
|
| 793 |
+
elif not hasattr(model.config, 'use_cache'):
|
| 794 |
+
# Default to True if use_cache is not found anywhere
|
| 795 |
+
model.config.use_cache = True
|
| 796 |
+
logger.info("✅ Added use_cache=True to model config for llmcompressor compatibility")
|
| 797 |
+
|
| 798 |
+
# Log GPU memory usage after loading
|
| 799 |
+
for i in range(torch.cuda.device_count()):
|
| 800 |
+
allocated = torch.cuda.memory_allocated(i) / (1024**3)
|
| 801 |
+
cached = torch.cuda.memory_reserved(i) / (1024**3)
|
| 802 |
+
logger.info(f" GPU {i}: {allocated:.1f}GB allocated, {cached:.1f}GB cached")
|
| 803 |
+
|
| 804 |
+
# Create quantization recipe
|
| 805 |
+
recipe = create_quantization_recipe(dynamic=dynamic)
|
| 806 |
+
|
| 807 |
+
# Handle output directory cleanup if force is enabled
|
| 808 |
+
if force and output_dir.exists():
|
| 809 |
+
logger.info(f"🗑️ Removing existing output directory: {output_dir}")
|
| 810 |
+
import shutil
|
| 811 |
+
shutil.rmtree(output_dir)
|
| 812 |
+
|
| 813 |
+
# Ensure output directory exists
|
| 814 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 815 |
+
|
| 816 |
+
if dynamic:
|
| 817 |
+
logger.info("🚀 Using FP8-Dynamic quantization - no calibration needed!")
|
| 818 |
+
logger.info("Note: trust_remote_code_model=True is set by default for VLM compatibility")
|
| 819 |
+
|
| 820 |
+
# For dynamic quantization, we can use the model directly without a dataset
|
| 821 |
+
oneshot(
|
| 822 |
+
model=model, # Use the already loaded model
|
| 823 |
+
recipe=recipe,
|
| 824 |
+
output_dir=str(output_dir),
|
| 825 |
+
trust_remote_code_model=True,
|
| 826 |
+
)
|
| 827 |
+
else:
|
| 828 |
+
logger.info("🔄 Starting FP8 static quantization...")
|
| 829 |
+
logger.info("This process will take 30-60 minutes depending on hardware")
|
| 830 |
+
logger.warning("Monitor GPU memory usage - process may require 120GB+ peak VRAM")
|
| 831 |
+
|
| 832 |
+
# Get calibration dataset with fallback
|
| 833 |
+
logger.info(f"📊 Preparing calibration dataset: {calibration_dataset}")
|
| 834 |
+
logger.info(f" Samples: {num_samples}, Max sequence length: {seq_length}")
|
| 835 |
+
logger.info("Note: Using text-only datasets for calibration (works well for VLMs)")
|
| 836 |
+
|
| 837 |
+
dataset = get_calibration_dataset(calibration_dataset, num_samples)
|
| 838 |
+
|
| 839 |
+
# Clear GPU cache before quantization to ensure maximum available memory
|
| 840 |
+
import gc
|
| 841 |
+
gc.collect()
|
| 842 |
+
torch.cuda.empty_cache()
|
| 843 |
+
logger.info("🧹 Cleared GPU cache before quantization")
|
| 844 |
+
|
| 845 |
+
# Apply quantization with calibration dataset
|
| 846 |
+
try:
|
| 847 |
+
oneshot(
|
| 848 |
+
model=model,
|
| 849 |
+
dataset=dataset,
|
| 850 |
+
recipe=recipe,
|
| 851 |
+
output_dir=str(output_dir),
|
| 852 |
+
max_seq_length=seq_length,
|
| 853 |
+
num_calibration_samples=num_samples,
|
| 854 |
+
trust_remote_code_model=True,
|
| 855 |
+
)
|
| 856 |
+
except Exception as e:
|
| 857 |
+
logger.error(f"Quantization failed with {dataset}: {e}")
|
| 858 |
+
if isinstance(dataset, str) and dataset != "open_platypus":
|
| 859 |
+
logger.info("Retrying with open_platypus dataset...")
|
| 860 |
+
oneshot(
|
| 861 |
+
model=model,
|
| 862 |
+
dataset="open_platypus",
|
| 863 |
+
recipe=recipe,
|
| 864 |
+
output_dir=str(output_dir),
|
| 865 |
+
max_seq_length=seq_length,
|
| 866 |
+
num_calibration_samples=num_samples,
|
| 867 |
+
trust_remote_code_model=True,
|
| 868 |
+
)
|
| 869 |
+
else:
|
| 870 |
+
raise
|
| 871 |
+
|
| 872 |
+
logger.success("🎉 Quantization completed successfully!")
|
| 873 |
+
|
| 874 |
+
# Save processor and tokenizer alongside quantized model
|
| 875 |
+
logger.info("💾 Saving processor and tokenizer configuration...")
|
| 876 |
+
processor.save_pretrained(output_dir)
|
| 877 |
+
|
| 878 |
+
# Also save tokenizer explicitly to ensure all tokenizer files are saved
|
| 879 |
+
tokenizer = AutoTokenizer.from_pretrained(source_model, trust_remote_code=True)
|
| 880 |
+
tokenizer.save_pretrained(output_dir)
|
| 881 |
+
logger.success("✅ Tokenizer and processor saved successfully")
|
| 882 |
+
|
| 883 |
+
# Generate and save model card
|
| 884 |
+
logger.info("📝 Generating model card...")
|
| 885 |
+
script_content = read_script_content()
|
| 886 |
+
model_card = generate_model_card(
|
| 887 |
+
source_model=source_model,
|
| 888 |
+
quantized_model_name=quantized_model_name,
|
| 889 |
+
hf_username=hf_username,
|
| 890 |
+
calibration_dataset=calibration_dataset if not dynamic else "N/A",
|
| 891 |
+
num_samples=num_samples if not dynamic else 0,
|
| 892 |
+
seq_length=seq_length if not dynamic else 0,
|
| 893 |
+
package_versions=package_versions,
|
| 894 |
+
script_content=script_content,
|
| 895 |
+
flash_attn_used=not no_flash_attn and torch.cuda.is_available(),
|
| 896 |
+
attention_implementation=attn_implementation,
|
| 897 |
+
dynamic=dynamic
|
| 898 |
+
)
|
| 899 |
+
|
| 900 |
+
model_card_path = output_dir / "README.md"
|
| 901 |
+
with open(model_card_path, 'w', encoding='utf-8') as f:
|
| 902 |
+
f.write(model_card)
|
| 903 |
+
|
| 904 |
+
logger.success(f"📄 Model card saved: {model_card_path}")
|
| 905 |
+
|
| 906 |
+
# Upload to Hugging Face Hub
|
| 907 |
+
if upload and hf_token:
|
| 908 |
+
logger.info("⬆️ Uploading to Hugging Face Hub...")
|
| 909 |
+
|
| 910 |
+
# Verify critical files exist before upload
|
| 911 |
+
critical_files = ["README.md", "tokenizer_config.json", "tokenizer.json"]
|
| 912 |
+
missing_files = []
|
| 913 |
+
|
| 914 |
+
for file in critical_files:
|
| 915 |
+
file_path = output_dir / file
|
| 916 |
+
if file_path.exists():
|
| 917 |
+
logger.info(f"✅ Found {file}")
|
| 918 |
+
else:
|
| 919 |
+
# Some models might use different tokenizer files
|
| 920 |
+
if file == "tokenizer.json":
|
| 921 |
+
# Check for alternative tokenizer files
|
| 922 |
+
alt_files = ["tokenizer.model", "vocab.json", "merges.txt"]
|
| 923 |
+
found_alt = any((output_dir / alt).exists() for alt in alt_files)
|
| 924 |
+
if found_alt:
|
| 925 |
+
logger.info(f"✅ Found alternative tokenizer files")
|
| 926 |
+
else:
|
| 927 |
+
missing_files.append(file)
|
| 928 |
+
else:
|
| 929 |
+
missing_files.append(file)
|
| 930 |
+
|
| 931 |
+
if missing_files:
|
| 932 |
+
logger.warning(f"⚠️ Missing files: {', '.join(missing_files)}")
|
| 933 |
+
|
| 934 |
+
try:
|
| 935 |
+
from huggingface_hub import HfApi
|
| 936 |
+
|
| 937 |
+
api = HfApi(token=hf_token)
|
| 938 |
+
|
| 939 |
+
# Create repository if it doesn't exist
|
| 940 |
+
|
| 941 |
+
try:
|
| 942 |
+
api.create_repo(repo_id=hf_repo, private=False, exist_ok=True) # --hf-repo is mapped to repo_id for backward compatibility
|
| 943 |
+
logger.info("✅ Repository created/verified")
|
| 944 |
+
except Exception as repo_e:
|
| 945 |
+
logger.warning(f"Repository creation warning: {repo_e}")
|
| 946 |
+
|
| 947 |
+
# Upload folder contents
|
| 948 |
+
logger.info("📤 Uploading model files...")
|
| 949 |
+
api.upload_folder(
|
| 950 |
+
folder_path=str(output_dir),
|
| 951 |
+
repo_id=hf_repo, # --hf-repo is mapped to repo_id for backward compatibility
|
| 952 |
+
repo_type="model"
|
| 953 |
+
)
|
| 954 |
+
|
| 955 |
+
logger.success("🎉 Model uploaded successfully!")
|
| 956 |
+
logger.success(f"🔗 View at: https://huggingface.co/{hf_repo}")
|
| 957 |
+
|
| 958 |
+
# List uploaded files
|
| 959 |
+
logger.info("Uploaded files include:")
|
| 960 |
+
for file in output_dir.iterdir():
|
| 961 |
+
if file.is_file():
|
| 962 |
+
size_mb = file.stat().st_size / (1024 * 1024)
|
| 963 |
+
logger.info(f" - {file.name} ({size_mb:.1f} MB)")
|
| 964 |
+
|
| 965 |
+
except Exception as e:
|
| 966 |
+
logger.error(f"Upload failed: {e}")
|
| 967 |
+
logger.info("Model saved locally - you can upload manually later")
|
| 968 |
+
|
| 969 |
+
# Final summary
|
| 970 |
+
logger.info("✨ Quantization Summary:")
|
| 971 |
+
logger.info(f" 📁 Model saved to: {output_dir}")
|
| 972 |
+
logger.info(f" 🔢 Quantization type: FP8-{'Dynamic' if dynamic else 'Static'}")
|
| 973 |
+
logger.info(" 🔢 Original size: ~76GB (FP16)")
|
| 974 |
+
logger.info(" 📉 Quantized size: ~38GB (FP8)")
|
| 975 |
+
logger.info(" 🚀 Expected speedup: ~2x on H100/L40S")
|
| 976 |
+
logger.info(" 💾 Memory savings: ~50%")
|
| 977 |
+
|
| 978 |
+
if upload and hf_token:
|
| 979 |
+
logger.info(f" 🌐 HuggingFace: https://huggingface.co/{hf_repo}")
|
| 980 |
+
|
| 981 |
+
logger.success("🎊 Quantization pipeline completed successfully!")
|
| 982 |
+
|
| 983 |
+
except Exception as e:
|
| 984 |
+
logger.error(f"❌ Quantization failed: {type(e).__name__}: {str(e)}")
|
| 985 |
+
logger.error("Check logs above for detailed error information")
|
| 986 |
+
import traceback
|
| 987 |
+
logger.error("Full traceback:")
|
| 988 |
+
logger.error(traceback.format_exc())
|
| 989 |
+
raise typer.Exit(1)
|
| 990 |
+
|
| 991 |
+
if __name__ == "__main__":
|
| 992 |
+
app()
|
| 993 |
+
```
|
| 994 |
+
|
| 995 |
+
</details>
|
| 996 |
+
|
| 997 |
+
## 🎯 Use Cases
|
| 998 |
+
|
| 999 |
+
This optimized model is ideal for:
|
| 1000 |
+
|
| 1001 |
+
- **Production VLM serving** with high throughput requirements
|
| 1002 |
+
- **Real-time image analysis** and visual question answering
|
| 1003 |
+
- **Document AI** and OCR applications
|
| 1004 |
+
- **Multimodal chatbots** and virtual assistants
|
| 1005 |
+
- **Edge deployment** on high-end GPUs
|
| 1006 |
+
|
| 1007 |
+
## ⚠️ Important Notes
|
| 1008 |
+
|
| 1009 |
+
- Requires GPU with FP8 support (H100, L40S) for optimal performance
|
| 1010 |
+
- Falls back to FP8-Marlin on Ampere GPUs (A100) with reduced benefits
|
| 1011 |
+
- Vision components preserved in FP16 for maximum compatibility
|
| 1012 |
+
- Calibrated with diverse multimodal data for robust performance
|
| 1013 |
+
|
| 1014 |
+
## 🚫 Limitations
|
| 1015 |
+
|
| 1016 |
+
- **Specialized hardware**: Best performance requires H100-class GPUs
|
| 1017 |
+
- **Model size**: Still requires significant VRAM despite quantization
|
| 1018 |
+
- **Research use**: Inherits license and usage restrictions from base model
|
| 1019 |
+
|
| 1020 |
+
## 📄 License
|
| 1021 |
+
|
| 1022 |
+
This quantized model inherits the license from the original model.
|
| 1023 |
+
Original model: [HuggingFaceTB/SmolLM-135M](https://huggingface.co/HuggingFaceTB/SmolLM-135M)
|
| 1024 |
+
|
| 1025 |
+
## 🙏 Acknowledgments
|
| 1026 |
+
|
| 1027 |
+
- **Original Model**: OpenGVLab team for InternVL3-38B
|
| 1028 |
+
- **Quantization**: LLM Compressor and Neural Magic team
|
| 1029 |
+
- **Inference**: vLLM project for optimized serving
|
| 1030 |
+
|
| 1031 |
+
## 📞 Contact
|
| 1032 |
+
|
| 1033 |
+
For questions about this quantized model:
|
| 1034 |
+
- **Issues**: [Create an issue](https://huggingface.co/JustJaro/InternVL3-38B-FP8-Dynamic/discussions)
|
| 1035 |
+
- **Original Model**: Refer to [HuggingFaceTB/SmolLM-135M](https://huggingface.co/HuggingFaceTB/SmolLM-135M)
|
| 1036 |
+
|
| 1037 |
+
---
|
| 1038 |
+
|
| 1039 |
+
*Quantized with ❤️ using LLM Compressor for the open-source community*
|
config.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"eos_token_id": 0,
|
| 9 |
+
"head_dim": 64,
|
| 10 |
+
"hidden_act": "silu",
|
| 11 |
+
"hidden_size": 576,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 1536,
|
| 14 |
+
"max_position_embeddings": 2048,
|
| 15 |
+
"mlp_bias": false,
|
| 16 |
+
"model_type": "llama",
|
| 17 |
+
"num_attention_heads": 9,
|
| 18 |
+
"num_hidden_layers": 30,
|
| 19 |
+
"num_key_value_heads": 3,
|
| 20 |
+
"pretraining_tp": 1,
|
| 21 |
+
"quantization_config": {
|
| 22 |
+
"config_groups": {
|
| 23 |
+
"group_0": {
|
| 24 |
+
"input_activations": {
|
| 25 |
+
"actorder": null,
|
| 26 |
+
"block_structure": null,
|
| 27 |
+
"dynamic": true,
|
| 28 |
+
"group_size": null,
|
| 29 |
+
"num_bits": 8,
|
| 30 |
+
"observer": null,
|
| 31 |
+
"observer_kwargs": {},
|
| 32 |
+
"strategy": "token",
|
| 33 |
+
"symmetric": true,
|
| 34 |
+
"type": "float"
|
| 35 |
+
},
|
| 36 |
+
"output_activations": null,
|
| 37 |
+
"targets": [
|
| 38 |
+
"Linear"
|
| 39 |
+
],
|
| 40 |
+
"weights": {
|
| 41 |
+
"actorder": null,
|
| 42 |
+
"block_structure": null,
|
| 43 |
+
"dynamic": false,
|
| 44 |
+
"group_size": null,
|
| 45 |
+
"num_bits": 8,
|
| 46 |
+
"observer": "minmax",
|
| 47 |
+
"observer_kwargs": {},
|
| 48 |
+
"strategy": "channel",
|
| 49 |
+
"symmetric": true,
|
| 50 |
+
"type": "float"
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"format": "float-quantized",
|
| 55 |
+
"global_compression_ratio": null,
|
| 56 |
+
"ignore": [
|
| 57 |
+
"lm_head"
|
| 58 |
+
],
|
| 59 |
+
"kv_cache_scheme": null,
|
| 60 |
+
"quant_method": "compressed-tensors",
|
| 61 |
+
"quantization_status": "compressed"
|
| 62 |
+
},
|
| 63 |
+
"rms_norm_eps": 1e-05,
|
| 64 |
+
"rope_scaling": null,
|
| 65 |
+
"rope_theta": 10000.0,
|
| 66 |
+
"tie_word_embeddings": true,
|
| 67 |
+
"torch_dtype": "bfloat16",
|
| 68 |
+
"transformers_version": "4.53.0",
|
| 69 |
+
"use_cache": true,
|
| 70 |
+
"vocab_size": 49152
|
| 71 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 0,
|
| 4 |
+
"eos_token_id": 0,
|
| 5 |
+
"transformers_version": "4.53.0"
|
| 6 |
+
}
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b29852221e8b0fb7ce5364816d029fc8cd9fbdc0b790efad61cc32ce4dc2f36
|
| 3 |
+
size 163227736
|
recipe.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
default_stage:
|
| 2 |
+
default_modifiers:
|
| 3 |
+
QuantizationModifier:
|
| 4 |
+
targets: [Linear]
|
| 5 |
+
ignore: ['re:.*lm_head', 're:.*vision.*', 're:.*visual.*', 're:.*image.*', 're:.*patch_embed.*',
|
| 6 |
+
're:.*pos_embed.*', 're:.*norm.*', 're:.*layernorm.*']
|
| 7 |
+
scheme: FP8_DYNAMIC
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"<|endoftext|>",
|
| 4 |
+
"<|im_start|>",
|
| 5 |
+
"<|im_end|>",
|
| 6 |
+
"<repo_name>",
|
| 7 |
+
"<reponame>",
|
| 8 |
+
"<file_sep>",
|
| 9 |
+
"<filename>",
|
| 10 |
+
"<gh_stars>",
|
| 11 |
+
"<issue_start>",
|
| 12 |
+
"<issue_comment>",
|
| 13 |
+
"<issue_closed>",
|
| 14 |
+
"<jupyter_start>",
|
| 15 |
+
"<jupyter_text>",
|
| 16 |
+
"<jupyter_code>",
|
| 17 |
+
"<jupyter_output>",
|
| 18 |
+
"<jupyter_script>",
|
| 19 |
+
"<empty_output>"
|
| 20 |
+
],
|
| 21 |
+
"bos_token": {
|
| 22 |
+
"content": "<|endoftext|>",
|
| 23 |
+
"lstrip": false,
|
| 24 |
+
"normalized": false,
|
| 25 |
+
"rstrip": false,
|
| 26 |
+
"single_word": false
|
| 27 |
+
},
|
| 28 |
+
"eos_token": {
|
| 29 |
+
"content": "<|endoftext|>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false
|
| 34 |
+
},
|
| 35 |
+
"unk_token": {
|
| 36 |
+
"content": "<|endoftext|>",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false
|
| 41 |
+
}
|
| 42 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"1": {
|
| 13 |
+
"content": "<|im_start|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": false,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"2": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
},
|
| 28 |
+
"3": {
|
| 29 |
+
"content": "<repo_name>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false,
|
| 34 |
+
"special": true
|
| 35 |
+
},
|
| 36 |
+
"4": {
|
| 37 |
+
"content": "<reponame>",
|
| 38 |
+
"lstrip": false,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"rstrip": false,
|
| 41 |
+
"single_word": false,
|
| 42 |
+
"special": true
|
| 43 |
+
},
|
| 44 |
+
"5": {
|
| 45 |
+
"content": "<file_sep>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": false,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false,
|
| 50 |
+
"special": true
|
| 51 |
+
},
|
| 52 |
+
"6": {
|
| 53 |
+
"content": "<filename>",
|
| 54 |
+
"lstrip": false,
|
| 55 |
+
"normalized": false,
|
| 56 |
+
"rstrip": false,
|
| 57 |
+
"single_word": false,
|
| 58 |
+
"special": true
|
| 59 |
+
},
|
| 60 |
+
"7": {
|
| 61 |
+
"content": "<gh_stars>",
|
| 62 |
+
"lstrip": false,
|
| 63 |
+
"normalized": false,
|
| 64 |
+
"rstrip": false,
|
| 65 |
+
"single_word": false,
|
| 66 |
+
"special": true
|
| 67 |
+
},
|
| 68 |
+
"8": {
|
| 69 |
+
"content": "<issue_start>",
|
| 70 |
+
"lstrip": false,
|
| 71 |
+
"normalized": false,
|
| 72 |
+
"rstrip": false,
|
| 73 |
+
"single_word": false,
|
| 74 |
+
"special": true
|
| 75 |
+
},
|
| 76 |
+
"9": {
|
| 77 |
+
"content": "<issue_comment>",
|
| 78 |
+
"lstrip": false,
|
| 79 |
+
"normalized": false,
|
| 80 |
+
"rstrip": false,
|
| 81 |
+
"single_word": false,
|
| 82 |
+
"special": true
|
| 83 |
+
},
|
| 84 |
+
"10": {
|
| 85 |
+
"content": "<issue_closed>",
|
| 86 |
+
"lstrip": false,
|
| 87 |
+
"normalized": false,
|
| 88 |
+
"rstrip": false,
|
| 89 |
+
"single_word": false,
|
| 90 |
+
"special": true
|
| 91 |
+
},
|
| 92 |
+
"11": {
|
| 93 |
+
"content": "<jupyter_start>",
|
| 94 |
+
"lstrip": false,
|
| 95 |
+
"normalized": false,
|
| 96 |
+
"rstrip": false,
|
| 97 |
+
"single_word": false,
|
| 98 |
+
"special": true
|
| 99 |
+
},
|
| 100 |
+
"12": {
|
| 101 |
+
"content": "<jupyter_text>",
|
| 102 |
+
"lstrip": false,
|
| 103 |
+
"normalized": false,
|
| 104 |
+
"rstrip": false,
|
| 105 |
+
"single_word": false,
|
| 106 |
+
"special": true
|
| 107 |
+
},
|
| 108 |
+
"13": {
|
| 109 |
+
"content": "<jupyter_code>",
|
| 110 |
+
"lstrip": false,
|
| 111 |
+
"normalized": false,
|
| 112 |
+
"rstrip": false,
|
| 113 |
+
"single_word": false,
|
| 114 |
+
"special": true
|
| 115 |
+
},
|
| 116 |
+
"14": {
|
| 117 |
+
"content": "<jupyter_output>",
|
| 118 |
+
"lstrip": false,
|
| 119 |
+
"normalized": false,
|
| 120 |
+
"rstrip": false,
|
| 121 |
+
"single_word": false,
|
| 122 |
+
"special": true
|
| 123 |
+
},
|
| 124 |
+
"15": {
|
| 125 |
+
"content": "<jupyter_script>",
|
| 126 |
+
"lstrip": false,
|
| 127 |
+
"normalized": false,
|
| 128 |
+
"rstrip": false,
|
| 129 |
+
"single_word": false,
|
| 130 |
+
"special": true
|
| 131 |
+
},
|
| 132 |
+
"16": {
|
| 133 |
+
"content": "<empty_output>",
|
| 134 |
+
"lstrip": false,
|
| 135 |
+
"normalized": false,
|
| 136 |
+
"rstrip": false,
|
| 137 |
+
"single_word": false,
|
| 138 |
+
"special": true
|
| 139 |
+
}
|
| 140 |
+
},
|
| 141 |
+
"additional_special_tokens": [
|
| 142 |
+
"<|endoftext|>",
|
| 143 |
+
"<|im_start|>",
|
| 144 |
+
"<|im_end|>",
|
| 145 |
+
"<repo_name>",
|
| 146 |
+
"<reponame>",
|
| 147 |
+
"<file_sep>",
|
| 148 |
+
"<filename>",
|
| 149 |
+
"<gh_stars>",
|
| 150 |
+
"<issue_start>",
|
| 151 |
+
"<issue_comment>",
|
| 152 |
+
"<issue_closed>",
|
| 153 |
+
"<jupyter_start>",
|
| 154 |
+
"<jupyter_text>",
|
| 155 |
+
"<jupyter_code>",
|
| 156 |
+
"<jupyter_output>",
|
| 157 |
+
"<jupyter_script>",
|
| 158 |
+
"<empty_output>"
|
| 159 |
+
],
|
| 160 |
+
"bos_token": "<|endoftext|>",
|
| 161 |
+
"clean_up_tokenization_spaces": false,
|
| 162 |
+
"eos_token": "<|endoftext|>",
|
| 163 |
+
"extra_special_tokens": {},
|
| 164 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 165 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 166 |
+
"unk_token": "<|endoftext|>",
|
| 167 |
+
"vocab_size": 49152
|
| 168 |
+
}
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|