manbeast3b commited on
Commit
bd0bb1c
·
0 Parent(s):

Initial commit

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. README.md +19 -0
  3. base-flux-simpleflux9 +1 -0
  4. pyproject.toml +27 -0
  5. src/main.py +59 -0
  6. src/pipeline.py +70 -0
  7. uv.lock +0 -0
.gitattributes ADDED
@@ -0,0 +1 @@
 
 
1
+ backup.png filter=lfs diff=lfs merge=lfs -text
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`
base-flux-simpleflux9 ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit fa96c90df9ee60abf4d096a5e5659c8d15ee49dd
pyproject.toml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "7"
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
+ "torchao>=0.6.1"
21
+ ]
22
+
23
+ [tool.edge-maxxing]
24
+ models = ["black-forest-labs/FLUX.1-schnell", "madebyollin/taef1"]
25
+
26
+ [project.scripts]
27
+ start_inference = "main:main"
src/main.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ import torch
9
+
10
+ from PIL.JpegImagePlugin import JpegImageFile
11
+ from pipelines.models import TextToImageRequest
12
+
13
+ from pipeline import load_pipeline, infer
14
+
15
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
16
+
17
+
18
+ def at_exit():
19
+ torch.cuda.empty_cache()
20
+
21
+
22
+ def main():
23
+ atexit.register(at_exit)
24
+
25
+ print(f"Loading pipeline")
26
+ pipeline = load_pipeline()
27
+
28
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
29
+
30
+ if exists(SOCKET):
31
+ remove(SOCKET)
32
+
33
+ with Listener(SOCKET) as listener:
34
+ chmod(SOCKET, 0o777)
35
+
36
+ print(f"Awaiting connections")
37
+ with listener.accept() as connection:
38
+ print(f"Connected")
39
+
40
+ while True:
41
+ try:
42
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
43
+ except EOFError:
44
+ print(f"Inference socket exiting")
45
+
46
+ return
47
+
48
+ image = infer(request, pipeline)
49
+
50
+ data = BytesIO()
51
+ image.save(data, format=JpegImageFile.format)
52
+
53
+ packet = data.getvalue()
54
+
55
+ connection.send_bytes(packet)
56
+
57
+
58
+ if __name__ == '__main__':
59
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import AutoencoderKL, AutoencoderTiny
2
+ from diffusers.image_processor import VaeImageProcessor
3
+ import torch
4
+ import torch._dynamo
5
+ import gc
6
+ from PIL.Image import Image
7
+ from pipelines.models import TextToImageRequest
8
+ from torch import Generator
9
+ from diffusers import FluxPipeline
10
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
11
+
12
+ Pipeline = None
13
+ MODEL_ID = "black-forest-labs/FLUX.1-schnell"
14
+ DTYPE = torch.bfloat16
15
+ def clear():
16
+ gc.collect()
17
+ torch.cuda.empty_cache()
18
+ torch.cuda.reset_max_memory_allocated()
19
+ torch.cuda.reset_peak_memory_stats()
20
+
21
+ def load_pipeline() -> Pipeline:
22
+ clear()
23
+ # vae = AutoencoderKL.from_pretrained(
24
+ # MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
25
+ # )
26
+ vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=DTYPE)
27
+ # quantize_(vae, fpx_weight_only(3, 2))
28
+ quantize_(vae, int8_weight_only())
29
+ pipeline = FluxPipeline.from_pretrained(MODEL_ID,vae=vae,
30
+ torch_dtype=DTYPE)
31
+ torch.backends.cudnn.benchmark = True
32
+ torch.backends.cuda.matmul.allow_tf32 = True
33
+ torch.cuda.set_per_process_memory_fraction(0.99)
34
+ pipeline.text_encoder.to(memory_format=torch.channels_last)
35
+ pipeline.text_encoder_2.to(memory_format=torch.channels_last)
36
+ pipeline.transformer.to(memory_format=torch.channels_last)
37
+ pipeline.vae.to(memory_format=torch.channels_last)
38
+ pipeline.vae = torch.compile(pipeline.vae)
39
+ pipeline._exclude_from_cpu_offload = ["vae"]
40
+ pipeline.enable_sequential_cpu_offload()
41
+ clear()
42
+ for _ in range(1):
43
+ pipeline(prompt="unpervaded, unencumber, froggish, groundneedle, transnatural, fatherhood, outjump, cinerator", width=1024, height=1024, guidance_scale=0.1, num_inference_steps=4, max_sequence_length=256)
44
+ return pipeline
45
+
46
+ # sample = True
47
+ # @torch.inference_mode()
48
+ # def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
49
+ # global sample
50
+ # if sample:
51
+ # clear()
52
+ # sample = None
53
+ # # torch.cuda.reset_peak_memory_stats()
54
+ # generator = Generator("cuda").manual_seed(request.seed)
55
+ # image=pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pil").images[0]
56
+ # return(image)
57
+
58
+ sample = True
59
+ @torch.inference_mode()
60
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
61
+ global sample
62
+ if sample:
63
+ clear()
64
+ sample = None
65
+ # torch.cuda.reset_peak_memory_stats()
66
+ generator = Generator("cuda").manual_seed(request.seed)
67
+ image = None
68
+ with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False):
69
+ image=pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pil").images[0]
70
+ return(image)
uv.lock ADDED
The diff for this file is too large to render. See raw diff