Update README with Colab-optimized training workflow and dataset presets
Browse files
README.md
CHANGED
|
@@ -4,10 +4,31 @@
|
|
| 4 |
|
| 5 |
LiquidGen replaces self-attention in diffusion models with **Closed-form Continuous-depth (CfC)** liquid dynamics β making it fully parallelizable, memory-efficient, and trainable on a single consumer GPU (Colab free tier T4).
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
## ποΈ Architecture
|
| 8 |
|
| 9 |
```
|
| 10 |
-
Input Image β Flux VAE Encoder β Noisy Latent β LiquidGen Backbone β Predicted Velocity β Euler ODE β
|
| 11 |
```
|
| 12 |
|
| 13 |
### Key Components
|
|
@@ -23,12 +44,11 @@ Input Image β Flux VAE Encoder β Noisy Latent β LiquidGen Backbone β Pre
|
|
| 23 |
### Core Innovation: Liquid Time Constants
|
| 24 |
|
| 25 |
From the CfC paper (Hasani et al., Nature Machine Intelligence 2022):
|
| 26 |
-
|
| 27 |
```
|
| 28 |
x_{t+1} = exp(-Ξt/Ο_t) Β· x_t + (1 - exp(-Ξt/Ο_t)) Β· h(x_t, u_t)
|
| 29 |
```
|
| 30 |
|
| 31 |
-
Our parallelizable version:
|
| 32 |
```python
|
| 33 |
Ξ± = exp(-softplus(Ο)) # Per-channel learnable retention
|
| 34 |
output = Ξ± * state + (1 - Ξ±) * stimulus # Exponential relaxation
|
|
@@ -44,57 +64,31 @@ output = Ξ± * state + (1 - Ξ±) * stimulus # Exponential relaxation
|
|
| 44 |
| **LiquidGen-B** | ~140M | ~8-10 GB | 256/512px, balanced |
|
| 45 |
| **LiquidGen-L** | ~280M | ~12-14 GB | 512px, high quality |
|
| 46 |
|
| 47 |
-
All
|
| 48 |
-
|
| 49 |
-
## π Quick Start
|
| 50 |
-
|
| 51 |
-
### Using the Colab Notebook
|
| 52 |
-
Open `LiquidGen_Colab_Notebook.ipynb` in Google Colab and follow the steps. It includes:
|
| 53 |
-
- Complete model code (no external dependencies beyond PyTorch + diffusers)
|
| 54 |
-
- Configurable training on WikiArt dataset (artistic paintings)
|
| 55 |
-
- Support for 256px and 512px generation
|
| 56 |
-
- Class-conditional generation (27 art styles)
|
| 57 |
-
- Loss plotting and sample visualization
|
| 58 |
-
|
| 59 |
-
### Using the Python Scripts
|
| 60 |
-
|
| 61 |
-
```python
|
| 62 |
-
from model import liquidgen_base
|
| 63 |
-
import torch
|
| 64 |
-
|
| 65 |
-
# Create model
|
| 66 |
-
model = liquidgen_base(num_classes=27).cuda()
|
| 67 |
-
print(f"Parameters: {model.count_params()/1e6:.1f}M")
|
| 68 |
-
|
| 69 |
-
# Forward pass (predict velocity for flow matching)
|
| 70 |
-
x = torch.randn(4, 16, 32, 32).cuda() # 256px latent
|
| 71 |
-
t = torch.rand(4).cuda() # Timesteps
|
| 72 |
-
labels = torch.randint(0, 27, (4,)).cuda()
|
| 73 |
-
v = model(x, t, labels) # Predicted velocity
|
| 74 |
-
```
|
| 75 |
|
| 76 |
## π§ Training
|
| 77 |
|
| 78 |
-
### Default Configuration
|
| 79 |
```python
|
| 80 |
from train import TrainConfig, train
|
| 81 |
|
| 82 |
config = TrainConfig(
|
| 83 |
-
model_size="
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
batch_size=8,
|
| 89 |
-
gradient_accumulation_steps=4,
|
| 90 |
learning_rate=1e-4,
|
| 91 |
-
num_epochs=50,
|
| 92 |
)
|
| 93 |
train(config)
|
| 94 |
```
|
| 95 |
|
| 96 |
-
### Training
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
- **Objective**: Flow matching (velocity prediction) β `v = noise - x_0`
|
| 99 |
- **Optimizer**: AdamW (lr=1e-4, weight_decay=0.01)
|
| 100 |
- **Gradient clipping**: 2.0 (critical for stability, from ZigMa paper)
|
|
@@ -104,34 +98,12 @@ train(config)
|
|
| 104 |
## π Files
|
| 105 |
|
| 106 |
```
|
| 107 |
-
βββ model.py
|
| 108 |
-
βββ train.py
|
| 109 |
βββ LiquidGen_Colab_Notebook.ipynb # Ready-to-run Colab notebook
|
| 110 |
-
βββ README.md
|
| 111 |
```
|
| 112 |
|
| 113 |
-
## π¬ Research Background
|
| 114 |
-
|
| 115 |
-
This architecture synthesizes ideas from multiple research lineages:
|
| 116 |
-
|
| 117 |
-
### Liquid Neural Networks
|
| 118 |
-
- **Liquid Time-constant Networks** (Hasani et al., NeurIPS 2020) β ODE-based neurons with input-dependent Ο
|
| 119 |
-
- **Closed-form Continuous-depth Models** (Hasani et al., Nature Machine Intelligence 2022) β Analytical solution eliminating ODE solvers
|
| 120 |
-
- **Neural Circuit Policies** (Lechner et al., Nature Machine Intelligence 2020) β Sparse wiring: sensoryβinterβcommandβmotor
|
| 121 |
-
|
| 122 |
-
### Attention-Free Image Generation
|
| 123 |
-
- **ZigMa** (ECCV 2024) β Zigzag scanning for SSM-based diffusion (FID 14.27 CelebA-256)
|
| 124 |
-
- **DiMSUM** (NeurIPS 2024) β Spatial-frequency Mamba (FID 2.11 ImageNet 256)
|
| 125 |
-
- **DiffuSSM** (2023) β First attention-free diffusion model (FID 2.28 ImageNet 256)
|
| 126 |
-
- **DiM** (2024) β Multi-directional Mamba with padding tokens
|
| 127 |
-
|
| 128 |
-
### Parallelization
|
| 129 |
-
- **LiquidTAD** (2025) β Static decay Ξ±=exp(-softplus(Ο)) for fully parallel liquid dynamics (100Γ speedup vs ODE)
|
| 130 |
-
|
| 131 |
-
### Flow Matching
|
| 132 |
-
- **Flow Matching for Generative Modeling** (Lipman et al., 2023)
|
| 133 |
-
- **SiT** (2024) β Scalable Interpolant Transformers
|
| 134 |
-
|
| 135 |
## π Architecture Diagram
|
| 136 |
|
| 137 |
```
|
|
@@ -144,34 +116,44 @@ Input Latent [B, 16, H/8, W/8]
|
|
| 144 |
ββββ LiquidBlock Γ (depth/2) βββ save skip connections
|
| 145 |
β βββ AdaGN (timestep conditioned)
|
| 146 |
β βββ GatedDepthwiseStimulusConv (local spatial)
|
| 147 |
-
β βββ + ZigzagScan1D (global context)
|
| 148 |
β βββ LiquidTimeConstant #1 (CfC blend)
|
| 149 |
-
β βββ AdaGN
|
| 150 |
β βββ ChannelMixMLP (GELU)
|
| 151 |
β βββ LiquidTimeConstant #2 (CfC blend)
|
| 152 |
β
|
| 153 |
ββββ LiquidBlock Γ (depth/2) βββ add skip connections
|
| 154 |
-
β βββ (same structure as above)
|
| 155 |
β
|
| 156 |
ββββ GroupNorm + Conv + GELU
|
| 157 |
ββββ Unpatchify (ConvTranspose2d) βββ [B, 16, H/8, W/8]
|
| 158 |
```
|
| 159 |
|
| 160 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
|
| 168 |
## π License
|
| 169 |
|
| 170 |
MIT
|
| 171 |
-
|
| 172 |
-
## π Acknowledgments
|
| 173 |
-
|
| 174 |
-
- MIT CSAIL for Liquid Neural Networks research
|
| 175 |
-
- Black Forest Labs for FLUX.1-schnell VAE (Apache 2.0)
|
| 176 |
-
- WikiArt dataset contributors
|
| 177 |
-
- ZigMa, DiMSUM, DiffuSSM, DiM authors for attention-free diffusion insights
|
|
|
|
| 4 |
|
| 5 |
LiquidGen replaces self-attention in diffusion models with **Closed-form Continuous-depth (CfC)** liquid dynamics β making it fully parallelizable, memory-efficient, and trainable on a single consumer GPU (Colab free tier T4).
|
| 6 |
|
| 7 |
+
## π Quick Start (Colab)
|
| 8 |
+
|
| 9 |
+
1. Open `LiquidGen_Colab_Notebook.ipynb` in Google Colab
|
| 10 |
+
2. Select a dataset preset (see table below)
|
| 11 |
+
3. Run all cells β latents are pre-cached automatically, then training starts
|
| 12 |
+
|
| 13 |
+
**Training is optimized for Colab free tier:**
|
| 14 |
+
- **Latent pre-caching**: Encode all images with VAE once β save to disk β train on pure tensors
|
| 15 |
+
- **No VAE during training** β saves ~1GB VRAM, enables larger batches (32+)
|
| 16 |
+
- **Small curated datasets** that download in seconds (not 5GB WikiArt!)
|
| 17 |
+
|
| 18 |
+
### Dataset Presets
|
| 19 |
+
|
| 20 |
+
| Preset | Images | Download | Classes | Description |
|
| 21 |
+
|--------|--------|----------|---------|-------------|
|
| 22 |
+
| `paintings_mini` | ~200 | 1.7MB | 27 styles | Instant smoke test |
|
| 23 |
+
| `paintings` | ~8K | 204MB | 27 styles | **Recommended** β best quality/speed tradeoff |
|
| 24 |
+
| `cartoon` | ~2.5K | 181MB | unconditional | Cartoon/anime images |
|
| 25 |
+
| `flowers` | ~8K | 331MB | unconditional | Flower photography |
|
| 26 |
+
| `wikiart_stream` | ~80K | streaming | 27 styles | Full WikiArt via streaming (set `max_images`) |
|
| 27 |
+
|
| 28 |
## ποΈ Architecture
|
| 29 |
|
| 30 |
```
|
| 31 |
+
Input Image β Flux VAE Encoder β Noisy Latent β LiquidGen Backbone β Predicted Velocity β Euler ODE β VAE Decoder β Output
|
| 32 |
```
|
| 33 |
|
| 34 |
### Key Components
|
|
|
|
| 44 |
### Core Innovation: Liquid Time Constants
|
| 45 |
|
| 46 |
From the CfC paper (Hasani et al., Nature Machine Intelligence 2022):
|
|
|
|
| 47 |
```
|
| 48 |
x_{t+1} = exp(-Ξt/Ο_t) Β· x_t + (1 - exp(-Ξt/Ο_t)) Β· h(x_t, u_t)
|
| 49 |
```
|
| 50 |
|
| 51 |
+
Our parallelizable version (inspired by LiquidTAD 2025):
|
| 52 |
```python
|
| 53 |
Ξ± = exp(-softplus(Ο)) # Per-channel learnable retention
|
| 54 |
output = Ξ± * state + (1 - Ξ±) * stimulus # Exponential relaxation
|
|
|
|
| 64 |
| **LiquidGen-B** | ~140M | ~8-10 GB | 256/512px, balanced |
|
| 65 |
| **LiquidGen-L** | ~280M | ~12-14 GB | 512px, high quality |
|
| 66 |
|
| 67 |
+
All fit in **16GB VRAM** (Colab free T4). Training on cached latents = no VAE overhead.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
## π§ Training
|
| 70 |
|
|
|
|
| 71 |
```python
|
| 72 |
from train import TrainConfig, train
|
| 73 |
|
| 74 |
config = TrainConfig(
|
| 75 |
+
model_size="small",
|
| 76 |
+
dataset_preset="paintings", # 8K paintings, 204MB, 27 styles
|
| 77 |
+
image_size=256,
|
| 78 |
+
batch_size=32, # Large batches OK with cached latents!
|
| 79 |
+
num_epochs=100,
|
|
|
|
|
|
|
| 80 |
learning_rate=1e-4,
|
|
|
|
| 81 |
)
|
| 82 |
train(config)
|
| 83 |
```
|
| 84 |
|
| 85 |
+
### Training Pipeline
|
| 86 |
+
1. **Pre-cache**: Load dataset β encode all images with frozen Flux VAE β save latents to disk β unload VAE
|
| 87 |
+
2. **Train**: Load cached tensors β train LiquidGen backbone with flow matching β fast iterations!
|
| 88 |
+
3. **Sample**: Load VAE only when generating sample images (lazy loading)
|
| 89 |
+
|
| 90 |
+
### Details
|
| 91 |
+
- **VAE**: FLUX.1-schnell (frozen, 16ch latent, 8x compression, Apache 2.0)
|
| 92 |
- **Objective**: Flow matching (velocity prediction) β `v = noise - x_0`
|
| 93 |
- **Optimizer**: AdamW (lr=1e-4, weight_decay=0.01)
|
| 94 |
- **Gradient clipping**: 2.0 (critical for stability, from ZigMa paper)
|
|
|
|
| 98 |
## π Files
|
| 99 |
|
| 100 |
```
|
| 101 |
+
βββ model.py # LiquidGen model architecture (~55-280M params)
|
| 102 |
+
βββ train.py # Training pipeline with latent pre-caching
|
| 103 |
βββ LiquidGen_Colab_Notebook.ipynb # Ready-to-run Colab notebook
|
| 104 |
+
βββ README.md
|
| 105 |
```
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
## π Architecture Diagram
|
| 108 |
|
| 109 |
```
|
|
|
|
| 116 |
ββββ LiquidBlock Γ (depth/2) βββ save skip connections
|
| 117 |
β βββ AdaGN (timestep conditioned)
|
| 118 |
β βββ GatedDepthwiseStimulusConv (local spatial)
|
| 119 |
+
β βββ + ZigzagScan1D (global context)
|
| 120 |
β βββ LiquidTimeConstant #1 (CfC blend)
|
| 121 |
+
β βββ AdaGN
|
| 122 |
β βββ ChannelMixMLP (GELU)
|
| 123 |
β βββ LiquidTimeConstant #2 (CfC blend)
|
| 124 |
β
|
| 125 |
ββββ LiquidBlock Γ (depth/2) βββ add skip connections
|
|
|
|
| 126 |
β
|
| 127 |
ββββ GroupNorm + Conv + GELU
|
| 128 |
ββββ Unpatchify (ConvTranspose2d) βββ [B, 16, H/8, W/8]
|
| 129 |
```
|
| 130 |
|
| 131 |
+
## π¬ Research Background
|
| 132 |
+
|
| 133 |
+
### Liquid Neural Networks
|
| 134 |
+
- **Liquid Time-constant Networks** (Hasani et al., NeurIPS 2020) β ODE-based neurons with input-dependent Ο
|
| 135 |
+
- **Closed-form Continuous-depth Models** (Hasani et al., Nature Machine Intelligence 2022) β Analytical solution eliminating ODE solvers
|
| 136 |
+
- **Neural Circuit Policies** (Lechner et al., Nature Machine Intelligence 2020) β Sparse wiring: sensoryβinterβcommandβmotor
|
| 137 |
+
- **LiquidTAD** (2025) β Static decay Ξ±=exp(-softplus(Ο)) for fully parallel liquid dynamics (100Γ speedup)
|
| 138 |
+
|
| 139 |
+
### Attention-Free Image Generation
|
| 140 |
+
- **ZigMa** (ECCV 2024) β Zigzag scanning for SSM-based diffusion
|
| 141 |
+
- **DiMSUM** (NeurIPS 2024) β Spatial-frequency Mamba (FID 2.11 ImageNet 256)
|
| 142 |
+
- **DiffuSSM** (2023) β First attention-free diffusion model
|
| 143 |
+
- **DiM** (2024) β Multi-directional Mamba with padding tokens
|
| 144 |
+
|
| 145 |
+
### Flow Matching
|
| 146 |
+
- **Flow Matching for Generative Modeling** (Lipman et al., 2023)
|
| 147 |
+
- **SiT** (2024) β Scalable Interpolant Transformers
|
| 148 |
|
| 149 |
+
## β‘ Design Decisions
|
| 150 |
+
|
| 151 |
+
1. **No Attention** β O(n) complexity. Liquid dynamics + zigzag conv replace self-attention entirely.
|
| 152 |
+
2. **Liquid over Residual** β `Ξ±Β·x + (1-Ξ±)Β·f(x)` instead of `x + f(x)`. Explicit control over retention per channel.
|
| 153 |
+
3. **Zigzag Scanning** β Preserves spatial continuity at row boundaries (critical insight from ZigMa).
|
| 154 |
+
4. **Latent Pre-caching** β Encode once, train forever. No VAE overhead during training.
|
| 155 |
+
5. **Flow Matching** β Straighter ODE trajectories β fewer sampling steps, better quality.
|
| 156 |
|
| 157 |
## π License
|
| 158 |
|
| 159 |
MIT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|