# LiquidFlow: Liquid Neural Network ร— Mamba-2 SSD Image Generator **A lightweight, physics-informed image generator combining Liquid Neural Networks (CfC) with Mamba-2 State Space Duality โ€” trainable on Google Colab free tier, deployable on mobile devices.** [![Model on HF](https://img.shields.io/badge/๐Ÿค—-LiquidFlow--Gen-blue)](https://huggingface.co/krystv/LiquidFlow-Gen) [![Paper: CfC](https://img.shields.io/badge/๐Ÿ“„-CfC_Nature_MI_2022-green)](https://arxiv.org/abs/2106.13898) [![Paper: Mamba-2](https://img.shields.io/badge/๐Ÿ“„-Mamba2_2024-orange)](https://arxiv.org/abs/2405.21060) [![Paper: PINN Diffusion](https://img.shields.io/badge/๐Ÿ“„-PINN_Diff_ICLR_2025-red)](https://arxiv.org/abs/2403.14404) ## Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Image [128ร—128] โ†’ โ”‚ TAESD VAE โ”‚ โ†’ Latent [16ร—16ร—4] โ”‚ (< 1M params) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ LiquidFlow Backbone โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ LiquidMamba Block (ร—N) โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Input โ†’ CfC Gate โ”‚ โ”‚ โ”‚ โ”‚ โ†“ โ”‚ โ”‚ โ”‚ โ”‚ Mamba-2 SSD โ”‚ โ”‚ โ”‚ โ”‚ (multi-dir scan) โ”‚ โ”‚ โ”‚ โ”‚ โ†“ โ”‚ โ”‚ โ”‚ โ”‚ CfC Gate โ†’ Output โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ + Physics-Informed Loss โ”‚ โ”‚ (TV + Spectral + Gradient) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ Predicted Noise ``` ### Core Innovations 1. **CfC (Closed-form Continuous-time) Liquid Neural Networks** - `h(t) = ฯƒ(-f(x,I)ยทt) โŠ™ g(x,I) + (1-ฯƒ(-f(x,I)ยทt)) โŠ™ h(x,I)` - No ODE solving โ€” 100ร— faster than Neural ODEs - Time-continuous adaptive gating mechanism - From: Hasani et al., Nature Machine Intelligence (2022) 2. **Mamba-2 SSD (State Space Duality)** - `h_t = A_tยทh_{t-1} + B_tยทx_t`, `y_t = C_t^Tยทh_t` - O(N) linear complexity (vs O(Nยฒ) attention) - Fully parallelizable via associative scan - Pure PyTorch โ€” no CUDA kernels needed - From: Dao & Gu, "Transformers are SSMs" (2024) 3. **Physics-Informed Regularization** - Total Variation + Spectral + Gradient constraints - Training-only regularizer โ€” zero inference cost - Pattern from: Bastek & Sun, ICLR 2025 4. **TAESD VAE** - < 1M parameters โ€” 84ร— smaller than SD VAE - Near-instant encoding/decoding - From: madebyollin/taesd ## Model Variants | Variant | Parameters | Hidden Dim | Stages | Blocks/Stage | T4 VRAM | |---------|-----------|------------|--------|--------------|---------| | **Tiny** | ~2M | 128 | 2 | 2 | < 2 GB | | **Small** | ~8M | 256 | 4 | 4 | ~4 GB | | **Base** | ~30M | 384 | 6 | 6 | ~8 GB | ## Quick Start ### Google Colab [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/krystv/LiquidFlow-Gen/blob/main/LiquidFlow_Colab.ipynb) 1. Open the notebook 2. Runtime โ†’ Change runtime type โ†’ **GPU (T4)** 3. Run all cells ### Local Training ```bash # Clone git clone https://huggingface.co/krystv/LiquidFlow-Gen cd LiquidFlow-Gen # Install pip install torch torchvision diffusers tqdm pillow numpy # Train (small model, 128px, CIFAR-10) python train.py \ --dataset cifar10 \ --image_size 128 \ --variant small \ --batch_size 32 \ --epochs 100 \ --lr 2e-4 # Train (base model, 512px) python train.py \ --dataset cifar10 \ --image_size 512 \ --variant base \ --batch_size 8 \ --epochs 200 \ --lr 1e-4 ``` ### Generate Samples ```python from liquid_flow.generator import create_liquidflow from liquid_flow.vae_wrapper import TAESDWrapper # Load model model = create_liquidflow(variant='small', image_size=128) model.load_state_dict(torch.load('best_model.pt')) model = model.cuda().eval() # Load VAE vae = TAESDWrapper.load('cuda') # Generate latents = model.sample(batch_size=16, steps=50, ddim=True) images = TAESDWrapper.decode(vae, latents) ``` ## Training Details ### Default Hyperparameters - Optimizer: AdamW (ฮฒโ‚=0.9, ฮฒโ‚‚=0.999) - LR: 2ร—10โปโด (tiny/small), 1ร—10โปโด (base) - Weight Decay: 10โปโด - LR Schedule: Cosine annealing - Gradient Clipping: 1.0 - AMP: Enabled (when CUDA available) ### Physics Regularization Weights - TV (Total Variation): 0.01 - Conservation of Intensity: 0.001 - Spectral Regularizer: 0.01 - Gradient Penalty: 0.001 ### Datasets Supported - CIFAR-10, CIFAR-100, STL-10 - CelebA, LSUN (requires download) - ImageNet (provide path) ## Mobile Deployment LiquidFlow uses pure PyTorch โ€” **no custom CUDA kernels**: ```python # Export to ONNX torch.onnx.export(model, (x, t), 'liquidflow.onnx', input_names=['noisy_latent', 'timestep'], output_names=['predicted_noise'], opset_version=14) # Convert to CoreML (iOS) # coremltools.converters.onnx.convert(model='liquidflow.onnx') # Convert to TFLite (Android) # onnx-tf convert -i liquidflow.onnx -o liquidflow.pb ``` ## Why This Works (Research Validation) ### DiMSUM (NeurIPS 2024) Mamba-based diffusion beats DiT transformers on ImageNet generation (FID 2.11 vs 2.27). Mamba's O(N) complexity enables 3ร— faster convergence than attention-based models. ### PINNMamba (ICML 2025) SSM + Physics constraints are compatible and synergistic. Mamba's selective scan naturally handles the spatio-temporal nature of PDE residuals. ### LiteVAE / TAESD Wavelet-based and tiny VAEs provide sufficient latent quality for diffusion at < 1% of the parameter count of standard VAEs. TAESD is used by 100+ real-time diffusion demos on HF Spaces. ### DeepSeek V3 Insights - Auxiliary-loss-free training (apply to physics weights) - Multi-head architecture for efficiency - DualPipe for overlapping computation ## Repository Structure ``` LiquidFlow-Gen/ โ”œโ”€โ”€ liquid_flow/ โ”‚ โ”œโ”€โ”€ __init__.py # Package init โ”‚ โ”œโ”€โ”€ cfc_cell.py # CfC Liquid NN implementation โ”‚ โ”œโ”€โ”€ mamba2_ssd.py # Mamba-2 SSD implementation โ”‚ โ”œโ”€โ”€ liquid_flow_block.py # Hybrid CfC+Mamba block โ”‚ โ”œโ”€โ”€ generator.py # Full diffusion generator โ”‚ โ”œโ”€โ”€ vae_wrapper.py # VAE interfaces โ”‚ โ”œโ”€โ”€ physics_loss.py # Physics regularizers โ”‚ โ””โ”€โ”€ trainer.py # Training utilities โ”œโ”€โ”€ train.py # CLI training script โ”œโ”€โ”€ LiquidFlow_Colab.ipynb # Colab notebook โ””โ”€โ”€ README.md # This file ``` ## Citations ```bibtex @article{hasani2022cfc, title={Closed-form continuous-time neural networks}, author={Hasani, Ramin and Lechner, Mathias and Amini, Alexander and others}, journal={Nature Machine Intelligence}, year={2022} } @article{dao2024mamba2, title={Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality}, author={Dao, Tri and Gu, Albert}, journal={arXiv:2405.21060}, year={2024} } @inproceedings{bastek2025physics, title={Physics-Informed Diffusion Models}, author={Bastek, Jan-Hendrik and Sun, WaiChing}, booktitle={ICLR}, year={2025} } @article{pham2024dimsum, title={DiMSUM: Diffusion Mamba -- A Scalable and Unified Spatial-Frequency Method for Image Generation}, author={Pham, Hao and others}, journal={NeurIPS}, year={2024} } ``` ## License MIT