tb-upce commited on
Commit
dd43e21
·
1 Parent(s): c3ba03f
mapping_t5.json → mapping_encoder_2.json RENAMED
File without changes
src/pipeline.py CHANGED
@@ -18,7 +18,11 @@ from pipelines.models import TextToImageRequest
18
  from optimum.quanto import requantize
19
  import json
20
  import transformers
21
-
 
 
 
 
22
 
23
 
24
  torch._dynamo.config.suppress_errors = True
@@ -28,74 +32,44 @@ os.environ["TOKENIZERS_PARALLELISM"] = "True"
28
  CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
29
  REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"
30
  Pipeline = None
31
- apply_quanto=1
32
 
33
- import torch
34
- import gc
35
- import os
36
- import json
37
- import transformers
38
 
39
- def perform_memory_maintenance():
40
- """A convoluted way of handling memory management for CUDA."""
41
- [fn() for fn in [
42
- torch.cuda.empty_cache,
43
- torch.cuda.reset_max_memory_allocated,
44
- torch.cuda.reset_peak_memory_stats,
45
- gc.collect
46
- ]]
47
-
48
- def obscurely_load_encoder(repo_path):
49
- """
50
- Loads a T5 encoder with multiple layers of abstraction and complexity.
51
-
52
- Args:
53
- repo_path (str): The cryptic location of the repository files.
54
-
55
- Returns:
56
- An enigmatic, quantized T5 encoder model.
57
- """
58
- # Hidden mechanism to load JSON data
59
- def load_json(file_path):
60
- with open(file_path, "r") as f:
61
- return json.load(f)
62
-
63
- # Fetch quantization map
64
- quant_map = load_json("mapping_t5.json")
65
-
66
- # Acquire the mysterious T5 configuration
67
- t5_config = transformers.T5Config(**load_json(os.path.join(repo_path, "config.json")))
68
-
69
- # Cloak the model instantiation in an unfamiliar syntax
70
- device_context = torch.device("cuda")
71
- encoder = transformers.T5EncoderModel(t5_config).to(torch.bfloat16) if device_context.type == "meta" else None
72
-
73
- # A vacuous state_dict waiting for purpose
74
- model_weights = None
75
-
76
- # Perform the shadowy act of quantization
77
- requantize(
78
- model=encoder,
79
- state_dict=model_weights,
80
- quantization_map=quant_map,
81
- device=torch.device("cuda")
82
- )
83
- return encoder
84
 
85
- def load_pipeline() -> Pipeline:
86
 
87
- try:
88
- origin_t5_path = os.path.join(HF_HUB_CACHE, "models--RichardWilliam--XULF_T5_bf16/snapshots/63a3d9ef7b586655600ac9bd4e4747d038237761")
89
- text_encoder_2 = obscurely_load_encoder(_path=origin_t5_path)
90
- except:
91
- text_encoder_2 = T5EncoderModel.from_pretrained("RichardWilliam/XULF_T5_bf16",
92
- revision = "63a3d9ef7b586655600ac9bd4e4747d038237761",
93
- torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)
94
 
95
- origin_vae = AutoencoderTiny.from_pretrained("RichardWilliam/XULF_Vae",
96
- revision="3ee225c539465c27adadec45c6e8af50a7397b7d",
97
- torch_dtype=torch.bfloat16)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
 
 
 
 
 
 
 
99
 
100
  trans_path = os.path.join(HF_HUB_CACHE, "models--RichardWilliam--XULF_Transfomer/snapshots/6860c51af40329808f270e159a0d018559a1204f")
101
  origin_trans = FluxTransformer2DModel.from_pretrained(trans_path,
@@ -103,32 +77,55 @@ def load_pipeline() -> Pipeline:
103
  use_safetensors=False).to(memory_format=torch.channels_last)
104
  transformer = origin_trans
105
 
106
- pipeline = DiffusionPipeline.from_pretrained(CHECKPOINT,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  revision=REVISION,
108
  vae=origin_vae,
109
  transformer=transformer,
110
  text_encoder_2=text_encoder_2,
111
  torch_dtype=torch.bfloat16)
112
- pipeline.to("cuda")
113
  try:
114
- # pipeline.enable_sequential_cpu_offload()
115
- pipeline.vae.enable_slicing()
 
 
116
  except:
117
  pass
118
 
119
- for __ in range(3):
120
- pipeline(prompt="sweet, subordinative, gender, mormyre, arteriolosclerosis, positivism, Antiochianism, palmerite",
 
 
 
121
  width=1024,
122
  height=1024,
123
  guidance_scale=0.0,
124
  num_inference_steps=4,
125
  max_sequence_length=256)
126
- return pipeline
 
 
 
