JAX/Equinox implementation of Megalodon for efficient long-context training and inference
Project description
megalodon-jax
A JAX/Equinox implementation of Megalodon: Efficient LLM Pretraining and Inference with Unlimited Context Length, aligned with the original released PyTorch/CUDA implementation.
Highlights
- Long-context language modeling with chunked attention and ComplexEMA
- JIT-compatible streaming inference with fixed-capacity caches
- Packed-sequence training with document-isolated attention, recurrent state, normalization, and loss
- SafeTensors checkpoints and original-upstream PyTorch checkpoint conversion
- Source-level parity checks and the official released tokenizer bundle
Installation
For CPU:
pip install megalodon-jax
For NVIDIA GPUs using the pip-managed CUDA 13 runtime:
pip install "megalodon-jax[cuda13]"
Python 3.11 or newer is required. See Installation and setup for CUDA requirements, optional features, unreleased main installs, development setup, and troubleshooting.
Quick start
import jax
from megalodon_jax import MegalodonConfig, MegalodonForCausalLM
model_key, input_key = jax.random.split(jax.random.PRNGKey(0))
config = MegalodonConfig(
vocab_size=32_000,
model_dim=512,
num_layers=8,
num_heads=1,
chunk_size=256,
cema_ndim=16,
)
model = MegalodonForCausalLM(config, key=model_key)
input_ids = jax.random.randint(input_key, (1, 128), 0, config.vocab_size)
logits, _ = model(input_ids)
print(logits.shape) # (1, 128, 32000)
Where to go next
- Installation and setup: CUDA compatibility, optional extras, source installs, and development setup
- Long-context streaming: generation, cache behavior, sliding attention, padding, and packed training
- Dtypes and numerical stability: precision policies, training, memory-bounded loss, and data parallelism
- JAX and PyTorch interoperability: checkpoints, conversion, resume state, and parity gates
- Paper and source differences: named model presets and deliberate compatibility choices
- Development: tests, release process, correctness verification, and benchmarks
The documentation index covers the remaining implementation and compatibility references.
Related
- Original Megalodon - released PyTorch/CUDA source
Citation
@misc{ma2024megalodon,
title={Megalodon: Efficient LLM Pretraining and Inference with Unlimited Context Length},
author={Xuezhe Ma and Xiaomeng Yang and Wenhan Xiong and Beidi Chen and Lili Yu and Hao Zhang and Jonathan May and Luke Zettlemoyer and Omer Levy and Chunting Zhou},
year={2024},
eprint={2404.08801},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file megalodon_jax-0.2.1.tar.gz.
File metadata
- Download URL: megalodon_jax-0.2.1.tar.gz
- Upload date:
- Size: 754.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0be4b9706b8e272671d888be3e6d56a9c6235339413465663fc62925f762dc0c
|
|
| MD5 |
2c38149347ccb27dda8384d3dec7f284
|
|
| BLAKE2b-256 |
2905cb55d237373bfc8b6a6a90e1dc11a0840dab50f0c225d6893e2035318512
|
Provenance
The following attestation bundles were made for megalodon_jax-0.2.1.tar.gz:
Publisher:
publish.yml on pszemraj/megalodon-jax
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
megalodon_jax-0.2.1.tar.gz -
Subject digest:
0be4b9706b8e272671d888be3e6d56a9c6235339413465663fc62925f762dc0c - Sigstore transparency entry: 2193548834
- Sigstore integration time:
-
Permalink:
pszemraj/megalodon-jax@476834dfd1068d8ee1abc0ec66bcdeef4681ab81 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/pszemraj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@476834dfd1068d8ee1abc0ec66bcdeef4681ab81 -
Trigger Event:
release
-
Statement type:
File details
Details for the file megalodon_jax-0.2.1-py3-none-any.whl.
File metadata
- Download URL: megalodon_jax-0.2.1-py3-none-any.whl
- Upload date:
- Size: 93.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acebaed160d7069c209b3807854979f882e8edd8072ffa949995f81bc1b6a319
|
|
| MD5 |
c56d71b2efd21db46b747dabffaf29b1
|
|
| BLAKE2b-256 |
c4941076c52894ce3c0785c483929727ea8390c1b6ed66398764ec495ea1e8b6
|
Provenance
The following attestation bundles were made for megalodon_jax-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on pszemraj/megalodon-jax
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
megalodon_jax-0.2.1-py3-none-any.whl -
Subject digest:
acebaed160d7069c209b3807854979f882e8edd8072ffa949995f81bc1b6a319 - Sigstore transparency entry: 2193548841
- Sigstore integration time:
-
Permalink:
pszemraj/megalodon-jax@476834dfd1068d8ee1abc0ec66bcdeef4681ab81 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/pszemraj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@476834dfd1068d8ee1abc0ec66bcdeef4681ab81 -
Trigger Event:
release
-
Statement type: