sharper740 commited on
Commit
20739ad
·
verified ·
1 Parent(s): 200123d

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +39 -0
  2. src/main.py +53 -0
  3. src/pipeline.py +84 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "city96/t5-v1_1-xxl-encoder-bf16"
26
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
27
+
28
+ [[tool.edge-maxxing.models]]
29
+ repository = "park234/Flux1-schnell-f8"
30
+ revision = "68cf99dad933e87d84733ffef52445fe15cb7386"
31
+
32
+ [[tool.edge-maxxing.models]]
33
+ repository = "park234/Flux1-schnell-int8"
34
+ revision = "ba40332fa9ca3e25d439616fca8ac934a96b73f7"
35
+
36
+
37
+ [project.scripts]
38
+ start_inference = "main:main"
39
+
src/main.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
13
+
14
+
15
+ def at_exit():
16
+ torch.cuda.empty_cache()
17
+
18
+
19
+ def main():
20
+ atexit.register(at_exit)
21
+
22
+ print(f"Loading pipeline")
23
+ pipeline = load_pipeline()
24
+
25
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
26
+
27
+ if exists(SOCKET):
28
+ remove(SOCKET)
29
+
30
+ with Listener(SOCKET) as listener:
31
+ chmod(SOCKET, 0o777)
32
+
33
+ print(f"Awaiting connections")
34
+ with listener.accept() as connection:
35
+ print(f"Connected")
36
+ generator = torch.Generator("cuda")
37
+ while True:
38
+ try:
39
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
40
+ except EOFError:
41
+ print(f"Inference socket exiting")
42
+
43
+ return
44
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
45
+ data = BytesIO()
46
+ image.save(data, format=JpegImageFile.format)
47
+
48
+ packet = data.getvalue()
49
+
50
+ connection.send_bytes(packet )
51
+
52
+ if __name__ == '__main__':
53
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
20
+ Pipeline = None
21
+ torch.backends.cuda.matmul.allow_tf32 = True
22
+ torch.backends.cudnn.enabled = True
23
+ torch.backends.cudnn.benchmark = True
24
+
25
+ CKPT_ID = "park234/Flux1-schnell-f8"
26
+ CKPT_REV = "68cf99dad933e87d84733ffef52445fe15cb7386"
27
+
28
+ def clear_gpu_cache():
29
+ gc.collect()
30
+ torch.cuda.empty_cache()
31
+ torch.cuda.reset_max_memory_allocated()
32
+ torch.cuda.reset_peak_memory_stats()
33
+
34
+ def load_pipeline() -> DiffusionPipeline:
35
+ clear_gpu_cache()
36
+ dtype, dev = torch.bfloat16, "cuda"
37
+ encoder = T5EncoderModel.from_pretrained(
38
+ "city96/t5-v1_1-xxl-encoder-bf16",
39
+ revision="1b9c856aadb864af93c1dcdc226c2774fa67bc86",
40
+ torch_dtype=dtype,
41
+ ).to(memory_format=torch.channels_last)
42
+ model_path = os.path.join(
43
+ HF_HUB_CACHE,
44
+ "models--park234--Flux1-schnell-int8/snapshots/ba40332fa9ca3e25d439616fca8ac934a96b73f7",
45
+ )
46
+ transformer_model = FluxTransformer2DModel.from_pretrained(
47
+ model_path, torch_dtype=dtype, use_safetensors=False
48
+ ).to(memory_format=torch.channels_last)
49
+ pipe = DiffusionPipeline.from_pretrained(
50
+ CKPT_ID,
51
+ revision=CKPT_REV,
52
+ transformer=transformer_model,
53
+ text_encoder_2=encoder,
54
+ torch_dtype=dtype,
55
+ ).to(dev)
56
+ quantize_(pipe.vae, int8_weight_only())
57
+ pipe.vae = torch.compile(pipe.vae, mode="max-autotune")
58
+ for _ in range(3):
59
+ pipe(
60
+ prompt="search history profile tab view edit help",
61
+ width=1024,
62
+ height=1024,
63
+ guidance_scale=0e0,
64
+ num_inference_steps=4,
65
+ max_sequence_length=256,
66
+ )
67
+ clear_gpu_cache()
68
+ return pipe
69
+
70
+ @torch.no_grad()
71
+ def generate_image(
72
+ request: TextToImageRequest, pipe: DiffusionPipeline, gen: Generator
73
+ ) -> Image:
74
+ output = pipe(
75
+ request.prompt,
76
+ generator=gen,
77
+ guidance_scale=0e0,
78
+ num_inference_steps=4,
79
+ max_sequence_length=256,
80
+ height=request.height,
81
+ width=request.width,
82
+ output_type="pil",
83
+ )
84
+ return output.images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff