Upload folder using huggingface_hub
Browse filesFiles changed (5)
- README.mdAdded+19
- pyproject.tomlAdded+29
- src/main.pyAdded+50
- src/pipeline.pyAdded+55
- uv.lockAdded
12345678910111213141516171819# flux-schnell-edge-inference
This holds the baseline for the FLUX Schnel NVIDIA GeForce RTX 4090 contest, which can be forked freely and optimized
Some recommendations are as follows:- Installing dependencies should be done in `pyproject.toml`, including git dependencies- HuggingFace models should be specified in the `models` array in the `pyproject.toml` file, and will be downloaded before benchmarking - The pipeline does **not** have internet access so all dependencies and models must be included in the `pyproject.toml` - 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- Avoid changing `src/main.py`, as that includes mostly protocol logic. Most changes should be in `models` and `src/pipeline.py`- Ensure the entire repository (excluding dependencies and HuggingFace models) is under 16MB
For testing, you need a docker container with pytorch and ubuntu 22.04.You can download your listed dependencies with `uv`, installed with:```bashpipx ensurepathpipx install uv```You can then relock with `uv lock`, and then run with `uv run start_inference`1234567891011121314151617181920212223242526272829[build-system]requires = ["setuptools >= 75.0"]build-backend = "setuptools.build_meta"
[project]name = "flux-schnell-edge-inference"description = "An edge-maxxing model submission for the 4090 Flux contest"requires-python = ">=3.10,<3.13"version = "8"dependencies = [ "diffusers==0.31.0", "transformers==4.46.2", "accelerate==1.1.0", "omegaconf==2.3.0", "torch==2.5.1", "protobuf==5.28.3", "sentencepiece==0.2.0", "torchao==0.6.1", "hf_transfer==0.1.8", "edge-maxxing-pipelines @ git+https://github.com/womboai/edge-maxxing@7c760ac54f6052803dadb3ade8ebfc9679a94589#subdirectory=pipelines",]
[[tool.edge-maxxing.models]]repository = "slobers/Flux.1.Schnella"revision = "e34d670e44cecbbc90e4962e7aada2ac5ce8b55b"
[project.scripts]start_inference = "main:main"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950from io import BytesIOfrom multiprocessing.connection import Listenerfrom os import chmod, removefrom os.path import abspath, existsfrom pathlib import Path
from PIL.JpegImagePlugin import JpegImageFilefrom pipelines.models import TextToImageRequest
from pipeline import load_pipeline, infer
SOCKET = abspath(Path(__file__).parent.parent / "inferences.sock")
def main(): print(f"Loading pipeline") pipeline = load_pipeline()
print(f"Pipeline loaded, creating socket at '{SOCKET}'")
if exists(SOCKET): remove(SOCKET)
with Listener(SOCKET) as listener: chmod(SOCKET, 0o777)
print(f"Awaiting connections") with listener.accept() as connection: print(f"Connected")
while True: try: request = TextToImageRequest.model_validate_json(connection.recv_bytes().decode("utf-8")) except EOFError: print(f"Inference socket exiting")
return
image = infer(request, pipeline)
data = BytesIO() image.save(data, format=JpegImageFile.format)
packet = data.getvalue()
connection.send_bytes(packet)
if __name__ == '__main__': main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import gcimport osfrom typing import TypeAlias
import torchfrom PIL.Image import Imagefrom diffusers import FluxPipeline, FluxTransformer2DModel, AutoencoderTiny, DiffusionPipelinefrom huggingface_hub.constants import HF_HUB_CACHEfrom pipelines.models import TextToImageRequestfrom torch import Generatorfrom transformers import T5EncoderModel, CLIPTextModel
os.environ["TOKENIZERS_PARALLELISM"] = "True"torch._dynamo.config.suppress_errors = Trueos.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"
Pipeline = None
ids = "slobers/Flux.1.Schnella"Revision = "e34d670e44cecbbc90e4962e7aada2ac5ce8b55b".
def load_pipeline() -> Pipeline: text_encoder = CLIPTextModel.from_pretrained(ids, revision=Revision, subfolder="text_encoder", local_files_only=True, torch_dtype=torch.bfloat16)
text_encoder_2 = T5EncoderModel.from_pretrained(ids, revision=Revision, subfolder="text_encoder_2", torch_dtype=torch.bfloat16)
vae = AutoencoderTiny.from_pretrained(ids, revision=Revision, subfolder="vae", torch_dtype=torch.bfloat16)
transformer = FluxTransformer2DModel.from_pretrained(ids, revision=Revision, subfolder="transformer", torch_dtype=torch.bfloat16, use_safetensors=False)
transformer = torch.compile(transformer, options={"triton.cudagraphs": True}) transformer.to(device="cuda", memory_format=torch.channels_last)
pipeline = DiffusionPipeline.from_pretrained(ids, vae=vae, revision=Revision, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16) pipeline.to("cuda")
for _ in range(2): pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus", width=1024, height=1024, guidance_scale=0.0, num_inference_steps=5, max_sequence_length=256)
return pipeline
def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image: generator = Generator(pipeline.device).manual_seed(request.seed)
return pipeline( request.prompt, generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, ).images[0]README.mdAdded+19
pyproject.tomlAdded+29
src/main.pyAdded+50
src/pipeline.pyAdded+55
uv.lockAdded