Spaces:
Running on Zero
Running on Zero
Lazy-load CUDA quantizer configuration inside GPU call
Browse files- __pycache__/app.cpython-313.pyc +0 -0
- app.py +20 -14
__pycache__/app.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -154,21 +154,27 @@ def compare_vectors(
|
|
| 154 |
}
|
| 155 |
|
| 156 |
|
| 157 |
-
QUANTIZERS =
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
def run_quantized_control(variant: str) -> dict:
|
| 170 |
if variant not in QUANTIZERS:
|
| 171 |
raise ValueError(f"unsupported quantizer: {variant}")
|
|
|
|
| 172 |
pairs = json.loads(INPUT_PATH.read_text())
|
| 173 |
query_texts = [detailed_instruction(item["query"]) for item in pairs]
|
| 174 |
document_texts = [item["document"] for item in pairs]
|
|
@@ -181,7 +187,7 @@ def run_quantized_control(variant: str) -> dict:
|
|
| 181 |
load_started = time.perf_counter()
|
| 182 |
quantized_model = AutoModel.from_pretrained(
|
| 183 |
MODEL_PATH,
|
| 184 |
-
quantization_config=
|
| 185 |
device_map={"": 0},
|
| 186 |
trust_remote_code=True,
|
| 187 |
).eval()
|
|
@@ -202,7 +208,7 @@ def run_quantized_control(variant: str) -> dict:
|
|
| 202 |
"model": "Qwen/Qwen3-Embedding-0.6B",
|
| 203 |
"source_revision": "97b0c614be4d77ee51c0cef4e5f07c00f9eb65b3",
|
| 204 |
"quantizer": variant,
|
| 205 |
-
"quantization_config":
|
| 206 |
"load_seconds": load_seconds,
|
| 207 |
"encode_seconds": encode_seconds,
|
| 208 |
"texts_per_second": len(query_texts + document_texts) / encode_seconds,
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
|
| 157 |
+
QUANTIZERS = ("bnb-int8", "bnb-nf4")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def quantization_config(variant: str) -> BitsAndBytesConfig:
|
| 161 |
+
if variant == "bnb-int8":
|
| 162 |
+
return BitsAndBytesConfig(load_in_8bit=True)
|
| 163 |
+
if variant == "bnb-nf4":
|
| 164 |
+
return BitsAndBytesConfig(
|
| 165 |
+
load_in_4bit=True,
|
| 166 |
+
bnb_4bit_quant_type="nf4",
|
| 167 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 168 |
+
bnb_4bit_use_double_quant=False,
|
| 169 |
+
)
|
| 170 |
+
raise ValueError(f"unsupported quantizer: {variant}")
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@spaces.GPU(duration=300)
|
| 174 |
def run_quantized_control(variant: str) -> dict:
|
| 175 |
if variant not in QUANTIZERS:
|
| 176 |
raise ValueError(f"unsupported quantizer: {variant}")
|
| 177 |
+
config = quantization_config(variant)
|
| 178 |
pairs = json.loads(INPUT_PATH.read_text())
|
| 179 |
query_texts = [detailed_instruction(item["query"]) for item in pairs]
|
| 180 |
document_texts = [item["document"] for item in pairs]
|
|
|
|
| 187 |
load_started = time.perf_counter()
|
| 188 |
quantized_model = AutoModel.from_pretrained(
|
| 189 |
MODEL_PATH,
|
| 190 |
+
quantization_config=config,
|
| 191 |
device_map={"": 0},
|
| 192 |
trust_remote_code=True,
|
| 193 |
).eval()
|
|
|
|
| 208 |
"model": "Qwen/Qwen3-Embedding-0.6B",
|
| 209 |
"source_revision": "97b0c614be4d77ee51c0cef4e5f07c00f9eb65b3",
|
| 210 |
"quantizer": variant,
|
| 211 |
+
"quantization_config": config.to_dict(),
|
| 212 |
"load_seconds": load_seconds,
|
| 213 |
"encode_seconds": encode_seconds,
|
| 214 |
"texts_per_second": len(query_texts + document_texts) / encode_seconds,
|