jokerbit commited on
Commit
7bc507a
·
verified ·
1 Parent(s): 5e21011

Upload folder using huggingface_hub

Browse files
Files changed (8) hide show
  1. .gitattributes +1 -0
  2. .gitignore +8 -0
  3. README.md +19 -0
  4. pyproject.toml +27 -0
  5. sample.png +3 -0
  6. src/main.py +59 -0
  7. src/pipeline.py +138 -0
  8. 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
+ sample.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ **/.cache
2
+ **/__pycache__
3
+ **/*.egg-info
4
+ *.safetensors
5
+ **/.venv
6
+ .venv
7
+ .git
8
+
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # flux-schnell-edge-inference
2
+
3
+ This holds the baseline for the FLUX Schnel NVIDIA GeForce RTX 4090 contest, which can be forked freely and optimized
4
+
5
+ Some recommendations are as follows:
6
+ - Installing dependencies should be done in `pyproject.toml`, including git dependencies
7
+ - HuggingFace models should be specified in the `models` array in the `pyproject.toml` file, and will be downloaded before benchmarking
8
+ - The pipeline does **not** have internet access so all dependencies and models must be included in the `pyproject.toml`
9
+ - Compiled models should be hosted on HuggingFace and included in the `models` array in the `pyproject.toml` (rather than compiling during loading). Loading time matters far more than file sizes
10
+ - Avoid changing `src/main.py`, as that includes mostly protocol logic. Most changes should be in `models` and `src/pipeline.py`
11
+ - Ensure the entire repository (excluding dependencies and HuggingFace models) is under 16MB
12
+
13
+ For testing, you need a docker container with pytorch and ubuntu 22.04.
14
+ You can download your listed dependencies with `uv`, installed with:
15
+ ```bash
16
+ pipx ensurepath
17
+ pipx install uv
18
+ ```
19
+ You can then relock with `uv lock`, and then run with `uv run start_inference`
pyproject.toml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "7"
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
+ "torchao>=0.6.1",
20
+ "ipython>=8.29.0",
21
+ ]
22
+
23
+ [tool.edge-maxxing]
24
+ models = ["black-forest-labs/FLUX.1-schnell"]
25
+
26
+ [project.scripts]
27
+ start_inference = "main:main"
sample.png ADDED

Git LFS Details

  • SHA256: b516bf16c0ae0a532a8d6628534b71e6001857fc54a67d8d4e1f56afbd066595
  • Pointer size: 132 Bytes
  • Size of remote file: 1.34 MB
src/main.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ import torch
9
+
10
+ from PIL.JpegImagePlugin import JpegImageFile
11
+ from pipelines.models import TextToImageRequest
12
+
13
+ from pipeline import load_pipeline, infer
14
+
15
+ SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
16
+
17
+
18
+ def at_exit():
19
+ torch.cuda.empty_cache()
20
+
21
+
22
+ def main():
23
+ atexit.register(at_exit)
24
+
25
+ print(f"Loading pipeline")
26
+ pipeline = load_pipeline()
27
+
28
+ print(f"Pipeline loaded, creating socket at '{SOCKET}'")
29
+
30
+ if exists(SOCKET):
31
+ remove(SOCKET)
32
+
33
+ with Listener(SOCKET) as listener:
34
+ chmod(SOCKET, 0o777)
35
+
36
+ print(f"Awaiting connections")
37
+ with listener.accept() as connection:
38
+ print(f"Connected")
39
+
40
+ while True:
41
+ try:
42
+ request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8"))
43
+ except EOFError:
44
+ print(f"Inference socket exiting")
45
+
46
+ return
47
+
48
+ image = infer(request, pipeline)
49
+
50
+ data = BytesIO()
51
+ image.save(data, format=JpegImageFile.format)
52
+
53
+ packet = data.getvalue()
54
+
55
+ connection.send_bytes(packet)
56
+
57
+
58
+ if __name__ == '__main__':
59
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from diffusers import FluxPipeline, AutoencoderKL, FluxTransformer2DModel
3
+ from diffusers.image_processor import VaeImageProcessor
4
+ from transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModel, CLIPTextConfig, T5Config
5
+ import torch
6
+ import gc
7
+ from PIL.Image import Image
8
+ from pipelines.models import TextToImageRequest
9
+ from torch import Generator
10
+ from torchao.quantization import quantize_, int8_weight_only
11
+ from time import perf_counter
12
+
13
+
14
+ HOME = os.environ["HOME"]
15
+ FLUX_CHECKPOINT = os.path.join(HOME,
16
+ ".cache/huggingface/hub/models--black-forest-labs--FLUX.1-schnell/snapshots/741f7c3ce8b383c54771c7003378a50191e9efe9/")
17
+ QUANTIZED_MODEL = ["transformer"]
18
+
19
+
20
+ QUANT_CONFIG = int8_weight_only()
21
+ DTYPE = torch.bfloat16
22
+ NUM_STEPS = 4
23
+
24
+ def get_transformer(quantize: bool = True, quant_config = int8_weight_only(), quant_ckpt: str = None):
25
+ if quant_ckpt is not None:
26
+ config = FluxTransformer2DModel.load_config(FLUX_CHECKPOINT, subfolder="transformer", local_files_only=True)
27
+ model = FluxTransformer2DModel.from_config(config).to(DTYPE)
28
+ state_dict = torch.load(quant_ckpt, map_location="cpu")
29
+ model.load_state_dict(state_dict, assign=True)
30
+ print(f"Loaded {quant_ckpt}")
31
+ return model
32
+
33
+ model = FluxTransformer2DModel.from_pretrained(
34
+ FLUX_CHECKPOINT, subfolder="transformer", torch_dtype=DTYPE, local_files_only=True
35
+ )
36
+ if quantize:
37
+ quantize_(model, quant_config)
38
+ return model
39
+
40
+
41
+ def get_text_encoder(quantize: bool = True, quant_config = int8_weight_only(), quant_ckpt: str = None):
42
+ if quant_ckpt is not None:
43
+ config = CLIPTextConfig.from_pretrained(FLUX_CHECKPOINT, subfolder="text_encoder", local_files_only=True)
44
+ model = CLIPTextModel(config).to(DTYPE)
45
+ state_dict = torch.load(quant_ckpt, map_location="cpu")
46
+ model.load_state_dict(state_dict, assign=True)
47
+ print(f"Loaded {quant_ckpt}")
48
+ return model
49
+
50
+ model = CLIPTextModel.from_pretrained(
51
+ FLUX_CHECKPOINT, subfolder="text_encoder", torch_dtype=DTYPE, local_files_only=True
52
+ )
53
+ if quantize:
54
+ quantize_(model, quant_config)
55
+ return model
56
+
57
+
58
+ def get_text_encoder_2(quantize: bool = True, quant_config = int8_weight_only(), quant_ckpt: str = None):
59
+ if quant_ckpt is not None:
60
+ config = T5Config.from_pretrained(FLUX_CHECKPOINT, subfolder="text_encoder_2", local_files_only=True)
61
+ model = T5EncoderModel(config).to(DTYPE)
62
+ state_dict = torch.load(quant_ckpt, map_location="cpu")
63
+ print(f"Loaded {quant_ckpt}")
64
+ model.load_state_dict(state_dict, assign=True)
65
+ return model
66
+
67
+ model = T5EncoderModel.from_pretrained(
68
+ FLUX_CHECKPOINT, subfolder="text_encoder_2", torch_dtype=DTYPE, local_files_only=True
69
+ )
70
+ if quantize:
71
+ quantize_(model, quant_config)
72
+ return model
73
+
74
+
75
+ def get_vae(quantize: bool = True, quant_config = int8_weight_only(), quant_ckpt: str = None):
76
+ if quant_ckpt is not None:
77
+ config = AutoencoderKL.load_config(FLUX_CHECKPOINT, subfolder="vae", local_files_only=True)
78
+ model = AutoencoderKL.from_config(config).to(DTYPE)
79
+ state_dict = torch.load(quant_ckpt, map_location="cpu")
80
+ model.load_state_dict(state_dict, assign=True)
81
+ print(f"Loaded {quant_ckpt}")
82
+ return model
83
+ model = AutoencoderKL.from_pretrained(
84
+ FLUX_CHECKPOINT, subfolder="vae", torch_dtype=DTYPE, local_files_only=True
85
+ )
86
+ if quantize:
87
+ quantize_(model, quant_config)
88
+ return model
89
+
90
+
91
+ def empty_cache():
92
+ gc.collect()
93
+ torch.cuda.empty_cache()
94
+ torch.cuda.reset_max_memory_allocated()
95
+ torch.cuda.reset_peak_memory_stats()
96
+
97
+
98
+ def load_pipeline() -> FluxPipeline:
99
+ empty_cache()
100
+ transformer = get_transformer('transformer' in QUANTIZED_MODEL, QUANT_CONFIG)
101
+ text_encoder = get_text_encoder("text_encoder" in QUANTIZED_MODEL, QUANT_CONFIG)
102
+ text_encoder_2 = get_text_encoder_2("text_encoder_2" in QUANTIZED_MODEL, QUANT_CONFIG)
103
+ vae = get_vae("vae" in QUANTIZED_MODEL, QUANT_CONFIG)
104
+
105
+ pipe = FluxPipeline.from_pretrained(FLUX_CHECKPOINT,
106
+ transformer=transformer,
107
+ vae=vae,
108
+ text_encoder=text_encoder,
109
+ text_encoder_2=text_encoder_2,
110
+ torch_dtype=torch.bfloat16).to("cuda")
111
+ empty_cache()
112
+ pipe("cat", guidance_scale=0., max_sequence_length=256, num_inference_steps=4)
113
+ return pipe
114
+
115
+
116
+ def infer(request: TextToImageRequest, _pipeline: FluxPipeline) -> Image:
117
+ if request.seed is None:
118
+ generator = None
119
+ else:
120
+ generator = Generator(device=_pipeline.device).manual_seed(request.seed)
121
+
122
+ empty_cache()
123
+ image = _pipeline(prompt=request.prompt,
124
+ width=request.width,
125
+ height=request.height,
126
+ guidance_scale=0.0,
127
+ generator=generator,
128
+ output_type="pil",
129
+ max_sequence_length=256,
130
+ num_inference_steps=NUM_STEPS).images[0]
131
+ return image
132
+
133
+ if __name__ == "__main__":
134
+ start_time = perf_counter()
135
+ pipe = load_pipeline()
136
+ stop_time = perf_counter()
137
+ print(f"Pipeline is loaded in {stop_time - start_time}s")
138
+ pipe("cat holding a womboai sign", num_inference_steps=4, guidance_scale=0, generator=torch.Generator(pipe.device).manual_seed(666)).images[0].save("sample.png")
uv.lock ADDED
The diff for this file is too large to render. See raw diff