farapart commited on
Commit
1cd895b
·
verified ·
1 Parent(s): b7a3461

Initial commit with folder contents

Browse files
Files changed (4) hide show
  1. pyproject.toml +29 -0
  2. src/main.py +59 -0
  3. src/pipeline.py +64 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "setuptools >= 75.0",
15
+ "omegaconf==2.3.0",
16
+ "torch==2.5.1",
17
+ "protobuf==5.28.3",
18
+ "sentencepiece==0.2.0",
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
+ "gitpython>=3.1.43",
23
+ ]
24
+
25
+ [tool.edge-maxxing]
26
+ models = ["farapart/NEW_FLUX"]
27
+
28
+ [project.scripts]
29
+ 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,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from diffusers import FluxPipeline
4
+ from pipelines.models import TextToImageRequest
5
+ from torch import Generator
6
+ import os
7
+ from diffusers import FluxPipeline, AutoencoderKL
8
+ from diffusers.image_processor import VaeImageProcessor
9
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
10
+ import diffusers
11
+ import gc
12
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
13
+
14
+ from torchao.quantization import quantize_, int8_weight_only
15
+
16
+ torch.set_float32_matmul_precision("high")
17
+
18
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
19
+
20
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
21
+
22
+ torch.backends.cudnn.benchmark = True
23
+ torch.backends.cuda.matmul.allow_tf32 = True
24
+ torch.cuda.set_per_process_memory_fraction(0.99)
25
+
26
+
27
+ Pipeline = None
28
+ ckpt_id = "farapart/NEW_FLUX"
29
+
30
+
31
+ def fart():
32
+ gc.collect()
33
+ torch.cuda.empty_cache()
34
+ torch.cuda.reset_max_memory_allocated()
35
+ torch.cuda.reset_peak_memory_stats()
36
+
37
+
38
+ def load_pipeline() -> Pipeline:
39
+
40
+ fart()
41
+
42
+ vae = AutoencoderKL.from_pretrained(ckpt_id, subfolder="vae", torch_dtype=torch.bfloat16)
43
+ quantize_(vae, int8_weight_only())
44
+
45
+ pipeline = DiffusionPipeline.from_pretrained(ckpt_id, vae=vae, torch_dtype=torch.bfloat16)
46
+ pipeline.transformer.to(memory_format=torch.channels_last)
47
+ pipeline.text_encoder.to(memory_format=torch.channels_last)
48
+ pipeline.text_encoder_2.to(memory_format=torch.channels_last)
49
+ pipeline.vae.to(memory_format=torch.channels_last)
50
+ pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune")
51
+ pipeline.vae.enable_tiling()
52
+ pipeline._exclude_from_cpu_offload = ["vae"]
53
+ pipeline.enable_sequential_cpu_offload()
54
+
55
+ for _ in range(2):
56
+ pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
57
+ fart()
58
+ return pipeline
59
+ fart()
60
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
61
+ fart()
62
+ generator = Generator("cuda").manual_seed(request.seed)
63
+ 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]
64
+ return(image)
uv.lock ADDED
The diff for this file is too large to render. See raw diff