slobers commited on
Commit
5fe7ba0
·
verified ·
1 Parent(s): d098dff

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +19 -0
  2. pyproject.toml +42 -0
  3. src/main.py +50 -0
  4. src/pipeline.py +56 -0
  5. uv.lock +0 -0
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # flux-schnell-edge-inference
2
+
3
+ This holds the baseline for the FLUX Schnel NVIDIA GeForce RTX 4090 contest, which can be forked freely and optimized
4
+
5
+ Some recommendations are as follows:
6
+ - Installing dependencies should be done in `pyproject.toml`, including git dependencies
7
+ - HuggingFace models should be specified in the `models` array in the `pyproject.toml` file, and will be downloaded before benchmarking
8
+ - The pipeline does **not** have internet access so all dependencies and models must be included in the `pyproject.toml`
9
+ - Compiled models should be hosted on HuggingFace and included in the `models` array in the `pyproject.toml` (rather than compiling during loading). Loading time matters far more than file sizes
10
+ - Avoid changing `src/main.py`, as that includes mostly protocol logic. Most changes should be in `models` and `src/pipeline.py`
11
+ - Ensure the entire repository (excluding dependencies and HuggingFace models) is under 16MB
12
+
13
+ For testing, you need a docker container with pytorch and ubuntu 22.04.
14
+ You can download your listed dependencies with `uv`, installed with:
15
+ ```bash
16
+ pipx ensurepath
17
+ pipx install uv
18
+ ```
19
+ You can then relock with `uv lock`, and then run with `uv run start_inference`
pyproject.toml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]
22
+
23
+ [[tool.edge-maxxing.models]]
24
+ repository = "black-forest-labs/FLUX.1-schnell"
25
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
26
+ exclude = ["transformer", "vae", "text_encoder_2"]
27
+
28
+ [[tool.edge-maxxing.models]]
29
+ repository = "RobertML/FLUX.1-schnell-int8wo"
30
+ revision = "307e0777d92df966a3c0f99f31a6ee8957a9857a"
31
+
32
+ [[tool.edge-maxxing.models]]
33
+ repository = "madebyollin/taef1"
34
+ revision = "5463ee684fd9131a724bea777a2f50d89b0b6b24"
35
+
36
+ [[tool.edge-maxxing.models]]
37
+ repository = "city96/t5-v1_1-xxl-encoder-bf16"
38
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
39
+
40
+
41
+ [project.scripts]
42
+ 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,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #5
2
+ import gc
3
+ import os
4
+ from typing import TypeAlias
5
+
6
+ import torch
7
+ from PIL.Image import Image
8
+ from diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, AutoencoderTiny
9
+ from huggingface_hub.constants import HF_HUB_CACHE
10
+ from pipelines.models import TextToImageRequest
11
+ from torch import Generator
12
+ from transformers import T5EncoderModel, CLIPTextModel
13
+
14
+ Pipeline: TypeAlias = FluxPipeline
15
+
16
+ CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
17
+ REVISION = "741f7c3ce8b383c54771c7003378a50191e9efe9"
18
+
19
+
20
+ def load_pipeline() -> Pipeline:
21
+ text_encoder = CLIPTextModel.from_pretrained(CHECKPOINT, revision=REVISION, subfolder="text_encoder", local_files_only=True, torch_dtype=torch.bfloat16,)
22
+
23
+ path2 = os.path.join(HF_HUB_CACHE, "models--city96--t5-v1_1-xxl-encoder-bf16/snapshots/1b9c856aadb864af93c1dcdc226c2774fa67bc86")
24
+
25
+ text_encoder_2 = T5EncoderModel.from_pretrained(path2, torch_dtype=torch.bfloat16,)
26
+
27
+ pathV = os.path.join(HF_HUB_CACHE, "models--madebyollin--taef1/snapshots/5463ee684fd9131a724bea777a2f50d89b0b6b24")
28
+
29
+ vae = AutoencoderTiny.from_pretrained(pathV, torch_dtype=torch.bfloat16,)
30
+
31
+ pathT = os.path.join(HF_HUB_CACHE, "models--RobertML--FLUX.1-schnell-int8wo/snapshots/307e0777d92df966a3c0f99f31a6ee8957a9857a")
32
+
33
+ transformer = FluxTransformer2DModel.from_pretrained(pathT, torch_dtype=torch.bfloat16, use_safetensors=False,)
34
+
35
+ pipeline = FluxPipeline.from_pretrained(CHECKPOINT, revision=REVISION, local_files_only=True, text_encoder=text_encoder, text_encoder_2=text_encoder_2, transformer=transformer, vae=vae, torch_dtype=torch.bfloat16,).to("cuda")
36
+
37
+ pipeline("")
38
+
39
+ return pipeline
40
+
41
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
42
+ gc.collect()
43
+ torch.cuda.empty_cache()
44
+ torch.cuda.reset_peak_memory_stats()
45
+
46
+ generator = Generator(pipeline.device).manual_seed(request.seed)
47
+
48
+ return pipeline(
49
+ request.prompt,
50
+ generator=generator,
51
+ guidance_scale=7.0,
52
+ num_inference_steps=4,
53
+ max_sequence_length=256,
54
+ height=request.height,
55
+ width=request.width,
56
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff