Act commited on
Commit
9a2d26a
·
verified ·
1 Parent(s): 34c8825

Initial commit with folder contents

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. pyproject.toml +44 -0
  3. src/main.py +54 -0
  4. src/pipeline.py +73 -0
  5. uv.lock +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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
36
+ RobertML.png filter=lfs diff=lfs merge=lfs -text
pyproject.toml ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "black-forest-labs/FLUX.1-schnell"
26
+ revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
27
+ exclude = ["transformer"]
28
+
29
+ [[tool.edge-maxxing.models]]
30
+ repository = "RobertML/FLUX.1-schnell-int8wo"
31
+ revision = "307e0777d92df966a3c0f99f31a6ee8957a9857a"
32
+
33
+ [[tool.edge-maxxing.models]]
34
+ repository = "city96/t5-v1_1-xxl-encoder-bf16"
35
+ revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86"
36
+
37
+ [[tool.edge-maxxing.models]]
38
+ repository = "proact/PRO_FLUX_9"
39
+ revision = "0c58d671a73b6ed093b5064d453c4cf2b3f13183"
40
+
41
+
42
+ [project.scripts]
43
+ start_inference = "main:main"
44
+
src/main.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
13
+
14
+
15
+ def at_exit():
16
+ torch.cuda.empty_cache()
17
+
18
+
19
+ def main():
20
+ atexit.register(at_exit)
21
+
22
+ print(f"Loading pipeline")
23
+ pipeline = load_pipeline()
24
+
25
+ print(f"Pipeline loaded! , creating socket at '{SOCKET}'")
26
+
27
+ if exists(SOCKET):
28
+ remove(SOCKET)
29
+
30
+ with Listener(SOCKET) as listener:
31
+ chmod(SOCKET, 0o777)
32
+
33
+ print(f"Awaiting connections")
34
+ with listener.accept() as connection:
35
+ print(f"Connected")
36
+ generator = torch.Generator("cuda")
37
+ while True:
38
+ try:
39
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
40
+ except EOFError:
41
+ print(f"Inference socket exiting")
42
+
43
+ return
44
+ image = infer(request, pipeline, generator.manual_seed(request.seed))
45
+ data = BytesIO()
46
+ image.save(data, format=JpegImageFile.format)
47
+
48
+ packet = data.getvalue()
49
+
50
+ connection.send_bytes(packet )
51
+
52
+
53
+ if __name__ == '__main__':
54
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
+
3
+ from huggingface_hub.constants import HF_HUB_CACHE
4
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer
5
+ import torch
6
+ import torch._dynamo
7
+ import gc
8
+ from PIL import Image as img
9
+ from PIL.Image import Image
10
+ from pipelines.models import TextToImageRequest
11
+ from torch import Generator
12
+ import time
13
+ from diffusers import FluxTransformer2DModel, DiffusionPipeline
14
+ from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
15
+ import os
16
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
17
+
18
+
19
+ torch.backends.cuda.matmul.allow_tf32 = True
20
+ torch.backends.cudnn.enabled = True
21
+ torch.backends.cudnn.benchmark = True
22
+
23
+ ckpt_id = "black-forest-labs/FLUX.1-schnell"
24
+ ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
25
+
26
+
27
+ Pipeline = None
28
+ def empty_cache():
29
+ gc.collect()
30
+ torch.cuda.empty_cache()
31
+ torch.cuda.reset_max_memory_allocated()
32
+ torch.cuda.reset_peak_memory_stats()
33
+
34
+ def load_pipeline() -> Pipeline:
35
+ empty_cache()
36
+
37
+ dtype, device = torch.bfloat16, "cuda"
38
+
39
+ text_encoder_2 = T5EncoderModel.from_pretrained(
40
+ "city96/t5-v1_1-xxl-encoder-bf16", revision = "1b9c856aadb864af93c1dcdc226c2774fa67bc86", torch_dtype=torch.bfloat16
41
+ ).to(memory_format=torch.channels_last)
42
+
43
+
44
+ path = os.path.join(HF_HUB_CACHE, "models--RobertML--FLUX.1-schnell-int8wo/snapshots/307e0777d92df966a3c0f99f31a6ee8957a9857a")
45
+ model = FluxTransformer2DModel.from_pretrained(path, torch_dtype=dtype, use_safetensors=False).to(memory_format=torch.channels_last)
46
+ pipeline = DiffusionPipeline.from_pretrained(
47
+ ckpt_id,
48
+ revision=ckpt_revision,
49
+ transformer=model,
50
+ text_encoder_2=text_encoder_2,
51
+ torch_dtype=dtype,
52
+ ).to(device)
53
+ quantize_(pipeline.vae, int8_weight_only())
54
+
55
+ pipeline(prompt="imprisonable, forechamber, demagogic, monotropic, blandiloquious, blechnoid, uncarnivorous", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
56
+
57
+ empty_cache()
58
+ return pipeline
59
+
60
+
61
+ @torch.no_grad()
62
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image:
63
+
64
+ image=pipeline(request.prompt,
65
+ generator=generator,
66
+ guidance_scale=0.0,
67
+ num_inference_steps=4,
68
+ max_sequence_length=256,
69
+ height=request.height,
70
+ width=request.width,
71
+ output_type="pil").images[0]
72
+
73
+ return(image)
uv.lock ADDED
The diff for this file is too large to render. See raw diff