malicious546 commited on
Commit
2087ece
·
verified ·
1 Parent(s): 0da4a70

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. .gitattributes +0 -1
  2. pyproject.toml +39 -0
  3. src/main.py +57 -0
  4. src/pipeline.py +87 -0
  5. uv.lock +0 -0
.gitattributes CHANGED
@@ -32,4 +32,3 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.xz filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
32
  *.xz filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
 
pyproject.toml ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]
23
+
24
+ [[tool.edge-maxxing.models]]
25
+ repository = "city96/t5-v1_1-xxl-encoder-bf16"
26
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
27
+
28
+ [[tool.edge-maxxing.models]]
29
+ repository = "malicious546/flux.1-schnell-dat-q8"
30
+ revision = "f312bd733a4f351e328e94f12e1cd4d0d4baa5eb"
31
+ exclude = ["transformer"]
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "malicious546/flux.1-schnell-dat-int8"
35
+ revision = "017b51c93e8b1a0a7ab7a05cdcce2270f139de60"
36
+
37
+ [project.scripts]
38
+ start_inference = "main:main"
39
+
src/main.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import torch
8
+
9
+ from PIL.JpegImagePlugin import JpegImageFile
10
+ from pipelines.models import TextToImageRequest
11
+ from pipeline import load_pipeline, infer
12
+
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(
41
+ connection.recv_bytes().decode("utf-8")
42
+ )
43
+ except EOFError:
44
+ print(f"Inference socket exiting")
45
+
46
+ return
47
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
48
+ data = BytesIO()
49
+ image.save(data, format=JpegImageFile.format)
50
+
51
+ packet = data.getvalue()
52
+
53
+ connection.send_bytes(packet)
54
+
55
+
56
+ if __name__ == "__main__":
57
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
+ from diffusers.image_processor import VaeImageProcessor
3
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
+ from huggingface_hub.constants import HF_HUB_CACHE
5
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
6
+ import torch
7
+ import torch._dynamo
8
+ import gc
9
+ from PIL import Image as img
10
+ from PIL.Image import Image
11
+ from pipelines.models import TextToImageRequest
12
+ from torch import Generator
13
+ import time
14
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
16
+ import os
17
+
18
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
19
+
20
+ Pipeline = None
21
+
22
+ ckpt_id = "malicious546/flux.1-schnell-dat-q8"
23
+ ckpt_revision = "f312bd733a4f351e328e94f12e1cd4d0d4baa5eb"
24
+
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() -> Pipeline:
34
+ empty_cache()
35
+
36
+ dtype, device = torch.bfloat16, "cuda"
37
+
38
+ text_encoder_2 = T5EncoderModel.from_pretrained(
39
+ "city96/t5-v1_1-xxl-encoder-bf16",
40
+ revision="1b9c856aadb864af93c1dcdc226c2774fa67bc86",
41
+ torch_dtype=torch.bfloat16,
42
+ ).to(memory_format=torch.channels_last)
43
+
44
+ path = os.path.join(
45
+ HF_HUB_CACHE,
46
+ "models--malicious546--flux.1-schnell-dat-int8/snapshots/017b51c93e8b1a0a7ab7a05cdcce2270f139de60",
47
+ )
48
+ model = FluxTransformer2DModel.from_pretrained(
49
+ path, torch_dtype=dtype, use_safetensors=False
50
+ ).to(memory_format=torch.channels_last)
51
+ pipeline = DiffusionPipeline.from_pretrained(
52
+ ckpt_id,
53
+ revision=ckpt_revision,
54
+ transformer=model,
55
+ text_encoder_2=text_encoder_2,
56
+ torch_dtype=dtype,
57
+ ).to(device)
58
+ pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune")
59
+
60
+ for _ in range(3):
61
+ pipeline(
62
+ prompt="divination, aftermath, airy, flatworm, adjuster, fruity, dullard, presence",
63
+ width=1024,
64
+ height=1024,
65
+ guidance_scale=0.0,
66
+ num_inference_steps=4,
67
+ max_sequence_length=256,
68
+ )
69
+
70
+ empty_cache()
71
+ return pipeline
72
+
73
+
74
+ @torch.no_grad()
75
+ def infer(
76
+ request: TextToImageRequest, pipeline: Pipeline, generator: Generator
77
+ ) -> Image:
78
+ return pipeline(
79
+ request.prompt,
80
+ generator=generator,
81
+ guidance_scale=0.0,
82
+ num_inference_steps=4,
83
+ max_sequence_length=256,
84
+ height=request.height,
85
+ width=request.width,
86
+ output_type="pil",
87
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff