escort321 commited on
Commit
09ceb13
·
verified ·
1 Parent(s): 1f01720

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +35 -0
  2. src/main.py +57 -0
  3. src/pipeline.py +80 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "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 = "black-forest-labs/FLUX.1-schnell"
26
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
27
+ exclude = ["transformer"]
28
+
29
+ [[tool.edge-maxxing.models]]
30
+ repository = "escort321/FLUX.1-schnell1-up"
31
+ revision = "d7e70e3a8fbc36ec3c47e78913e9c7142bc87b7b"
32
+
33
+ [project.scripts]
34
+ start_inference = "main:main"
35
+
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,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub.constants import HF_HUB_CACHE
2
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
3
+ import torch
4
+ import torch._dynamo
5
+ import gc
6
+ from PIL import Image as img
7
+ from PIL.Image import Image
8
+ from pipelines.models import TextToImageRequest
9
+ from torch import Generator
10
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
11
+ import os
12
+
13
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
14
+
15
+ Pipeline = None
16
+
17
+ ckpt_id = "black-forest-labs/FLUX.1-schnell"
18
+ ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
19
+
20
+
21
+ def empty_cache():
22
+ gc.collect()
23
+ torch.cuda.empty_cache()
24
+ torch.cuda.reset_max_memory_allocated()
25
+ torch.cuda.reset_peak_memory_stats()
26
+
27
+
28
+ def load_pipeline() -> Pipeline:
29
+ empty_cache()
30
+
31
+ dtype, device = torch.bfloat16, "cuda"
32
+ text_encoder_2 = T5EncoderModel.from_pretrained(
33
+ "escort321/FLUX.1-schnell1-up",
34
+ revision="d7e70e3a8fbc36ec3c47e78913e9c7142bc87b7b",
35
+ subfolder="text_encoder_2",
36
+ torch_dtype=torch.bfloat16,
37
+ )
38
+
39
+ path = os.path.join(
40
+ HF_HUB_CACHE,
41
+ "models--escort321--FLUX.1-schnell1-up/snapshots/d7e70e3a8fbc36ec3c47e78913e9c7142bc87b7b/transformer",
42
+ )
43
+ transformer = FluxTransformer2DModel.from_pretrained(
44
+ path, torch_dtype=torch.bfloat16, use_safetensors=False
45
+ )
46
+ pipeline = DiffusionPipeline.from_pretrained(
47
+ ckpt_id,
48
+ revision=ckpt_revision,
49
+ transformer=transformer,
50
+ text_encoder_2=text_encoder_2,
51
+ torch_dtype=dtype,
52
+ ).to(device)
53
+ # quantize_(pipeline.vae, int8_weight_only())
54
+ pipeline(
55
+ prompt="wordcraft, radiance, ethereal, cartilaginous, tuner, fruity, dullard, existence",
56
+ width=1024,
57
+ height=1024,
58
+ guidance_scale=0.0,
59
+ num_inference_steps=4,
60
+ max_sequence_length=256,
61
+ )
62
+
63
+ empty_cache()
64
+ return pipeline
65
+
66
+
67
+ @torch.no_grad()
68
+ def infer(
69
+ request: TextToImageRequest, pipeline: Pipeline, generator: Generator
70
+ ) -> Image:
71
+ return pipeline(
72
+ request.prompt,
73
+ generator=generator,
74
+ guidance_scale=0.0,
75
+ num_inference_steps=4,
76
+ max_sequence_length=256,
77
+ height=request.height,
78
+ width=request.width,
79
+ output_type="pil",
80
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff