FlashAttention 2
FlashAttention 2 is a highly optimized attention implementation that significantly reduces GPU memory usage and improves inference speed.Installation
Install FlashAttention 2 with pip:Hardware Requirements
FlashAttention 2 requires specific hardware:- GPU Architecture: Ampere (A100, A10) or newer (H100, L40)
- Compute Capability: 8.0+ (Ampere) or 9.0+ (Hopper)
- Data Types: Only works with
torch.float16ortorch.bfloat16
Usage
Enable FlashAttention 2 when loading the model:Performance Impact
Batch Processing
Batch inference processes multiple requests simultaneously, dramatically improving throughput.Batch Generation Example
Batch Size Guidelines
Throughput Gains
Memory Optimization
Data Type Selection
Choose the right dtype based on your hardware and quality requirements:bfloat16 (Recommended)
- Compatible with FlashAttention 2
- Better numerical stability than float16
- Native support on modern GPUs (Ampere, Hopper)
- Minimal quality loss
- Requires Ampere or newer GPUs
float16
- Compatible with FlashAttention 2
- Wide GPU support (Pascal, Turing, Ampere, Hopper)
- Lower memory usage
- Less numerical stability than bfloat16
- Potential for overflow/underflow in some cases
float32
- Maximum numerical precision
- Universal GPU support
- 2x memory usage vs. float16/bfloat16
- Slower inference
- Cannot use FlashAttention 2
Memory Comparison
Memory usage shown is for model weights only. Add 2-4GB for activation memory during inference.
Gradient Checkpointing (Training)
For fine-tuning, gradient checkpointing trades computation for memory:- Standard training: ~12-16GB for 1.7B model
- With gradient accumulation (4 steps): Effective batch size 4x larger with same memory
GPU Selection
Choose the right GPU for your deployment:Consumer GPUs
Data Center GPUs
Cloud GPU Recommendations
Streaming vs Non-Streaming
Qwen3-TTS supports both streaming and non-streaming generation.Non-Streaming (Default)
Generates complete audio before returning:- Simpler to use
- Better for batch processing
- Easier error handling
- Higher latency (wait for complete generation)
- Not suitable for real-time interaction
Streaming
Outputs audio chunks as they’re generated: Key advantage: Qwen3-TTS features extreme low-latency streaming based on the Dual-Track hybrid streaming architecture:- First audio packet: Output immediately after a single character input
- End-to-end latency: As low as 97ms
- Real-time factor: Can generate audio faster than real-time playback
- Real-time conversation systems
- Interactive voice response (IVR)
- Live audiobook narration
- Low-latency TTS applications
Generation Parameters
Fine-tune generation quality and speed with these parameters:max_new_tokens
- Default: 2048 (recommended)
- Lower values: Faster, but may truncate long audio
- Higher values: Allow longer generation, but slower
Sampling Parameters
Pass Hugging Face Transformers sampling parameters:- temperature: 0.6-0.8 for natural variation, 0.3-0.5 for consistent output
- top_p: 0.9-0.95 for balanced quality
- top_k: 50 (default is usually good)
Evaluation Benchmark
Official evaluation was conducted with:- Seed-TTS test-zh: 0.77 WER (1.7B model)
- Seed-TTS test-en: 1.24 WER (1.7B model)
Performance Checklist
For optimal performance:- Install FlashAttention 2
- Use
dtype=torch.bfloat16(ortorch.float16) - Enable
attn_implementation="flash_attention_2" - Use batch processing when possible
- Select appropriate GPU with sufficient VRAM
- Tune batch size based on GPU memory
- Use streaming for low-latency applications
- Set
max_new_tokens=2048for standard use cases - Profile your specific workload to find optimal settings
Troubleshooting
Out of Memory (OOM)
- Reduce batch size
- Switch to smaller model (1.7B → 0.6B)
- Use bfloat16/float16 instead of float32
- Enable FlashAttention 2
- Free up GPU memory from other processes
Slow Generation
- Install FlashAttention 2
- Enable
attn_implementation="flash_attention_2" - Use bfloat16 instead of float32
- Increase batch size for throughput
- Consider vLLM-Omni for production workloads
Quality Issues
- Avoid too-low temperature (less than 0.3)
- Use bfloat16 for better stability
- Ensure input text is clean and well-formatted
- Check that language is correctly specified
Next Steps
- Deploy with vLLM integration for production
- Use DashScope API for managed cloud deployment
- Fine-tune models for domain-specific optimization