Production-ready xLSTM (Extended LSTM) implementation optimized for Apple Silicon using MLX and Metal acceleration. Features automatic model loading, config-driven architecture with NCPS wiring patterns, and a simple generation API.
Author: Sydney Renee (The Solace Project)
Email: sydney@solace.ofharmony.ai
License: Apache 2.0
# Install dependencies
pip install mlx transformers tokenizers
# Run inference with local model
python generate.py --model xlstm_7b_model --prompt "The capital of France is" --max-tokens 50
# Interactive mode
python generate.py --model xlstm_7b_model --interactive
xLSTM-Metal uses MLX for native Apple Silicon acceleration with automatic configuration loading:
from xlstm_metal.mlx_jit.generate import xLSTMRunner
from xlstm_metal.mlx_jit.tokenizer import TokenizerBlock, TokenizerConfig
# Load model (config-driven, works with any xLSTM size)
runner = xLSTMRunner("xlstm_7b_model")
# Initialize tokenizer
tokenizer_config = TokenizerConfig(model_path="xlstm_7b_model")
tokenizer = TokenizerBlock(tokenizer_config)
# Generate text
prompt_ids = tokenizer.encode("Hello world").tolist()
generated_ids = runner.generate(
prompt_ids,
max_tokens=50,
temperature=0.8,
top_p=0.9
)
output = tokenizer.decode(generated_ids)
print(output)
Key Design Principles:
config.jsonNovember 2024 - Stable Release
See COMPLETE_FIX_SUMMARY.md and DOCSTRING_ENRICHMENT_SUMMARY.md for full details.
xLSTM-Metal uses a modular, auto-discovery architecture inspired by Neural Circuit Policies (NCPS):
1. WiredxLSTM Model (xlstm_metal/mlx_jit/models/wired_xlstm.py)
2. NCPS Auto-Wiring (xlstm_metal/mlx_jit/wiring/auto_wiring.py)
model.safetensors.index.json to discover architecture3. mLSTM/sLSTM Blocks (xlstm_metal/mlx_jit/blocks/)
4. Generation Engine (xlstm_metal/mlx_jit/generate.py)
Input Token IDs [B, S]
↓ embedding (token → vector)
Embeddings [B, S, D]
↓ blocks[0..N-1] (mLSTM/sLSTM with residuals + FFN)
Hidden States [B, S, D]
↓ out_norm (RMSNorm)
Normalized [B, S, D]
↓ lm_head (Linear projection)
Logits [B, S, vocab_size]
↓ soft_cap (tanh-based capping for stability)
↓ sampling (temperature/top-k/top-p)
Generated Tokens
Each block typically follows this pattern:
residual = x
x = norm_mlstm(x)
x, state = mlstm_cell(x, state) # Stateful recurrence
x = x + residual
residual = x
x = norm_ffn(x)
x = ffn(x) # Feed-forward network
x = x + residual
The NCPS-inspired wiring system provides:
MLX provides native Apple Silicon optimization:
NX-AI/xLSTM-7b): 7 billion parameter model
Any xLSTM model with a config.json file is supported. The implementation automatically:
# Install MLX (Apple Silicon only)
pip install mlx
# Install tokenizer support
pip install transformers tokenizers
# Clone repository
git clone https://github.com/SolaceHarmony/xLSTM-Metal.git
cd xLSTM-metal
# Download xLSTM-7B model from HuggingFace (~14GB)
# You can use the provided download script or download manually:
python scripts/downloads/download_model.py
# Or download manually from HuggingFace:
# https://huggingface.co/NX-AI/xLSTM-7b
# Run inference
python generate.py --model ./xlstm_7b_model --prompt "Hello world"
pip install mlx transformers tokenizers
Note: This implementation requires Apple Silicon (M1/M2/M3/M4) and macOS 13.0+.
# Basic generation
python generate.py --model xlstm_7b_model --prompt "Once upon a time"
# Advanced sampling
python generate.py --model xlstm_7b_model \
--prompt "The future of AI" \
--max-tokens 200 \
--temperature 0.8 \
--top-p 0.9
# Interactive mode
python generate.py --model xlstm_7b_model --interactive
# Model information
python generate.py --model xlstm_7b_model --info
# Debug wiring structure
python generate.py --model xlstm_7b_model --prompt "Test" --show-wiring
from xlstm_metal.mlx_jit.generate import xLSTMRunner
from xlstm_metal.mlx_jit.tokenizer import TokenizerBlock, TokenizerConfig
# Initialize runner
runner = xLSTMRunner("xlstm_7b_model")
# Initialize tokenizer
tokenizer_config = TokenizerConfig(model_path="xlstm_7b_model")
tokenizer = TokenizerBlock(tokenizer_config)
# Get model information
info = runner.get_model_info()
print(f"Model: {info['num_blocks']} blocks, {info['embedding_dim']}d")
# Generate with custom parameters
prompt_ids = tokenizer.encode("Hello world").tolist()
generated_ids = runner.generate(
prompt_ids,
max_tokens=100,
temperature=0.7,
top_k=50
)
output = tokenizer.decode(generated_ids)
print(output)
# Stateful generation (efficient for long sequences)
runner.reset_state()
prompt_ids = tokenizer.encode("Tell me a story").tolist()
current_ids = prompt_ids
for i in range(50): # Generate 50 tokens
next_token = runner.generate_next_token(
mx.array([current_ids], dtype=mx.int64),
temperature=0.8
)
current_ids = [int(next_token)]
print(tokenizer.decode([int(next_token)]), end='', flush=True)
xLSTM-Metal leverages Apple Silicon's unified memory architecture and Metal acceleration through MLX:
Performance characteristics depend on model size, sequence length, and hardware generation (M1/M2/M3/M4).
Typical Performance (xLSTM-7B on M2 Max):
See docs/ for detailed architecture documentation and optimization guides.
The Extended Long Short-Term Memory (xLSTM) architecture combines:
xLSTM extends traditional LSTM with:
├── xlstm_metal/ # Core implementation
│ └── mlx_jit/ # MLX backend (primary)
│ ├── generate.py # Generation runner
│ ├── tokenizer/ # Tokenizer wrapper
│ ├── models/ # WiredxLSTM model
│ ├── wiring/ # NCPS auto-wiring
│ ├── blocks/ # mLSTM/sLSTM blocks
│ └── utils/ # Config and weight loading
├── docs/ # Technical documentation
├── tests/ # Test suite
├── scripts/ # Utilities and tools
└── kernel_development/ # Metal kernel experiments
# Run test suite
python run_pytest.py
# Test specific components
python -m pytest tests/test_pretrained_inference.py -v
# Test numerical parity
python test_numerical_parity.py
This is an independent port to Apple Silicon. Contributions are welcome! Please:
xLSTM-Metal is an independent port to Apple Silicon with MLX.
This port includes:
xLSTM was introduced by Beck et al. (2024):
Beck, M., Pöppel, K., Spanring, M., Auer, A., Prudnikova, O., Kopp, M., Klambauer, G., Brandstetter, J., & Hochreiter, S. (2024). xLSTM: Extended Long Short-Term Memory. arXiv preprint arXiv:2405.04517.
Official xLSTM-7B model weights provided by NX-AI under Apache 2.0 license.
Built on MLX, Apple's machine learning framework for Apple Silicon.
Apache License 2.0. See LICENSE for full text.
Model weights from NX-AI are also under Apache 2.0.
If you use this implementation, please cite the original xLSTM paper:
@article{beck2024xlstm,
title={xLSTM: Extended Long Short-Term Memory},
author={Beck, Maximilian and P{\"o}ppel, Korbinian and Spanring, Markus and Auer, Andreas and Prudnikova, Oleksandra and Kopp, Michael and Klambauer, G{\"u}nter and Brandstetter, Johannes and Hochreiter, Sepp},
journal={arXiv preprint arXiv:2405.04517},
year={2024}
}
Complete technical documentation available in docs/:
This is an unofficial implementation optimized for Apple Silicon. For the original research and reference implementation, see the xLSTM paper.
43 commits
Python
97.0%
Objective-C++
1.5%
Metal
1.5%
Production-ready xLSTM (Extended LSTM) implementation optimized for Apple Silicon using MLX and Metal acceleration. Features automatic model loading, config-driven architecture with NCPS wiring patterns, and a simple generation API.
Author: Sydney Renee (The Solace Project)
Email: sydney@solace.ofharmony.ai
License: Apache 2.0
# Install dependencies
pip install mlx transformers tokenizers
# Run inference with local model
python generate.py --model xlstm_7b_model --prompt "The capital of France is" --max-tokens 50
# Interactive mode
python generate.py --model xlstm_7b_model --interactive
xLSTM-Metal uses MLX for native Apple Silicon acceleration with automatic configuration loading:
from xlstm_metal.mlx_jit.generate import xLSTMRunner
from xlstm_metal.mlx_jit.tokenizer import TokenizerBlock, TokenizerConfig
# Load model (config-driven, works with any xLSTM size)
runner = xLSTMRunner("xlstm_7b_model")
# Initialize tokenizer
tokenizer_config = TokenizerConfig(model_path="xlstm_7b_model")
tokenizer = TokenizerBlock(tokenizer_config)
# Generate text
prompt_ids = tokenizer.encode("Hello world").tolist()
generated_ids = runner.generate(
prompt_ids,
max_tokens=50,
temperature=0.8,
top_p=0.9
)
output = tokenizer.decode(generated_ids)
print(output)
Key Design Principles:
config.jsonNovember 2024 - Stable Release
See COMPLETE_FIX_SUMMARY.md and DOCSTRING_ENRICHMENT_SUMMARY.md for full details.
xLSTM-Metal uses a modular, auto-discovery architecture inspired by Neural Circuit Policies (NCPS):
1. WiredxLSTM Model (xlstm_metal/mlx_jit/models/wired_xlstm.py)
2. NCPS Auto-Wiring (xlstm_metal/mlx_jit/wiring/auto_wiring.py)
model.safetensors.index.json to discover architecture3. mLSTM/sLSTM Blocks (xlstm_metal/mlx_jit/blocks/)
4. Generation Engine (xlstm_metal/mlx_jit/generate.py)
Input Token IDs [B, S]
↓ embedding (token → vector)
Embeddings [B, S, D]
↓ blocks[0..N-1] (mLSTM/sLSTM with residuals + FFN)
Hidden States [B, S, D]
↓ out_norm (RMSNorm)
Normalized [B, S, D]
↓ lm_head (Linear projection)
Logits [B, S, vocab_size]
↓ soft_cap (tanh-based capping for stability)
↓ sampling (temperature/top-k/top-p)
Generated Tokens
Each block typically follows this pattern:
residual = x
x = norm_mlstm(x)
x, state = mlstm_cell(x, state) # Stateful recurrence
x = x + residual
residual = x
x = norm_ffn(x)
x = ffn(x) # Feed-forward network
x = x + residual
The NCPS-inspired wiring system provides:
MLX provides native Apple Silicon optimization:
NX-AI/xLSTM-7b): 7 billion parameter model
Any xLSTM model with a config.json file is supported. The implementation automatically:
# Install MLX (Apple Silicon only)
pip install mlx
# Install tokenizer support
pip install transformers tokenizers
# Clone repository
git clone https://github.com/SolaceHarmony/xLSTM-Metal.git
cd xLSTM-metal
# Download xLSTM-7B model from HuggingFace (~14GB)
# You can use the provided download script or download manually:
python scripts/downloads/download_model.py
# Or download manually from HuggingFace:
# https://huggingface.co/NX-AI/xLSTM-7b
# Run inference
python generate.py --model ./xlstm_7b_model --prompt "Hello world"
pip install mlx transformers tokenizers
Note: This implementation requires Apple Silicon (M1/M2/M3/M4) and macOS 13.0+.
# Basic generation
python generate.py --model xlstm_7b_model --prompt "Once upon a time"
# Advanced sampling
python generate.py --model xlstm_7b_model \
--prompt "The future of AI" \
--max-tokens 200 \
--temperature 0.8 \
--top-p 0.9
# Interactive mode
python generate.py --model xlstm_7b_model --interactive
# Model information
python generate.py --model xlstm_7b_model --info
# Debug wiring structure
python generate.py --model xlstm_7b_model --prompt "Test" --show-wiring
from xlstm_metal.mlx_jit.generate import xLSTMRunner
from xlstm_metal.mlx_jit.tokenizer import TokenizerBlock, TokenizerConfig
# Initialize runner
runner = xLSTMRunner("xlstm_7b_model")
# Initialize tokenizer
tokenizer_config = TokenizerConfig(model_path="xlstm_7b_model")
tokenizer = TokenizerBlock(tokenizer_config)
# Get model information
info = runner.get_model_info()
print(f"Model: {info['num_blocks']} blocks, {info['embedding_dim']}d")
# Generate with custom parameters
prompt_ids = tokenizer.encode("Hello world").tolist()
generated_ids = runner.generate(
prompt_ids,
max_tokens=100,
temperature=0.7,
top_k=50
)
output = tokenizer.decode(generated_ids)
print(output)
# Stateful generation (efficient for long sequences)
runner.reset_state()
prompt_ids = tokenizer.encode("Tell me a story").tolist()
current_ids = prompt_ids
for i in range(50): # Generate 50 tokens
next_token = runner.generate_next_token(
mx.array([current_ids], dtype=mx.int64),
temperature=0.8
)
current_ids = [int(next_token)]
print(tokenizer.decode([int(next_token)]), end='', flush=True)
xLSTM-Metal leverages Apple Silicon's unified memory architecture and Metal acceleration through MLX:
Performance characteristics depend on model size, sequence length, and hardware generation (M1/M2/M3/M4).
Typical Performance (xLSTM-7B on M2 Max):
See docs/ for detailed architecture documentation and optimization guides.
The Extended Long Short-Term Memory (xLSTM) architecture combines:
xLSTM extends traditional LSTM with:
├── xlstm_metal/ # Core implementation
│ └── mlx_jit/ # MLX backend (primary)
│ ├── generate.py # Generation runner
│ ├── tokenizer/ # Tokenizer wrapper
│ ├── models/ # WiredxLSTM model
│ ├── wiring/ # NCPS auto-wiring
│ ├── blocks/ # mLSTM/sLSTM blocks
│ └── utils/ # Config and weight loading
├── docs/ # Technical documentation
├── tests/ # Test suite
├── scripts/ # Utilities and tools
└── kernel_development/ # Metal kernel experiments
# Run test suite
python run_pytest.py
# Test specific components
python -m pytest tests/test_pretrained_inference.py -v
# Test numerical parity
python test_numerical_parity.py
This is an independent port to Apple Silicon. Contributions are welcome! Please:
xLSTM-Metal is an independent port to Apple Silicon with MLX.
This port includes:
xLSTM was introduced by Beck et al. (2024):
Beck, M., Pöppel, K., Spanring, M., Auer, A., Prudnikova, O., Kopp, M., Klambauer, G., Brandstetter, J., & Hochreiter, S. (2024). xLSTM: Extended Long Short-Term Memory. arXiv preprint arXiv:2405.04517.
Official xLSTM-7B model weights provided by NX-AI under Apache 2.0 license.
Built on MLX, Apple's machine learning framework for Apple Silicon.
Apache License 2.0. See LICENSE for full text.
Model weights from NX-AI are also under Apache 2.0.
If you use this implementation, please cite the original xLSTM paper:
@article{beck2024xlstm,
title={xLSTM: Extended Long Short-Term Memory},
author={Beck, Maximilian and P{\"o}ppel, Korbinian and Spanring, Markus and Auer, Andreas and Prudnikova, Oleksandra and Kopp, Michael and Klambauer, G{\"u}nter and Brandstetter, Johannes and Hochreiter, Sepp},
journal={arXiv preprint arXiv:2405.04517},
year={2024}
}
Complete technical documentation available in docs/:
This is an unofficial implementation optimized for Apple Silicon. For the original research and reference implementation, see the xLSTM paper.
43 commits
Python
97.0%
Objective-C++
1.5%
Metal
1.5%