Skip to content

[Wasm] Let the tokenizer bindings load HF and Tekken tokenizers - #25103

Open
abdelaziz-mahdy wants to merge 1 commit into
pytorch:mainfrom
abdelaziz-mahdy:fix/wasm-tokenizers-exceptions
Open

abdelaziz-mahdy wants to merge 1 commit into
pytorch:mainfrom
abdelaziz-mahdy:fix/wasm-tokenizers-exceptions

Conversation

@abdelaziz-mahdy

Copy link
Copy Markdown
Contributor

Summary

Fixes #25102.

Bug: the Wasm tokenizer bindings can't load HF or Tekken tokenizers. That includes test_hf_tokenizer.json and test_tekken.json from this repo.

Two causes, two fixes

Cause Fix
The tokenizers library uses try/catch in normal operation, but Emscripten runs no catch blocks without -fexceptions. extension/wasm/tokenizers/CMakeLists.txt compiles tokenizers and regex_lookahead with -fexceptions, and passes it on to the final link through tokenizers_wasm.
HF and Tekken patterns use lookahead, which needs regex_lookahead. It was only enabled for the LLM runners. The top-level CMakeLists.txt also sets SUPPORT_REGEX_LOOKAHEAD for EXECUTORCH_BUILD_TOKENIZERS_WASM.

Native builds don't change: both settings apply only to the Wasm tokenizers build.

Test plan

Built tokenizers_wasm_lib exactly as the README describes (Emscripten 4.0.10, Release), then loaded the repo's test tokenizers in node and encoded "Hello world":

Build HFTokenizer Tekken Tiktoken / SPTokenizer
main C++ exception load fails load
-fexceptions only fails (lookahead) fails (lookahead) load
lookahead only C++ exception loads load
this PR loads loads load

So both changes are needed.

Repro script (node)
const fs = require('fs');
const loadTokenizers = require(process.argv[2]); // tokenizers_wasm_lib.js
const res = 'extension/llm/tokenizers/test/resources/';
(async () => {
  const mod = await loadTokenizers();
  for (const [cls, file] of [['HFTokenizer', 'test_hf_tokenizer.json'], ['Tiktoken', 'test_tiktoken_tokenizer.model'],
                             ['SPTokenizer', 'test_sentencepiece.model'], ['Tekken', 'test_tekken.json']]) {
    const t = new mod[cls]();
    try {
      t.load(new Uint8Array(fs.readFileSync(res + file)));
      console.log(cls, 'loaded', Array.from(t.encode('Hello world', 0, 0), String));
    } catch (e) {
      console.log(cls, 'FAILED', typeof e === 'number' ? 'C++ exception' : e.message);
    }
  }
})();

The Wasm tokenizers have no CI test today. I can add a jest test like extension/wasm/test has for the runtime, if you'd like it in this PR.

Two build settings were missing for EXECUTORCH_BUILD_TOKENIZERS_WASM:

- The tokenizers library catches its own exceptions in normal operation
  (HFTokenizer probes optional JSON keys and {"String": ...} patterns).
  Emscripten runs no catch blocks without -fexceptions, so the throw
  escaped to JS and HFTokenizer.load() failed.
- HF and Tekken patterns use lookahead, which needs regex_lookahead.
  SUPPORT_REGEX_LOOKAHEAD was only set for the LLM runners.

Fixes pytorch#25102
Copilot AI balanced review requested due to automatic review settings November 15, 2026 14:17
@pytorch-bot

pytorch-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25103

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 29 Awaiting Approval

As of commit 74fb21b with merge base d3369c4 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wasm tokenizers: HF and Tekken tokenizers fail to load

2 participants