smash3211 commited on
Commit
d94b170
·
verified ·
1 Parent(s): 554dbcd

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. pyproject.toml +38 -0
  2. src/main.py +52 -0
  3. src/pipeline.py +77 -0
  4. uv.lock +0 -0
pyproject.toml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "setuptools==75.2.0",
21
+ "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",
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 = "smash3211/edge-transformer-fi"
31
+ revision = "47ebcfb8963763b7a2523d57ae5787c36eb67095"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "smash3211/edge-text_encoder_2-fi"
35
+ revision = "2623eb3c2024cd5e9851e5210412b9baf238c7f0"
36
+
37
+ [project.scripts]
38
+ start_inference = "main:main"
src/main.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+
19
+ print(f"pipelien 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(
34
+ connection.recv_bytes().decode("utf-8")
35
+ )
36
+ except EOFError:
37
+ print(f"Inference socket exiting")
38
+
39
+ return
40
+
41
+ image = infer(request, pipeline)
42
+
43
+ data = BytesIO()
44
+ image.save(data, format=JpegImageFile.format)
45
+
46
+ packet = data.getvalue()
47
+
48
+ connection.send_bytes(packet)
49
+
50
+
51
+ if __name__ == "__main__":
52
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 8
2
+ from huggingface_hub.constants import HF_HUB_CACHE
3
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel
4
+ import torch
5
+ import torch._dynamo
6
+ import os
7
+ from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
8
+ from PIL.Image import Image
9
+ from pipelines.models import TextToImageRequest
10
+ from torch import Generator
11
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
12
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
13
+
14
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
15
+ os.environ["TOKENIZERS_PARALLELISM"] = "True"
16
+ torch._dynamo.config.suppress_errors = True
17
+
18
+ Pipeline = None
19
+ ids = "black-forest-labs/FLUX.1-schnell"
20
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
21
+
22
+
23
+ def load_pipeline() -> Pipeline:
24
+ vae = AutoencoderKL.from_pretrained(
25
+ ids,
26
+ revision=revision,
27
+ subfolder="vae",
28
+ local_files_only=True,
29
+ torch_dtype=torch.bfloat16,
30
+ )
31
+ quantize_(vae, int8_weight_only())
32
+
33
+ text_encoder_2 = T5EncoderModel.from_pretrained(
34
+ "smash3211/edge-text_encoder_2-fi",
35
+ revision="2623eb3c2024cd5e9851e5210412b9baf238c7f0",
36
+ torch_dtype=torch.bfloat16,
37
+ ).to(memory_format=torch.channels_last)
38
+ path = os.path.join(
39
+ HF_HUB_CACHE,
40
+ "models--smash3211--edge-transformer-fi/snapshots/47ebcfb8963763b7a2523d57ae5787c36eb67095",
41
+ )
42
+ transformer = FluxTransformer2DModel.from_pretrained(
43
+ path, torch_dtype=torch.bfloat16, use_safetensors=False
44
+ ).to(memory_format=torch.channels_last)
45
+
46
+ pipeline = DiffusionPipeline.from_pretrained(
47
+ ids,
48
+ revision=revision,
49
+ transformer=transformer,
50
+ text_encoder_2=text_encoder_2,
51
+ torch_dtype=torch.bfloat16,
52
+ )
53
+ pipeline.to("cuda")
54
+
55
+ pipeline(
56
+ prompt="satiated, everlasting, tiny, caprine, edmund, foundation, whispering",
57
+ width=1024,
58
+ height=1024,
59
+ guidance_scale=0.0,
60
+ num_inference_steps=4,
61
+ max_sequence_length=256,
62
+ )
63
+ return pipeline
64
+
65
+
66
+ @torch.no_grad()
67
+ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
68
+ generator = Generator(pipeline.device).manual_seed(request.seed)
69
+ return pipeline(
70
+ request.prompt,
71
+ generator=generator,
72
+ guidance_scale=0.0,
73
+ num_inference_steps=4,
74
+ max_sequence_length=256,
75
+ height=request.height,
76
+ width=request.width,
77
+ ).images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff