jade012 commited on
Commit
402cc8d
·
verified ·
1 Parent(s): 04766d7

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. README.md +0 -0
  2. pyproject.toml +35 -0
  3. src/check.py +4 -0
  4. src/main.py +69 -0
  5. src/pipeline.py +50 -0
  6. uv.lock +0 -0
README.md ADDED
File without changes
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 = "jade012/FLUX.1-schnell1"
31
+ revision = "124d80794ed0cf70cac48ecd204dbe04b00dff94"
32
+
33
+ [project.scripts]
34
+ start_inference = "main:main"
35
+
src/check.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import torch
2
+ loaded_data = torch.load("loss_params.pth")
3
+ loaded_metadata = loaded_data["metadata"]['author']
4
+ print(loaded_metadata)
src/main.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def get_git_remote_url():
55
+ try:
56
+ # Load the current repository
57
+ repo = Repo(".")
58
+
59
+ # Get the remote named 'origin'
60
+ remote = repo.remotes.origin
61
+
62
+ # Return the URL of the remote
63
+ return remote.url
64
+ except Exception as e:
65
+ print(f"Error: {e}")
66
+ return None
67
+
68
+ if __name__ == '__main__':
69
+ main()
src/pipeline.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
13
+
14
+ Pipeline = None
15
+
16
+ ckpt_id = "black-forest-labs/FLUX.1-schnell"
17
+ ckpt_revision = "741f7c3ce8b383c54771c7003378a50191e9efe9"
18
+ def empty_cache():
19
+ gc.collect()
20
+ torch.cuda.empty_cache()
21
+ torch.cuda.reset_max_memory_allocated()
22
+ torch.cuda.reset_peak_memory_stats()
23
+
24
+ def load_pipeline() -> Pipeline:
25
+ empty_cache()
26
+
27
+ dtype, device = torch.bfloat16, "cuda"
28
+ text_encoder_2 = T5EncoderModel.from_pretrained("jade012/FLUX.1-schnell1", revision = "124d80794ed0cf70cac48ecd204dbe04b00dff94", subfolder="text_encoder_2",torch_dtype=torch.bfloat16)
29
+
30
+
31
+ path = os.path.join(HF_HUB_CACHE, "models--jade012--FLUX.1-schnell1/snapshots/124d80794ed0cf70cac48ecd204dbe04b00dff94/transformer")
32
+ transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False)
33
+ pipeline = DiffusionPipeline.from_pretrained(
34
+ ckpt_id,
35
+ revision=ckpt_revision,
36
+ transformer=transformer,
37
+ text_encoder_2=text_encoder_2,
38
+ torch_dtype=dtype,
39
+ ).to(device)
40
+ #quantize_(pipeline.vae, int8_weight_only())
41
+ pipeline(prompt="logomancy, afterglow, aetheric, chondrichthyes, calibrator, pomeous, lackwit, presentness", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256)
42
+
43
+ empty_cache()
44
+ return pipeline
45
+
46
+
47
+ @torch.no_grad()
48
+ def infer(request: TextToImageRequest, pipeline: Pipeline, generator: Generator) -> Image:
49
+ image=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]
50
+ return(image)
uv.lock ADDED
The diff for this file is too large to render. See raw diff