TensorTonic
Study Plans
Study Plan
Apply Ranked BPE Merges
Language Models
Medium

Implement encoding with ordered byte-pair merge rules and decoding with a supplied byte vocabulary. Encoding starts from the text's UTF-8 byte IDs. Apply each rule once in list order, replacing all non-overlapping matches from left to right. Later rules may use tokens produced by earlier rules.

(lk,rk)→nk(l_k,r_k)\rightarrow n_k(lk​,rk​)→nk​

Here k is the rule's position, l and r are its left and right token IDs, and n is the replacement ID. Decoding concatenates the vocabulary bytes in token order before interpreting the complete sequence as UTF-8. Empty inputs produce empty outputs.

Return a Python list of integer token IDs from encode and a Python string from decode.

Loading visualization...

Examples

Input: text = "low", merges = [[108,111,256],[256,119,257]]

Output: [257]

Explanation: The first rule changes [108,111,119] to [256,119], and the second changes it to [257].

Input: ids = [257], vocab = {256:[108,111],257:[108,111,119]}

Output: "low"

Input: text = "aaa", merges = [[97,97,256]]

Output: [256,97]

Hint 1

str.encode produces UTF-8 bytes for the initial token IDs.

Hint 2

A separate output list avoids shifting positions during a merge pass.

Hint 3

list.extend combines byte lists, and bytes.decode converts the complete sequence to text.

Requirements

  • Use Python strings, lists, dictionaries, and integers; NumPy is not required.
  • Leave the supplied merge rules, token IDs, and vocabulary unchanged.

Constraints

  • text is a Unicode string; text, merges, and ids may be empty.
  • merges is a Python list of [left_id, right_id, new_id] lists containing nonnegative Python integers.
  • ids is a Python list of nonnegative integers; every listed ID exists in vocab.
  • vocab maps Python integer IDs to lists of byte values from 0 to 255; byte lists may be empty.
  • The full byte sequence selected by ids is valid UTF-8, even when individual entries are not.
Try Similar Problems
Train a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumParallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumParameter-Matched SwiGLU BlockMediumImplement Symmetric CLIP Contrastive LossMediumPlan KV-Cache CapacityMediumImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMediumBeam Search DecodingMediumText Generation SamplingMedium

Accepts: string

Accepts: array

You must run your code first.

Apply Ranked BPE Merges

Language Models
Medium

Implement encoding with ordered byte-pair merge rules and decoding with a supplied byte vocabulary. Encoding starts from the text's UTF-8 byte IDs. Apply each rule once in list order, replacing all non-overlapping matches from left to right. Later rules may use tokens produced by earlier rules.

(lk,rk)→nk(l_k,r_k)\rightarrow n_k(lk​,rk​)→nk​

Here k is the rule's position, l and r are its left and right token IDs, and n is the replacement ID. Decoding concatenates the vocabulary bytes in token order before interpreting the complete sequence as UTF-8. Empty inputs produce empty outputs.

Return a Python list of integer token IDs from encode and a Python string from decode.

Loading visualization...

Examples

Input: text = "low", merges = [[108,111,256],[256,119,257]]

Output: [257]

Explanation: The first rule changes [108,111,119] to [256,119], and the second changes it to [257].

Input: ids = [257], vocab = {256:[108,111],257:[108,111,119]}

Output: "low"

Input: text = "aaa", merges = [[97,97,256]]

Output: [256,97]

Hint 1

str.encode produces UTF-8 bytes for the initial token IDs.

Hint 2

A separate output list avoids shifting positions during a merge pass.

Hint 3

list.extend combines byte lists, and bytes.decode converts the complete sequence to text.

Requirements

  • Use Python strings, lists, dictionaries, and integers; NumPy is not required.
  • Leave the supplied merge rules, token IDs, and vocabulary unchanged.

Constraints

  • text is a Unicode string; text, merges, and ids may be empty.
  • merges is a Python list of [left_id, right_id, new_id] lists containing nonnegative Python integers.
  • ids is a Python list of nonnegative integers; every listed ID exists in vocab.
  • vocab maps Python integer IDs to lists of byte values from 0 to 255; byte lists may be empty.
  • The full byte sequence selected by ids is valid UTF-8, even when individual entries are not.
Try Similar Problems
Train a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumParallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumParameter-Matched SwiGLU BlockMediumImplement Symmetric CLIP Contrastive LossMediumPlan KV-Cache CapacityMediumImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMediumBeam Search DecodingMediumText Generation SamplingMedium

Accepts: string

Accepts: array

You must run your code first.