VictorTn commited on
Commit
fd7197b
·
verified ·
1 Parent(s): 3b1187a

Initial commit with folder contents

Browse files
Files changed (1) hide show
  1. src/pipeline.py +76 -10
src/pipeline.py CHANGED
@@ -1,17 +1,20 @@
 
 
 
1
  import torch
2
  import torch._dynamo
3
- import os
4
  import torch.nn.functional as F
5
  from PIL import Image
6
- from pipelines.models import TextToImageRequest
7
  from torch import Generator
8
- from typing import Type
9
  from diffusers import DiffusionPipeline, FluxTransformer2DModel
10
  from huggingface_hub.constants import HF_HUB_CACHE
11
  from transformers import T5EncoderModel
 
12
 
13
- os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
 
14
  os.environ["TOKENIZERS_PARALLELISM"] = "True"
 
15
  torch._dynamo.config.suppress_errors = True
16
  torch.backends.cuda.matmul.allow_tf32 = True
17
  torch.backends.cudnn.enabled = True
@@ -19,18 +22,81 @@ torch.backends.cudnn.enabled = True
19
  Pipeline = None
20
 
21
  def load_pipeline() -> Pipeline:
 
 
 
 
 
 
 
22
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
23
  ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
24
- text_encoder_2 = T5EncoderModel.from_pretrained("VictorTn/extra0izer0", revision = "ea3cc69c8eba166304100f994e9b6ec9f5179a9d", subfolder="text_encoder_2",torch_dtype=torch.bfloat16)
25
- path = os.path.join(HF_HUB_CACHE, "models--VictorTn--extra0izer0/snapshots/ea3cc69c8eba166304100f994e9b6ec9f5179a9d/transformer")
26
- transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False)
27
- pipeline = DiffusionPipeline.from_pretrained(ckpt_id, revision=ckpt_revision, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16,)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  pipeline.to("cuda")
29
  pipeline.to(memory_format=torch.channels_last)
 
 
30
  with torch.inference_mode():
31
- 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)
 
 
 
 
 
 
 
 
32
  return pipeline
33
 
34
  @torch.no_grad()
35
  def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image:
36
- 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]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Type
3
+
4
  import torch
5
  import torch._dynamo
 
6
  import torch.nn.functional as F
7
  from PIL import Image
 
8
  from torch import Generator
 
9
  from diffusers import DiffusionPipeline, FluxTransformer2DModel
10
  from huggingface_hub.constants import HF_HUB_CACHE
11
  from transformers import T5EncoderModel
12
+ from pipelines.models import TextToImageRequest
13
 
14
+ # Configure environment variables
15
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = "expandable_segments:True"
16
  os.environ["TOKENIZERS_PARALLELISM"] = "True"
17
+
18
  torch._dynamo.config.suppress_errors = True
19
  torch.backends.cuda.matmul.allow_tf32 = True
20
  torch.backends.cudnn.enabled = True
 
22
  Pipeline = None
23
 
24
  def load_pipeline() -> Pipeline:
25
+ """
26
+ Load and initialize the Diffusion Pipeline with custom components.
27
+
28
+ Returns:
29
+ Pipeline: Initialized diffusion pipeline.
30
+ """
31
+ # Configuration for model checkpoints
32
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
33
  ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
34
+
35
+ # Load the secondary text encoder
36
+ text_encoder_2 = T5EncoderModel.from_pretrained(
37
+ "VictorTn/extra0izer0",
38
+ revision="ea3cc69c8eba166304100f994e9b6ec9f5179a9d",
39
+ subfolder="text_encoder_2",
40
+ torch_dtype=torch.bfloat16
41
+ )
42
+
43
+ # Load the transformer model
44
+ transformer_path = os.path.join(
45
+ HF_HUB_CACHE,
46
+ "models--VictorTn--extra0izer0/snapshots/ea3cc69c8eba166304100f994e9b6ec9f5179a9d/transformer"
47
+ )
48
+ transformer = FluxTransformer2DModel.from_pretrained(
49
+ transformer_path,
50
+ torch_dtype=torch.bfloat16,
51
+ use_safetensors=False
52
+ )
53
+
54
+ # Initialize the diffusion pipeline
55
+ pipeline = DiffusionPipeline.from_pretrained(
56
+ ckpt_id,
57
+ revision=ckpt_revision,
58
+ transformer=transformer,
59
+ text_encoder_2=text_encoder_2,
60
+ torch_dtype=torch.bfloat16
61
+ )
62
+
63
+ # Move pipeline to GPU and optimize memory format
64
  pipeline.to("cuda")
65
  pipeline.to(memory_format=torch.channels_last)
66
+
67
+ # Perform a warm-up run
68
  with torch.inference_mode():
69
+ pipeline(
70
+ prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus",
71
+ width=1024,
72
+ height=1024,
73
+ guidance_scale=0.0,
74
+ num_inference_steps=4,
75
+ max_sequence_length=256
76
+ )
77
+
78
  return pipeline
79
 
80
  @torch.no_grad()
81
  def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image:
82
+ """
83
+ Perform inference using the provided diffusion pipeline.
84
+
85
+ Args:
86
+ request (TextToImageRequest): The text-to-image request containing prompt and image dimensions.
87
+ pipeline (Pipeline): The initialized diffusion pipeline.
88
+ generator (Generator): Random generator for reproducibility.
89
+
90
+ Returns:
91
+ Image: Generated PIL image.
92
+ """
93
+ return pipeline(
94
+ request.prompt,
95
+ generator=generator,
96
+ guidance_scale=0.0,
97
+ num_inference_steps=4,
98
+ max_sequence_length=256,
99
+ height=request.height,
100
+ width=request.width,
101
+ output_type="pil"
102
+ ).images[0]