Home

Awesome

llama-zip

llama-zip is a lossless compression utility that leverages a user-provided LLM (large language model) as the probabilistic model for an arithmetic coder. This allows llama-zip to achieve high compression ratios on structured or natural language text, since few bits are needed to encode tokens that the model predicts with high confidence. By employing a sliding context window, llama-zip is not limited by the context length of the LLM and can compress strings of arbitrary length. Furthermore, by encoding invalid UTF-8 bytes using code points in Unicode's private use areas, llama-zip is not limited to text inputs and can handle arbitrary binary data, albeit with reduced compression ratios compared to text inputs.

Interactive Mode Demo: Lorem Ipsum Text

Compression Performance

In the table below, the compression ratios achieved by llama-zip on the text files of the Calgary Corpus (as well as on llama-zip's own source code, llama_zip.py) are compared with other popular or high-performance compression utilities. Compression ratios are calculated by dividing the number of bytes in the uncompressed input by the number of bytes in the compressed output, so higher values indicate more effective compression.

For llama-zip, two LLMs were benchmarked at two context lengths, with a consistent window overlap of 25% (see Options for more info):

For the other utilities, the maximum compression level offered was used.

Filellama‑zip (llama‑8k)llama‑zip (llama‑32k)llama‑zip (phi‑32k)llama‑zip (phi‑8k)cmixpaq8pxpaq8pxdzpaqbrotlibzip2lzmaxzzstdgzip
bib15.001<ins>13.577</ins>10.4739.9205.6335.6685.5904.6113.9204.0513.6413.6363.4853.171
book18.745<ins>8.278</ins>7.0876.9974.2094.1924.2043.8232.9993.3052.9422.9412.9042.460
book212.250<ins>11.852</ins>10.68210.1085.3815.3465.3254.6493.6963.8803.5983.5963.5142.963
news9.976<ins>9.350</ins>8.3957.9354.5424.5314.4943.8173.3383.1803.1733.1713.0732.610
paper1<ins>12.577</ins>12.86910.44210.0724.2644.3024.2123.5723.4393.2113.0833.0743.0172.867
paper2<ins>12.370</ins>12.46010.78710.5614.1804.2084.1353.6793.3083.2833.0203.0152.9822.769
progc<ins>13.802</ins>14.00210.71410.1884.4394.4384.3523.4953.4093.1583.1623.1513.0962.968
progl20.429<ins>20.228</ins>14.73314.0547.4977.4647.3475.5545.1164.5994.8014.7874.7284.432
progp<ins>20.438</ins>21.88816.72215.4507.7057.6657.5085.3484.9984.6114.7924.7724.7244.414
trans<ins>12.523</ins>13.49711.7469.7768.6508.4848.4096.5976.0835.2355.6285.6135.4174.949
llama_zip.py29.08329.083<ins>23.383</ins><ins>23.383</ins>4.9044.9764.6893.0183.9803.5083.6083.5523.6333.542

The best-performing compressor for each file is listed in bold, and the second-best is underlined. The columns are sorted by average compression ratio achieved across all files, with overall better-performing compressors listed further to the left.

These results show that llama-zip can significantly outperform traditional compression utilities, at least with the LLMs and files tested. Note that increased context length might not lead to better compression ratios past a certain point, though, as evidenced by the fact that Llama 3.1 performed better on average with an 8k-token context length than with a 32k-token context length. Context lengths longer than 32k tokens were not tested due to memory and time constraints.

Installation

git clone https://github.com/alexbuz/llama-zip.git
cd llama-zip
pip3 install .

LLM Download

To use llama-zip, you must download an LLM that is compatible with llama.cpp, such as Llama 3.1 8B. Make sure to download a quantized version (one of the .gguf files listed on the "Files and versions" tab on Hugging Face) that is small enough to fit in your system's memory.

CLI Usage

llama-zip <llm_path> [options] <mode> [input]

Modes

llama-zip supports three modes of operation:

  1. Compress mode (specified by the -c or --compress flag): The string to be compressed can be provided as an argument or piped to stdin. The compressed output will be written to stdout.
  2. Decompress mode (specified by the -d or --decompress flag): The compressed string can be provided as an argument or piped to stdin. The decompressed output will be written to stdout.
  3. Interactive mode (specified by the -i or --interactive flag): A prompt is displayed where the user can enter strings to be compressed or decompressed. When a base64-encoded string is entered, it will be treated as representing compressed data and will be decompressed; otherwise, it will be compressed. After each compression or decompression operation, the user is prompted to enter another string. To exit interactive mode, press Ctrl+C.
    • Note: If you would like to compress a string that consists entirely of base64 characters (i.e., letters, numbers, +, and /, without any other symbols or spaces), you must use compression mode directly, as interactive mode assumes that base64-encoded strings are meant to be decompressed and will result in nonsensical output if the input did not come from a compression operation. Alternatively, you can add a non-base64 character to your string (such as a space at the end) if you don't mind that character being compressed along with the rest of the string.

Options

Examples

Compression

Decompression

Interactive Mode

Colab Notebook

Open In Colab

API Usage

The LlamaZip class can be used to compress and decompress data programmatically. The compress method takes a bytes object and returns a another bytes object containing the compressed data. The decompress method takes a bytes object containing compressed data and returns the original uncompressed data.

from llama_zip import LlamaZip

# Initialize the compressor
compressor = LlamaZip(model_path="/path/to/model.gguf")

# Compress some data
original = b"The quick brown fox jumps over the lazy dog."
compressed = compressor.compress(original)
assert len(compressed) < len(original)

# Decompress the data
decompressed = compressor.decompress(compressed)
assert decompressed == original

The LlamaZip constructor also accepts the n_ctx, n_gpu_layers, and use_mlock arguments, which correspond to the CLI options of the same names. The window_overlap argument can be passed to the compress and decompress methods directly to specify the window overlap for that particular operation.

Limitations

  1. Speed: Compression and decompression speeds are limited by the speed of LLM inference. This renders llama-zip significantly slower than traditional compression utilities. However, the compression ratios achieved by llama-zip may justify the trade-off in speed for certain use cases.
  2. Portability: llama-zip requires identical LLM behavior during compression and decompression. However, the backend that llama-zip uses for LLM inference, llama.cpp, does not currently guarantee deterministic behavior. This limits the portability of the compressed output of llama-zip, as it may not be decompressible on a different system, even if the same model is used. In practice, behavior also differs depending on the number of GPU layers offloaded, so the --n-gpu-layers option should be set to the same value during compression and decompression, in addition to the window overlap (--window-overlap) and context length (--n-ctx) options.
  3. Binary Compression: Due to its reliance on an LLM for prediction, llama-zip is best suited for compressing inputs that consist primarily of text. Although llama-zip can handle binary data by encoding invalid UTF-8 bytes using code points in Unicode's private use areas, it may not achieve high compression ratios on such data, potentially producing compressed output that is larger than the original input.