Skip to content

Commit 3146703

Browse files
authored
Add null tokenizer (NVIDIA-NeMo#11789)
* Add null tokenizer Signed-off-by: Sangkug Lym <slym@nvidia.com> * Apply isort and black reformatting Signed-off-by: erhoo82 <erhoo82@users.noreply.github.com> * cleanup Signed-off-by: Sangkug Lym <slym@nvidia.com> --------- Signed-off-by: Sangkug Lym <slym@nvidia.com> Signed-off-by: erhoo82 <erhoo82@users.noreply.github.com> Co-authored-by: erhoo82 <erhoo82@users.noreply.github.com>
1 parent 6db1bfe commit 3146703

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from megatron.core.datasets.megatron_tokenizer import MegatronTokenizer
16+
17+
18+
class NullTokenizer(MegatronTokenizer):
19+
"""
20+
Synthetic tokenizer for performance benchmarking and debugging
21+
22+
Args:
23+
vocab_size: vocabulary size for embedding
24+
"""
25+
26+
def __init__(self, vocab_size):
27+
super().__init__(None, vocab_size=vocab_size)
28+
self._vocab_size_without_eod = int(vocab_size)
29+
self._eod_id = self._vocab_size_without_eod
30+
31+
def tokenize(self, text):
32+
return [int(x) for x in text.split(' ')]
33+
34+
def detokenize(self, ids):
35+
text = [str(x) for x in ids]
36+
return ' '.join(text)
37+
38+
def offsets(self, ids: list[int], text: str) -> list[int]:
39+
offsets, start_idx = [], 0
40+
for id_ in ids:
41+
offsets.append(start_idx)
42+
start_idx += 1 + len(str(id_))
43+
return offsets
44+
45+
@property
46+
def vocab_size(self):
47+
return self._vocab_size_without_eod + 1
48+
49+
@property
50+
def vocab(self):
51+
raise NotImplementedError
52+
53+
@property
54+
def inv_vocab(self):
55+
raise NotImplementedError
56+
57+
@property
58+
def cls(self):
59+
return -1
60+
61+
@property
62+
def sep(self):
63+
return -1
64+
65+
@property
66+
def mask(self):
67+
return -1
68+
69+
@property
70+
def eod(self):
71+
return self._eod_id
72+
73+
@property
74+
def additional_special_tokens_ids(self):
75+
return None

nemo/collections/nlp/modules/common/tokenizer_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def get_nmt_tokenizer(
156156
delimiter: Optional[str] = None,
157157
trust_remote_code: Optional[bool] = False,
158158
chat_template: Optional[Dict] = None,
159+
vocab_size: Optional[int] = None,
159160
):
160161
"""
161162
Args:
@@ -246,6 +247,11 @@ def get_nmt_tokenizer(
246247
from nemo.collections.common.tokenizers.tiktoken_tokenizer import TiktokenTokenizer
247248

248249
return TiktokenTokenizer(vocab_file=vocab_file)
250+
elif library == 'null':
251+
assert vocab_size is not None
252+
from nemo.collections.common.tokenizers.null_tokenizer import NullTokenizer
253+
254+
return NullTokenizer(vocab_size)
249255
else:
250256
raise NotImplementedError(
251257
'Currently we only support "huggingface", "sentencepiece", "megatron", and "byte-level" tokenizer'

0 commit comments

Comments
 (0)