tensor-tech commited on
Commit
c9c0047
·
verified ·
1 Parent(s): a449bb9

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +36 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +93 -0
  4. uv.lock +0 -0
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 = "jokerbit/flux.1-schnell-Robert-int8wo"
27
+ revision = "5ef0012f11a863e5111ec56540302a023bc8587b"
28
+
29
+
30
+ [[tool.edge-maxxing.models]]
31
+ repository = "madebyollin/taef1"
32
+ revision = "2d552378e58c9c94201075708d7de4e1163b2689"
33
+
34
+
35
+ [project.scripts]
36
+ start_inference = "main:main"
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
+ import torch
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
+ generator = torch.Generator(pipeline.device)
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, generator.manual_seed(request.seed))
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,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # partial
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, logging
13
+ from functools import partial
14
+ from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
15
+ my_partial_compile = partial(torch.compile, mode="max-autotune")
16
+
17
+ Pipeline: TypeAlias = FluxPipeline
18
+ torch.backends.cudnn.benchmark = True
19
+ torch.backends.cudnn.benchmark = True
20
+ torch._inductor.config.conv_1x1_as_mm = True
21
+ torch._inductor.config.coordinate_descent_tuning = True
22
+ torch._inductor.config.epilogue_fusion = False
23
+ torch._inductor.config.coordinate_descent_check_all_directions = True
24
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
25
+
26
+ CHECKPOINT = "jokerbit/flux.1-schnell-Robert-int8wo"
27
+ REVISION = "5ef0012f11a863e5111ec56540302a023bc8587b"
28
+
29
+ TinyVAE = "madebyollin/taef1"
30
+ TinyVAE_REV = "2d552378e58c9c94201075708d7de4e1163b2689"
31
+
32
+
33
+ def load_pipeline() -> Pipeline:
34
+ path = os.path.join(HF_HUB_CACHE, "models--jokerbit--flux.1-schnell-Robert-int8wo/snapshots/5ef0012f11a863e5111ec56540302a023bc8587b/transformer")
35
+ transformer = FluxTransformer2DModel.from_pretrained(
36
+ path,
37
+ use_safetensors=False,
38
+ local_files_only=True,
39
+ torch_dtype=torch.bfloat16)
40
+ vae = AutoencoderTiny.from_pretrained(
41
+ TinyVAE,
42
+ revision=TinyVAE_REV,
43
+ local_files_only=True,
44
+ torch_dtype=torch.bfloat16)
45
+ pipeline = FluxPipeline.from_pretrained(
46
+ CHECKPOINT,
47
+ revision=REVISION,
48
+ transformer=transformer,
49
+ vae=vae,
50
+ local_files_only=True,
51
+ torch_dtype=torch.bfloat16,
52
+ ).to("cuda")
53
+
54
+ pipeline.to(memory_format=torch.channels_last)
55
+ # quantize_(pipeline.vae, int8_weight_only())
56
+ pipeline.vae = my_partial_compile(pipeline.vae)
57
+ apply_cache_on_pipe(pipeline, residual_diff_threshold=0.65)
58
+ with torch.inference_mode():
59
+ for _ in range(2):
60
+ pipeline("cats running on a road with a dog chasing", num_inference_steps=4)
61
+
62
+ return pipeline
63
+
64
+ @torch.inference_mode()
65
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: torch.Generator) -> Image:
66
+
67
+ return pipeline(
68
+ request.prompt,
69
+ generator=generator,
70
+ guidance_scale=0.0,
71
+ num_inference_steps=4,
72
+ max_sequence_length=256,
73
+ height=request.height,
74
+ width=request.width,
75
+ ).images[0]
76
+
77
+
78
+ if __name__ == "__main__":
79
+ from time import perf_counter
80
+ PROMPT = 'martyr, semiconformity, peregrination, quip, twineless, emotionless, tawa, depickle'
81
+ request = TextToImageRequest(prompt=PROMPT,
82
+ height=None,
83
+ width=None,
84
+ seed=666)
85
+ start_time = perf_counter()
86
+ pipe_ = load_pipeline()
87
+ stop_time = perf_counter()
88
+ print(f"Pipeline is loaded in {stop_time - start_time}s")
89
+ for _ in range(4):
90
+ start_time = perf_counter()
91
+ infer(request, pipe_)
92
+ stop_time = perf_counter()
93
+ 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