Download src/pipeline.py from manbeast3b/newcomer2_fix1: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/manbeast3b/newcomer2_fix1/resolve/main/src/pipeline.py
- Command line
-
hf download hf://manbeast3b/newcomer2_fix1/src/pipeline.py
-
curl -L -o pipeline.py https://huggingface.co/manbeast3b/newcomer2_fix1/resolve/main/src/pipeline.py
3.2 kB
| from diffusers import ( | |
| DiffusionPipeline, | |
| AutoencoderKL, | |
| AutoencoderTiny, | |
| FluxPipeline, | |
| FluxTransformer2DModel | |
| ) | |
| from diffusers.image_processor import VaeImageProcessor | |
| from diffusers.schedulers import FlowMatchEulerDiscreteScheduler | |
| from huggingface_hub.constants import HF_HUB_CACHE | |
| from transformers import ( | |
| T5EncoderModel, | |
| T5TokenizerFast, | |
| CLIPTokenizer, | |
| CLIPTextModel | |
| ) | |
| import torch | |
| import torch._dynamo | |
| import gc | |
| from PIL import Image | |
| from pipelines.models import TextToImageRequest | |
| from torch import Generator | |
| import time | |
| import math | |
| from typing import Type, Dict, Any, Tuple, Callable, Optional, Union | |
| import numpy as np | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from torchao.quantization import quantize_, int8_weight_only | |
| # preconfigs | |
| import os | |
| os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True" | |
| os.environ["TOKENIZERS_PARALLELISM"] = "True" | |
| torch._dynamo.config.suppress_errors = True | |
| torch.backends.cuda.matmul.allow_tf32 = True | |
| torch.backends.cudnn.enabled = True | |
| torch.backends.cudnn.benchmark = True | |
| # globals | |
| Pipeline = None | |
| ckpt_id = "manbeast3b/Flux.1.Schnell-full-quant1" | |
| ckpt_revision = "72f5862ef1b1ac1326765aa2e6aafe1c56a7c001" | |
| def empty_cache(): | |
| gc.collect() | |
| torch.cuda.empty_cache() | |
| torch.cuda.reset_max_memory_allocated() | |
| torch.cuda.reset_peak_memory_stats() | |
| def load_pipeline() -> Pipeline: | |
| path = os.path.join(HF_HUB_CACHE, "models--manbeast3b--Flux.1.Schnell-full-quant1/snapshots/72f5862ef1b1ac1326765aa2e6aafe1c56a7c001/transformer") | |
| transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False) | |
| pipeline = FluxPipeline.from_pretrained(ckpt_id, revision=ckpt_revision, transformer=transformer, | |
| # vae=vae, | |
| local_files_only=True, torch_dtype=torch.bfloat16,) | |
| basepath = os.path.join(HF_HUB_CACHE, "models--manbeast3b--Flux.1.schnell-vae-kl-p10/snapshots/facb90ac7d8e13df9a8c177f18b0d450a3e1ed41") | |
| pipeline.vae.encoder.load_state_dict(torch.load(os.path.join(basepath, "encoder.pth")), strict=False) | |
| pipeline.vae.decoder.load_state_dict(torch.load(os.path.join(basepath, "decoder.pth")), strict=False) | |
| pipeline.to("cuda") | |
| quantize_(pipeline.vae, int8_weight_only()) | |
| pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True) | |
| for component in [pipeline.text_encoder, pipeline.text_encoder_2, pipeline.transformer, pipeline.vae]: | |
| component.to(memory_format=torch.channels_last) | |
| for _ in range(3): | |
| pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256) | |
| # empty_cache() | |
| return pipeline | |
| def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image: | |
| return pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pil").images[0] |