smash3211 commited on
Commit
e8bf693
·
verified ·
1 Parent(s): 31fefae

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +33 -0
  2. src/main.py +50 -0
  3. src/pipeline.py +101 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "torchao==0.6.1",
19
+ "hf_transfer==0.1.8",
20
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
21
+ ]
22
+
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "smash3211/Flux.1.schnell"
26
+ revision = "26534bc47459428a6763951757fd63892119ee08"
27
+
28
+ [[tool.edge-maxxing.models]]
29
+ repository = "smash3211/tae1-update"
30
+ revision = "4aa8fbe28d8631db070810bc2b9ff9f9320effda"
31
+
32
+ [project.scripts]
33
+ 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,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL.Image import Image
2
+ from diffusers import (
3
+ FluxPipeline,
4
+ FluxTransformer2DModel,
5
+ AutoencoderKL,
6
+ AutoencoderTiny,
7
+ )
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
+ import gc
14
+ import os
15
+ from typing import TypeAlias
16
+ import torch
17
+
18
+ Pipeline = FluxPipeline
19
+ torch.backends.cudnn.benchmark = True
20
+ torch.backends.cudnn.benchmark = True
21
+ torch._inductor.config.conv_1x1_as_mm = True
22
+ torch._inductor.config.coordinate_descent_tuning = True
23
+ torch._inductor.config.epilogue_fusion = False
24
+ torch._inductor.config.coordinate_descent_check_all_directions = True
25
+ torch._dynamo.config.suppress_errors = True
26
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
27
+
28
+ repo = "smash3211/Flux.1.schnell"
29
+ revision = "26534bc47459428a6763951757fd63892119ee08"
30
+
31
+ vae_repo = "smash3211/tae1-update"
32
+ vae_revision = "4aa8fbe28d8631db070810bc2b9ff9f9320effda"
33
+
34
+
35
+ def load_pipeline() -> Pipeline:
36
+ path = os.path.join(
37
+ HF_HUB_CACHE,
38
+ f"models--{repo.split('/')[0]}--{repo.split('/')[1]}/snapshots/{revision}/transformer",
39
+ )
40
+ transformer = FluxTransformer2DModel.from_pretrained(
41
+ path, use_safetensors=False, local_files_only=True, torch_dtype=torch.bfloat16
42
+ )
43
+ vae = AutoencoderTiny.from_pretrained(
44
+ vae_repo,
45
+ revision=vae_revision,
46
+ local_files_only=True,
47
+ torch_dtype=torch.bfloat16,
48
+ )
49
+ vae_path = os.path.join(
50
+ HF_HUB_CACHE,
51
+ f"models--{vae_repo.split('/')[0]}--{vae_repo.split('/')[1]}/snapshots/{vae_revision}",
52
+ )
53
+ vae.encoder.load_state_dict(torch.load(f"{vae_path}/encoder.pth"), strict=False)
54
+ vae.decoder.load_state_dict(torch.load(f"{vae_path}/decoder.pth"), strict=False)
55
+ pipeline = FluxPipeline.from_pretrained(
56
+ repo,
57
+ revision=revision,
58
+ transformer=transformer,
59
+ vae=vae,
60
+ local_files_only=True,
61
+ torch_dtype=torch.bfloat16,
62
+ )
63
+ pipeline.to('cuda')
64
+ pipeline.to(memory_format=torch.channels_last)
65
+ quantize_(pipeline.vae, int8_weight_only())
66
+ pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune", fullgraph=True)
67
+ for _ in range(4):
68
+ pipeline(prompt="satiety, unwitherable, Pygmy, ramlike, Curtis, fingerstone, rewhisper", num_inference_steps=4)
69
+ return pipeline
70
+
71
+
72
+ @torch.inference_mode()
73
+ def infer(
74
+ request: TextToImageRequest, pipeline: Pipeline, generator: torch.Generator
75
+ ) -> Image:
76
+ return pipeline(
77
+ prompt = request.prompt,
78
+ generator=generator,
79
+ guidance_scale=0.0,
80
+ num_inference_steps=4,
81
+ max_sequence_length=256,
82
+ height=request.height,
83
+ width=request.width,
84
+ ).images[0]
85
+
86
+ # Example Usage
87
+ if __name__ == "__main__":
88
+ print("load pipeline...")
89
+ diffusion_pipeline = load_pipeline()
90
+
91
+ sample_request = TextToImageRequest(
92
+ prompt="A futuristic cityscape with neon lights",
93
+ height=1024,
94
+ width=1024,
95
+ )
96
+
97
+ generator = torch.Generator(device="cuda").manual_seed(42)
98
+
99
+ print("Generating image...")
100
+ generated_img = infer(sample_request, diffusion_pipeline, generator)
101
+ generated_img.show()
uv.lock ADDED
The diff for this file is too large to render. See raw diff