circuits-llm
Safe HaskellNone
LanguageGHC2024

Circuit.LLM.BPE

Synopsis

Data Types

data BPEModel Source #

BPE model loaded from .model file Stores merge rules, vocabulary, and special token mappings for encoding/decoding

Constructors

BPEModel 

Fields

Instances

Instances details
Eq BPEModel Source # 
Instance details

Defined in Circuit.LLM.BPE

Show BPEModel Source # 
Instance details

Defined in Circuit.LLM.BPE

data BPEEncoding Source #

Encoding result with metadata

Constructors

BPEEncoding 

Fields

Instances

Instances details
Eq BPEEncoding Source # 
Instance details

Defined in Circuit.LLM.BPE

Show BPEEncoding Source # 
Instance details

Defined in Circuit.LLM.BPE

data BPEError Source #

BPE operation errors

Constructors

ModelParseError !FilePath !String

File path and error message

InvalidTokenId !Word32

Invalid token ID during decode

RegexCompileError !String

Regex pattern compilation error

Instances

Instances details
Eq BPEError Source # 
Instance details

Defined in Circuit.LLM.BPE

Exception BPEError Source # 
Instance details

Defined in Circuit.LLM.BPE

Show BPEError Source # 
Instance details

Defined in Circuit.LLM.BPE

Model Loading

loadBPEModel :: FilePath -> IO BPEModel Source #

Load BPE model from .model file (Rust format)

File format: Line 1: Version string ("simple-bpe v1") Line 2: Regex pattern for text splitting Line 3: Number of special tokens (integer) Next N lines: Special token and its ID (e.g., "|endoftext| 256") Remaining lines: Merge pairs - two token IDs per line (e.g., "65 66")

loadBPEModelWithPerf :: FilePath -> IO (BPEModel, Map Text [Nanos]) Source #

Load BPE model with performance measurement

TODO: implement proper perf measurement with pure functions

Encoding & Decoding

encodeBPE :: BPEModel -> Text -> BPEEncoding Source #

Encode text using BPE model

Algorithm: 1. Split text by regex pattern into chunks 2. For each chunk: - Check if it's a special token → encode directly - Otherwise: convert to bytes → apply BPE merges 3. Concatenate all results

encodeBPEWithPerf :: BPEModel -> Text -> IO (BPEEncoding, Map Text [Nanos]) Source #

Encode with performance measurement

TODO: implement proper perf measurement with pure functions

decodeBPE :: BPEModel -> Vector Word32 -> Text Source #

Decode token IDs back to text

Algorithm: 1. For each token ID, lookup bytes in vocabulary 2. If not found in initial vocab, compute recursively from merge rules 3. Concatenate all bytes 4. Decode as UTF-8 (lossy)

decodeBPEWithPerf :: BPEModel -> Vector Word32 -> IO (Text, Map Text [Nanos]) Source #

Decode with performance measurement

TODO: implement proper perf measurement with pure functions

Display Functions

prettifyBPEModel :: BPEModel -> String Source #

Pretty-print BPE model information

prettifyEncoding :: BPEEncoding -> String Source #

Pretty-print encoding result