Update README for Remote Text Encoding architecture
Browse files123345678910111213141516171819789102021222324251415161718262728293031323325 unmodified lines59606150626364659 unmodified lines757677666768697879808182837475767784858687888968 unmodified lines158159160152153154161162163157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194164165196166167198199168169201202170171172173204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236174175238176177240178179242243180181182183184185248186187188189# FLUX.2-dev Handler for HuggingFace Dedicated Endpoints
High-performance image generation using FLUX.2-dev with FP8 quantization for HuggingFace Dedicated Endpoints.
Image generation using FLUX.2-dev with Remote Text Encoding for HuggingFace Dedicated Endpoints.
## Architecture
This handler uses **Remote Text Encoding** to work around PyTorch version limitations on HuggingFace Inference Endpoints:
- HF Endpoints runtime: PyTorch 2.2.0, transformers 4.48.0
- FLUX.2 Mistral3 text encoder requires: transformers 4.52+ (PyTorch 2.6+)
- Solution: Use HuggingFace's remote text encoding API
Benefits:
- Saves ~24GB VRAM (no local Mistral3 text encoder)
- Works with HF Endpoints' PyTorch 2.2.0 runtime
- BF16 precision for transformer and VAE
## Features
- **FP8 Quantization**: Via torchao for memory efficiency
- **Multi-Reference Images**: Up to 10 reference images for character/style consistency
- **torch.compile**: Optimized inference with reduce-overhead mode
- **1920x1080 Output**: Full HD resolution by default
- Remote text encoding via HuggingFace API
- Multi-reference image support (up to 10 images)
- 1920x1080 output resolution by default
## GPU Requirements
| GPU | VRAM | Mode | Recommended |
|-----|------|------|-------------|
| H200 | 141GB | Full GPU + FP8 + compile | Best |
| H100 | 80GB | Full GPU + FP8 + compile | Good |
| A100 | 80GB | Full GPU + FP8 | Minimum |
| GPU | VRAM | Mode | Notes |
|-----|------|------|-------|
| H200 | 141GB | Full GPU | Recommended |
| H100 | 80GB | Full GPU | Good |
| A100 | 80GB | CPU offload | Minimum |
## Deployment Steps
25 unmodified lines
# Commit and push
git add .
git commit -m "Add FLUX.2-dev handler"
git commit -m "Add FLUX.2-dev handler with remote text encoding"
git push
```
9 unmodified lines - **Scaling**: Min 0, Max 1 (scale-to-zero enabled)
- **Scale-to-Zero Timeout**: 15 minutes
4. Environment Variables:
- `HF_TOKEN`: Your HuggingFace token (for gated model access)
- `USE_FP8`: `true`
- `USE_COMPILE`: `true`
- `NUM_INFERENCE_STEPS`: `50`
- `HF_TOKEN`: Your HuggingFace token (REQUIRED - for model access AND remote text encoding)
- `NUM_INFERENCE_STEPS`: `50` (optional, default is 50)
5. Click "Create Endpoint"
### 4. Wait for Deployment
First deployment takes 10-15 minutes to:
- Download FLUX.2-dev model (~60GB)
- Apply FP8 quantization
- Run torch.compile warmup
First deployment takes 5-10 minutes to:
- Download FLUX.2-dev transformer and VAE (~35GB without text encoder)
- Test remote text encoding connection
### 5. Test the Endpoint
68 unmodified lines
| Variable | Default | Description |
|----------|---------|-------------|
| `HF_TOKEN` | - | HuggingFace token (required) |
| `USE_FP8` | `true` | Enable FP8 quantization |
| `USE_COMPILE` | `true` | Enable torch.compile |
| `HF_TOKEN` | - | HuggingFace token (REQUIRED for model access and remote text encoding) |
| `NUM_INFERENCE_STEPS` | `50` | Default inference steps |
## Endpoint Management
### Scale to Zero Manually
```python
from huggingface_hub import HfApi
api = HfApi()
api.scale_to_zero_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME'
)
```
### Update Scale-to-Zero Timeout
```python
from huggingface_hub import HfApi
api = HfApi()
api.update_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME',
scale_to_zero_timeout=15 # minutes
)
```
### Resume Endpoint
```python
from huggingface_hub import HfApi
api = HfApi()
api.resume_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME'
)
```
Note: Unlike the Runpod version, this handler does NOT support FP8 quantization or torch.compile due to HF Endpoints' PyTorch 2.2.0 runtime.
## Frontend Integration
## Troubleshooting
```typescript
// services/huggingfaceService.ts
### Endpoint fails to start
const HF_TOKEN = import.meta.env.VITE_HF_TOKEN;
const ENDPOINT_URL = import.meta.env.VITE_HF_FLUX2_DEV_ENDPOINT_URL;
- Check HF_TOKEN has access to FLUX.2-dev (gated model)
- Ensure HF_TOKEN is valid for remote text encoding API
- Ensure GPU instance type has sufficient VRAM (80GB+)
export const generateWithFlux2Dev = async (
prompt: string,
options?: {
referenceImages?: string[];
width?: number;
height?: number;
}
): Promise<string> => {
const hasRefs = options?.referenceImages && options.referenceImages.length > 0;
const response = await fetch(ENDPOINT_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${HF_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
inputs: prompt,
parameters: {
mode: hasRefs ? 'generate_with_refs' : 'generate',
reference_images: options?.referenceImages,
width: options?.width ?? 1920,
height: options?.height ?? 1080,
num_inference_steps: 50,
guidance_scale: hasRefs ? 2.5 : 4.0,
}
})
});
const result = await response.json();
return result.image;
};
```
### "module 'torch' has no attribute 'int1'" error
## Troubleshooting
This means transformers was upgraded beyond what's compatible with PyTorch 2.2.0. Do NOT add `transformers>=4.47.0` to requirements.txt.
### Endpoint fails to start
### Remote text encoding fails
- Check HF_TOKEN has access to FLUX.2-dev (gated model)
- Ensure GPU instance type has sufficient VRAM
- Verify HF_TOKEN is set and valid
- Check network connectivity to `remote-text-encoder-flux-2.huggingface.co`
### Slow cold start
- First inference after scale-up takes 2-3 minutes
- Model loading + torch.compile warmup is cached
- Model loading is the main bottleneck
### Out of memory
README.md+41-103