db900 commited on
Commit
9db270d
·
verified ·
1 Parent(s): ad9a487

Initial commit with folder contents

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -1
  2. pyproject.toml +40 -0
  3. src/main.py +55 -0
  4. src/pipeline.py +45 -0
  5. uv.lock +0 -0
.gitattributes CHANGED
@@ -32,4 +32,4 @@ 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
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
pyproject.toml ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "setuptools>=75.3.0",
23
+ ]
24
+
25
+ [[tool.edge-maxxing.models]]
26
+ repository = "black-forest-labs/FLUX.1-schnell"
27
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
28
+ exclude = ["transformer"]
29
+
30
+ [[tool.edge-maxxing.models]]
31
+ repository = "db900/encode-stream"
32
+ revision = "8a6b7bd09dc66733fa582900186f929353f63619"
33
+
34
+ [[tool.edge-maxxing.models]]
35
+ repository = "db900/deval"
36
+ revision = "cf3aa8b38736807b622d7397036564a1cdc868de"
37
+
38
+ [project.scripts]
39
+ start_inference = "main:main"
40
+
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,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
+ import torch
4
+ import torch._dynamo
5
+ from PIL import Image
6
+ from typing import Type
7
+ from torch import Generator
8
+ from transformers import T5EncoderModel
9
+ from huggingface_hub.constants import HF_HUB_CACHE
10
+ from pipelines.models import TextToImageRequest
11
+ from torchao.quantization import quantize_, int8_weight_only
12
+ from diffusers import AutoencoderKL, FluxPipeline, FluxTransformer2DModel
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
+ torch.backends.cuda.matmul.allow_tf32 = True
18
+ torch.backends.cudnn.enabled = True
19
+ torch.backends.cudnn.benchmark = True
20
+
21
+ Pipeline = None
22
+
23
+ def empty_cache():
24
+ gc.collect()
25
+ torch.cuda.empty_cache()
26
+ torch.cuda.reset_max_memory_allocated()
27
+ torch.cuda.reset_peak_memory_stats()
28
+
29
+ def load_pipeline() -> Pipeline:
30
+ ckpt_id = "black-forest-labs/FLUX.1-schnell"
31
+ ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
32
+
33
+ text_encoder_2 = T5EncoderModel.from_pretrained("db900/encode-stream", revision = "8a6b7bd09dc66733fa582900186f929353f63619", subfolder="text_encoder_2", torch_dtype=torch.bfloat16).to(memory_format=torch.channels_last)
34
+ path = os.path.join(HF_HUB_CACHE, "models--db900--encode-stream/snapshots/8a6b7bd09dc66733fa582900186f929353f63619/transformer")
35
+ transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False).to(memory_format=torch.channels_last)
36
+ quantize_(AutoencoderKL.from_pretrained(ckpt_id, revision=ckpt_revision, subfolder="vae", local_files_only=True, torch_dtype=torch.bfloat16,), int8_weight_only())
37
+ pipeline = FluxPipeline.from_pretrained(ckpt_id, revision=ckpt_revision, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16,)
38
+ pipeline.to("cuda")
39
+ with torch.inference_mode():
40
+ pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
41
+ return pipeline
42
+
43
+ @torch.no_grad()
44
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image:
45
+ return pipeline(request.prompt, generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pil").images[0]
uv.lock ADDED
The diff for this file is too large to render. See raw diff