tryAGI / tryAGI/Tiktoken

Investigate Aho-Corasick pre-tokenization for ASCII throughput

Open
#118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
90
Forks
7
Avg merge
17h 57m
Merged PRs (30d)
3

Description

Context

Our current throughput on ASCII-dominant text is 120-143 MiB/s (CountTokens/Encode on Python code and Bitcoin whitepaper). On cached multilingual/CJK text we reach 571 MiB/s, which is competitive with top Rust implementations.

However, GitHub's bpe crate achieves ~400-500 MiB/s on general text by using Aho-Corasick for pre-tokenization, giving it O(n) worst-case complexity vs the standard O(n²) BPE merge loop.

Goal

Close the gap on ASCII throughput from ~140 MiB/s to 300+ MiB/s.

Research areas

  1. Aho-Corasick pre-tokenization — Build an Aho-Corasick automaton from the BPE vocabulary to match longest tokens in a single linear scan, bypassing iterative merge. GitHub's bpe crate proves this approach works.

  2. SIMD-accelerated byte processing — Use System.Runtime.Intrinsics (AVX2/NEON) for:

    • Fast ASCII detection (skip UTF-8 decode for pure ASCII runs)
    • Vectorized pattern matching in the pre-tokenizer regex
    • Bulk byte classification
  3. Pre-computed merge tables — For common token pairs, store pre-computed merge results to skip the BPE priority queue entirely.

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by benchmarking CountTokens and Encode on the described ASCII-dominant inputs, then compare the Aho-Corasick approach with GitHub's bpe crate and the SIMD and pre-computed-table options. Done means selecting and implementing a viable approach that raises ASCII throughput from about 140 MiB/s toward the 300+ MiB/s goal.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.