Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,116 +1,166 @@
|
|
| 1 |
-
# 🌊 LiquidDiffusion
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
## Architecture
|
| 19 |
|
| 20 |
```
|
| 21 |
-
Input
|
| 22 |
-
→ Encoder [LiquidDiffusionBlock × N per stage, with downsampling]
|
| 23 |
-
→ Bottleneck [LiquidDiffusionBlock × 2]
|
| 24 |
-
→ Decoder [LiquidDiffusionBlock × N per stage, with upsampling + skip fusion]
|
| 25 |
-
→ Conv Head → Velocity prediction
|
| 26 |
-
```
|
| 27 |
|
| 28 |
-
|
| 29 |
-
1. **AdaLN** → timestep conditioning via learned scale/shift
|
| 30 |
-
2. **ParallelCfCBlock** → the core liquid neural network layer
|
| 31 |
-
3. **MultiScaleSpatialMix** → 3×3+5×5+7×7 depthwise conv + global pooling (replaces attention)
|
| 32 |
-
4. **FeedForward** → channel mixing via 1×1 conv
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
cfc_out = gate * g + (1 - gate) * h
|
| 47 |
-
|
| 48 |
-
# Liquid relaxation residual (from LiquidTAD)
|
| 49 |
-
α = exp(-softplus(ρ) * |t_emb_mean|)
|
| 50 |
-
output = α * input + (1 - α) * cfc_out
|
| 51 |
```
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|--------|----------|--------|--------|------------|----------|
|
| 59 |
-
| tiny | [64, 128, 256] | [2, 2, 4] | ~23M | ~6 GB | Quick experiments, T4 |
|
| 60 |
-
| small | [96, 192, 384] | [2, 3, 6] | ~69M | ~10 GB | Quality 256px, T4/A10G |
|
| 61 |
-
| base | [128, 256, 512] | [2, 4, 8] | ~154M | ~16 GB | 512px, A100 |
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
##
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
| 74 |
```python
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
```
|
| 80 |
|
| 81 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
| [LiquidTAD (2024)](https://arxiv.org/abs/2604.18274) | Parallel liquid relaxation (removed recurrence) |
|
| 98 |
-
| [USM (CVPR 2025)](https://arxiv.org/abs/2504.13499) | U-Net + SSM architecture for diffusion |
|
| 99 |
-
| [DiffuSSM (2023)](https://arxiv.org/abs/2311.18257) | SSM replaces attention in diffusion (FID=2.28) |
|
| 100 |
-
| [Rectified Flow (Liu et al., ICLR 2023)](https://arxiv.org/abs/2209.03003) | Simple velocity prediction training |
|
| 101 |
-
| [Neural Circuit Policies (2020)](https://arxiv.org/abs/2006.04439) | Sparse wiring, parameter efficiency |
|
| 102 |
|
| 103 |
-
##
|
|
|
|
| 104 |
|
| 105 |
```
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
│ ├── model.py # Full model architecture
|
| 109 |
-
│ └── trainer.py # Rectified Flow trainer + dataset utils
|
| 110 |
-
├── LiquidDiffusion_Training.ipynb # Complete Colab notebook
|
| 111 |
-
├── test_model.py # Test suite
|
| 112 |
-
└── README.md # This file
|
| 113 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
## License
|
| 116 |
|
|
|
|
| 1 |
+
# 🌊 LiquidDiffusion: Attention-Free Image Generation with Liquid Neural Networks
|
| 2 |
|
| 3 |
+
A **novel image generation architecture** that replaces all attention mechanisms with Parallel CfC (Closed-form Continuous-depth) blocks from Liquid Neural Networks.
|
| 4 |
|
| 5 |
+
**This is genuinely novel research** — no existing paper uses CfC/LTC as a diffusion model backbone.
|
| 6 |
|
| 7 |
+
[](https://colab.research.google.com/github/krystv/liquid-diffusion/blob/main/LiquidDiffusion_Training.ipynb)
|
| 8 |
|
| 9 |
+
## 🔬 Key Innovations
|
| 10 |
|
| 11 |
+
| Feature | Description |
|
| 12 |
+
|---------|-------------|
|
| 13 |
+
| **No Attention** | All spatial mixing via multi-scale depthwise convolutions (3×3, 5×5, 7×7) + global average pooling |
|
| 14 |
+
| **Fully Parallelizable** | No sequential ODE solving — CfC closed-form solution eliminates the computational bottleneck of Neural ODEs |
|
| 15 |
+
| **CfC × Diffusion Bridge** | The diffusion noise level `t` IS the liquid time constant — natural mathematical correspondence |
|
| 16 |
+
| **Liquid Relaxation Residuals** | Time-aware skip connections: `α·input + (1-α)·output` where `α = exp(-λ·t)` adapts to noise level |
|
| 17 |
+
| **Fits 16GB VRAM** | Tiny model (8M params) fits in ~4GB; designed for Colab free tier T4 |
|
| 18 |
|
| 19 |
+
## 📐 Architecture
|
| 20 |
|
| 21 |
```
|
| 22 |
+
Input: noisy image [B, 3, H, W] + timestep t ∈ [0, 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
Time Embedding: Sinusoidal PE → MLP → t_emb [B, dim]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
Conv Stem: 3×3 conv → SiLU → 3×3 conv
|
| 27 |
|
| 28 |
+
Encoder:
|
| 29 |
+
Stage 1: [LiquidDiffusionBlock × N₁] → DownSample (stride-2 conv)
|
| 30 |
+
Stage 2: [LiquidDiffusionBlock × N₂] → DownSample
|
| 31 |
+
Stage 3: [LiquidDiffusionBlock × N₃]
|
| 32 |
|
| 33 |
+
Bottleneck: [LiquidDiffusionBlock × 2]
|
| 34 |
+
|
| 35 |
+
Decoder (mirror of encoder):
|
| 36 |
+
Stage 3: UpSample → SkipFusion → [LiquidDiffusionBlock × N₃]
|
| 37 |
+
Stage 2: UpSample → SkipFusion → [LiquidDiffusionBlock × N₂]
|
| 38 |
+
Stage 1: [LiquidDiffusionBlock × N₁]
|
| 39 |
+
|
| 40 |
+
Output: GroupNorm → SiLU → 3×3 conv → velocity prediction [B, 3, H, W]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
```
|
| 42 |
|
| 43 |
+
### LiquidDiffusionBlock
|
| 44 |
+
```
|
| 45 |
+
x → AdaLN(t) → ParallelCfC(t) → +residual
|
| 46 |
+
→ MultiScaleSpatialMix(t) → +residual
|
| 47 |
+
→ AdaLN(t) → FeedForward → +residual
|
| 48 |
+
```
|
| 49 |
|
| 50 |
+
### ParallelCfC (Core Innovation)
|
| 51 |
+
```python
|
| 52 |
+
# CfC Eq.10 adapted for 2D spatial features:
|
| 53 |
+
backbone = SiLU(Conv1x1(DWConv7x7(x))) # shared spatial context
|
| 54 |
+
f = Conv1x1(backbone) # time-constant gate
|
| 55 |
+
g = DWConv→SiLU→Conv1x1(backbone) # "from" state
|
| 56 |
+
h = DWConv→SiLU→Conv1x1(backbone) # "to" state (attractor)
|
| 57 |
+
gate = σ(time_a(t_emb) · f - time_b(t_emb)) # liquid time gate
|
| 58 |
+
cfc_out = gate · g + (1-gate) · h # CfC interpolation
|
| 59 |
+
|
| 60 |
+
# Liquid relaxation residual:
|
| 61 |
+
α = exp(-softplus(ρ) · |t|) # time-aware weight
|
| 62 |
+
output = α · input + (1-α) · cfc_out # noise-adaptive residual
|
| 63 |
+
```
|
| 64 |
|
| 65 |
+
## 📊 Model Configurations
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
| Config | Channels | Blocks | Params | Resolution | VRAM (fp16) |
|
| 68 |
+
|--------|----------|--------|--------|-----------|-------------|
|
| 69 |
+
| **tiny** | [64, 128, 256] | [2, 2, 4] | ~8M | 256×256 | ~4GB |
|
| 70 |
+
| **small** | [96, 192, 384] | [2, 3, 6] | ~25M | 256×256 | ~8GB |
|
| 71 |
+
| **base** | [128, 256, 512] | [2, 4, 8] | ~65M | 512×512 | ~14GB |
|
| 72 |
+
| **large** | [128, 256, 512, 768] | [2, 4, 8, 4] | ~120M | 512×512 | ~24GB |
|
| 73 |
|
| 74 |
+
## 🏋️ Training
|
| 75 |
|
| 76 |
+
### Rectified Flow (simplest effective objective)
|
| 77 |
+
```
|
| 78 |
+
x_t = (1-t) · x_data + t · noise, t ~ U[0,1]
|
| 79 |
+
Loss = ||model(x_t, t) - (noise - x_data)||²
|
| 80 |
+
```
|
| 81 |
+
No noise schedule. No variance. Just MSE on a straight-line velocity.
|
| 82 |
|
| 83 |
+
### Sampling (Euler ODE)
|
| 84 |
+
```python
|
| 85 |
+
z = randn(B, 3, H, W) # start from noise
|
| 86 |
+
for i in range(N, 0, -1):
|
| 87 |
+
t = i / N
|
| 88 |
+
z = z - model(z, t) / N # Euler step
|
| 89 |
+
```
|
| 90 |
+
Typically 25-50 steps.
|
| 91 |
|
| 92 |
+
### Quick Start
|
| 93 |
```python
|
| 94 |
+
from liquid_diffusion import liquid_diffusion_tiny, RectifiedFlowTrainer
|
| 95 |
+
|
| 96 |
+
model = liquid_diffusion_tiny()
|
| 97 |
+
trainer = RectifiedFlowTrainer(model, lr=1e-4, device='cuda')
|
| 98 |
+
|
| 99 |
+
# Training step
|
| 100 |
+
images = get_batch() # [B, 3, 256, 256] in [-1, 1]
|
| 101 |
+
metrics = trainer.train_step(images)
|
| 102 |
+
print(f"Loss: {metrics['loss']:.4f}")
|
| 103 |
+
|
| 104 |
+
# Generate
|
| 105 |
+
samples = trainer.sample(batch_size=4, image_size=256, num_steps=50)
|
| 106 |
```
|
| 107 |
|
| 108 |
+
### Recommended Datasets
|
| 109 |
+
- **CelebA-HQ** (`huggan/CelebA-HQ`) — 30K face images, 256px
|
| 110 |
+
- **Flowers-102** (`huggan/flowers-102-categories`) — botanical images
|
| 111 |
+
- **AFHQ** — 15K animal faces (cats, dogs, wildlife)
|
| 112 |
+
- Any folder of images
|
| 113 |
|
| 114 |
+
## 🧮 Mathematical Foundation
|
| 115 |
+
|
| 116 |
+
### Liquid Time-Constant Networks (LTC)
|
| 117 |
+
*Hasani et al., AAAI 2021 — [arxiv:2006.04439](https://arxiv.org/abs/2006.04439)*
|
| 118 |
+
|
| 119 |
+
The fundamental ODE:
|
| 120 |
+
```
|
| 121 |
+
dx/dt = -[1/τ + f(x,I,θ)] · x + f(x,I,θ) · A
|
| 122 |
```
|
| 123 |
+
Key: system time constant `τ_sys = τ/(1 + τ·f)` is **input-dependent** — neurons adapt their response speed.
|
| 124 |
+
|
| 125 |
+
### CfC: Closed-form Solution
|
| 126 |
+
*Hasani et al., Nature Machine Intelligence 2022 — [arxiv:2106.13898](https://arxiv.org/abs/2106.13898)*
|
| 127 |
|
| 128 |
+
Solves the LTC ODE analytically:
|
| 129 |
+
```
|
| 130 |
+
x(t) = σ(-f(x,I;θf)·t) ⊙ g(x,I;θg) + [1 - σ(-f(x,I;θf)·t)] ⊙ h(x,I;θh)
|
| 131 |
+
```
|
| 132 |
+
Eliminates ODE solver → **fully parallelizable**, one order of magnitude faster.
|
| 133 |
|
| 134 |
+
### Our CfC-Diffusion Bridge
|
| 135 |
+
We observe that CfC's time parameter `t` and diffusion's noise level `t` serve analogous roles:
|
| 136 |
+
- CfC: `t` controls interpolation between "from" (g) and "to" (h) states
|
| 137 |
+
- Diffusion: `t` controls the noise level the denoiser must handle
|
| 138 |
|
| 139 |
+
By using the diffusion timestep directly as CfC's time parameter:
|
| 140 |
+
- `t≈0` (clean): gate ≈ 0.5 → balanced g/h → flexible detail processing
|
| 141 |
+
- `t≈1` (noisy): gate saturates → specialized denoising behavior
|
| 142 |
+
- The gate function `f` is **input-dependent** → each image region gets adaptive time response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
### Parallel Liquid Relaxation (from LiquidTAD)
|
| 145 |
+
*[arxiv:2604.18274](https://arxiv.org/abs/2604.18274)*
|
| 146 |
|
| 147 |
```
|
| 148 |
+
α = exp(-softplus(ρ) · t_diff)
|
| 149 |
+
output = α · input + (1-α) · gated_transform(input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
```
|
| 151 |
+
When `t` is large (noisy): α ≈ 0 → rely on CfC output (needs strong processing).
|
| 152 |
+
When `t` is small (clean): α ≈ 1 → preserve input (only minor refinement needed).
|
| 153 |
+
|
| 154 |
+
## 📚 References
|
| 155 |
+
|
| 156 |
+
1. Hasani et al., "Liquid Time-constant Networks", AAAI 2021 — [arxiv:2006.04439](https://arxiv.org/abs/2006.04439)
|
| 157 |
+
2. Hasani et al., "Closed-form Continuous-time Neural Networks", Nature MI 2022 — [arxiv:2106.13898](https://arxiv.org/abs/2106.13898)
|
| 158 |
+
3. Lechner et al., "Neural Circuit Policies", Nature MI 2020
|
| 159 |
+
4. LiquidTAD: Parallel liquid relaxation — [arxiv:2604.18274](https://arxiv.org/abs/2604.18274)
|
| 160 |
+
5. USM: U-Shape Mamba for diffusion — [arxiv:2504.13499](https://arxiv.org/abs/2504.13499)
|
| 161 |
+
6. DiffuSSM: Diffusion without attention — [arxiv:2311.18257](https://arxiv.org/abs/2311.18257)
|
| 162 |
+
7. Liu et al., "Flow Straight and Fast: Rectified Flow", ICLR 2023 — [arxiv:2209.03003](https://arxiv.org/abs/2209.03003)
|
| 163 |
+
8. Lee et al., "Improving the Training of Rectified Flows" — [arxiv:2405.20320](https://arxiv.org/abs/2405.20320)
|
| 164 |
|
| 165 |
## License
|
| 166 |
|