smartguy0505 commited on
Commit
e56dc9b
·
verified ·
1 Parent(s): 38bcc04

Initial commit

Browse files
Files changed (5) hide show
  1. README.md +19 -0
  2. pyproject.toml +36 -0
  3. src/main.py +42 -0
  4. src/pipeline.py +70 -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,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.11,<3.13"
9
+ version = "8"
10
+ dependencies = [
11
+ "setuptools==75",
12
+ "para-attn==0.3.15",
13
+ "diffusers==0.31.0",
14
+ "transformers==4.46.2",
15
+ "accelerate==1.1.0",
16
+ "omegaconf==2.3.0",
17
+ "torch==2.5.1",
18
+ "protobuf==5.28.3",
19
+ "sentencepiece==0.2.0",
20
+ "torchao==0.6.1",
21
+ "hf_transfer==0.1.8",
22
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
23
+ ]
24
+
25
+ [[tool.edge-maxxing.models]]
26
+ repository = "smartguy0505/flux.1-schnell"
27
+ revision = "f0fd35d9eef4536098c7e5e7c2fa60fe7c9fd1e2"
28
+
29
+
30
+ [[tool.edge-maxxing.models]]
31
+ repository = "smartguy0505/tae"
32
+ revision = "2ec20af4a1fc7b66b9e4823b8eaef9518687bb72"
33
+
34
+
35
+ [project.scripts]
36
+ start_inference = "main:main"
src/main.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def main():
15
+ print(f"Loading pipeline")
16
+ pipeline = load_pipeline()
17
+ generator = torch.Generator(pipeline.device)
18
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
19
+
20
+ if exists(SOCKET):
21
+ remove(SOCKET)
22
+
23
+ with Listener(SOCKET) as listener:
24
+ chmod(SOCKET, 0o777)
25
+ print(f"Awaiting connections")
26
+ with listener.accept() as connection:
27
+ print(f"Connected")
28
+
29
+ while True:
30
+ try:
31
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
32
+ except EOFError:
33
+ print(f"Inference socket exiting")
34
+ return
35
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
36
+ data = BytesIO()
37
+ image.save(data, format=JpegImageFile.format)
38
+ packet = data.getvalue()
39
+ connection.send_bytes(packet)
40
+
41
+ if __name__ == '__main__':
42
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import TypeAlias
3
+
4
+ import torch
5
+ from PIL.Image import Image
6
+ from diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderKL, AutoencoderTiny
7
+ from huggingface_hub.constants import HF_HUB_CACHE
8
+ from pipelines.models import TextToImageRequest
9
+ from torch import Generator
10
+ from torchao.quantization import quantize_, int8_weight_only
11
+ from transformers import T5EncoderModel, CLIPTextModel, logging
12
+ from functools import partial
13
+ from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
14
+
15
+ my_partial_compile = partial(torch.compile, mode="max-autotune")
16
+ Pipeline: TypeAlias = FluxPipeline
17
+ torch.backends.cudnn.benchmark = True
18
+ torch.backends.cudnn.benchmark = True
19
+ torch._inductor.config.conv_1x1_as_mm = True
20
+ torch._inductor.config.coordinate_descent_tuning = True
21
+ torch._inductor.config.epilogue_fusion = False
22
+ torch._inductor.config.coordinate_descent_check_all_directions = True
23
+
24
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
25
+
26
+ CHECKPOINT = "smartguy0505/flux.1-schnell"
27
+ REVISION = "f0fd35d9eef4536098c7e5e7c2fa60fe7c9fd1e2"
28
+
29
+ TinyVAE = "smartguy0505/tae"
30
+ TinyVAE_REV = "2ec20af4a1fc7b66b9e4823b8eaef9518687bb72"
31
+
32
+ def load_pipeline() -> Pipeline:
33
+ path = os.path.join(HF_HUB_CACHE, "models--smartguy0505--flux.1-schnell/snapshots/f0fd35d9eef4536098c7e5e7c2fa60fe7c9fd1e2/transformer")
34
+ transformer = FluxTransformer2DModel.from_pretrained(
35
+ path,
36
+ use_safetensors=False,
37
+ local_files_only=True,
38
+ torch_dtype=torch.bfloat16)
39
+ vae = AutoencoderTiny.from_pretrained(
40
+ TinyVAE,
41
+ revision=TinyVAE_REV,
42
+ local_files_only=True,
43
+ torch_dtype=torch.bfloat16)
44
+ pipeline = FluxPipeline.from_pretrained(
45
+ CHECKPOINT,
46
+ revision=REVISION,
47
+ transformer=transformer,
48
+ vae=vae,
49
+ local_files_only=True,
50
+ torch_dtype=torch.bfloat16,
51
+ ).to("cuda")
52
+ pipeline.to(memory_format=torch.channels_last)
53
+ pipeline.vae = my_partial_compile(pipeline.vae)
54
+ apply_cache_on_pipe(pipeline, residual_diff_threshold=0.64)
55
+ with torch.inference_mode():
56
+ for _ in range(2):
57
+ pipeline("cats running on a road with a dog chasing", num_inference_steps=4)
58
+ return pipeline
59
+
60
+ @torch.inference_mode()
61
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: torch.Generator) -> Image:
62
+ return pipeline(
63
+ request.prompt,
64
+ generator=generator,
65
+ guidance_scale=0.0,
66
+ num_inference_steps=4,
67
+ max_sequence_length=256,
68
+ height=request.height,
69
+ width=request.width,
70
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff