Skip to content

Commit cf838ae

Browse files
committed
Move tokenizer.py into extension/llm/tokenizer
Pull Request resolved: #4255 as title ghstack-source-id: 233778092 Differential Revision: [D59698943](https://our.internmc.facebook.com/intern/diff/D59698943/)
1 parent 994ae7b commit cf838ae

File tree

16 files changed

+83
-55
lines changed

16 files changed

+83
-55
lines changed

.ci/scripts/test_llama.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ $PYTHON_EXECUTABLE -m examples.models.llama2.export_llama ${EXPORT_ARGS}
171171

172172
# Create tokenizer.bin.
173173
echo "Creating tokenizer.bin"
174-
$PYTHON_EXECUTABLE -m examples.models.llama2.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
174+
$PYTHON_EXECUTABLE -m extension.llm.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
175175

176176

177177
RUNTIME_ARGS="--model_path=${EXPORTED_MODEL_NAME} --tokenizer_path=tokenizer.bin --prompt=Once --temperature=0 --seq_len=10"

backends/vulkan/docs/android_demo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps":
6565
Then, create a tokenizer binary file:
6666

6767
```shell
68-
python -m examples.models.llama2.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
68+
python -m extension.llm.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
6969
```
7070

7171
Finally, export the `stories110M.pt` file into an ExecuTorch program:

