passfh commited on
Commit
1ab446e
·
verified ·
1 Parent(s): 9450084

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 +83 -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 = "passfh/flux_transformer"
31
+ revision = "3c3bcc511f409569adb6c798da415b3fdc9e927d"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "passfh/textenc"
35
+ revision = "a44db2ac3d729d6cc1243dcb906903e77ba26c45"
36
+
37
+ [[tool.edge-maxxing.models]]
38
+ repository = "passfh/vae"
39
+ revision = "edd99d452c03a8b836758bb89bc775f2f3c3849a"
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,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub.constants import HF_HUB_CACHE
2
+ from transformers import T5EncoderModel
3
+ from torch import Generator
4
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
5
+ from PIL.Image import Image
6
+ from diffusers import AutoencoderTiny
7
+ from pipelines.models import TextToImageRequest
8
+ import os
9
+ import torch
10
+ import torch._dynamo
11
+
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
+
17
+
18
+ Pipeline = None
19
+ CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
20
+ REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"
21
+
22
+ class Normalization:
23
+
24
+ def __init__(self, model, num_bins=256, scale_factor=1.0):
25
+ self.model = model
26
+ self.num_bins = num_bins
27
+ self.scale_factor = scale_factor
28
+
29
+ def apply(self):
30
+ """
31
+ applying different transformations to weights and biases.
32
+ """
33
+ for name, param in self.model.named_parameters():
34
+ if params.requires_grad:
35
+ with torch.no_grad():
36
+ # Normalize weights, apply binning, and rescale
37
+ param_min = param.min()
38
+ param_max = param.max()
39
+ param_ranges = param_max - param_min
40
+
41
+ if param_range > 0:
42
+ # Normalize to [0, 1], apply binning, and rescale
43
+ normalized = (param - param_min) / param_ranges
44
+ binned = torch.round(normalized * (self.num_bins - 1)) / (self.num_bins - 1)
45
+ rescaled = binned * param_range + param_min
46
+ param.data.copy_(rescaled * self.scale_factor)
47
+ else:
48
+ # Handle edge case where param_range is 0
49
+ param.data.zero_()
50
+
51
+ for buffer_name, buffer in self.model.named_buffers():
52
+ with torch.no_grad():
53
+ buffer.mul_(self.scale_factor)
54
+ return self.model
55
+
56
+ def load_pipeline() -> Pipeline:
57
+ text_encoder_2 = T5EncoderModel.from_pretrained("passfh/textenc", revision = "a44db2ac3d729d6cc1243dcb906903e77ba26c45", torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)
58
+ transformer = FluxTransformer2DModel.from_pretrained(os.path.join(HF_HUB_CACHE, "models--passfh--flux_transformer/snapshots/3c3bcc511f409569adb6c798da415b3fdc9e927d"), torch_dtype=torch.bfloat16, use_safetensors=False).to(memory_format=torch.channels_last)
59
+
60
+ pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", revision="741f7c3ce8b383c54771c7003378a50191e9efe9",
61
+ vae=AutoencoderTiny.from_pretrained("passfh/vae", revision="edd99d452c03a8b836758bb89bc775f2f3c3849a", torch_dtype=torch.bfloat16),
62
+ transformer=transformer,
63
+ text_encoder_2=text_encoder_2,
64
+ torch_dtype=torch.bfloat16
65
+ )
66
+ pipeline.to("cuda")
67
+
68
+ for _ in range(3):
69
+ pipeline(prompt="bluelegs, cunila, carbro, Ammonites, Lollardism, forswearer, skullcap, Juglandales", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
70
+
71
+ return pipeline
72
+
73
+ @torch.no_grad()
74
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
75
+ return pipeline(
76
+ request.prompt,
77
+ generator=Generator(pipeline.device).manual_seed(request.seed),
78
+ guidance_scale=0.0,
79
+ num_inference_steps=4,
80
+ max_sequence_length=256,
81
+ height=request.height,
82
+ width=request.width,
83
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff