Cartesia's From Tokens to Bytes posts score evaluation suites by "soft accuracy": the bits per byte a model assigns to the correct answer after the prompt. Implement a token bigram model and two ways of scoring an answer with it: the usual canonical way and the marginal way, which sums over tokenizations.
fit_bigram(sequences, vocab, k=0.01)
sequences is a list of token sequences (lists of bytes, each token in vocab), vocab a list of distinct bytes tokens.(start, trans) as NumPy arrays of shape (V,) and (V, V): start[v] is (number of sequences that begin with vocab[v] + k), normalized to sum to 1; trans[u][v] is (number of times vocab[v] directly follows vocab[u] + k), each row normalized to sum to 1. Empty sequences are skipped.canonical_bpb(prompt_tokens, answer_tokens, vocab, start, trans)
trans from the last prompt token, or with start if prompt_tokens is empty; every later answer token with trans from the token before it.ValueError if the answer has no bytes.marginal_bpb(prompt, answer, vocab, start, trans)
prompt and answer are bytes. Let be the prefix probability of the model, the probability that its endless token stream decodes to bytes starting with , summed over every tokenization. The last token may run past the end of , and of the empty string is 1. This is the quantity of the marginal prefix probability problem.len(answer).ValueError if answer is empty or if .Return plain Python floats from both scoring functions.