Nacrith-CPU / compressor.py
robtacconelli's picture
Upload 8 files
156e2a6 verified
Raw History Blame Contribute Delete
1.66 kB
"""
Nacrith CPU compressor -- same interface as the GPU version.
Internally uses trigram tables + arithmetic coding instead of a neural model.
Supports two formats:
TC01 -- lossless text compression (replaces NC01)
NC05 -- hybrid multi-table binary compression (replaces NC02)
"""
from trigram_compressor import TrigramCompressor
# Magic bytes exported for app.py validation
MAGIC = b'TC01' # CPU text format
MAGIC_BIN = b'NC05' # CPU binary format
HEADER_SIZE = 12 # minimum header bytes (magic + 2 x uint32)
class _ModelProxy:
"""Exposes tokenizer under comp.model.tokenizer, matching GPU interface."""
def __init__(self, tokenizer):
self.tokenizer = tokenizer
class NeuralCompressor:
"""CPU compressor with the same public API as the GPU NeuralCompressor.
Replaces the SmolLM2-135M neural model with trigram tables for
probability prediction, eliminating the GPU requirement.
"""
def __init__(self, verbose: bool = False):
self._trigram = TrigramCompressor(verbose=verbose)
# Expose tokenizer at comp.model.tokenizer (same as GPU version)
self.model = _ModelProxy(self._trigram.tokenizer)
def compress(self, text: str) -> bytes:
"""Compress text string -> TC01 format bytes."""
return self._trigram.compress(text)
def compress_bytes(self, data: bytes) -> bytes:
"""Compress raw bytes -> NC05 hybrid format bytes."""
return self._trigram.compress_bytes(data)
def decompress(self, data: bytes):
"""Decompress TC01 / NC03 / NC05 -> original text str or raw bytes."""
return self._trigram.decompress(data)