File size: 2,653 Bytes
f2b68e4
80a1334
 
f2b68e4
 
 
80a1334
f2b68e4
80a1334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
title: auto-diffuser-config
app_file: gradio_app.py
sdk: gradio
sdk_version: 5.31.0
---
# Auto-Diffusers

An intelligent code generator that creates optimized diffusers library snippets based on your hardware specifications using Google's Gemini API.

## Features

- **Hardware Detection**: Automatically detects your system specs (CPU, GPU, VRAM) or allows manual input
- **Real-time Optimization**: Fetches latest optimization techniques from HuggingFace documentation
- **Smart Code Generation**: Uses Gemini API to generate hardware-optimized diffusers code
- **Multiple Profiles**: Supports high-end GPU, mid-range GPU, low VRAM, Apple Silicon, and CPU-only configurations

## Setup

1. Create and activate conda environment:
```bash
conda create -n auto-diffusers python=3.11 -y
conda activate auto-diffusers
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

3. Set up Gemini API key:
```bash
export GEMINI_API_KEY="your_api_key_here"
```

## Usage

### Interactive Mode
```bash
python auto_diffusers.py
```

### Hardware Detection Only
```bash
python hardware_detector.py
```

### Programmatic Usage
```python
from auto_diffusers import AutoDiffusersGenerator

generator = AutoDiffusersGenerator(api_key="your_key")
code = generator.generate_optimized_code(
    model_name="black-forest-labs/FLUX.1-schnell",
    prompt_text="A cat holding a sign that says hello world",
    image_size=(768, 1360),
    num_inference_steps=4
)
print(code)
```

## Hardware Profiles

- **high_end_gpu**: 20+ GB VRAM (RTX 4090, A100, etc.)
- **mid_range_gpu**: 8-20 GB VRAM (RTX 3080, RTX 4070, etc.)  
- **low_vram_gpu**: <8 GB VRAM (GTX 1660, RTX 3060, etc.)
- **apple_silicon**: M1/M2/M3 Macs with MPS support
- **cpu_only**: No GPU acceleration

## Optimization Techniques Applied

The generator automatically applies appropriate optimizations based on your hardware:

- Memory optimizations (model offloading, attention slicing, VAE slicing)
- Speed optimizations (torch.compile, optimal dtypes)
- Hardware-specific optimizations (CUDA, MPS, CPU fallbacks)
- Model-specific configurations (schedulers, inference parameters)

## Example Output

```python
import torch
from diffusers import FluxPipeline

# Optimized for Apple Silicon (MPS)
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-schnell", 
    torch_dtype=torch.bfloat16
)
pipe.to("mps")
pipe.enable_attention_slicing()

prompt = "A cat holding a sign that says hello world"
out = pipe(
    prompt=prompt,
    guidance_scale=0.,
    height=768,
    width=1360,
    num_inference_steps=4,
    max_sequence_length=256,
).images[0]
out.save("image.png")
```