sharper740 commited on
Commit
0ed3f4c
·
verified ·
1 Parent(s): 6968fe2

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +29 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +87 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "setuptools>=75.3.0",
22
+ ]
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "jokerbit/flux.1-schnell-Robert-int8wo"
26
+ revision = "5ef0012f11a863e5111ec56540302a023bc8587b"
27
+
28
+ [project.scripts]
29
+ 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"accepted")
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,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (
8
+ FluxPipeline,
9
+ FluxTransformer2DModel,
10
+ AutoencoderKL,
11
+ AutoencoderTiny,
12
+ DiffusionPipeline,
13
+ )
14
+ from huggingface_hub.constants import HF_HUB_CACHE
15
+ from pipelines.models import TextToImageRequest
16
+ from torch import Generator
17
+ from torchao.quantization import quantize_, int8_weight_only
18
+ from transformers import T5EncoderModel, CLIPTextModel, logging
19
+ import torch._dynamo
20
+
21
+ torch._dynamo.config.suppress_errors = True
22
+
23
+ Pipeline: TypeAlias = FluxPipeline
24
+
25
+ torch.backends.cudnn.benchmark = True
26
+ torch._inductor.config.conv_1x1_as_mm = True
27
+ torch._inductor.config.coordinate_descent_tuning = True
28
+ torch._inductor.config.epilogue_fusion = False
29
+ torch._inductor.config.coordinate_descent_check_all_directions = True
30
+
31
+
32
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
33
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
34
+ CHECKPOINT = "jokerbit/flux.1-schnell-Robert-int8wo"
35
+ REVISION = "5ef0012f11a863e5111ec56540302a023bc8587b"
36
+
37
+
38
+ def load_pipeline() -> Pipeline:
39
+ path = os.path.join(
40
+ HF_HUB_CACHE,
41
+ "models--jokerbit--flux.1-schnell-Robert-int8wo/snapshots/5ef0012f11a863e5111ec56540302a023bc8587b/transformer",
42
+ )
43
+ transformer = FluxTransformer2DModel.from_pretrained(
44
+ path, use_safetensors=False, local_files_only=True, torch_dtype=torch.bfloat16
45
+ )
46
+
47
+ pipeline = FluxPipeline.from_pretrained(
48
+ CHECKPOINT,
49
+ revision=REVISION,
50
+ transformer=transformer,
51
+ local_files_only=True,
52
+ torch_dtype=torch.bfloat16,
53
+ ).to("cuda")
54
+
55
+ pipeline.to(memory_format=torch.channels_last)
56
+ pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune")
57
+ quantize_(pipeline.vae, int8_weight_only())
58
+ pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune")
59
+
60
+ with torch.no_grad():
61
+ for _ in range(5):
62
+ pipeline(
63
+ prompt="onomancy, aftergo, spirantic, Platyhelmia, modificator, drupaceous, jobbernowl, hereness",
64
+ width=1024,
65
+ height=1024,
66
+ guidance_scale=0.0,
67
+ num_inference_steps=4,
68
+ max_sequence_length=256,
69
+ )
70
+ torch.cuda.empty_cache()
71
+ return pipeline
72
+
73
+
74
+ @torch.no_grad()
75
+ def infer(
76
+ request: TextToImageRequest, pipeline: Pipeline, generator: torch.Generator
77
+ ) -> Image:
78
+
79
+ return pipeline(
80
+ request.prompt,
81
+ generator=generator,
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