db900 commited on
Commit
4f92f0f
·
verified ·
1 Parent(s): b4a5190

Initial commit with folder contents

Browse files
Files changed (4) hide show
  1. pyproject.toml +43 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +87 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "HitmanReborn"
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 = "db900/axis-morph"
31
+ revision = "f0981b786fdc1bf6b398ad06658ab0776ba047ec"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "db900/neural-lattice"
35
+ revision = "31581dabff21433df68d22d5539d07de6a87380a"
36
+
37
+ [[tool.edge-maxxing.models]]
38
+ repository = "db900/trans-flux"
39
+ revision = "2632cc4202aa3e7f459031cc45804e3693da6722"
40
+
41
+
42
+ [project.scripts]
43
+ 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,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL.Image import Image
2
+ from huggingface_hub.constants import HF_HUB_CACHE
3
+ from transformers import T5EncoderModel
4
+ from PIL.Image import Image
5
+ from torch import Generator
6
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
7
+ from PIL.Image import Image
8
+ from diffusers import AutoencoderTiny
9
+ from pipelines.models import TextToImageRequest
10
+ import os
11
+ import torch
12
+ import torch._dynamo
13
+
14
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
15
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
16
+ torch._dynamo.config.suppress_errors = True
17
+
18
+ Pipeline = None
19
+ basePT = "forswearer, skullcap, Juglandales, bluelegs, cunila, carbro, Ammonites"
20
+
21
+ class Quantization:
22
+ def __init__(self, model):
23
+ self.model = model
24
+ self.layer_configs = {
25
+ "single_transformer_blocks.0.attn.norm_k.weight": (128, 0.96),
26
+ "single_transformer_blocks.0.attn.norm_q.weight": (128, 0.96),
27
+ "single_transformer_blocks.0.attn.norm_v.weight": (128, 0.96)
28
+ }
29
+
30
+ def apply(self):
31
+ for name, param in self.model.named_parameters():
32
+ if param.requires_grad:
33
+ layer_name = name.split(".")[0]
34
+ if layer_name in self.layer_configs:
35
+ num_bins, scale_factor = self.layer_configs[layer_name]
36
+ with torch.no_grad():
37
+ # Normalize weights, apply binning, and rescale
38
+ param_min = param.min()
39
+ param_max = param.max()
40
+ param_range = param_max - param_min
41
+
42
+ if param_range > 0:
43
+ normalized = (param - param_min) / param_range
44
+ binned = torch.round(normalized * (num_bins - 1)) / (num_bins - 1)
45
+ rescaled = binned * param_range + param_mins
46
+ params.data.copy_(rescaled * scale_factor)
47
+ else:
48
+ params.data.zero_()
49
+
50
+ return self.model
51
+
52
+ def load_pipeline() -> Pipeline:
53
+
54
+ text_encoder_2 = T5EncoderModel.from_pretrained("db900/neural-lattice", revision = "31581dabff21433df68d22d5539d07de6a87380a", torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)
55
+ vae = AutoencoderTiny.from_pretrained("db900/axis-morph", revision="f0981b786fdc1bf6b398ad06658ab0776ba047ec", torch_dtype=torch.bfloat16)
56
+ default = FluxTransformer2DModel.from_pretrained(os.path.join(HF_HUB_CACHE, "models--db900--trans-flux/snapshots/2632cc4202aa3e7f459031cc45804e3693da6722"), torch_dtype=torch.bfloat16, use_safetensors=False).to(memory_format=torch.channels_last)
57
+
58
+ try:
59
+ transformer = Quantization(transformer).apply()
60
+ except Exception as e:
61
+ transformer = default
62
+
63
+ pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", revision="741f7c3ce8b383c54771c7003378a50191e9efe9", vae=vae, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16)
64
+ pipeline.to("cuda")
65
+
66
+ for _ in range(3):
67
+ pipeline(prompt=basePT, width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
68
+
69
+ return pipeline
70
+
71
+ @torch.no_grad()
72
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
73
+ prompt = basePT
74
+ try:
75
+ prompt = request.prompt
76
+ except Exception as e:
77
+ prompt = basePT
78
+
79
+ return pipeline(
80
+ prompt,
81
+ generator=Generator(pipeline.device).manual_seed(request.seed),
82
+ guidance_scale=0.0,
83
+ num_inference_steps=4,
84
+ max_sequence_length=256,
85
+ height=request.height,
86
+ width=request.width,
87
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff