Upload handler.py
Browse files- handler.py +10 -10
handler.py
CHANGED
|
@@ -9,7 +9,7 @@ import time
|
|
| 9 |
from PIL import Image
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
import torch
|
| 12 |
-
from torchao.quantization import quantize_, autoquant, int8_dynamic_activation_int4_weight, float8_dynamic_activation_float8_weight,
|
| 13 |
from torchao.quantization.quant_api import PerRow
|
| 14 |
from diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, TorchAoConfig
|
| 15 |
from transformers import T5EncoderModel
|
|
@@ -72,7 +72,7 @@ def pil_to_base64(image: Image.Image, modelname: str, prompt: str, height: int,
|
|
| 72 |
def load_te2(repo_id: str, dtype: torch.dtype) -> Any:
|
| 73 |
text_encoder_2 = T5EncoderModel.from_pretrained(repo_id, subfolder="text_encoder_2", torch_dtype=dtype)
|
| 74 |
if IS_4BIT: quantize_(text_encoder_2, int8_dynamic_activation_int4_weight())
|
| 75 |
-
else: quantize_(text_encoder_2,
|
| 76 |
return text_encoder_2
|
| 77 |
|
| 78 |
def load_pipeline_stable(repo_id: str, dtype: torch.dtype) -> Any:
|
|
@@ -128,7 +128,7 @@ def load_pipeline_turbo(repo_id: str, dtype: torch.dtype) -> Any:
|
|
| 128 |
pipe.unload_lora_weights()
|
| 129 |
pipe.transformer.fuse_qkv_projections()
|
| 130 |
pipe.vae.fuse_qkv_projections()
|
| 131 |
-
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else
|
| 132 |
quantize_(pipe.transformer, weight, device="cuda")
|
| 133 |
quantize_(pipe.vae, weight, device="cuda")
|
| 134 |
return pipe
|
|
@@ -141,7 +141,7 @@ def load_pipeline_turbo_compile(repo_id: str, dtype: torch.dtype) -> Any:
|
|
| 141 |
pipe.unload_lora_weights()
|
| 142 |
pipe.transformer.fuse_qkv_projections()
|
| 143 |
pipe.vae.fuse_qkv_projections()
|
| 144 |
-
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else
|
| 145 |
quantize_(pipe.transformer, weight, device="cuda")
|
| 146 |
quantize_(pipe.vae, weight, device="cuda")
|
| 147 |
pipe.transformer.to(memory_format=torch.channels_last)
|
|
@@ -152,7 +152,7 @@ def load_pipeline_turbo_compile(repo_id: str, dtype: torch.dtype) -> Any:
|
|
| 152 |
|
| 153 |
def load_pipeline_opt(repo_id: str, dtype: torch.dtype) -> Any:
|
| 154 |
quantization_config = TorchAoConfig("int4dq" if IS_4BIT else "float8dq" if IS_CC90 else "int8wo")
|
| 155 |
-
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else
|
| 156 |
transformer = FluxTransformer2DModel.from_pretrained(repo_id, subfolder="transformer", torch_dtype=dtype)
|
| 157 |
transformer.fuse_qkv_projections()
|
| 158 |
if IS_CC90: quantize_(transformer, float8_dynamic_activation_float8_weight(granularity=PerRow()), device="cuda")
|
|
@@ -170,14 +170,14 @@ def load_pipeline_opt(repo_id: str, dtype: torch.dtype) -> Any:
|
|
| 170 |
#vae = torch.compile(vae, mode="max-autotune", fullgraph=True)
|
| 171 |
vae = torch.compile(vae, mode="max-autotune-no-cudagraphs")
|
| 172 |
pipe = FluxPipeline.from_pretrained(repo_id, transformer=None, vae=None, torch_dtype=dtype, quantization_config=quantization_config)
|
| 173 |
-
quantize_(pipe.text_encoder,
|
| 174 |
-
quantize_(pipe.text_encoder_2,
|
| 175 |
pipe.transformer = transformer
|
| 176 |
pipe.vae = vae
|
| 177 |
return pipe
|
| 178 |
|
| 179 |
def load_pipeline_para(repo_id: str, dtype: torch.dtype) -> Any:
|
| 180 |
-
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else
|
| 181 |
pipe = FluxPipeline.from_pretrained(repo_id, torch_dtype=dtype)
|
| 182 |
if IS_PARA:
|
| 183 |
if IS_MGPU:
|
|
@@ -188,8 +188,8 @@ def load_pipeline_para(repo_id: str, dtype: torch.dtype) -> Any:
|
|
| 188 |
parallelize_pipe(pipe, mesh=mesh)
|
| 189 |
parallelize_vae(pipe.vae, mesh=mesh._flatten())
|
| 190 |
apply_cache_on_pipe(pipe, residual_diff_threshold=0.12)
|
| 191 |
-
quantize_(pipe.text_encoder,
|
| 192 |
-
quantize_(pipe.text_encoder_2,
|
| 193 |
if IS_CC90: quantize_(pipe.transformer, float8_dynamic_activation_float8_weight(granularity=PerRow()), device="cuda")
|
| 194 |
elif IS_CC89: quantize_(pipe.transformer, float8_dynamic_activation_float8_weight(), device="cuda")
|
| 195 |
else: quantize_(pipe.transformer, weight, device="cuda")
|
|
|
|
| 9 |
from PIL import Image
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
import torch
|
| 12 |
+
from torchao.quantization import quantize_, autoquant, int8_dynamic_activation_int4_weight, float8_dynamic_activation_float8_weight, int8_dynamic_activation_int8_weight
|
| 13 |
from torchao.quantization.quant_api import PerRow
|
| 14 |
from diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, TorchAoConfig
|
| 15 |
from transformers import T5EncoderModel
|
|
|
|
| 72 |
def load_te2(repo_id: str, dtype: torch.dtype) -> Any:
|
| 73 |
text_encoder_2 = T5EncoderModel.from_pretrained(repo_id, subfolder="text_encoder_2", torch_dtype=dtype)
|
| 74 |
if IS_4BIT: quantize_(text_encoder_2, int8_dynamic_activation_int4_weight())
|
| 75 |
+
else: quantize_(text_encoder_2, int8_dynamic_activation_int8_weight())
|
| 76 |
return text_encoder_2
|
| 77 |
|
| 78 |
def load_pipeline_stable(repo_id: str, dtype: torch.dtype) -> Any:
|
|
|
|
| 128 |
pipe.unload_lora_weights()
|
| 129 |
pipe.transformer.fuse_qkv_projections()
|
| 130 |
pipe.vae.fuse_qkv_projections()
|
| 131 |
+
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else int8_dynamic_activation_int8_weight()
|
| 132 |
quantize_(pipe.transformer, weight, device="cuda")
|
| 133 |
quantize_(pipe.vae, weight, device="cuda")
|
| 134 |
return pipe
|
|
|
|
| 141 |
pipe.unload_lora_weights()
|
| 142 |
pipe.transformer.fuse_qkv_projections()
|
| 143 |
pipe.vae.fuse_qkv_projections()
|
| 144 |
+
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else int8_dynamic_activation_int8_weight()
|
| 145 |
quantize_(pipe.transformer, weight, device="cuda")
|
| 146 |
quantize_(pipe.vae, weight, device="cuda")
|
| 147 |
pipe.transformer.to(memory_format=torch.channels_last)
|
|
|
|
| 152 |
|
| 153 |
def load_pipeline_opt(repo_id: str, dtype: torch.dtype) -> Any:
|
| 154 |
quantization_config = TorchAoConfig("int4dq" if IS_4BIT else "float8dq" if IS_CC90 else "int8wo")
|
| 155 |
+
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else int8_dynamic_activation_int8_weight()
|
| 156 |
transformer = FluxTransformer2DModel.from_pretrained(repo_id, subfolder="transformer", torch_dtype=dtype)
|
| 157 |
transformer.fuse_qkv_projections()
|
| 158 |
if IS_CC90: quantize_(transformer, float8_dynamic_activation_float8_weight(granularity=PerRow()), device="cuda")
|
|
|
|
| 170 |
#vae = torch.compile(vae, mode="max-autotune", fullgraph=True)
|
| 171 |
vae = torch.compile(vae, mode="max-autotune-no-cudagraphs")
|
| 172 |
pipe = FluxPipeline.from_pretrained(repo_id, transformer=None, vae=None, torch_dtype=dtype, quantization_config=quantization_config)
|
| 173 |
+
quantize_(pipe.text_encoder, int8_dynamic_activation_int8_weight())
|
| 174 |
+
quantize_(pipe.text_encoder_2, int8_dynamic_activation_int8_weight())
|
| 175 |
pipe.transformer = transformer
|
| 176 |
pipe.vae = vae
|
| 177 |
return pipe
|
| 178 |
|
| 179 |
def load_pipeline_para(repo_id: str, dtype: torch.dtype) -> Any:
|
| 180 |
+
weight = int8_dynamic_activation_int4_weight() if IS_4BIT else int8_dynamic_activation_int8_weight()
|
| 181 |
pipe = FluxPipeline.from_pretrained(repo_id, torch_dtype=dtype)
|
| 182 |
if IS_PARA:
|
| 183 |
if IS_MGPU:
|
|
|
|
| 188 |
parallelize_pipe(pipe, mesh=mesh)
|
| 189 |
parallelize_vae(pipe.vae, mesh=mesh._flatten())
|
| 190 |
apply_cache_on_pipe(pipe, residual_diff_threshold=0.12)
|
| 191 |
+
quantize_(pipe.text_encoder, int8_dynamic_activation_int8_weight())
|
| 192 |
+
quantize_(pipe.text_encoder_2, int8_dynamic_activation_int8_weight())
|
| 193 |
if IS_CC90: quantize_(pipe.transformer, float8_dynamic_activation_float8_weight(granularity=PerRow()), device="cuda")
|
| 194 |
elif IS_CC89: quantize_(pipe.transformer, float8_dynamic_activation_float8_weight(), device="cuda")
|
| 195 |
else: quantize_(pipe.transformer, weight, device="cuda")
|