Update src/pipeline.py
Browse files- src/pipeline.py +25 -12
src/pipeline.py
CHANGED
|
@@ -11,34 +11,47 @@ from pipelines.models import TextToImageRequest
|
|
| 11 |
from torch import Generator
|
| 12 |
from diffusers import FluxTransformer2DModel, DiffusionPipeline
|
| 13 |
from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
|
| 16 |
os.environ["TOKENIZERS_PARALLELISM"] = "True"
|
| 17 |
torch._dynamo.config.suppress_errors = True
|
| 18 |
-
Pipeline =
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
def load_pipeline() -> Pipeline:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
quantize_(pipeline.vae, int8_weight_only())
|
| 29 |
-
pipeline.
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
return pipeline
|
| 33 |
|
| 34 |
-
@torch.no_grad()
|
| 35 |
def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
generator = Generator(pipeline.device).manual_seed(request.seed)
|
| 37 |
|
| 38 |
return pipeline(
|
| 39 |
request.prompt,
|
| 40 |
generator=generator,
|
| 41 |
-
guidance_scale=
|
| 42 |
num_inference_steps=4,
|
| 43 |
max_sequence_length=256,
|
| 44 |
height=request.height,
|
|
|
|
| 11 |
from torch import Generator
|
| 12 |
from diffusers import FluxTransformer2DModel, DiffusionPipeline
|
| 13 |
from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
|
| 14 |
+
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
|
| 15 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
|
| 16 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "True"
|
| 17 |
+
torch._dynamo.config.suppress_errors = True
|
| 18 |
|
| 19 |
os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
|
| 20 |
os.environ["TOKENIZERS_PARALLELISM"] = "True"
|
| 21 |
torch._dynamo.config.suppress_errors = True
|
| 22 |
+
Pipeline: TypeAlias = FluxPipeline
|
| 23 |
+
|
| 24 |
+
CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
|
| 25 |
+
REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"
|
| 26 |
|
| 27 |
|
| 28 |
def load_pipeline() -> Pipeline:
|
| 29 |
+
text_encoder = CLIPTextModel.from_pretrained(CHECKPOINT, revision=REVISION, subfolder="text_encoder", local_files_only=True, torch_dtype=torch.bfloat16,)
|
| 30 |
+
text_encoder_2 = T5EncoderModel.from_pretrained(CHECKPOINT, revision=REVISION, subfolder="text_encoder_2", local_files_only=True, torch_dtype=torch.bfloat16,)
|
| 31 |
+
vae = AutoencoderKL.from_pretrained(CHECKPOINT, revision=REVISION, subfolder="vae", local_files_only=True, torch_dtype=torch.bfloat16,)
|
| 32 |
+
path = os.path.join(HF_HUB_CACHE, "models--RobertML--FLUX.1-schnell-int8wo/snapshots/307e0777d92df966a3c0f99f31a6ee8957a9857a")
|
| 33 |
+
transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False,)
|
| 34 |
+
pipeline = FluxPipeline.from_pretrained(CHECKPOINT, revision=REVISION, local_files_only=True, text_encoder=text_encoder, text_encoder_2=text_encoder_2, transformer=transformer, vae=vae, torch_dtype=torch.bfloat16,).to("cuda")
|
| 35 |
+
quantize_(pipeline.text_encoder, float8_weight_only())
|
| 36 |
quantize_(pipeline.vae, int8_weight_only())
|
| 37 |
+
#pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune-no-cudagraphs",)
|
| 38 |
+
pipeline = apply_cache_on_pipe(pipeline, residual_diff_threshold=0.31)
|
| 39 |
+
|
| 40 |
+
pipeline("")
|
| 41 |
+
|
| 42 |
return pipeline
|
| 43 |
|
|
|
|
| 44 |
def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
|
| 45 |
+
gc.collect()
|
| 46 |
+
torch.cuda.empty_cache()
|
| 47 |
+
torch.cuda.reset_peak_memory_stats()
|
| 48 |
+
|
| 49 |
generator = Generator(pipeline.device).manual_seed(request.seed)
|
| 50 |
|
| 51 |
return pipeline(
|
| 52 |
request.prompt,
|
| 53 |
generator=generator,
|
| 54 |
+
guidance_scale=0.0,
|
| 55 |
num_inference_steps=4,
|
| 56 |
max_sequence_length=256,
|
| 57 |
height=request.height,
|