Repository navigation
Refactor Tokenizer -> BaseTokenizer - #1333
Conversation
76476bc to
8876a97
Compare
tianyu-l
left a comment
There was a problem hiding this comment.
Looks great in general. Left some comments.
| 3. If you've changed APIs, update the documentation. | ||
| 4. Ensure the test suite passes. | ||
| 5. Make sure your code lints (`pre-commit run --all-files`). | ||
| 5. Make sure your code lints (`pre-commit run --from-ref origin/main --to-ref HEAD`). |
There was a problem hiding this comment.
IIUC this is restricting the linting to be changes between current main and the latest commit. Can I ask why?
There was a problem hiding this comment.
I needed to update this because when I was running pre-commit run --all-files, it includes files not in my pending changes so I was getting a lot of errors for files under torchtitan/experiments/kernels/. I updated this it to be pre-commit run --files $(git diff --name-only HEAD~1) which is more general
There was a problem hiding this comment.
I was getting a lot of errors for files under torchtitan/experiments/kernels/
That sounds very bad. I'm not sure how it escaped the check in https://github.com/pytorch/torchtitan/blob/main/.github/workflows/lint.yaml#L37
I don't think we should change official CONTRIBUTING.md to accommodate bad behavior. We should either refactor the code to make sure lint passes in all conditions, or just delete those code. We can do this in separate PRs.
There was a problem hiding this comment.
That sounds very bad. I'm not sure how it escaped the check in https://github.com/pytorch/torchtitan/blob/main/.github/workflows/lint.yaml#L37
This only lints the changed files, but yeah not sure how it was checked-in in the first place. Will do that in follow up, seems doable to update
There was a problem hiding this comment.
what's the source of the files under tests/assets/tokenizer?
asking because not sure about legal side of things
There was a problem hiding this comment.
The old test_tiktoken.model had a truncated vocabluary of 2000 based on tokenizer.model. Likewise I chatGPTed a python script that takes tokenizer.json and reduces it to 2000 tokens.
So this isn't pulled from anywhere, but it is derived from llama-3.1-8B like before, which i assume is okay
There was a problem hiding this comment.
Previously the tokenizer.model is a fake one taken from some other repo.
What matters is not vocab size, but legal issue -- we can't use llama tokenizer directly due to legal reasons. If your new tokenizer is capable of dealing with .model tokenizer, could we keep the old one?
There was a problem hiding this comment.
So we want to keep test_tiktoken.model because of CI tests? I remember CI do has access to network. Can we let integration test to download the tokenizer from Llama repo, and then run the integration test, does this sounds all right?
There was a problem hiding this comment.
This PR adds tests/assets/tokenizer/tokenizer.json which is functionally the same as test_tiktoken.model and I updated the .toml and tests to reference this instead
8876a97 to
4883438
Compare
9b2bb40 to
bbdca16
Compare
tianyu-l
left a comment
There was a problem hiding this comment.
LGTM, let's fix the linting thing in followup PRs.
Also please make sure CI passes / understand the error is not related.
|
After #1343, these changes are causing this test:
to fail. The hypothesis is that embedding layer doesn't have padding and uneven sharding might result in this bug |
bbdca16 to
69dab01
Compare
|
Update after changing the test vocab_size from 2002 -> 2000, now the test above is passing |
| name = "llama3" | ||
| flavor = "405B" | ||
| tokenizer_path = "./assets/tokenizer/original/tokenizer.model" | ||
| tokenizer_path = "./assets/tokenizer/Llama-3.1-8B" |
There was a problem hiding this comment.
Path is inconsistent with the download path here:
Line 107 in 681df50
There was a problem hiding this comment.
My bad, surprisingly both of these will work:
python scripts/download_tokenizer.py --repo_id meta-llama/Llama-3.1-8B
python scripts/download_tokenizer.py --repo_id meta-llama/Meta-Llama-3.1-8B
but the README and toml are using different folder names, I will update the README to just specify meta-llama/Llama-3.1-8B, please do
python scripts/download_tokenizer.py --repo_id meta-llama/Llama-3.1-8B
Fix #1333 (comment) Downloading from both `Meta-Llama-3.1-8B` and `Llama-3.1-8B` work, but the README instructions and the .toml file are different. This changes so the README matches
This causes breaking changes and users will need to redownload the tokenizer files (`python scripts/download_tokenizer.py ...`) - Remove `tiktoken` dependency, remove `tiktoken.py` - Refactor the `Tokenizer` base class - Update config files to point to directory instead of `tokenizer.model` - Raise exception if using `tokenizer.model` for tokenizer_path
Fix pytorch#1333 (comment) Downloading from both `Meta-Llama-3.1-8B` and `Llama-3.1-8B` work, but the README instructions and the .toml file are different. This changes so the README matches
This causes breaking changes and users will need to redownload the tokenizer files (`python scripts/download_tokenizer.py ...`) - Remove `tiktoken` dependency, remove `tiktoken.py` - Refactor the `Tokenizer` base class - Update config files to point to directory instead of `tokenizer.model` - Raise exception if using `tokenizer.model` for tokenizer_path
Fix pytorch#1333 (comment) Downloading from both `Meta-Llama-3.1-8B` and `Llama-3.1-8B` work, but the README instructions and the .toml file are different. This changes so the README matches
This causes breaking changes and users will need to redownload the tokenizer files (`python scripts/download_tokenizer.py ...`) - Remove `tiktoken` dependency, remove `tiktoken.py` - Refactor the `Tokenizer` base class - Update config files to point to directory instead of `tokenizer.model` - Raise exception if using `tokenizer.model` for tokenizer_path
Fix pytorch#1333 (comment) Downloading from both `Meta-Llama-3.1-8B` and `Llama-3.1-8B` work, but the README instructions and the .toml file are different. This changes so the README matches
Every non-debug Kimi config fails while building its tokenizer. Moonshot publishes vocabularies as ``tiktoken.model``, and torchtitan's tokenizer reads ``tokenizer.json`` or a vocab/merges pair, so ``moonlight_16b_a3b``, ``kimi_vl_a3b`` and ``kimi_k2_5`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. This has been true since #3532 added those configs, 12 months after #1333 removed tiktoken support and standardized on HuggingFace tokenizers. Rebuild the equivalent tokenizer.json during ``download_hf_assets.py`` rather than teaching the runtime a second vocabulary format, which keeps #1333's decision intact. Reading the tiktoken format and recovering BPE merge pairs from its ranks is delegated to ``transformers.convert_slow_tokenizer.TikTokenConverter``, so neither becomes torchtitan's to track as tiktoken evolves. The pre-tokenizer regex is read from the model's own tokenizer module via its ``tokenizer_config.json`` auto_map, not hardcoded here, so it cannot silently drift from upstream; ``--pattern`` overrides it for models that expose none. That leaves one thing upstream cannot supply: special-token ids. ``added_tokens_decoder`` names only some reserved slots -- Moonlight names 9 across ids 163584-163839 -- and the unnamed ones still occupy ids, so they are filled with placeholders. Conversion happens at download rather than at tokenizer load because every rank builds a tokenizer; converting at load would have them race to write the same file, and guarding that needs process-group awareness the tokenizer component should not have. ``transformers`` and ``tiktoken`` are needed to run the script. Both are already used under scripts/ -- numerical_tests_kimi.py imports transformers and documents needing tiktoken -- and trust_remote_code is already used there for this same model family. tiktoken joins requirements-dev.txt so CI exercises the conversion. Nothing reaches the training runtime, which still reads only tokenizer.json. Test plan: - pytest tests/unit_tests/test_convert_tiktoken.py -- 10 passed. Special-token id placement is covered without any optional dependency; the conversion tests skip when transformers/tiktoken are absent. - Cross-checked against reference tiktoken on the real Moonlight vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. - The delegated output was also compared against a hand-rolled implementation of the same conversion over 667 strings: 0 mismatches, and the regex read from the model matched the published pat_str exactly. - Converted vocabulary is 163840 tokens, matching moonlight_16b_a3b's decoder_vocab_size; moonlight_16b_a3b().tokenizer.build() now succeeds from its own hf_assets_path with bos=163584 eos=163585. - End to end: `download_hf_assets.py --repo_id moonshotai/Moonlight-16B-A3B --assets tokenizer` fetches tiktoken.model plus the tokenization module and writes tokenizer.json. ghstack-source-id: 77a1c96 Pull-Request: #4262
Kimi's non-debug configs cannot build a tokenizer. Moonshot ships vocabularies as ``tiktoken.model``, and torchtitan reads ``tokenizer.json`` or a vocab/merges pair, so ``moonlight_16b_a3b``, ``kimi_vl_a3b`` and ``kimi_k2_5`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. That has been true since #3532 added them, a year after #1333 dropped tiktoken and standardized on HuggingFace tokenizers. Convert during ``download_hf_assets.py``. Nothing is added to the runtime, the install, or the dev requirements -- the conversion uses ``tokenizers``, already required, and ``tiktoken`` stays gone. Two things the file does not store have to be reconstructed. A BPE model needs the pair each token was merged from, but tiktoken records only the rank it was learned at; replaying the merge loop over a token's own bytes, stopping below its rank, leaves the two halves. And ``tokenizers`` keys its vocabulary by ``str`` where tiktoken uses bytes, so the usual byte-to-character mapping applies. The pre-tokenizer regex is not in the file either and is model-specific, so Moonshot's is a named constant. Special tokens keep their configured ids: ``added_tokens_decoder`` names only some reserved slots, and the unnamed ones still occupy ids, so they are filled with placeholders. Test plan: - pytest tests/unit_tests/test_convert_tiktoken.py -- 3 passed. - Cross-checked against reference tiktoken on the real Moonlight vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. tiktoken was installed locally for that check only. - 163840 tokens, matching moonlight_16b_a3b's decoder_vocab_size; ``moonlight_16b_a3b().tokenizer.build()`` now succeeds from its own hf_assets_path with bos=163584 eos=163585. ghstack-source-id: da16c92 Pull-Request: #4262
Kimi's non-debug configs cannot build a tokenizer. The Kimi family ships its vocabulary as ``tiktoken.model``, and torchtitan reads ``tokenizer.json`` or a vocab/merges pair, so ``kimi_k2_5``, ``kimi_vl_a3b`` and ``moonlight_16b_a3b`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. That has been true since #3532 added them, a year after #1333 dropped tiktoken and standardized on HuggingFace tokenizers. Convert during ``download_hf_assets.py``. Nothing is added to the runtime, the install, or the dev requirements -- the conversion uses ``tokenizers``, already required, and ``tiktoken`` stays gone. Two things the file does not store have to be reconstructed. A BPE model needs the pair each token was merged from, but tiktoken records only the rank it was learned at; replaying the merge loop over a token's own bytes, stopping below its rank, leaves the two halves. And ``tokenizers`` keys its vocabulary by ``str`` where tiktoken uses bytes, so the usual byte-to-character mapping applies. The pre-tokenizer regex is not in the file either, so Kimi's is a named constant. Special tokens keep their configured ids: ``added_tokens_decoder`` names only some reserved slots, and the unnamed ones still occupy ids, so they are filled with placeholders. Test plan: - pytest tests/unit_tests/test_convert_tiktoken.py -- 3 passed. - Cross-checked against reference tiktoken on the real Kimi vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. tiktoken was installed locally for that check only. - 163840 tokens, matching the Kimi decoder_vocab_size; ``moonlight_16b_a3b().tokenizer.build()`` now succeeds from its own hf_assets_path with bos=163584 eos=163585. ghstack-source-id: 9b29e5e Pull-Request: #4262
Kimi's non-debug configs cannot build a tokenizer. The Kimi family ships its vocabulary as ``tiktoken.model``, and torchtitan reads ``tokenizer.json`` or a vocab/merges pair, so ``kimi_k2_5``, ``kimi_vl_a3b`` and ``moonlight_16b_a3b`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. That has been true since #3532 added them, a year after #1333 dropped tiktoken and standardized on HuggingFace tokenizers. Convert during ``download_hf_assets.py``. Nothing is added to the runtime, the install, or the dev requirements -- the conversion uses ``tokenizers``, already required, and ``tiktoken`` stays gone. Two things the file does not store have to be reconstructed. A BPE model needs the pair each token was merged from, but tiktoken records only the rank it was learned at; replaying the merge loop over a token's own bytes, stopping below its rank, leaves the two halves. And ``tokenizers`` keys its vocabulary by ``str`` where tiktoken uses bytes, so the usual byte-to-character mapping applies. The pre-tokenizer regex is not in the file either, so Kimi's is a named constant. Special tokens keep their configured ids: ``added_tokens_decoder`` names only some reserved slots, and the unnamed ones still occupy ids, so they are filled with placeholders. Test plan: - Cross-checked against reference tiktoken on the real Kimi vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. tiktoken was installed locally for that check only. - 163840 tokens, matching the Kimi decoder_vocab_size; ``moonlight_16b_a3b().tokenizer.build()`` now succeeds from its own hf_assets_path with bos=163584 eos=163585. ghstack-source-id: c4b9ef6 Pull-Request: #4262
Kimi's non-debug configs cannot build a tokenizer. The Kimi family ships its vocabulary as ``tiktoken.model``, and torchtitan reads ``tokenizer.json`` or a vocab/merges pair, so ``kimi_k2_5``, ``kimi_vl_a3b`` and ``moonlight_16b_a3b`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. That has been true since #3532 added them, a year after #1333 dropped tiktoken and standardized on HuggingFace tokenizers. Convert during ``download_hf_assets.py``. Nothing is added to the runtime, the install, or the dev requirements -- the conversion uses ``tokenizers``, already required, and ``tiktoken`` stays gone. Two things the file does not store have to be reconstructed. A BPE model needs the pair each token was merged from, but tiktoken records only the rank it was learned at; replaying the merge loop over a token's own bytes, stopping below its rank, leaves the two halves. And ``tokenizers`` keys its vocabulary by ``str`` where tiktoken uses bytes, so the usual byte-to-character mapping applies. The pre-tokenizer regex is not in the file either, so Kimi's is a named constant. Special tokens keep their configured ids: ``added_tokens_decoder`` names only some reserved slots, and the unnamed ones still occupy ids, so they are filled with placeholders. Test plan: - Cross-checked against reference tiktoken on the real Kimi vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. tiktoken was installed locally for that check only. - 163840 tokens, matching the Kimi decoder_vocab_size; ``moonlight_16b_a3b().tokenizer.build()`` now succeeds from its own hf_assets_path with bos=163584 eos=163585. ghstack-source-id: 8e87830 Pull-Request: #4262
Kimi's non-debug configs cannot build a tokenizer. The Kimi family ships its vocabulary as ``tiktoken.model``, and torchtitan reads ``tokenizer.json`` or a vocab/merges pair, so ``kimi_k2_5``, ``kimi_vl_a3b`` and ``moonlight_16b_a3b`` all raise "No supported tokenizer files found". Only the debug model works, because it points at the bundled test tokenizer. That has been true since #3532 added them, a year after #1333 dropped tiktoken and standardized on HuggingFace tokenizers. Convert during ``download_hf_assets.py``. Nothing is added to the runtime, the install, or the dev requirements -- the conversion uses ``tokenizers``, already required, and ``tiktoken`` stays gone. Two things the file does not store have to be reconstructed. A BPE model needs the pair each token was merged from, but tiktoken records only the rank it was learned at; replaying the merge loop over a token's own bytes, stopping below its rank, leaves the two halves. And ``tokenizers`` keys its vocabulary by ``str`` where tiktoken uses bytes, so the usual byte-to-character mapping applies. The pre-tokenizer regex is not in the file either, so Kimi's is a named constant. Special tokens keep their configured ids: ``added_tokens_decoder`` names only some reserved slots, and the unnamed ones still occupy ids, so they are filled with placeholders. Test plan: - Cross-checked against reference tiktoken on the real Kimi vocabulary: 812 strings from repo sources plus CJK, emoji, whitespace and literal special-token text encode to identical ids, 0 mismatches. tiktoken was installed locally for that check only. - 163840 tokens, matching the Kimi decoder_vocab_size; ``moonlight_16b_a3b().tokenizer.build()`` now succeeds from its own hf_assets_path with bos=163584 eos=163585. ghstack-source-id: 8e87830 Pull-Request: #4262
This causes breaking changes and users will need to redownload the tokenizer files (
python scripts/download_tokenizer.py ...)tiktokendependency, removetiktoken.pyTokenizerbase classtokenizer.modeltokenizer.modelfor tokenizer_path