A token model scores token sequences, but the same bytes can be cut into tokens in many ways, and a prompt can end in the middle of a token. Compute the probability a token bigram model assigns to a byte prefix, summed over every tokenization, and the distribution of the next byte.
The model
vocab is a list of distinct, non-empty bytes tokens; token v is vocab[v].start[v] is the probability that the first token is vocab[v]; trans[u][v] is the probability that vocab[v] follows vocab[u]. Both may be NumPy arrays or nested lists, and every distribution sums to 1.decode of the token sequence.prefix_probability(s, vocab, start, trans)
s, as a float. Equivalently, sum over every token sequence whose bytes contain s as a prefix while the bytes of are strictly shorter than s. The last token may end exactly at the end of s or run past it.prefix_probability(b'', ...) is 1.0.next_byte_distribution(s, vocab, start, trans)
int) to probability, containing only bytes with positive probability, keys in increasing order. The values sum to 1.ValueError if the prefix has probability 0.The number of tokenizations grows exponentially with the length of s; tests use prefixes of about 50 bytes, so enumerating them is far too slow. Use a dynamic program over byte positions instead.