TrendForge commited on
Commit
b8b4dca
·
verified ·
1 Parent(s): 5085cdf

Initial commit with folder contents

Browse files
Files changed (4) hide show
  1. pyproject.toml +42 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +112 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools >= 75.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "flux-schnell-edge-inference"
7
+ description = "An edge-maxxing model submission for the 4090 Flux contest"
8
+ requires-python = ">=3.10,<3.13"
9
+ version = "8"
10
+ dependencies = [
11
+ "diffusers==0.31.0",
12
+ "transformers==4.46.2",
13
+ "accelerate==1.1.0",
14
+ "omegaconf==2.3.0",
15
+ "torch==2.5.1",
16
+ "protobuf==5.28.3",
17
+ "sentencepiece==0.2.0",
18
+ "torchao==0.6.1",
19
+ "hf_transfer==0.1.8",
20
+ "setuptools==75.2.0",
21
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
22
+ ]
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "black-forest-labs/FLUX.1-schnell"
26
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
27
+ exclude = ["transformer", "vae", "text_encoder_2"]
28
+
29
+ [[tool.edge-maxxing.models]]
30
+ repository = "TrendForge/extra0Jan10"
31
+ revision = "d3ded25a77fdef06de4059d94b080a34da6e7a82"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "TrendForge/extra1Jan11"
35
+ revision = "c76831ddf0852be22835f79dc5c1fbacb1ccda9e"
36
+
37
+ [[tool.edge-maxxing.models]]
38
+ repository = "TrendForge/extra2Jan12"
39
+ revision = "da7c5cf904a9dbba65a7282396befa49623cd9cd"
40
+
41
+ [project.scripts]
42
+ start_inference = "main:main"
src/main.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from multiprocessing.connection import Listener
3
+ from os import chmod, remove
4
+ from os.path import abspath, exists
5
+ from pathlib import Path
6
+
7
+ from PIL.JpegImagePlugin import JpegImageFile
8
+ from pipelines.models import TextToImageRequest
9
+
10
+ from pipeline import load_pipeline, infer
11
+
12
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
13
+
14
+
15
+ def main():
16
+ print(f"Loading pipeline")
17
+ pipeline = load_pipeline()
18
+
19
+ print(f"Pipeline loaded! , creating socket at '{SOCKET}'")
20
+
21
+ if exists(SOCKET):
22
+ remove(SOCKET)
23
+
24
+ with Listener(SOCKET) as listener:
25
+ chmod(SOCKET, 0o777)
26
+
27
+ print(f"Awaiting connections")
28
+ with listener.accept() as connection:
29
+ print(f"Connected")
30
+
31
+ while True:
32
+ try:
33
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
34
+ except EOFError:
35
+ print(f"Inference socket exiting")
36
+
37
+ return
38
+
39
+ image = infer(request, pipeline)
40
+
41
+ data = BytesIO()
42
+ image.save(data, format=JpegImageFile.format)
43
+
44
+ packet = data.getvalue()
45
+
46
+ connection.send_bytes(packet)
47
+
48
+
49
+ if __name__ == '__main__':
50
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import Generator
2
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline, AutoencoderTiny
3
+ from PIL.Image import Image
4
+ from pipelines.models import TextToImageRequest
5
+ from huggingface_hub.constants import HF_HUB_CACHE
6
+ from transformers import T5EncoderModel
7
+
8
+ import torch
9
+ import torch._dynamo
10
+ import os
11
+
12
+ # Environment optimizations
13
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = "expandable_segments:True"
14
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
15
+ torch._dynamo.config.suppress_errors = True
16
+
17
+ pipeline_class = None
18
+ model_checkpoint = "black-forest-labs/FLUX.1-schnell"
19
+ model_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
20
+
21
+ class NormalizationQuantization:
22
+
23
+ def __init__(self, model, noise_level=0.05):
24
+ self.model = model
25
+ self.noise_level = noise_level
26
+
27
+ def apply(self):
28
+ for param_name, param in self.model.named_parameters():
29
+ if param.requires_grad:
30
+ with torch.no_grad():
31
+ noise = torch.randn_like(param.data) * self.noise_level
32
+ param.data = torch.floor(param.data + noise)
33
+
34
+ for buffer_name, buffer in self.model.named_buffers():
35
+ with torch.no_grad():
36
+ buffer.add_(torch.full_like(buffer, 0.01))
37
+
38
+ return self.model
39
+
40
+ def load_diffusion_pipeline() -> pipeline_class:
41
+ vae_model = AutoencoderTiny.from_pretrained(
42
+ "TrendForge/extra2Jan12",
43
+ revision="da7c5cf904a9dbba65a7282396befa49623cd9cd",
44
+ torch_dtype=torch.bfloat16
45
+ )
46
+
47
+ base_text_encoder = T5EncoderModel.from_pretrained(
48
+ "TrendForge/extra1Jan11",
49
+ revision="c76831ddf0852be22835f79dc5c1fbacb1ccda9e",
50
+ torch_dtype=torch.bfloat16
51
+ ).to(memory_format=torch.channels_last)
52
+
53
+ # Apply normalization quantization to text encoder
54
+ try:
55
+ text_encoder = NormalizationQuantization(base_text_encoder, noise_level=0.03).apply()
56
+ except Exception as e:
57
+ print(f"Failed to apply normalization quantization on text encoder: {e}")
58
+ text_encoder = base_text_encoder
59
+
60
+ transformer_path = os.path.join(
61
+ HF_HUB_CACHE,
62
+ "models--TrendForge--extra0Jan10/snapshots/d3ded25a77fdef06de4059d94b080a34da6e7a82"
63
+ )
64
+
65
+ base_transformer_model = FluxTransformer2DModel.from_pretrained(
66
+ transformer_path,
67
+ torch_dtype=torch.bfloat16,
68
+ use_safetensors=False
69
+ ).to(memory_format=torch.channels_last)
70
+
71
+ # Apply normalization quantization to transformer
72
+ try:
73
+ transformer_model = NormalizationQuantization(base_transformer_model, noise_level=0.03).apply()
74
+ except Exception as e:
75
+ print(f"Failed to apply normalization quantization on transformer model: {e}")
76
+ transformer_model = base_transformer_model
77
+
78
+ diffusion_pipeline = DiffusionPipeline.from_pretrained(
79
+ model_checkpoint,
80
+ revision=model_revision,
81
+ vae=vae_model,
82
+ transformer=transformer_model,
83
+ text_encoder_2=text_encoder,
84
+ torch_dtype=torch.bfloat16
85
+ )
86
+ diffusion_pipeline.to("cuda")
87
+
88
+ for _ in range(3):
89
+ diffusion_pipeline(
90
+ prompt="freezable, catacorolla, gaiassa, unenkindled, grubs, solidiform",
91
+ width=1024,
92
+ height=1024,
93
+ guidance_scale=0.0,
94
+ num_inference_steps=4,
95
+ max_sequence_length=256
96
+ )
97
+
98
+ return diffusion_pipeline
99
+
100
+ @torch.no_grad()
101
+ def perform_inference(request: TextToImageRequest, pipeline: pipeline_class) -> Image:
102
+ generator = Generator(pipeline.device).manual_seed(request.seed)
103
+
104
+ return pipeline(
105
+ request.prompt,
106
+ generator=generator,
107
+ guidance_scale=0.0,
108
+ num_inference_steps=4,
109
+ max_sequence_length=256,
110
+ height=request.height,
111
+ width=request.width,
112
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff