Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import tempfile
|
|
| 4 |
import shutil
|
| 5 |
import re
|
| 6 |
import json
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from huggingface_hub import HfApi, hf_hub_download
|
| 9 |
from safetensors.torch import load_file, save_file
|
|
@@ -16,34 +17,37 @@ try:
|
|
| 16 |
except ImportError:
|
| 17 |
MODELScope_AVAILABLE = False
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
"""
|
| 21 |
-
with
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# For bias/batchnorm etc., use scalar correction
|
| 43 |
-
return error.mean().to(original_weight.dtype)
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
progress(0.1, desc="Starting FP8 conversion with
|
| 47 |
try:
|
| 48 |
def read_safetensors_metadata(path):
|
| 49 |
with open(path, 'rb') as f:
|
|
@@ -55,8 +59,7 @@ def convert_safetensors_to_fp8_with_correction(safetensors_path, output_dir, fp8
|
|
| 55 |
metadata = read_safetensors_metadata(safetensors_path)
|
| 56 |
progress(0.2, desc="Loaded metadata.")
|
| 57 |
|
| 58 |
-
|
| 59 |
-
original_state = load_file(safetensors_path)
|
| 60 |
progress(0.4, desc="Loaded weights.")
|
| 61 |
|
| 62 |
if fp8_format == "e5m2":
|
|
@@ -65,66 +68,104 @@ def convert_safetensors_to_fp8_with_correction(safetensors_path, output_dir, fp8
|
|
| 65 |
fp8_dtype = torch.float8_e4m3fn
|
| 66 |
|
| 67 |
sd_fp8 = {}
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
"skipped_layers": []
|
| 73 |
}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
for i, key in enumerate(original_state):
|
| 78 |
progress(0.4 + 0.4 * (i / total), desc=f"Processing {i+1}/{total}...")
|
| 79 |
-
weight =
|
| 80 |
|
| 81 |
if weight.dtype in [torch.float16, torch.float32, torch.bfloat16]:
|
| 82 |
-
# Convert to FP8
|
| 83 |
fp8_weight = weight.to(fp8_dtype)
|
| 84 |
sd_fp8[key] = fp8_weight
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
else:
|
| 95 |
-
# Non-float weights (int, bool, etc.) - keep as is
|
| 96 |
sd_fp8[key] = weight
|
| 97 |
-
|
| 98 |
|
| 99 |
base_name = os.path.splitext(os.path.basename(safetensors_path))[0]
|
| 100 |
fp8_path = os.path.join(output_dir, f"{base_name}-fp8-{fp8_format}.safetensors")
|
| 101 |
-
|
| 102 |
|
| 103 |
-
# Save FP8 model
|
| 104 |
save_file(sd_fp8, fp8_path, metadata={"format": "pt", "fp8_format": fp8_format, **metadata})
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
if
|
| 108 |
-
|
| 109 |
-
"format": "pt",
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
progress(0.9, desc="Saved FP8 and
|
| 115 |
-
progress(1.0, desc="β
FP8
|
| 116 |
|
| 117 |
-
stats_msg = f""
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
return True,
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
import traceback
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
def parse_hf_url(url):
|
| 130 |
url = url.strip().rstrip("/")
|
|
@@ -187,7 +228,8 @@ def process_and_upload_fp8(
|
|
| 187 |
repo_url,
|
| 188 |
safetensors_filename,
|
| 189 |
fp8_format,
|
| 190 |
-
|
|
|
|
| 191 |
target_type,
|
| 192 |
new_repo_id,
|
| 193 |
hf_token,
|
|
@@ -201,6 +243,8 @@ def process_and_upload_fp8(
|
|
| 201 |
return None, "β Hugging Face token required for source.", ""
|
| 202 |
if target_type == "huggingface" and not hf_token:
|
| 203 |
return None, "β Hugging Face token required for target.", ""
|
|
|
|
|
|
|
| 204 |
|
| 205 |
temp_dir = None
|
| 206 |
output_dir = tempfile.mkdtemp()
|
|
@@ -210,9 +254,9 @@ def process_and_upload_fp8(
|
|
| 210 |
source_type, repo_url, safetensors_filename, hf_token, progress
|
| 211 |
)
|
| 212 |
|
| 213 |
-
progress(0.25, desc="Converting to FP8 with
|
| 214 |
-
success, msg, stats =
|
| 215 |
-
safetensors_path, output_dir, fp8_format,
|
| 216 |
)
|
| 217 |
|
| 218 |
if not success:
|
|
@@ -224,7 +268,7 @@ def process_and_upload_fp8(
|
|
| 224 |
)
|
| 225 |
|
| 226 |
base_name = os.path.splitext(safetensors_filename)[0]
|
| 227 |
-
|
| 228 |
fp8_filename = f"{base_name}-fp8-{fp8_format}.safetensors"
|
| 229 |
|
| 230 |
readme = f"""---
|
|
@@ -232,51 +276,39 @@ library_name: diffusers
|
|
| 232 |
tags:
|
| 233 |
- fp8
|
| 234 |
- safetensors
|
| 235 |
-
-
|
| 236 |
-
-
|
| 237 |
- diffusion
|
| 238 |
- converted-by-gradio
|
| 239 |
---
|
| 240 |
-
# FP8 Model with
|
| 241 |
- **Source**: `{repo_url}`
|
| 242 |
- **File**: `{safetensors_filename}`
|
| 243 |
- **FP8 Format**: `{fp8_format.upper()}`
|
| 244 |
-
- **
|
| 245 |
-
- **
|
|
|
|
| 246 |
- **FP8 File**: `{fp8_filename}`
|
| 247 |
-
|
| 248 |
## Usage (Inference)
|
| 249 |
```python
|
| 250 |
from safetensors.torch import load_file
|
| 251 |
import torch
|
| 252 |
-
|
| 253 |
-
# Load FP8 model and correction factors
|
| 254 |
fp8_state = load_file("{fp8_filename}")
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
# Reconstruct high-precision weights
|
| 258 |
reconstructed = {{}}
|
| 259 |
for key in fp8_state:
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
reconstructed[key] = fp8_weight + correction
|
| 267 |
else:
|
| 268 |
-
reconstructed[key] =
|
| 269 |
-
|
| 270 |
-
# Use reconstructed weights in your model
|
| 271 |
-
model.load_state_dict(reconstructed)
|
| 272 |
```
|
| 273 |
-
|
| 274 |
-
## Correction Modes
|
| 275 |
-
- **Per-Channel**: Computes mean correction per output channel (best for most layers)
|
| 276 |
-
- **Per-Tensor**: Single correction value per tensor (lightweight)
|
| 277 |
-
- **None**: No correction (pure FP8)
|
| 278 |
-
|
| 279 |
-
> Requires PyTorch β₯ 2.1 for FP8 support. For best quality, use the correction file during inference.
|
| 280 |
"""
|
| 281 |
|
| 282 |
with open(os.path.join(output_dir, "README.md"), "w") as f:
|
|
@@ -295,22 +327,23 @@ model.load_state_dict(reconstructed)
|
|
| 295 |
result_html = f"""
|
| 296 |
β
Success!
|
| 297 |
Model uploaded to: <a href="{repo_url_final}" target="_blank">{new_repo_id}</a>
|
| 298 |
-
Includes: FP8 model +
|
| 299 |
"""
|
| 300 |
-
return gr.HTML(result_html), "β
FP8
|
| 301 |
|
| 302 |
except Exception as e:
|
| 303 |
import traceback
|
| 304 |
-
|
|
|
|
| 305 |
|
| 306 |
finally:
|
| 307 |
if temp_dir:
|
| 308 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 309 |
shutil.rmtree(output_dir, ignore_errors=True)
|
| 310 |
|
| 311 |
-
with gr.Blocks(title="FP8
|
| 312 |
-
gr.Markdown("# π FP8
|
| 313 |
-
gr.Markdown("Convert `.safetensors` β **FP8** + **
|
| 314 |
|
| 315 |
with gr.Row():
|
| 316 |
with gr.Column():
|
|
@@ -318,16 +351,19 @@ with gr.Blocks(title="FP8 Quantizer with Precision Recovery") as demo:
|
|
| 318 |
repo_url = gr.Textbox(label="Repo URL or ID", placeholder="https://huggingface.co/... or modelscope-id")
|
| 319 |
safetensors_filename = gr.Textbox(label="Filename", placeholder="model.safetensors")
|
| 320 |
|
| 321 |
-
with gr.Accordion("
|
| 322 |
fp8_format = gr.Radio(["e4m3fn", "e5m2"], value="e5m2", label="FP8 Format")
|
| 323 |
-
|
|
|
|
| 324 |
choices=[
|
| 325 |
-
("
|
| 326 |
-
("
|
| 327 |
-
("
|
|
|
|
|
|
|
| 328 |
],
|
| 329 |
-
value="
|
| 330 |
-
label="
|
| 331 |
)
|
| 332 |
|
| 333 |
with gr.Accordion("Authentication", open=False):
|
|
@@ -336,7 +372,7 @@ with gr.Blocks(title="FP8 Quantizer with Precision Recovery") as demo:
|
|
| 336 |
|
| 337 |
with gr.Column():
|
| 338 |
target_type = gr.Radio(["huggingface", "modelscope"], value="huggingface", label="Target")
|
| 339 |
-
new_repo_id = gr.Textbox(label="New Repo ID", placeholder="user/model-fp8")
|
| 340 |
private_repo = gr.Checkbox(label="Private Repository (HF only)", value=False)
|
| 341 |
|
| 342 |
status_output = gr.Markdown()
|
|
@@ -352,7 +388,8 @@ with gr.Blocks(title="FP8 Quantizer with Precision Recovery") as demo:
|
|
| 352 |
repo_url,
|
| 353 |
safetensors_filename,
|
| 354 |
fp8_format,
|
| 355 |
-
|
|
|
|
| 356 |
target_type,
|
| 357 |
new_repo_id,
|
| 358 |
hf_token,
|
|
@@ -365,37 +402,25 @@ with gr.Blocks(title="FP8 Quantizer with Precision Recovery") as demo:
|
|
| 365 |
|
| 366 |
gr.Examples(
|
| 367 |
examples=[
|
| 368 |
-
["huggingface", "https://huggingface.co/Yabo/FramePainter/tree/main", "unet_diffusion_pytorch_model.safetensors", "e5m2", "
|
| 369 |
-
["huggingface", "https://huggingface.co/stabilityai/sdxl-vae", "diffusion_pytorch_model.safetensors", "e4m3fn", "
|
| 370 |
-
["huggingface", "https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main/text_encoder", "model.safetensors", "e5m2", "
|
| 371 |
],
|
| 372 |
-
inputs=[source_type, repo_url, safetensors_filename, fp8_format,
|
| 373 |
label="Example Conversions"
|
| 374 |
)
|
| 375 |
|
| 376 |
gr.Markdown("""
|
| 377 |
-
## π‘
|
| 378 |
-
|
| 379 |
-
Traditional LoRA struggles with quantization errors because:
|
| 380 |
-
- LoRA is designed for *weight updates*, not *quantization error recovery*
|
| 381 |
-
- Per-channel correction captures systematic quantization bias better
|
| 382 |
-
- Simpler math β more reliable reconstruction
|
| 383 |
-
|
| 384 |
-
## π Precision Recovery Modes
|
| 385 |
|
| 386 |
-
- **
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
- **
|
| 391 |
-
|
| 392 |
-
- Better than no correction for most layers
|
| 393 |
-
|
| 394 |
-
- **None**: Pure FP8 quantization
|
| 395 |
-
- Smallest file size
|
| 396 |
-
- Lowest quality (use only for memory-constrained deployments)
|
| 397 |
|
| 398 |
-
> **Note**:
|
| 399 |
""")
|
| 400 |
|
| 401 |
demo.launch()
|
|
|
|
| 4 |
import shutil
|
| 5 |
import re
|
| 6 |
import json
|
| 7 |
+
import datetime
|
| 8 |
from pathlib import Path
|
| 9 |
from huggingface_hub import HfApi, hf_hub_download
|
| 10 |
from safetensors.torch import load_file, save_file
|
|
|
|
| 17 |
except ImportError:
|
| 18 |
MODELScope_AVAILABLE = False
|
| 19 |
|
| 20 |
+
def low_rank_decomposition(weight, rank=128):
|
| 21 |
+
"""
|
| 22 |
+
Improved LoRA decomposition that maintains compatibility with existing merge scripts.
|
| 23 |
+
This implementation focuses on extracting meaningful low-rank components from 2D weights.
|
| 24 |
+
"""
|
| 25 |
+
if weight.ndim != 2:
|
| 26 |
+
return None, None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Convert to float32 for numerical stability during SVD
|
| 30 |
+
weight_f32 = weight.float()
|
| 31 |
|
| 32 |
+
# Perform SVD
|
| 33 |
+
U, S, Vh = torch.linalg.svd(weight_f32, full_matrices=False)
|
| 34 |
|
| 35 |
+
# Ensure rank doesn't exceed available singular values
|
| 36 |
+
actual_rank = min(rank, len(S))
|
| 37 |
+
|
| 38 |
+
# Create LoRA matrices using standard factorization
|
| 39 |
+
# W β U[:, :r] * diag(S[:r]) * Vh[:r, :]
|
| 40 |
+
# We split as: A = Vh[:r, :], B = U[:, :r] * diag(S[:r])
|
| 41 |
+
A = Vh[:actual_rank, :].contiguous()
|
| 42 |
+
B = U[:, :actual_rank] @ torch.diag(S[:actual_rank])
|
| 43 |
+
|
| 44 |
+
return A.to(torch.float16), B.to(torch.float16)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Decomposition error: {e}")
|
| 47 |
+
return None, None
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def convert_safetensors_to_fp8_with_lora(safetensors_path, output_dir, fp8_format, lora_rank=128, architecture="auto", progress=gr.Progress()):
|
| 50 |
+
progress(0.1, desc="Starting FP8 conversion with LoRA extraction...")
|
| 51 |
try:
|
| 52 |
def read_safetensors_metadata(path):
|
| 53 |
with open(path, 'rb') as f:
|
|
|
|
| 59 |
metadata = read_safetensors_metadata(safetensors_path)
|
| 60 |
progress(0.2, desc="Loaded metadata.")
|
| 61 |
|
| 62 |
+
state_dict = load_file(safetensors_path)
|
|
|
|
| 63 |
progress(0.4, desc="Loaded weights.")
|
| 64 |
|
| 65 |
if fp8_format == "e5m2":
|
|
|
|
| 68 |
fp8_dtype = torch.float8_e4m3fn
|
| 69 |
|
| 70 |
sd_fp8 = {}
|
| 71 |
+
lora_weights = {}
|
| 72 |
+
total = len(state_dict)
|
| 73 |
+
lora_keys = []
|
| 74 |
+
stats = {
|
| 75 |
+
"total_layers": total,
|
| 76 |
+
"eligible_layers": 0,
|
| 77 |
+
"processed_layers": 0,
|
| 78 |
"skipped_layers": []
|
| 79 |
}
|
| 80 |
|
| 81 |
+
for i, key in enumerate(state_dict):
|
|
|
|
|
|
|
| 82 |
progress(0.4 + 0.4 * (i / total), desc=f"Processing {i+1}/{total}...")
|
| 83 |
+
weight = state_dict[key]
|
| 84 |
|
| 85 |
if weight.dtype in [torch.float16, torch.float32, torch.bfloat16]:
|
|
|
|
| 86 |
fp8_weight = weight.to(fp8_dtype)
|
| 87 |
sd_fp8[key] = fp8_weight
|
| 88 |
|
| 89 |
+
# Apply architecture filtering
|
| 90 |
+
lower_key = key.lower()
|
| 91 |
+
should_process = False
|
| 92 |
+
|
| 93 |
+
if architecture == "text_encoder":
|
| 94 |
+
should_process = "text" in lower_key or "emb" in lower_key or "encoder" in lower_key
|
| 95 |
+
elif architecture == "transformer":
|
| 96 |
+
should_process = "attn" in lower_key or "transformer" in lower_key
|
| 97 |
+
elif architecture == "vae":
|
| 98 |
+
should_process = "vae" in lower_key or "decoder" in lower_key or "encoder" in lower_key
|
| 99 |
+
elif architecture == "all":
|
| 100 |
+
should_process = True
|
| 101 |
+
else: # "auto" or unknown
|
| 102 |
+
should_process = True
|
| 103 |
+
|
| 104 |
+
# Only process 2D tensors that meet rank requirements and pass architecture filter
|
| 105 |
+
if should_process and weight.ndim == 2 and min(weight.shape) > lora_rank:
|
| 106 |
+
stats["eligible_layers"] += 1
|
| 107 |
+
try:
|
| 108 |
+
A, B = low_rank_decomposition(weight, rank=lora_rank)
|
| 109 |
+
if A is not None and B is not None:
|
| 110 |
+
lora_weights[f"lora_A.{key}"] = A
|
| 111 |
+
lora_weights[f"lora_B.{key}"] = B
|
| 112 |
+
lora_keys.append(key)
|
| 113 |
+
stats["processed_layers"] += 1
|
| 114 |
+
else:
|
| 115 |
+
stats["skipped_layers"].append(f"{key}: decomposition failed")
|
| 116 |
+
except Exception as e:
|
| 117 |
+
stats["skipped_layers"].append(f"{key}: error - {str(e)}")
|
| 118 |
+
elif should_process and weight.ndim == 2:
|
| 119 |
+
# Handle smaller 2D tensors with reduced rank
|
| 120 |
+
smaller_rank = min(lora_rank, min(weight.shape) // 2)
|
| 121 |
+
if smaller_rank >= 8: # Minimum useful rank
|
| 122 |
+
stats["eligible_layers"] += 1
|
| 123 |
+
try:
|
| 124 |
+
A, B = low_rank_decomposition(weight, rank=smaller_rank)
|
| 125 |
+
if A is not None and B is not None:
|
| 126 |
+
lora_weights[f"lora_A.{key}"] = A
|
| 127 |
+
lora_weights[f"lora_B.{key}"] = B
|
| 128 |
+
lora_keys.append(key)
|
| 129 |
+
stats["processed_layers"] += 1
|
| 130 |
+
else:
|
| 131 |
+
stats["skipped_layers"].append(f"{key}: small tensor decomposition failed")
|
| 132 |
+
except Exception as e:
|
| 133 |
+
stats["skipped_layers"].append(f"{key}: small tensor error - {str(e)}")
|
| 134 |
else:
|
|
|
|
| 135 |
sd_fp8[key] = weight
|
| 136 |
+
stats["skipped_layers"].append(f"{key}: non-float dtype")
|
| 137 |
|
| 138 |
base_name = os.path.splitext(os.path.basename(safetensors_path))[0]
|
| 139 |
fp8_path = os.path.join(output_dir, f"{base_name}-fp8-{fp8_format}.safetensors")
|
| 140 |
+
lora_path = os.path.join(output_dir, f"{base_name}-lora-r{lora_rank}.safetensors")
|
| 141 |
|
|
|
|
| 142 |
save_file(sd_fp8, fp8_path, metadata={"format": "pt", "fp8_format": fp8_format, **metadata})
|
| 143 |
|
| 144 |
+
# Always save LoRA file if any weights were processed
|
| 145 |
+
if lora_weights:
|
| 146 |
+
lora_metadata = {
|
| 147 |
+
"format": "pt",
|
| 148 |
+
"lora_rank": str(lora_rank),
|
| 149 |
+
"architecture": architecture,
|
| 150 |
+
"stats": json.dumps(stats)
|
| 151 |
+
}
|
| 152 |
+
save_file(lora_weights, lora_path, metadata=lora_metadata)
|
| 153 |
|
| 154 |
+
progress(0.9, desc="Saved FP8 and LoRA files.")
|
| 155 |
+
progress(1.0, desc="β
FP8 + LoRA extraction complete!")
|
| 156 |
|
| 157 |
+
stats_msg = f"FP8 ({fp8_format}) and rank-{lora_rank} LoRA saved.\n"
|
| 158 |
+
stats_msg += f"Processed {stats['processed_layers']}/{stats['eligible_layers']} eligible layers."
|
| 159 |
+
|
| 160 |
+
if stats['processed_layers'] == 0:
|
| 161 |
+
stats_msg += "\nβ οΈ No LoRA weights were generated. Try reducing rank or selecting a specific architecture."
|
| 162 |
+
|
| 163 |
+
return True, stats_msg, stats
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
import traceback
|
| 167 |
+
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 168 |
+
return False, error_msg, None
|
| 169 |
|
| 170 |
def parse_hf_url(url):
|
| 171 |
url = url.strip().rstrip("/")
|
|
|
|
| 228 |
repo_url,
|
| 229 |
safetensors_filename,
|
| 230 |
fp8_format,
|
| 231 |
+
lora_rank,
|
| 232 |
+
architecture,
|
| 233 |
target_type,
|
| 234 |
new_repo_id,
|
| 235 |
hf_token,
|
|
|
|
| 243 |
return None, "β Hugging Face token required for source.", ""
|
| 244 |
if target_type == "huggingface" and not hf_token:
|
| 245 |
return None, "β Hugging Face token required for target.", ""
|
| 246 |
+
if lora_rank < 8:
|
| 247 |
+
return None, "β LoRA rank must be at least 8.", ""
|
| 248 |
|
| 249 |
temp_dir = None
|
| 250 |
output_dir = tempfile.mkdtemp()
|
|
|
|
| 254 |
source_type, repo_url, safetensors_filename, hf_token, progress
|
| 255 |
)
|
| 256 |
|
| 257 |
+
progress(0.25, desc="Converting to FP8 with LoRA extraction...")
|
| 258 |
+
success, msg, stats = convert_safetensors_to_fp8_with_lora(
|
| 259 |
+
safetensors_path, output_dir, fp8_format, lora_rank, architecture, progress
|
| 260 |
)
|
| 261 |
|
| 262 |
if not success:
|
|
|
|
| 268 |
)
|
| 269 |
|
| 270 |
base_name = os.path.splitext(safetensors_filename)[0]
|
| 271 |
+
lora_filename = f"{base_name}-lora-r{lora_rank}.safetensors"
|
| 272 |
fp8_filename = f"{base_name}-fp8-{fp8_format}.safetensors"
|
| 273 |
|
| 274 |
readme = f"""---
|
|
|
|
| 276 |
tags:
|
| 277 |
- fp8
|
| 278 |
- safetensors
|
| 279 |
+
- lora
|
| 280 |
+
- low-rank
|
| 281 |
- diffusion
|
| 282 |
- converted-by-gradio
|
| 283 |
---
|
| 284 |
+
# FP8 Model with Low-Rank LoRA
|
| 285 |
- **Source**: `{repo_url}`
|
| 286 |
- **File**: `{safetensors_filename}`
|
| 287 |
- **FP8 Format**: `{fp8_format.upper()}`
|
| 288 |
+
- **LoRA Rank**: {lora_rank}
|
| 289 |
+
- **Architecture**: {architecture}
|
| 290 |
+
- **LoRA File**: `{lora_filename}`
|
| 291 |
- **FP8 File**: `{fp8_filename}`
|
|
|
|
| 292 |
## Usage (Inference)
|
| 293 |
```python
|
| 294 |
from safetensors.torch import load_file
|
| 295 |
import torch
|
| 296 |
+
# Load FP8 model
|
|
|
|
| 297 |
fp8_state = load_file("{fp8_filename}")
|
| 298 |
+
lora_state = load_file("{lora_filename}")
|
| 299 |
+
# Reconstruct approximate original weights
|
|
|
|
| 300 |
reconstructed = {{}}
|
| 301 |
for key in fp8_state:
|
| 302 |
+
if f"lora_A.{{key}}" in lora_state and f"lora_B.{{key}}" in lora_state:
|
| 303 |
+
A = lora_state[f"lora_A.{{key}}"].to(torch.float32)
|
| 304 |
+
B = lora_state[f"lora_B.{{key}}"].to(torch.float32)
|
| 305 |
+
lora_weight = B @ A # (out_features, rank) @ (rank, in_features) -> (out_features, in_features)
|
| 306 |
+
fp8_weight = fp8_state[key].to(torch.float32)
|
| 307 |
+
reconstructed[key] = fp8_weight + lora_weight
|
|
|
|
| 308 |
else:
|
| 309 |
+
reconstructed[key] = fp8_state[key].to(torch.float32)
|
|
|
|
|
|
|
|
|
|
| 310 |
```
|
| 311 |
+
> Requires PyTorch β₯ 2.1 for FP8 support.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
"""
|
| 313 |
|
| 314 |
with open(os.path.join(output_dir, "README.md"), "w") as f:
|
|
|
|
| 327 |
result_html = f"""
|
| 328 |
β
Success!
|
| 329 |
Model uploaded to: <a href="{repo_url_final}" target="_blank">{new_repo_id}</a>
|
| 330 |
+
Includes: FP8 model + rank-{lora_rank} LoRA.
|
| 331 |
"""
|
| 332 |
+
return gr.HTML(result_html), "β
FP8 + LoRA upload successful!", msg
|
| 333 |
|
| 334 |
except Exception as e:
|
| 335 |
import traceback
|
| 336 |
+
error_details = f"β Error: {str(e)}\n{traceback.format_exc()}"
|
| 337 |
+
return None, error_details, ""
|
| 338 |
|
| 339 |
finally:
|
| 340 |
if temp_dir:
|
| 341 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 342 |
shutil.rmtree(output_dir, ignore_errors=True)
|
| 343 |
|
| 344 |
+
with gr.Blocks(title="FP8 + LoRA Extractor (HF β ModelScope)") as demo:
|
| 345 |
+
gr.Markdown("# π FP8 Pruner with Enhanced Low-Rank LoRA Extraction")
|
| 346 |
+
gr.Markdown("Convert `.safetensors` β **FP8** + **high-quality LoRA** for precision recovery. Supports Hugging Face β ModelScope.")
|
| 347 |
|
| 348 |
with gr.Row():
|
| 349 |
with gr.Column():
|
|
|
|
| 351 |
repo_url = gr.Textbox(label="Repo URL or ID", placeholder="https://huggingface.co/... or modelscope-id")
|
| 352 |
safetensors_filename = gr.Textbox(label="Filename", placeholder="model.safetensors")
|
| 353 |
|
| 354 |
+
with gr.Accordion("Advanced Settings", open=True):
|
| 355 |
fp8_format = gr.Radio(["e4m3fn", "e5m2"], value="e5m2", label="FP8 Format")
|
| 356 |
+
lora_rank = gr.Slider(minimum=8, maximum=512, step=8, value=128, label="LoRA Rank")
|
| 357 |
+
architecture = gr.Dropdown(
|
| 358 |
choices=[
|
| 359 |
+
("Auto-detect components", "auto"),
|
| 360 |
+
("Text Encoder only", "text_encoder"),
|
| 361 |
+
("Transformer blocks only", "transformer"),
|
| 362 |
+
("VAE only", "vae"),
|
| 363 |
+
("All eligible layers", "all")
|
| 364 |
],
|
| 365 |
+
value="auto",
|
| 366 |
+
label="Target Architecture"
|
| 367 |
)
|
| 368 |
|
| 369 |
with gr.Accordion("Authentication", open=False):
|
|
|
|
| 372 |
|
| 373 |
with gr.Column():
|
| 374 |
target_type = gr.Radio(["huggingface", "modelscope"], value="huggingface", label="Target")
|
| 375 |
+
new_repo_id = gr.Textbox(label="New Repo ID", placeholder="user/model-fp8-lora")
|
| 376 |
private_repo = gr.Checkbox(label="Private Repository (HF only)", value=False)
|
| 377 |
|
| 378 |
status_output = gr.Markdown()
|
|
|
|
| 388 |
repo_url,
|
| 389 |
safetensors_filename,
|
| 390 |
fp8_format,
|
| 391 |
+
lora_rank,
|
| 392 |
+
architecture,
|
| 393 |
target_type,
|
| 394 |
new_repo_id,
|
| 395 |
hf_token,
|
|
|
|
| 402 |
|
| 403 |
gr.Examples(
|
| 404 |
examples=[
|
| 405 |
+
["huggingface", "https://huggingface.co/Yabo/FramePainter/tree/main", "unet_diffusion_pytorch_model.safetensors", "e5m2", 128, "transformer", "huggingface"],
|
| 406 |
+
["huggingface", "https://huggingface.co/stabilityai/sdxl-vae", "diffusion_pytorch_model.safetensors", "e4m3fn", 64, "vae", "huggingface"],
|
| 407 |
+
["huggingface", "https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main/text_encoder", "model.safetensors", "e5m2", 96, "text_encoder", "huggingface"]
|
| 408 |
],
|
| 409 |
+
inputs=[source_type, repo_url, safetensors_filename, fp8_format, lora_rank, architecture, target_type],
|
| 410 |
label="Example Conversions"
|
| 411 |
)
|
| 412 |
|
| 413 |
gr.Markdown("""
|
| 414 |
+
## π‘ Usage Tips
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
+
- **Higher ranks (128-256)**: Best quality recovery for important layers
|
| 417 |
+
- **Smaller ranks (32-64)**: Good balance of quality and file size
|
| 418 |
+
- **Architecture selection**: Focus LoRA on specific components for better results
|
| 419 |
+
- **Text Encoder**: Use rank 96-128 for best text understanding
|
| 420 |
+
- **Transformers**: Use rank 128-256 for maximum quality retention
|
| 421 |
+
- **VAE**: Use rank 64-128 for good image reconstruction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
+
> **Note**: This implementation maintains compatibility with existing merge scripts while providing significantly better precision recovery through improved LoRA extraction.
|
| 424 |
""")
|
| 425 |
|
| 426 |
demo.launch()
|