golaststep commited on
Commit
7593332
·
verified ·
1 Parent(s): 3abc3f1

Initial commit with folder contents

Browse files
Files changed (4) hide show
  1. pyproject.toml +35 -0
  2. src/main.py +59 -0
  3. src/pipeline.py +70 -0
  4. uv.lock +0 -0
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 = "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
+ "torchao",
20
+ "huggingface-hub==0.25.2",
21
+ "oneflow",
22
+ "setuptools>=75.2.0",
23
+ "onediff",
24
+ "onediffx"
25
+ ]
26
+
27
+ [tool.edge-maxxing]
28
+ models = ["black-forest-labs/FLUX.1-schnell", "madebyollin/taef1"]
29
+
30
+ [tool.uv.sources]
31
+ oneflow = { url = "https://github.com/siliconflow/oneflow_releases/releases/download/community_cu118/oneflow-0.9.1.dev20240802%2Bcu118-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" }
32
+
33
+
34
+ [project.scripts]
35
+ 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
+ import os
2
+ from diffusers import FluxPipeline, AutoencoderKL, FluxTransformer2DModel
3
+ from diffusers import AutoencoderTiny
4
+ from diffusers.image_processor import VaeImageProcessor
5
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel, CLIPTextConfig, T5Config
6
+ import torch
7
+ import gc
8
+ from PIL import Image
9
+ from pipelines.models import TextToImageRequest
10
+ from torch import Generator
11
+ from torchao.quantization import quantize_, int8_weight_only, int8_dynamic_activation_int8_weight
12
+ from time import perf_counter
13
+ # from onediffx import compile_pipe, load_pipe
14
+
15
+ HOME = os.environ["HOME"]
16
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
17
+ FLUX_CHECKPOINT = "black-forest-labs/FLUX.1-schnell"
18
+ # QUANTIZED_MODEL = []
19
+ QUANTIZED_MODEL = ["transformer", "text_encoder_2", "text_encoder", "vae"]
20
+
21
+
22
+ QUANT_CONFIG = int8_weight_only()
23
+ DTYPE = torch.bfloat16
24
+ NUM_STEPS = 4
25
+
26
+ def empty_cache():
27
+ gc.collect()
28
+ torch.cuda.empty_cache()
29
+ torch.cuda.reset_max_memory_allocated()
30
+ torch.cuda.reset_peak_memory_stats()
31
+
32
+
33
+ def load_pipeline() -> FluxPipeline:
34
+ empty_cache()
35
+
36
+ pipe = FluxPipeline.from_pretrained(FLUX_CHECKPOINT,
37
+ torch_dtype=DTYPE)
38
+ pipe.enable_attention_slicing()
39
+ pipe.text_encoder.to(memory_format=torch.channels_last)
40
+ pipe.text_encoder_2.to(memory_format=torch.channels_last)
41
+ pipe.transformer.to(memory_format=torch.channels_last)
42
+ pipe.vae.to(memory_format=torch.channels_last)
43
+ # pipe.vae.enable_tiling()
44
+ pipe.vae = torch.compile(pipe.vae)
45
+
46
+ pipe._exclude_from_cpu_offload = ["vae"]
47
+
48
+ pipe.enable_sequential_cpu_offload()
49
+
50
+ empty_cache()
51
+ pipe("cat", guidance_scale=0., max_sequence_length=256, num_inference_steps=4)
52
+ return pipe
53
+
54
+ @torch.inference_mode()
55
+ def infer(request: TextToImageRequest, _pipeline: FluxPipeline) -> Image:
56
+ if request.seed is None:
57
+ generator = None
58
+ else:
59
+ generator = Generator(device="cuda").manual_seed(request.seed)
60
+
61
+ empty_cache()
62
+ image = _pipeline(prompt=request.prompt,
63
+ width=request.width,
64
+ height=request.height,
65
+ guidance_scale=0.0,
66
+ generator=generator,
67
+ output_type="pil",
68
+ max_sequence_length=256,
69
+ num_inference_steps=NUM_STEPS).images[0]
70
+ return image
uv.lock ADDED
The diff for this file is too large to render. See raw diff