Upload folder using huggingface_hub
Browse filesFiles changed (5)
- README.mdAdded+19
- pyproject.tomlAdded+29
- src/main.pyAdded+50
- src/pipeline.pyAdded+53
- 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", "setuptools==75.2.0", "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()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#5.1from huggingface_hub.constants import HF_HUB_CACHEfrom transformers import T5EncoderModel, T5TokenizerFast, CLIPTokenizer, CLIPTextModelimport torchimport torch._dynamoimport gcimport osfrom diffusers import FluxPipeline, AutoencoderKL, AutoencoderTinyfrom PIL.Image import Imagefrom pipelines.models import TextToImageRequestfrom torch import Generatorfrom diffusers import FluxTransformer2DModel, DiffusionPipelinefrom torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
os.environ['PYTORCH_CUDA_ALLOC_CONF']="expandable_segments:True"os.environ["TOKENIZERS_PARALLELISM"] = "True"torch._dynamo.config.suppress_errors = True
Pipeline = Noneids = "slobers/Flux.1.Schnella"Revision = "e34d670e44cecbbc90e4962e7aada2ac5ce8b55b"
def empty_cache(): gc.collect() torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
def load_pipeline() -> Pipeline: empty_cache() path = os.path.join(HF_HUB_CACHE, "models--slobers--Flux.1.Schnella/snapshots/e34d670e44cecbbc90e4962e7aada2ac5ce8b55b/transformer") transformer = FluxTransformer2DModel.from_pretrained(path, torch_dtype=torch.bfloat16, use_safetensors=False) pipeline = FluxPipeline.from_pretrained(ids, revision=Revision, transformer=transformer, local_files_only=True, torch_dtype=torch.bfloat16,) pipeline.to("cuda") pipeline.vae = torch.compile(pipeline.vae, mode="max-autotune", fullgraph=True, dynamic=True) for _ in range(3): pipeline(prompt="insensible, timbale, pothery, electrovital, actinogram, taxis, intracerebellar, centrodesmus", width=1024, height=1024, guidance_scale=5.0, num_inference_steps=4, max_sequence_length=256) return pipeline empty_cache()
@torch.no_grad()def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image: generator = Generator(pipeline.device).manual_seed(request.seed)
return pipeline( request.prompt, generator=generator, guidance_scale=6.5, 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+53
uv.lockAdded