sharper740 commited on
Commit
4e29faa
·
verified ·
1 Parent(s): be9c768

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +40 -0
  2. src/main.py +57 -0
  3. src/pipeline.py +93 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 by RobertML 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
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
19
+ "gitpython>=3.1.43",
20
+ "hf_transfer==0.1.8",
21
+ "torchao==0.6.1",
22
+ "setuptools>=75.3.0",
23
+ ]
24
+
25
+ [[tool.edge-maxxing.models]]
26
+ repository = "black-forest-labs/FLUX.1-schnell"
27
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
28
+
29
+ [[tool.edge-maxxing.models]]
30
+ repository = "city96/t5-v1_1-xxl-encoder-bf16"
31
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "position025/FLUX.1-schnell-8-pda"
35
+ revision = "cdee1db6a8b1858435ec4397b19ed703265d2153"
36
+
37
+
38
+ [project.scripts]
39
+ start_inference = "main:main"
40
+
src/main.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import atexit
2
+ from io import BytesIO
3
+ from multiprocessing.connection import Listener
4
+ from os import chmod, remove
5
+ from os.path import abspath, exists
6
+ from pathlib import Path
7
+ import torch
8
+
9
+ from PIL.JpegImagePlugin import JpegImageFile
10
+ from pipelines.models import TextToImageRequest
11
+ from pipeline import load_pipeline, infer
12
+
13
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
14
+
15
+
16
+ def at_exit():
17
+ torch.cuda.empty_cache()
18
+
19
+
20
+ def main():
21
+ atexit.register(at_exit)
22
+
23
+ print(f"Loading pipeline")
24
+ pipeline = load_pipeline()
25
+
26
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
27
+
28
+ if exists(SOCKET):
29
+ remove(SOCKET)
30
+
31
+ with Listener(SOCKET) as listener:
32
+ chmod(SOCKET, 0o777)
33
+
34
+ print(f"Awaiting connections")
35
+ with listener.accept() as connection:
36
+ print(f"Connected")
37
+ generator = torch.Generator("cuda")
38
+ while True:
39
+ try:
40
+ request = TextToImageRequest.model_validate_json(
41
+ connection.recv_bytes().decode("utf-8")
42
+ )
43
+ except EOFError:
44
+ print(f"Inference socket exiting")
45
+
46
+ return
47
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
48
+ data = BytesIO()
49
+ image.save(data, format=JpegImageFile.format)
50
+
51
+ packet = data.getvalue()
52
+
53
+ connection.send_bytes(packet)
54
+
55
+
56
+ if __name__ == "__main__":
57
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
+ from diffusers.image_processor import VaeImageProcessor
3
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
+ from huggingface_hub.constants import HF_HUB_CACHE
5
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
6
+ import torch
7
+ import torch._dynamo
8
+ import gc
9
+ from PIL import Image as img
10
+ from PIL.Image import Image
11
+ from pipelines.models import TextToImageRequest
12
+ from torch import Generator
13
+ import time
14
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
16
+ import os
17
+
18
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
19
+ torch._dynamo.config.suppress_errors = True
20
+
21
+ Pipeline = None
22
+
23
+ ckpt_id = "black-forest-labs/FLUX.1-schnell"
24
+ ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
25
+
26
+
27
+ def empty_cache():
28
+ gc.collect()
29
+ torch.cuda.empty_cache()
30
+ torch.cuda.reset_max_memory_allocated()
31
+ torch.cuda.reset_peak_memory_stats()
32
+
33
+
34
+ def load_text_encoder_2():
35
+ id = "city96/t5-v1_1-xxl-encoder-bf16"
36
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
37
+
38
+ return T5EncoderModel.from_pretrained(
39
+ id,
40
+ revision=revision,
41
+ torch_dtype=torch.bfloat16,
42
+ ).to(memory_format=torch.channels_last)
43
+
44
+ def load_transformer_model():
45
+ id = "position025/FLUX.1-schnell-8-pda"
46
+ revision = "cdee1db6a8b1858435ec4397b19ed703265d2153"
47
+
48
+ transformer_path = os.path.join(
49
+ HF_HUB_CACHE,
50
+ f"models--{id.split('/')[0]}--{id.split('/')[1]}/snapshots/{revision}",
51
+ )
52
+ return FluxTransformer2DModel.from_pretrained(
53
+ transformer_path, torch_dtype=torch.bfloat16, use_safetensors=False
54
+ ).to(memory_format=torch.channels_last)
55
+
56
+
57
+
58
+ def load_pipeline() -> Pipeline:
59
+ empty_cache()
60
+
61
+ text_encoder_2 = load_text_encoder_2()
62
+ transformer_model = load_transformer_model()
63
+
64
+ pipeline = FluxPipeline.from_pretrained(
65
+ ckpt_id,
66
+ revision=ckpt_revision,
67
+ transformer=transformer_model,
68
+ text_encoder_2=text_encoder_2,
69
+ torch_dtype=torch.bfloat16,
70
+ ).to("cuda")
71
+
72
+ pipeline.transformer = torch.compile(pipeline.transformer, mode="reduce-overhead")
73
+ for _ in range(3):
74
+ pipeline(prompt="onomancy, aftergo, spirantic, Platyhelmia, modificator, drupaceous, jobbernowl, hereness", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
75
+
76
+ empty_cache()
77
+ return pipeline
78
+
79
+
80
+ @torch.no_grad()
81
+ def infer(
82
+ request: TextToImageRequest, pipeline: Pipeline, generator: Generator
83
+ ) -> Image:
84
+ return pipeline(
85
+ request.prompt,
86
+ generator=generator,
87
+ guidance_scale=0.0,
88
+ num_inference_steps=4,
89
+ max_sequence_length=256,
90
+ height=request.height,
91
+ width=request.width,
92
+ output_type="pil",
93
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff