jokerbit commited on
Commit
246e5d7
·
verified ·
1 Parent(s): 5415191

Upload folder using huggingface_hub

Browse files
Files changed (8) hide show
  1. .gitattributes +1 -0
  2. .gitignore +9 -0
  3. README.md +19 -0
  4. pyproject.toml +35 -0
  5. src/.pipeline.py.swp +0 -0
  6. src/main.py +50 -0
  7. src/pipeline.py +107 -0
  8. uv.lock +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ sample.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ **/.cache
2
+ **/__pycache__
3
+ **/*.egg-info
4
+ *.safetensors
5
+ **/.venv
6
+ .venv
7
+ .git
8
+ *.png
9
+ *.jpeg
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,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
+ "hf_transfer==0.1.8",
19
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
20
+ "torchao>=0.6.1",
21
+ "ipython>=8.29.0",
22
+ "setuptools >= 75.0",
23
+ "optimum-quanto>=0.2.6",
24
+ ]
25
+
26
+ [[tool.edge-maxxing.models]]
27
+ repository = "jokerbit/flux.1-schnell-Robert-int8wo"
28
+ revision = "5ef0012f11a863e5111ec56540302a023bc8587b"
29
+
30
+ [[tool.edge-maxxing.models]]
31
+ repository = "madebyollin/taef1"
32
+ revision = "2d552378e58c9c94201075708d7de4e1163b2689"
33
+
34
+ [project.scripts]
35
+ start_inference = "main:main"
src/.pipeline.py.swp ADDED
Binary file (12.3 kB). View file
 
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,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
13
+
14
+
15
+ Pipeline: TypeAlias = FluxPipeline
16
+ torch.backends.cudnn.benchmark = True
17
+
18
+ CHECKPOINT = "jokerbit/flux.1-schnell-Robert-int8wo"
19
+ REVISION = "5ef0012f11a863e5111ec56540302a023bc8587b"
20
+
21
+ TinyVAE = "madebyollin/taef1"
22
+ TinyVAE_REV = "2d552378e58c9c94201075708d7de4e1163b2689"
23
+
24
+
25
+ def load_pipeline() -> Pipeline:
26
+ text_encoder = CLIPTextModel.from_pretrained(
27
+ CHECKPOINT,
28
+ revision=REVISION,
29
+ subfolder="text_encoder",
30
+ local_files_only=True,
31
+ torch_dtype=torch.bfloat16,
32
+ )
33
+
34
+ text_encoder_2 = T5EncoderModel.from_pretrained(
35
+ CHECKPOINT,
36
+ revision=REVISION,
37
+ subfolder="text_encoder_2",
38
+ local_files_only=True,
39
+ torch_dtype=torch.bfloat16,
40
+ )
41
+
42
+ vae = AutoencoderTiny.from_pretrained(
43
+ TinyVAE,
44
+ revision=TinyVAE_REV,
45
+ local_files_only=True,
46
+ torch_dtype=torch.bfloat16,
47
+ )
48
+
49
+ path = os.path.join(HF_HUB_CACHE, "models--jokerbit--flux.1-schnell-Robert-int8wo/snapshots/5ef0012f11a863e5111ec56540302a023bc8587b/transformer")
50
+
51
+ transformer = FluxTransformer2DModel.from_pretrained(
52
+ path,
53
+ torch_dtype=torch.bfloat16,
54
+ use_safetensors=False,
55
+ )
56
+ pipeline = FluxPipeline.from_pretrained(
57
+ CHECKPOINT,
58
+ revision=REVISION,
59
+ local_files_only=True,
60
+ text_encoder=text_encoder,
61
+ text_encoder_2=text_encoder_2,
62
+ transformer=transformer,
63
+ vae=vae,
64
+ torch_dtype=torch.bfloat16,
65
+ ).to("cuda")
66
+ # pipeline.text_encoder_2.to(memory_format=torch.channels_last)
67
+ # pipeline.transformer.to(memory_format=torch.channels_last)
68
+ # pipeline.vae.to(memory_format=torch.channels_last)
69
+ for _ in range(2):
70
+ pipeline("cat", num_inference_steps=4)
71
+
72
+ return pipeline
73
+
74
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
75
+ gc.collect()
76
+ torch.cuda.empty_cache()
77
+ torch.cuda.reset_peak_memory_stats()
78
+
79
+ generator = Generator(pipeline.device).manual_seed(request.seed)
80
+
81
+ return pipeline(
82
+ request.prompt,
83
+ generator=generator,
84
+ guidance_scale=0.0,
85
+ num_inference_steps=4,
86
+ max_sequence_length=256,
87
+ height=request.height,
88
+ width=request.width,
89
+ ).images[0]
90
+
91
+
92
+ if __name__ == "__main__":
93
+ from time import perf_counter
94
+ PROMPT = 'martyr, semiconformity, peregrination, quip, twineless, emotionless, tawa, depickle'
95
+ request = TextToImageRequest(prompt=PROMPT,
96
+ height=None,
97
+ width=None,
98
+ seed=666)
99
+ start_time = perf_counter()
100
+ pipe_ = load_pipeline()
101
+ stop_time = perf_counter()
102
+ print(f"Pipeline is loaded in {stop_time - start_time}s")
103
+ for _ in range(4):
104
+ start_time = perf_counter()
105
+ infer(request, pipe_)
106
+ stop_time = perf_counter()
107
+ 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