Compress files using overfitted neural networks and arithmetic coding
pym-particles overfits a transformer to a single file, then arithmetic-codes its next-byte predictions - up to 14.2x compression on structured data.
1.0.0Add to Favorites
Why it matters
Compress individual files by overfitting a transformer model to learn file-specific patterns and using arithmetic coding to convert byte predictions into compressed bitstreams, achieving compression ratios that scale with data predictability.
Outcomes
What it gets done
Train a transformer model on a target file to memorize its byte-level patterns
Predict next-byte probabilities autoregressively using overlapping context windows
Encode predictions into compressed .pym archives using arithmetic coding
Decompress archives back to byte-identical reconstructed files with verification
Source
Get it from source
Spark does not host a copy of it.
Open sourceReports
Agent outcome reports
No reports yet
Overview
Pym Particles
pym-particles overfits a transformer to a single target file, then uses an arithmetic coder to turn its next-byte predictions into a compressed .pym archive, reaching 14.2x compression on structured CSV data and 4.7x on natural-language text versus zip. Use it to explore neural single-file compression, especially on highly structured data; a CUDA GPU is strongly recommended, with a dedicated branch for CUDA-specific optimizations.
What it does
pym-particles is an experimental neural compression system that combines an overfitted transformer with arithmetic coding to compress individual files. A transformer is trained to overfit on one target file, predicting the next byte as accurately as possible; the more confident the model is in that prediction, the more an arithmetic coder can squeeze out of it. The network itself never compresses anything - it only predicts, and the arithmetic coder turns those predictions into the compressed bitstream. Overfitting is the entire goal here rather than a failure mode: techniques normally added to help a model generalize, like dropout and weight decay, actively hurt this system because they work against exactly what it's trying to do. The project isn't positioned as a new compression algorithm - combining neural prediction with entropy coding has been explored before, most notably by DeepZip (Goyal, Tatwawadi, Chandak, and Ochoa, DCC 2019).
Measured benchmark results show compression scaling directly with how structurally predictable the target file is: on a 100MB NYC Taxi Trip Data CSV, it reached roughly 0.50 bits/byte for a 7MB compressed output, a 14.2x ratio (versus 27MB for zip on the same file); on a 100MB slice of the enwik9 text dataset, it reached roughly 1.68 bits/byte for a 21MB output, a 4.7x ratio (versus 38MB for zip). Highly structured data lets the model's loss approach zero; complex natural language hits a semantic bottleneck instead.
When to use - and when NOT to
Use it to explore how far a neural network can compress a single file, particularly one with high structural regularity like tabular or CSV data, where it substantially outperformed zip in the published benchmarks. The compressor operates directly on raw bytes rather than text tokens, so it works on text, images, archives, executables, audio, video, or any other file format.
Don't expect fast turnaround without a CUDA GPU - one is strongly recommended, and while the code falls back to CPU automatically when CUDA is unavailable, training and compression become significantly slower. CUDA users should switch to the cuda_version branch for flash attention and fused kernels via torch.compile; AMD users need HSA_OVERRIDE_GFX_VERSION=11.0.0 set before running.
Inputs and outputs
Running python main.py and entering a filename trains a transformer on that file (if no trained model exists yet), compresses it into a .pym archive, decompresses it back, and verifies the reconstruction is byte-identical to the original. For my_file.txt, this produces my_file.txt.pym (the arithmetic-coded stream), my_file.txt.bin (seed contexts for parallel decompression), and my_file_reconstructed.txt (the recovered file). The default config trains on only the first 0.5MB of the input (SIZE = 0.5) for fast tests; setting SIZE = None trains on the whole file, improving compression at the cost of longer training and compression time. Other tunables include WINDOW_SIZE (256), STRIDE (128), HIDDEN_DIMS (128), LAYERS (2), BATCH_SIZE (64), and NUM_CHUNKS (100).
Integrations
python main.py
The repository is intentionally structured as a single-file workflow: place the target file in the project root, run the script, and enter its filename when prompted.
Who it's for
Researchers and engineers exploring neural approaches to lossless single-file compression, especially on highly structured data where the approach's benchmark results substantially beat zip.
Source README
pym-particles
PymParticles is an experimental neural compression system that combines an overfitted transformer with arithmetic coding to compress individual files.
The core idea: a transformer is trained to overfit on a single target file, predicting the next byte as accurately as possible. The more confident the model is in the next byte, the more compression an arithmetic coder can squeeze out of that prediction. The network never compresses anything directly, it just predicts; the arithmetic coder turns those predictions into a compressed bitstream.
Unlike most ML systems, overfitting is not a failure mode here, it's the entire goal. Things you'd normally add to help a model generalize (dropout, weight decay) actively hurt this system, since they fight the thing it's trying to do.
This project exists to explore how far a neural network can compress a single file.
Full writeup on how and why this works: ARCHITECTURE.md
It's not intended as a new compression algorithm, systems combining neural prediction with entropy coding have been explored before, most notably DeepZip.
@inproceedings{7fcb664b03ac4d6497048954d756b91f,
title = "DeepZip: Lossless Data Compression Using Recurrent Neural Networks",
author = "Mohit Goyal and Kedar Tatwawadi and Shubham Chandak and Idoia Ochoa",
year = "2019",
month = "5",
day = "10",
doi = "10.1109/DCC.2019.00087",
language = "English (US)",
series = "Data Compression Conference Proceedings",
publisher = "Institute of Electrical and Electronics Engineers Inc.",
editor = "Ali Bilgin and Storer, {James A.} and Marcellin, {Michael W.} and Joan Serra-Sagrista",
booktitle = "Proceedings - DCC 2019",
address = "United States",
}
Demo
Benchmark Results
Compression performance scales directly with the structural predictability (entropy) of the target file. Highly structured data lets the model's loss approach zero; complex natural language hits a semantic bottleneck.
| Dataset / File Type | Original Size | Bits/Byte | Compressed Size | Compression Ratio | zip |
|---|---|---|---|---|---|
| NYC Taxi Trip Data (CSV) | 100 MB | ~0.50 | 7 MB | 14.2x | 27 MB |
| enwik9 dataset (text slice) | 100 MB | ~1.68 | 21 MB | 4.7x | 38 MB |
You can download the dataset used for these benchmarks here.
For a list of things that I tried and didn't worked (MoE routing, bitmap masking, window shuffling, chunk slicing), see EXPERIMENTS.md.
Running Locally
The repository is intentionally structured as a single-file workflow.
Place the file you want to compress in the project root and run:
python main.py
When prompted, enter the file name:
my_file.txt
The script will:
- Train a transformer on that file if no trained model exists.
- Compress the file into a
.pymarchive. - Decompress the archive back into a reconstructed file.
- Verify that the reconstructed file is byte-identical to the original.
For example, given my_file.txt, the following files will be produced:
my_file.txt.pym- the arithmetic-coded compressed streammy_file.txt.bin- the seed contexts used for parallel decompressionmy_file_reconstructed.txt- the recovered file
NOTE - CUDA users: use the cuda_version branch. It includes CUDA-specific optimizations like flash attention and fused kernels via torch.compile.
The compressor operates directly on raw bytes rather than text tokens, so it works on text files, images, archives, executables, audio, video, or any other file format.
Config
The default configuration trains on the first 0.5 MB of the input file (for fast tests):
SIZE = 0.5
Set SIZE = None to train on the whole file. Increasing SIZE generally improves compression at the cost of longer training and compression times.
| Param | Default |
|---|---|
WINDOW_SIZE |
256 |
STRIDE |
128 |
HIDDEN_DIMS |
128 |
LAYERS |
2 |
BATCH_SIZE |
64 |
NUM_CHUNKS |
100 |
A CUDA GPU is strongly recommended. The code falls back to CPU automatically if CUDA is unavailable, but training and compression will be significantly slower.
For AMD users:
HSA_OVERRIDE_GFX_VERSION=11.0.0 python3 main.py
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.