Fix PCRE2 UTF-8 validation errors for Qwen tokenizers #16631
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Qwen tokenizer patterns contain byte sequences that aren't valid UTF-8, causing PCRE2 compilation to fail with "UTF-8 error: isolated byte with 0x80 bit set". This prevents loading tokenizers for Qwen2.5 and OuteTTS models.
Changes:
PCRE2_UTFflag when UTF-8 validation fails (checks all 21PCRE2_ERROR_UTF8_ERR*codes)regex_errorexceptions infind_all()to prevent crashes from pattern complexity errorsPattern compilation now follows: RE2 → PCRE2 (with UTF) → PCRE2 (without UTF) → std::regex, with proper error handling at each stage.
Test plan
Built and ran tokenizers test suite:
Log output confirms UTF-8 fallback works:
Original prompt
This section details on the original issue you should resolve
<issue_title>Error [tokenizers:re2_regex.cpp:26] Failed to compile Regex for Qwen2.5 Model</issue_title>
<issue_description>### 🐛 Describe the bug
Similar issue as related in "Running Qwen3 in Android LlamaDemo App shows an error while loading tokenizer #11311 while trying to run the LLM Model (Qwen-2.5-0.5B) of OuteTTS v0.2 500M.
The tokenizer is a tokenizer.json file. I am using the one from OuteTTS-0.2-500M HuggingFace over here: https://huggingface.co/OuteAI/OuteTTS-0.2-500M/blob/main/tokenizer.json
Using Executorch git main branch (deaf37f)
Error as reported in #11311
Steps used to install ExecuTorch. Follow the models/llama/Readme.md Instructions :
Note: I do get a CMake Warning regarding the regex flag:
Command to test the OuteTTS LLM Model:
Config file (.yaml) to convert the outetts llm model to .pte: