Update src/pipeline.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940#4from 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 = "slobers/Flux.1.Schnella"Revision = "e34d670e44cecbbc90e4962e7aada2ac5ce8b55b"def load_pipeline() -> Pipeline:path = os.path.join(HF_HUB_CACHE, "models--slobers--Flux.1.Schnella/snapshots/e34d670e44cecbbc90e4962e7aada2ac5ce8b55b/transformer")transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False)pipeline = FluxPipeline.from_pretrained(ids, revision=Revision, transformer=transformer, local_files_only=True, torch_dtype=torch.bfloat16,)pipeline.to("cuda")quantize_(pipeline.vae, int8_weight_only())pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True)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(request.prompt,generator=generator,123456789101112131415161718192021222324252627282930313233343536373839404142#2.1from huggingface_hub.constants import HF_HUB_CACHEfrom transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModelimport torchimport torch._dynamoimport gcimport osfrom diffusers import FluxPipeline, 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_onlyfrom para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipeos.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"os.environ["TOKENIZERS_PARALLELISM"] = "True"torch._dynamo.config.suppress_errors = TruePipeline = Noneids = "slobers/Flux.1.Schnella"Revision = "e34d670e44cecbbc90e4962e7aada2ac5ce8b55b"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--slobers--Flux.1.Schnella/snapshots/e34d670e44cecbbc90e4962e7aada2ac5ce8b55b/transformer")transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False).to(memory_format=torch.channels_last)pipeline = FluxPipeline.from_pretrained(ids, revision=Revision, transformer=transformer, local_files_only=True, torch_dtype=torch.bfloat16,)pipeline.to("cuda")quantize_(pipeline.vae, int8_weight_only())apply_cache_on_pipe(pipeline, residual_diff_threshold=0.55)#"torch vae"pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune", fullgraph=True)for _ in range(2):pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, boobs, 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(request.prompt,generator=generator,
src/pipeline.py+13-11