Skip to main content

LFM2 - Pytorch

Project description

LFM2 - Liquid Foundation Model 2 (Minimal Implementation)

PyPI version License: MIT

This is a minimal, open-source implementation of the Liquid Foundation Model 2 (LFM2) architecture described in Liquid AI's blog post. Since there is no official open-source implementation available, this repository provides a PyTorch implementation of the core architecture for research and educational purposes.

Features

  • Hybrid architecture combining short-range convolutions with grouped query attention
  • 16 blocks: 10 LIV convolution blocks + 6 GQA blocks
  • Double-gated short-range convolutions for efficient local processing
  • Grouped Query Attention (GQA) for efficient global attention
  • SwiGLU activation functions
  • RMSNorm normalization
  • Rotary Positional Embeddings (RoPE)

Model Sizes

The implementation supports three model sizes:

  • 350M parameters (768 hidden size)
  • 700M parameters (1024 hidden size)
  • 1.2B parameters (1536 hidden size)

Installation

pip install -r requirements.txt

Quick Start

import torch
from lfm2.main import create_lfm2_model

# Create a model
model = create_lfm2_model(
    model_size="700M",  # Choose from: "350M", "700M", "1.2B"
    vocab_size=32768,
    max_seq_length=32768,
    verbose=True
)

# Example forward pass
batch_size = 2
seq_length = 32
input_ids = torch.randint(0, model.config.vocab_size, (batch_size, seq_length))

# Generate outputs
with torch.no_grad():
    outputs = model(input_ids)
    logits = outputs["logits"]

Architecture Details

LIV Convolution Blocks

The model uses Linear Input-Varying (LIV) convolution blocks that combine double-gating with short-range convolutions:

def lfm2_conv(x):
    B, C, x = linear(x)    # input projection
    x = B*x                # gating (gate depends on input)
    x = conv(x)            # short conv
    x = C*x                # gating
    x = linear(x)
    return x

Grouped Query Attention

The model implements Grouped Query Attention (GQA) for efficient global attention processing, reducing memory and computational requirements while maintaining model quality.

Usage Examples

Check example.py for detailed usage examples including:

  1. Basic forward pass
  2. Forward pass with attention masks
  3. Forward pass with caching
  4. Forward pass with all outputs
  5. Forward pass with custom position IDs

Citation

If you use this implementation in your research, please cite:

@misc{lfm2_minimal,
  author = {Kye Gomez},
  title = {LFM2: Minimal Implementation of Liquid Foundation Model 2},
  year = {2024},
  publisher = {GitHub},
  url = {https://github.com/kyegomez/LFM2}
}

Disclaimer

This is an unofficial, minimal implementation based on publicly available information about the LFM2 architecture. It is not affiliated with or endorsed by Liquid AI. The implementation may differ from the original model in various aspects.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lfm2-0.0.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lfm2-0.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file lfm2-0.0.1.tar.gz.

File metadata

  • Download URL: lfm2-0.0.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0

File hashes

Hashes for lfm2-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1fde1336920a00060f61a0c2dbc7efcf56c1c784e20185f39b6d3b676be3fe12
MD5 9c29dbe1ce0b2e343efc4b31abe6e4b5
BLAKE2b-256 d0ef314a3f462242a1c86ebb0c578dc51408f584c744313345369490dd42c2c3

See more details on using hashes here.

File details

Details for the file lfm2-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: lfm2-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0

File hashes

Hashes for lfm2-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7849b67f170c06c0445897d712b3fc29e5e3da548ec6caeff2988193cc7ef1fa
MD5 4111e72860a5e9db976bc8fd96981e17
BLAKE2b-256 14f2bfa2adaed078ad84f735a8954504a82a69e5b5a3fffa52117401acb1ec5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page