Skip to content

Add @stdlib/tokenizer module: BPE tokenizer for tiktoken-compatible token counting#441

Open
nbeerbower wants to merge 1 commit into
mainfrom
claude/implement-tokenizer-0rSci
Open

Add @stdlib/tokenizer module: BPE tokenizer for tiktoken-compatible token counting#441
nbeerbower wants to merge 1 commit into
mainfrom
claude/implement-tokenizer-0rSci

Conversation

@nbeerbower

Copy link
Copy Markdown
Collaborator

Implements a full BPE (Byte Pair Encoding) tokenizer as a new stdlib module,
enabling Hemlock programs to count tokens and stay under LLM context limits.

Core features:

  • Load tiktoken-format vocabulary files (base64 token bytes + rank)
  • Encode text to token IDs via standard BPE merge algorithm
  • Decode token IDs back to text
  • Count tokens efficiently
  • Add special tokens at runtime
  • Full interpreter + compiler parity

Implementation:

  • C builtins for interpreter (src/backends/interpreter/builtins/tokenizer.c)
  • Runtime library for compiler (runtime/src/builtins_tokenizer.c)
  • Hemlock stdlib wrapper with Tokenizer object API (stdlib/tokenizer.hml)
  • Compiler codegen for all 7 builtin functions
  • Registered in builtins_registry.h, internal.h, registration.c

API:
import { Tokenizer } from "@stdlib/tokenizer";
let tok = Tokenizer("vocab.bpe");
let tokens = tok.encode("Hello, world!");
let count = tok.count("Hello, world!");
let text = tok.decode(tokens);
tok.free();

https://claude.ai/code/session_019TgFT84gP7VHFPLPThQRuG

…oken counting

Implements a full BPE (Byte Pair Encoding) tokenizer as a new stdlib module,
enabling Hemlock programs to count tokens and stay under LLM context limits.

Core features:
- Load tiktoken-format vocabulary files (base64 token bytes + rank)
- Encode text to token IDs via standard BPE merge algorithm
- Decode token IDs back to text
- Count tokens efficiently
- Add special tokens at runtime
- Full interpreter + compiler parity

Implementation:
- C builtins for interpreter (src/backends/interpreter/builtins/tokenizer.c)
- Runtime library for compiler (runtime/src/builtins_tokenizer.c)
- Hemlock stdlib wrapper with Tokenizer object API (stdlib/tokenizer.hml)
- Compiler codegen for all 7 builtin functions
- Registered in builtins_registry.h, internal.h, registration.c

API:
  import { Tokenizer } from "@stdlib/tokenizer";
  let tok = Tokenizer("vocab.bpe");
  let tokens = tok.encode("Hello, world!");
  let count = tok.count("Hello, world!");
  let text = tok.decode(tokens);
  tok.free();

https://claude.ai/code/session_019TgFT84gP7VHFPLPThQRuG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants