YouTokenToMe Documentation

repository·master·Indexed 21 days ago

https://github.com/vkcom/youtokentome

A high-performance, unsupervised text tokenizer implementing fast Byte Pair Encoding (BPE). It supports multithreading for training and tokenization and provides both a Python API and a CLI (yttm) for training models, encoding text to IDs or subwords, and decoding IDs back to text.

Tokens
1.7K
Snippets
10
Records
13
Agent score
26%

What's inside YouTokenToMe

  1. Performance and Multithreading in YouTokenToMe

    master

    YouTokenToMe is designed for high-speed BPE training and tokenization, particularly for languages with large alphabets. It supports multithreading for both training and tokenization processes.

    Threading Behavior

    • Training: Performance scaling for training typically plateaus after 8 threads. The library effectively uses min(8, n_threads) for training operations.
    • Tokenization: Scales more linearly with thread count compared to training.

    To allow the library to automatically determine the optimal number of threads for your hardware, use n_threads=-1.

  2. Install YouTokenToMe via pip

    master

    Install the youtokentome package using pip to access the BPE tokenizer via Python or the CLI.

    pip install youtokentome
  3. Access vocabulary information

    master

    The BPE class provides methods to inspect the learned vocabulary:

    • vocab(): Returns a list of vocab_size strings representing the subwords.
    • vocab_size(): Returns the total number of tokens in the vocabulary.
    • subword_to_id(subword): Returns the integer ID for a given subword string, or unk_id if not found.
    • id_to_subword(id): Returns the subword string corresponding to a specific integer ID.
  4. Train a BPE model in Python

    master

    Use youtokentome.BPE.train() to train a Byte Pair Encoding model from a text file and save it to a model file. This method is highly efficient and supports multithreading.

    Arguments:

    • data (str): Path to the training data file.
    • model (str): Path where the trained model will be saved.
    • vocab_size (int): Number of tokens in the final vocabulary.
    • coverage (float): Fraction of characters covered by the model [0, 1]. Recommended value is 0.9999.
    • n_threads (int): Number of parallel threads. Use -1 to use all available threads (limited to 8).
    • pad_id (int): Reserved ID for padding (default: 0).
    • unk_id (int): Reserved ID for unknown symbols (default: 1).
    • bos_id (int): Reserved ID for begin of sentence token (default: 2).
    • eos_id (int): Reserved ID for end of sentence token (default: 3).

    Returns: A youtokentome.BPE instance with the loaded model.

    import youtokentome as yttm
    
    yttm.BPE.train(data="train.txt", model="model.bin", vocab_size=5000, coverage=0.9999)
  5. Encode text to IDs or Subwords

    master

    The encode method converts a list of strings into tokens. You can choose between integer IDs or string subwords.

    Arguments:

    • sentences (list[str]): The input sentences.
    • output_type (yttm.OutputType): Use yttm.OutputType.ID for integer IDs or yttm.OutputType.SUBWORD for subword strings.
    • bos (bool): If True, add the 'beginning of sentence' token.
    • eos (bool): If True, add the 'end of sentence' token.
    • reverse (bool): If True, reverse the output sequence.
    • dropout_prob (float): BPE-dropout probability [0, 1].

    Returns: A list of lists (either list[list[int]] or list[list[str]]).

    import youtokentome as yttm
    
    bpe = yttm.BPE(model="model.bin")
    
    # Encode to IDs
    ids = bpe.encode(["hello world"], output_type=yttm.OutputType.ID)
    
    # Encode to Subwords
    subwords = bpe.encode(["hello world"], output_type=yttm.OutputType.SUBWORD)
  6. Decode IDs back to text

    master

    Convert a list of token IDs back into a concatenated string using the decode method.

    import youtokentome as yttm
    
    bpe = yttm.BPE(model="model.bin")
    
    # ids is a list of lists of integers
    decoded_text = bpe.decode([[10, 25, 3], ignore_ids=[1, 2]])
  7. Load a trained BPE model

    master

    Initialize a youtokentome.BPE object by passing the path to a previously trained model file.

    import youtokentome as yttm
    
    bpe = yttm.BPE(model="example.model", n_threads=-1)
  8. Reference: `yttm encode` CLI options

    master

    Options for the encode command:

    • --model PATH (required): Path to the learned model file.
    • --output_type TEXT (required): Either id or subword.
    • --n_threads INTEGER: Number of threads [default: -1].
    • --bos: Add 'begin of sentence' token.
    • --eos: Add 'end of sentence' token.
    • --reverse: Reverse the output sequence of tokens.
    • --stream: Process each line one by one (ignores --n_threads).
    • --dropout_prob: BPE-dropout probability [default: 0].
    yttm encode --help
  9. Reference: `yttm decode` CLI options

    master

    Options for the decode command:

    • --model PATH (required): Path to the learned model file.
    • --ignore_ids LIST: List of indices to ignore (e.g., --ignore_ids=1,2,3).
    yttm decode --help
  10. Reference: `yttm vocab` CLI options

    master

    Options for the vocab command:

    • --model PATH (required): Path to the learned model file.
    • --verbose: Add merging rules to the output.
    yttm vocab --help
  11. Reference: `yttm bpe` CLI options

    master

    Options for the bpe command to train a model:

    • --data PATH (required): Training data file path.
    • --model PATH (required): Output model file path.
    • --vocab_size INTEGER (required): Number of tokens in the final vocabulary.
    • --coverage FLOAT: Fraction of characters covered [default: 1.0].
    • --n_threads INTEGER: Number of threads [default: -1].
    • --pad_id INTEGER: Padding token id [default: 0].
    • --unk_id INTEGER: Unknown token id [default: 1].
    • --bos_id INTEGER: 'Begin of sentence' token id [default: 2].
    • --eos_id INTEGER: 'End of sentence' token id [default: 3].
    yttm bpe --help
  12. Configure the number of threads

    master

    When using YouTokenToMe, you can control the parallelism of the training and tokenization processes using the n_threads parameter.

    • Set n_threads to a specific integer to use that exact number of threads.
    • Set n_threads=-1 to enable automatic thread detection, which is recommended for most use cases.
    n_threads=-1