manbeast3b commited on
Commit
136f352
·
verified ·
1 Parent(s): 687aa09

Update src/pipeline.py

Browse files
Files changed (1) hide show
  1. src/pipeline.py +12 -22
src/pipeline.py CHANGED
@@ -1,53 +1,43 @@
1
- from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
  from diffusers.image_processor import VaeImageProcessor
3
- from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
-
5
- from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
6
  import torch
7
  import torch._dynamo
8
  import gc
9
- from PIL import Image as img
10
  from PIL.Image import Image
11
  from pipelines.models import TextToImageRequest
12
  from torch import Generator
13
- import time
14
- from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
  from torchao.quantization import quantize_, int8_weight_only
16
- Pipeline = None
17
 
18
- ckpt_id = "black-forest-labs/FLUX.1-schnell"
19
- def empty_cache():
20
- start = time.time()
21
  gc.collect()
22
  torch.cuda.empty_cache()
23
  torch.cuda.reset_max_memory_allocated()
24
  torch.cuda.reset_peak_memory_stats()
25
- print(f"Flush took: {time.time() - start}")
26
 
27
  def load_pipeline() -> Pipeline:
28
- empty_cache()
29
  dtype, device = torch.bfloat16, "cuda"
30
  vae = AutoencoderKL.from_pretrained(
31
- ckpt_id, subfolder="vae", torch_dtype=torch.bfloat16
32
  )
33
- quantize_(vae, int8_weight_only())
34
  pipeline = DiffusionPipeline.from_pretrained(
35
- ckpt_id,
36
  vae=vae,
37
  torch_dtype=dtype,
38
  )
39
  pipeline.enable_sequential_cpu_offload()
40
  for _ in range(2):
41
- empty_cache()
42
- pipeline(prompt="onomancy, aftergo, spirantic, Platyhelmia, modificator, drupaceous, jobbernowl, hereness", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
43
-
44
  return pipeline
45
 
46
-
47
- from datetime import datetime
48
  @torch.inference_mode()
49
  def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
50
- empty_cache()
51
  generator = Generator("cuda").manual_seed(request.seed)
52
  image=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]
53
  return image
 
1
+ from diffusers import AutoencoderKL
2
  from diffusers.image_processor import VaeImageProcessor
 
 
 
3
  import torch
4
  import torch._dynamo
5
  import gc
 
6
  from PIL.Image import Image
7
  from pipelines.models import TextToImageRequest
8
  from torch import Generator
9
+ from diffusers import DiffusionPipeline
 
10
  from torchao.quantization import quantize_, int8_weight_only
 
11
 
12
+ Pipeline = None
13
+ MODEL_ID = "black-forest-labs/FLUX.1-schnell"
14
+ def clear():
15
  gc.collect()
16
  torch.cuda.empty_cache()
17
  torch.cuda.reset_max_memory_allocated()
18
  torch.cuda.reset_peak_memory_stats()
 
19
 
20
  def load_pipeline() -> Pipeline:
21
+ clear()
22
  dtype, device = torch.bfloat16, "cuda"
23
  vae = AutoencoderKL.from_pretrained(
24
+ MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
25
  )
26
+ quantize_(vae, int8_weight_only(), device="cuda")
27
  pipeline = DiffusionPipeline.from_pretrained(
28
+ MODEL_ID,
29
  vae=vae,
30
  torch_dtype=dtype,
31
  )
32
  pipeline.enable_sequential_cpu_offload()
33
  for _ in range(2):
34
+ pipeline(prompt="unpervaded, unencumber, froggish, groundneedle, transnatural, fatherhood, outjump, cinerator", width=1024, height=1024, guidance_scale=0.1, num_inference_steps=4, max_sequence_length=256)
35
+ clear()
 
36
  return pipeline
37
 
 
 
38
  @torch.inference_mode()
39
  def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
40
+ clear()
41
  generator = Generator("cuda").manual_seed(request.seed)
42
  image=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]
43
  return image