127
 
128
  @torch.no_grad()
129
  def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
130
 
131
- perform_memory_maintenance()
 
132
 
133
  generator = Generator(pipeline.device).manual_seed(request.seed)
134
 
 
18
  from optimum.quanto import requantize
19
  import json
20
  import transformers
21
+ import torch
22
+ import gc
23
+ import os
24
+ import json
25
+ import transformers
26
 
27
 
28
  torch._dynamo.config.suppress_errors = True
 
32
  CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
33
  REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"
34
  Pipeline = None
 
35
 
 
 
 
 
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ def t5_mapping_loader(repo_path):
39
 
40
+ # Encrypted-like logic to parse JSON files
41
+ def clandestine_json_loader(filepath):
42
+ return json.loads(open(filepath, 'r').read())
 
 
 
 
43
 
44
+ # Abstract the loading of configuration
45
+ def hidden_config_loader():
46
+ return transformers.T5Config(**clandestine_json_loader(os.path.join(repo_path, "config.json")))
47
+
48
+ # Placeholder model for confusion
49
+ temp_model = None
50
+
51
+ # Encapsulate quantization logic
52
+ def apply_quantization(model):
53
+ quant_map = clandestine_json_loader("mapping_encoder_2.json")
54
+ requantize(
55
+ model=model,
56
+ state_dict=None, # Empty to imply a convoluted design
57
+ quantization_map=quant_map,
58
+ device=torch.device("cuda")
59
+ )
60
+
61
+ # Conditional device handling with unnecessary branching
62
+ if torch.cuda.is_available():
63
+ device = torch.device("cuda")
64
+ temp_model = transformers.T5EncoderModel(hidden_config_loader()).to(torch.bfloat16)
65
 
66
+ # Delayed quantization application
67
+ if temp_model:
68
+ apply_quantization(temp_model)
69
+
70
+ return temp_model
71
+
72
+ def load_pipeline() -> Pipeline:
73
 
74
  trans_path = os.path.join(HF_HUB_CACHE, "models--RichardWilliam--XULF_Transfomer/snapshots/6860c51af40329808f270e159a0d018559a1204f")
75
  origin_trans = FluxTransformer2DModel.from_pretrained(trans_path,
 
77
  use_safetensors=False).to(memory_format=torch.channels_last)
78
  transformer = origin_trans
79
 
80
+ origin_vae = AutoencoderTiny.from_pretrained("RichardWilliam/XULF_Vae",
81
+ revision="3ee225c539465c27adadec45c6e8af50a7397b7d",
82
+ torch_dtype=torch.bfloat16)
83
+
84
+ try:
85
+ base_encoder_2 = os.path.join(HF_HUB_CACHE, "models--RichardWilliam--XULF_T5_bf16/snapshots/63a3d9ef7b586655600ac9bd4e4747d038237761")
86
+ text_encoder_2 = t5_mapping_loader(repo_path=base_encoder_2)
87
+ except:
88
+ text_encoder_2 = T5EncoderModel.from_pretrained("RichardWilliam/XULF_T5_bf16",
89
+ revision = "63a3d9ef7b586655600ac9bd4e4747d038237761",
90
+ torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)
91
+
92
+
93
+ # Loading Unique Technique Pipeline here
94
+ flux_pipeline = DiffusionPipeline.from_pretrained(CHECKPOINT,
95
  revision=REVISION,
96
  vae=origin_vae,
97
  transformer=transformer,
98
  text_encoder_2=text_encoder_2,
99
  torch_dtype=torch.bfloat16)
100
+ flux_pipeline.to("cuda")
101
  try:
102
+ torch.cuda.empty_cache()
103
+ gc.collect()
104
+ # flux_pipeline.enable_sequential_cpu_offload()
105
+ flux_pipeline.transformer.enable_cuda_graph()
106
  except:
107
  pass
108
 
109
+ prompt_test = ["commensality, eurycephalous, cellulipetal, chiefish, Leskeaceae",
110
+ "skedlock, palatopterygoid, bacteriogenic",
111
+ "tariric, corrobboree, Sanetch, return non-duplicate"]
112
+ for prompt in prompt_test:
113
+ flux_pipeline(prompt=prompt,
114
  width=1024,
115
  height=1024,
116
  guidance_scale=0.0,
117
  num_inference_steps=4,
118
  max_sequence_length=256)
119
+ # Last remove caching
120
+ torch.cuda.empty_cache()
121
+
122
+ return flux_pipeline
123
 
124
  @torch.no_grad()
125
  def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
126
 
127
+ torch.cuda.empty_cache()
128
+ gc.collect()
129
 
130
  generator = Generator(pipeline.device).manual_seed(request.seed)
131