examples/models/llama2/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ You can export and run the original Llama 2 7B model.
9090
4. Create tokenizer.bin.
9191
9292
```
93-
python -m examples.models.llama2.tokenizer.tokenizer -t <tokenizer.model> -o tokenizer.bin
93+
python -m extension.llm.tokenizer.tokenizer -t <tokenizer.model> -o tokenizer.bin
9494
```
9595
9696
### Option B: Download and export stories110M model
@@ -113,7 +113,7 @@ If you want to deploy and run a smaller model for educational purposes. From `ex
113113
4. Create tokenizer.bin.
114114
115115
```
116-
python -m examples.models.llama2.tokenizer.tokenizer -t <tokenizer.model> -o tokenizer.bin
116+
python -m extension.llm.tokenizer.tokenizer -t <tokenizer.model> -o tokenizer.bin
117117
```
118118
119119
### Option C: Download and export Llama 3 8B instruct model

examples/models/llama2/eval_llama_lib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
get_quantizer_and_quant_params,
1616
)
1717
from executorch.examples.models.llama2.tokenizer.tiktoken import Tokenizer as Tiktoken
18-
from executorch.examples.models.llama2.tokenizer.tokenizer import (
19-
Tokenizer as SentencePieceTokenizer,
20-
)
2118

2219
from executorch.extension.llm.export import LLMEdgeManager
20+
from executorch.extension.llm.tokenizer.tokenizer import (
21+
Tokenizer as SentencePieceTokenizer,
22+
)
2323

2424
from lm_eval.api.model import LM
2525

examples/models/llama2/evaluate/eager_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import lm_eval
1111
import torch
1212
from executorch.examples.models.llama2.tokenizer.tiktoken import Tokenizer as Tiktoken
13-
from executorch.examples.models.llama2.tokenizer.tokenizer import (
13+
from executorch.extension.llm.tokenizer.tokenizer import (
1414
Tokenizer as SentencePieceTokenizer,
1515
)
1616

examples/models/llama2/tokenizer/targets.bzl

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,3 @@ def define_common_targets():
4242
"re2",
4343
],
4444
)
45-
46-
runtime.python_library(
47-
name = "tokenizer_py_lib",
48-
srcs = [
49-
"__init__.py",
50-
"tokenizer.py",
51-
],
52-
base_module = "executorch.examples.models.llama2.tokenizer",
53-
visibility = [
54-
"//executorch/examples/...",
55-
"//bento/...",
56-
"//bento_kernels/...",
57-
],
58-
_is_external_target = True,
59-
deps = [] if runtime.is_oss else ["fbsource//third-party/pypi/sentencepiece:sentencepiece"],
60-
)
61-
62-
runtime.python_binary(
63-
name = "tokenizer_py",
64-
main_module = "executorch.examples.models.llama2.tokenizer.tokenizer",
65-
visibility = [
66-
"//executorch/examples/...",
67-
"fbsource//xplat/executorch/examples/...",
68-
],
69-
_is_external_target = True,
70-
deps = [
71-
":tokenizer_py_lib",
72-
],
73-
)

examples/models/llama2/tokenizer/test/__init__.py

Whitespace-only changes.

examples/models/llama2/tokenizer/test/targets.bzl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,3 @@ def define_common_targets():
4242
"resources/**",
4343
]),
4444
)
45-
46-
runtime.python_test(
47-
name = "test_bpe_tokenizer_py",
48-
srcs = [
49-
"test_bpe_tokenizer.py",
50-
],
51-
visibility = [
52-
"//executorch/examples/...",
53-
],
54-
deps = [
55-
"//executorch/examples/models/llama2/tokenizer:tokenizer_py_lib",
56-
],
57-
)

examples/qualcomm/llama2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ wget "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt"
2323
wget "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model"
2424

2525
# tokenizer.bin:
26-
python -m examples.models.llama2.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
26+
python -m extension.llm.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
2727

2828
# params.json:
2929
echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json

extension/llm/tokenizer/TARGETS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Any targets that should be shared between fbcode and xplat must be defined in
2+
# targets.bzl. This file can contain fbcode-only targets.
3+
4+
load(":targets.bzl", "define_common_targets")
5+
6+
oncall("executorch")
7+
8+
define_common_targets()

extension/llm/tokenizer/targets.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
def define_common_targets():
4+
"""Defines targets that should be shared between fbcode and xplat.
5+
6+
The directory containing this targets.bzl file should also contain both
7+
TARGETS and BUCK files that call this function.
8+
"""
9+
runtime.python_library(
10+
name = "tokenizer_py_lib",
11+
srcs = [
12+
"__init__.py",
13+
"tokenizer.py",
14+
],
15+
base_module = "executorch.extension.llm.tokenizer",
16+
visibility = [
17+
"//executorch/examples/...",
18+
"//executorch/extension/llm/tokenizer/...",
19+
"//bento/...",
20+
"//bento_kernels/...",
21+
],
22+
_is_external_target = True,
23+
# TODO: Define an external_deps entry for sentencepiece instead of pointing to an fbsource path.
24+
deps = [] if runtime.is_oss else ["fbsource//third-party/pypi/sentencepiece:sentencepiece"],
25+
)
26+
27+
runtime.python_binary(
28+
name = "tokenizer_py",
29+
main_module = "executorch.extension.llm.tokenizer.tokenizer",
30+
visibility = [
31+
"//executorch/examples/...",
32+
"fbsource//xplat/executorch/examples/...",
33+
],
34+
_is_external_target = True,
35+
deps = [
36+
":tokenizer_py_lib",
37+
],
38+
)

extension/llm/tokenizer/test/TARGETS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Any targets that should be shared between fbcode and xplat must be defined in
2+
# targets.bzl. This file can contain fbcode-only targets.
3+
4+
load(":targets.bzl", "define_common_targets")
5+
6+
oncall("executorch")
7+
8+
define_common_targets()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
def define_common_targets():
4+
"""Defines targets that should be shared between fbcode and xplat.
5+
6+
The directory containing this targets.bzl file should also contain both
7+
TARGETS and BUCK files that call this function.
8+
"""
9+
10+
runtime.python_test(
11+
name = "test_tokenizer_py",
12+
srcs = [
13+
"test_tokenizer.py",
14+
],
15+
deps = [
16+
"//executorch/extension/llm/tokenizer:tokenizer_py_lib",
17+
],
18+
)

examples/models/llama2/tokenizer/test/test_bpe_tokenizer.py renamed to extension/llm/tokenizer/test/test_tokenizer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
import unittest
1111
from unittest.mock import patch
1212

13-
from executorch.examples.models.llama2.tokenizer.tokenizer import Tokenizer
13+
from executorch.extension.llm.tokenizer.tokenizer import Tokenizer
1414

1515

1616
class TestTokenizer(unittest.TestCase):
17-
@patch(
18-
"executorch.examples.models.llama2.tokenizer.tokenizer.SentencePieceProcessor"
19-
)
17+
@patch("executorch.extension.llm.tokenizer.tokenizer.SentencePieceProcessor")
2018
def test_export(self, mock_sp):
2119
# Set up the mock SentencePieceProcessor
2220
mock_sp.return_value.vocab_size.return_value = 0

0 commit comments

Comments
 (0)