Update src/pipeline.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import gcimport osfrom typing import TypeAliasimport torchfrom PIL.Image import Imagefrom diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, AutoencoderTinyfrom huggingface_hub.constants import HF_HUB_CACHEfrom pipelines.models import TextToImageRequestfrom torch import Generatorfrom transformers import T5EncoderModel, CLIPTextModelPipeline: TypeAlias = FluxPipelineCHECKPOINT = "black-forest-labs/FLUX.1-schnell"REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"def load_pipeline() -> Pipeline:text_encoder = CLIPTextModel.from_pretrained(CHECKPOINT, revision=REVISION, subfolder="text_encoder", local_files_only=True, torch_dtype=torch.bfloat16,)path2 = os.path.join(HF_HUB_CACHE, "models--city96--t5-v1_1-xxl-encoder-bf16/snapshots/1b9c856aadb864af93c1dcdc226c2774fa67bc86")text_encoder_2 = T5EncoderModel.from_pretrained(path2, torch_dtype=torch.bfloat16,)pathV = os.path.join(HF_HUB_CACHE, "models--madebyollin--taef1/snapshots/5463ee684fd9131a724bea777a2f50d89b0b6b24")vae = AutoencoderTiny.from_pretrained(pathV, torch_dtype=torch.bfloat16,)pathT = os.path.join(HF_HUB_CACHE, "models--RobertML--FLUX.1-schnell-int8wo/snapshots/307e0777d92df966a3c0f99f31a6ee8957a9857a")transformer = FluxTransformer2DModel.from_pretrained(pathT, torch_dtype=torch.bfloat16, use_safetensors=False,)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")pipeline("")return pipelinedef infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:gc.collect()torch.cuda.empty_cache()torch.cuda.reset_peak_memory_stats()generator = Generator(pipeline.device).manual_seed(request.seed)return pipeline(12345678910111213141516171819202122232425262728293031323334353637383940#2from huggingface_hub.constants import HF_HUB_CACHEfrom transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModelimport torchimport torch._dynamoimport gcimport osfrom diffusers import FluxPipeline, AutoencoderKL, AutoencoderTinyfrom PIL.Image import Imagefrom pipelines.models import TextToImageRequestfrom torch import Generatorfrom diffusers import FluxTransformer2DModel, DiffusionPipelinefrom torchao.quantization import quantize_, int8_weight_only, fpx_weight_onlyos.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"os.environ["TOKENIZERS_PARALLELISM"] = "True"torch._dynamo.config.suppress_errors = TruePipeline = Noneids = "black-forest-labs/FLUX.1-schnell"Revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"def load_pipeline() -> Pipeline:vae = AutoencoderKL.from_pretrained(ids,revision=Revision, subfolder="vae", local_files_only=True, torch_dtype=torch.bfloat16,)quantize_(vae, int8_weight_only())text_encoder_2 = T5EncoderModel.from_pretrained("city96/t5-v1_1-xxl-encoder-bf16", revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86", torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)path = os.path.join(HF_HUB_CACHE, "models--RobertML--FLUX.1-schnell-int8wo/snapshots/307e0777d92df966a3c0f99f31a6ee8957a9857a")transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False).to(memory_format=torch.channels_last)pipeline = DiffusionPipeline.from_pretrained(ids, revision=Revision, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16,)pipeline.to("cuda")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)return pipeline@torch.no_grad()def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:generator = Generator(pipeline.device).manual_seed(request.seed)return pipeline(
src/pipeline.py+25-32