HyperAIHyperAI

Command Palette

Search for a command to run...

AI Image Generation in Pure C: Flux 2 Klein 4B Model Inference Without Python or Dependencies

GitHub repository antirez/flux2.c presents a pure C implementation of the FLUX.2-klein-4B image generation model from Black Forest Labs. This project enables text-to-image and image-to-image generation directly in C with no external dependencies beyond the standard C library. It supports optional acceleration via MPS (Apple Silicon) or BLAS (on Linux), which is recommended for better performance. The project was created by Salvatore, also known as antirez, as an experiment in AI-assisted software development. He used Claude Code with the Claude Max plan to generate the entire codebase without writing a single line of code himself. The goal was to demonstrate that complex AI inference systems can be built in C—avoiding the Python stack, PyTorch, and CUDA toolkits—making AI more accessible and portable. The implementation runs directly on safetensors model files using full 32-bit floating-point precision, eliminating the need for model conversion or quantization. This simplifies usage while prioritizing reproducibility and ease of deployment. To get started, users can generate an image with a simple command like: ./flux -d flux-klein-model -p "A picture of a woman in 1960 America. Sunglasses. ASA 400 film. Black and White." -W 250 -H 250 -o /tmp/woman.png The same model can be used for image-to-image generation, where an existing image is transformed based on a new prompt using the -t (strength) parameter to control the degree of change. Key features include: - Text-to-image and image-to-image generation - Support for custom resolution (minimum 64x64, maximum 1024x1024, must be multiples of 16) - Seed output to stderr for reproducibility - Automatic release of the text encoder after encoding to reduce peak memory usage - Integration as a C library via libflux.a and flux.h The model architecture includes a rectified flow transformer with 5 double blocks and 20 single blocks, a Qwen3-4B text encoder, and an AutoencoderKL-based VAE. Inference uses exactly 4 steps, making it fast and efficient. Memory requirements peak at around 16GB during generation, with the text encoder using ~8GB and the diffusion phase using ~8GB. The text encoder is automatically reloaded when changing prompts. The project includes a complete C API for integration into other applications. A sample program shows how to load the model, configure parameters, generate an image, and save it to a file. Building the project requires selecting a backend. For BLAS support on Linux, OpenBLAS must be installed first. The model weights are downloaded automatically from Hugging Face and stored in a local directory. This project is licensed under the MIT License and represents a bold step toward lightweight, portable, and open-source AI inference without reliance on heavy frameworks.

Related Links