Act commited on
Commit
3edba61
·
verified ·
1 Parent(s): 0262373

Initial commit with folder contents

Browse files
Files changed (4) hide show
  1. pyproject.toml +35 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +90 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
21
+ "torch-tensorrt>=2.5.0",
22
+ ]
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "jokerbit/flux.1-schnell-Robert-int8wo"
26
+ revision = "5ef0012f11a863e5111ec56540302a023bc8587b"
27
+
28
+
29
+ [[tool.edge-maxxing.models]]
30
+ repository = "madebyollin/taef1"
31
+ revision = "2d552378e58c9c94201075708d7de4e1163b2689"
32
+
33
+
34
+ [project.scripts]
35
+ 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
+ import torch
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
+ generator = torch.Generator(pipeline.device)
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, generator.manual_seed(request.seed))
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,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gc
2
+ import os
3
+ from typing import TypeAlias
4
+
5
+ import torch
6
+ from PIL.Image import Image
7
+ from diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, AutoencoderTiny
8
+ from huggingface_hub.constants import HF_HUB_CACHE
9
+ from pipelines.models import TextToImageRequest
10
+ from torch import Generator
11
+ from torchao.quantization import quantize_, int8_weight_only
12
+ from transformers import T5EncoderModel, CLIPTextModel, logging
13
+
14
+
15
+ Pipeline: TypeAlias = FluxPipeline
16
+ torch.backends.cudnn.benchmark = True
17
+ torch._inductor.config.conv_1x1_as_mm = True
18
+ torch._inductor.config.coordinate_descent_tuning = True
19
+ torch._inductor.config.epilogue_fusion = False
20
+ torch._inductor.config.coordinate_descent_check_all_directions = True
21
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
22
+
23
+ CHECKPOINT = "jokerbit/flux.1-schnell-Robert-int8wo"
24
+ REVISION = "5ef0012f11a863e5111ec56540302a023bc8587b"
25
+
26
+ TinyVAE = "madebyollin/taef1"
27
+ TinyVAE_REV = "2d552378e58c9c94201075708d7de4e1163b2689"
28
+
29
+
30
+ def load_pipeline() -> Pipeline:
31
+ path = os.path.join(HF_HUB_CACHE, "models--jokerbit--flux.1-schnell-Robert-int8wo/snapshots/5ef0012f11a863e5111ec56540302a023bc8587b/transformer")
32
+ transformer = FluxTransformer2DModel.from_pretrained(
33
+ path,
34
+ use_safetensors=False,
35
+ local_files_only=True,
36
+ torch_dtype=torch.bfloat16)
37
+ vae = AutoencoderTiny.from_pretrained(
38
+ TinyVAE,
39
+ revision=TinyVAE_REV,
40
+ local_files_only=True,
41
+ torch_dtype=torch.bfloat16)
42
+ pipeline = FluxPipeline.from_pretrained(
43
+ CHECKPOINT,
44
+ revision=REVISION,
45
+ transformer=transformer,
46
+ vae=vae,
47
+ local_files_only=True,
48
+ torch_dtype=torch.bfloat16,
49
+ )
50
+
51
+ pipeline.to(memory_format=torch.channels_last)
52
+ # quantize_(pipeline.vae, int8_weight_only())
53
+ pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune", fullgraph=True)
54
+ pipeline.to("cuda")
55
+
56
+ for _ in range(2):
57
+ pipeline("cat", num_inference_steps=4)
58
+
59
+ return pipeline
60
+
61
+ @torch.inference_mode()
62
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: torch.Generator) -> Image:
63
+
64
+ return pipeline(
65
+ request.prompt,
66
+ generator=generator,
67
+ guidance_scale=0.0,
68
+ num_inference_steps=4,
69
+ max_sequence_length=256,
70
+ height=request.height,
71
+ width=request.width,
72
+ ).images[0]
73
+
74
+
75
+ if __name__ == "__main__":
76
+ from time import perf_counter
77
+ PROMPT = 'martyr, semiconformity, peregrination, quip, twineless, emotionless, tawa, depickle'
78
+ request = TextToImageRequest(prompt=PROMPT,
79
+ height=None,
80
+ width=None,
81
+ seed=666)
82
+ start_time = perf_counter()
83
+ pipe_ = load_pipeline()
84
+ stop_time = perf_counter()
85
+ print(f"Pipeline is loaded in {stop_time - start_time}s")
86
+ for _ in range(4):
87
+ start_time = perf_counter()
88
+ infer(request, pipe_)
89
+ stop_time = perf_counter()
90
+ print(f"Request in {stop_time - start_time}s")
uv.lock ADDED
The diff for this file is too large to render. See raw diff