soft987 commited on
Commit
a36fb70
·
verified ·
1 Parent(s): 8973fa5

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +34 -0
  2. src/main.py +55 -0
  3. src/pipeline.py +111 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 by RobertML 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
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
19
+ "gitpython>=3.1.43",
20
+ "hf_transfer==0.1.8",
21
+ "torchao==0.6.1",
22
+ "setuptools>=75.3.0",
23
+ ]
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "soft987/FLUX.1.schnell-quant2"
26
+ revision = "6d93094cc0c92f72236c6de41bddf789b8b0b38e"
27
+
28
+ [[tool.edge-maxxing.models]]
29
+ repository = "soft987/FLUX1.schnell-full"
30
+ revision = "a05d320df4f5795fb4eff2f85ec117e870c078cb"
31
+
32
+ [project.scripts]
33
+ start_inference = "main:main"
34
+
src/main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from git import Repo
8
+ import torch
9
+
10
+ from PIL.JpegImagePlugin import JpegImageFile
11
+ from pipelines.models import TextToImageRequest
12
+ from pipeline import load_pipeline, infer
13
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
14
+
15
+
16
+ def at_exit():
17
+ torch.cuda.empty_cache()
18
+
19
+
20
+ def main():
21
+ atexit.register(at_exit)
22
+
23
+ print(f"Loading pipeline")
24
+ pipeline = load_pipeline()
25
+
26
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
27
+
28
+ if exists(SOCKET):
29
+ remove(SOCKET)
30
+
31
+ with Listener(SOCKET) as listener:
32
+ chmod(SOCKET, 0o777)
33
+
34
+ print(f"Awaiting connections")
35
+ with listener.accept() as connection:
36
+ print(f"Connected")
37
+ generator = torch.Generator("cuda")
38
+ while True:
39
+ try:
40
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
41
+ except EOFError:
42
+ print(f"Inference socket exiting")
43
+
44
+ return
45
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
46
+ data = BytesIO()
47
+ image.save(data, format=JpegImageFile.format)
48
+
49
+ packet = data.getvalue()
50
+
51
+ connection.send_bytes(packet )
52
+
53
+
54
+ if __name__ == '__main__':
55
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import (
2
+ DiffusionPipeline,
3
+ AutoencoderKL,
4
+ FluxPipeline,
5
+ FluxTransformer2DModel,
6
+ )
7
+ from diffusers.image_processor import VaeImageProcessor
8
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
9
+ from huggingface_hub.constants import HF_HUB_CACHE
10
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
11
+ import torch
12
+ import torch._dynamo
13
+ import gc
14
+ from PIL import Image
15
+ from pipelines.models import TextToImageRequest
16
+ from torch import Generator
17
+ import time
18
+ import math
19
+ import torch.nn.functional as F
20
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
21
+
22
+ # preconfigs
23
+ import os
24
+
25
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
26
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
27
+ torch._dynamo.config.suppress_errors = True
28
+ torch.backends.cuda.matmul.allow_tf32 = True
29
+ torch.backends.cudnn.enabled = True
30
+
31
+ # globals
32
+ Pipeline = None
33
+ ckpt_id = "soft987/FLUX.1.schnell-quant2"
34
+ ckpt_revision = "6d93094cc0c92f72236c6de41bddf789b8b0b38e"
35
+
36
+
37
+ def empty_cache():
38
+ gc.collect()
39
+ torch.cuda.empty_cache()
40
+ torch.cuda.reset_max_memory_allocated()
41
+ torch.cuda.reset_peak_memory_stats()
42
+
43
+
44
+ def load_pipeline() -> Pipeline:
45
+ vae = AutoencoderKL.from_pretrained(
46
+ ckpt_id,
47
+ revision=ckpt_revision,
48
+ subfolder="vae",
49
+ local_files_only=True,
50
+ torch_dtype=torch.bfloat16,
51
+ )
52
+ quantize_(vae, int8_weight_only())
53
+ text_encoder_2 = T5EncoderModel.from_pretrained(
54
+ "soft987/FLUX1.schnell-full",
55
+ revision="a05d320df4f5795fb4eff2f85ec117e870c078cb",
56
+ subfolder="text_encoder_2",
57
+ torch_dtype=torch.bfloat16,
58
+ )
59
+ path = os.path.join(
60
+ HF_HUB_CACHE,
61
+ "models--soft987--FLUX1.schnell-full/snapshots/a05d320df4f5795fb4eff2f85ec117e870c078cb/transformer",
62
+ )
63
+ transformer = FluxTransformer2DModel.from_pretrained(
64
+ path, torch_dtype=torch.bfloat16, use_safetensors=False
65
+ )
66
+ pipeline = FluxPipeline.from_pretrained(
67
+ ckpt_id,
68
+ revision=ckpt_revision,
69
+ transformer=transformer,
70
+ text_encoder_2=text_encoder_2,
71
+ torch_dtype=torch.bfloat16,
72
+ )
73
+ pipeline.to("cuda")
74
+ pipeline.to(memory_format=torch.channels_last)
75
+ for _ in range(1):
76
+ pipeline(
77
+ prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus",
78
+ width=1024,
79
+ height=1024,
80
+ guidance_scale=0.0,
81
+ num_inference_steps=4,
82
+ max_sequence_length=256,
83
+ )
84
+ return pipeline
85
+
86
+
87
+ sample = 1
88
+
89
+
90
+ @torch.no_grad()
91
+ def infer(
92
+ request: TextToImageRequest, pipeline: Pipeline, generator: Generator
93
+ ) -> Image:
94
+ global sample
95
+ if not sample:
96
+ sample = 1
97
+ empty_cache()
98
+ try:
99
+ img = pipeline(
100
+ request.prompt,
101
+ generator=generator,
102
+ guidance_scale=0.0,
103
+ num_inference_steps=4,
104
+ max_sequence_length=256,
105
+ height=request.height,
106
+ width=request.width,
107
+ output_type="pil",
108
+ ).images[0]
109
+ return img
110
+ except Exception as e:
111
+ return None
uv.lock ADDED
The diff for this file is too large to render. See raw diff