22
33import json
44import pickle
5+ from pathlib import Path
56from types import SimpleNamespace
7+ from typing import Any
68from unittest .mock import MagicMock , patch
79
810import numpy as np
@@ -138,9 +140,16 @@ def test_hf_tokenizer():
138140
139141
140142@pytest .fixture
141- def gigatoken_hf_pair (tmp_path ) :
143+ def gigatoken_hf_pair (tmp_path : Path ) -> tuple [ HuggingFaceTokenizer , HuggingFaceTokenizer ] :
142144 pytest .importorskip ('gigatoken' )
143- from tokenizers import Tokenizer , decoders , models , pre_tokenizers , processors , trainers
145+ from tokenizers import ( # type: ignore[import-untyped]
146+ Tokenizer ,
147+ decoders ,
148+ models ,
149+ pre_tokenizers ,
150+ processors ,
151+ trainers ,
152+ )
144153 from transformers import PreTrainedTokenizerFast
145154
146155 backend = Tokenizer (models .BPE ())
@@ -177,7 +186,9 @@ def gigatoken_hf_pair(tmp_path):
177186 return baseline , native
178187
179188
180- def test_gigatoken_user_text_and_special_filtering (gigatoken_hf_pair ):
189+ def test_gigatoken_user_text_and_special_filtering (
190+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
191+ ) -> None :
181192 hf , native = gigatoken_hf_pair
182193 texts = ['Invoice 1234567' , 'x<eos>12345<extra>y' , '<ordinary> café 中文 ١٢٣٤٥٦٧' ]
183194 with patch .object (
@@ -211,7 +222,9 @@ def test_gigatoken_user_text_and_special_filtering(gigatoken_hf_pair):
211222 'character' ,
212223 ['\u0295 ' , '\ua7cf ' , '\u1acf ' , '\U00011de0 ' , '\U00016ff4 ' , '\U0002b73a ' , '\U00033479 ' ],
213224)
214- def test_gigatoken_unicode_fallback (gigatoken_hf_pair , character ):
225+ def test_gigatoken_unicode_fallback (
226+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ], character : str
227+ ) -> None :
215228 hf , native = gigatoken_hf_pair
216229 with (
217230 patch .object (native .tokenizer , '_gigatoken' ) as encode ,
@@ -239,7 +252,9 @@ def test_gigatoken_unicode_fallback(gigatoken_hf_pair, character):
239252
240253
241254@pytest .mark .parametrize ('split_special_tokens' , [False , True ])
242- def test_gigatoken_unicode_batch_fallback (gigatoken_hf_pair , split_special_tokens ):
255+ def test_gigatoken_unicode_batch_fallback (
256+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ], split_special_tokens : bool
257+ ) -> None :
243258 hf , native = gigatoken_hf_pair
244259 batch = ['Invoice 1234567 café 中文' , '12345\U00011de0 6789 <extra><ordinary>' ]
245260 name = '_split_special_tokenizer' if split_special_tokens else '_gigatoken'
@@ -250,7 +265,9 @@ def test_gigatoken_unicode_batch_fallback(gigatoken_hf_pair, split_special_token
250265 encode .assert_not_called ()
251266
252267
253- def test_gigatoken_unicode_chat_fallback (gigatoken_hf_pair ):
268+ def test_gigatoken_unicode_chat_fallback (
269+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
270+ ) -> None :
254271 hf , native = gigatoken_hf_pair
255272 conversation = [{'role' : 'user' , 'content' : 'Invoice 1234567' }]
256273 # The rendered template can introduce an affected character absent from messages.
@@ -263,7 +280,9 @@ def test_gigatoken_unicode_chat_fallback(gigatoken_hf_pair):
263280
264281
265282@pytest .mark .parametrize ('text' , ['Invoice 1234567' , 'café e\u0301 中文 ١٢٣٤٥٦٧ 12345 😀' ])
266- def test_gigatoken_compatible_unicode_stays_native (gigatoken_hf_pair , text ):
283+ def test_gigatoken_compatible_unicode_stays_native (
284+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ], text : str
285+ ) -> None :
267286 hf , native = gigatoken_hf_pair
268287 for split in (False , True ):
269288 name = '_split_special_tokenizer' if split else '_gigatoken'
@@ -277,9 +296,14 @@ def test_gigatoken_compatible_unicode_stays_native(gigatoken_hf_pair, text):
277296@pytest .mark .parametrize ('batched' , [False , True ])
278297@pytest .mark .parametrize ('return_dict' , [False , True , None ])
279298@pytest .mark .parametrize ('tensor_type' , [None , 'np' , 'pt' ])
280- def test_gigatoken_chat_template_parity (gigatoken_hf_pair , batched , return_dict , tensor_type ):
299+ def test_gigatoken_chat_template_parity (
300+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
301+ batched : bool ,
302+ return_dict : bool | None ,
303+ tensor_type : str | None ,
304+ ) -> None :
281305 hf , native = gigatoken_hf_pair
282- conversation = [{'role' : 'user' , 'content' : 'Invoice 1234567 <extra> café' }]
306+ conversation : list [ Any ] = [{'role' : 'user' , 'content' : 'Invoice 1234567 <extra> café' }]
283307 if batched :
284308 conversation = [conversation , [{'role' : 'user' , 'content' : '中文 ١٢٣٤٥٦٧' }]]
285309 kwargs = {'tokenize' : True , 'add_generation_prompt' : True , 'return_tensors' : tensor_type }
@@ -314,7 +338,11 @@ def test_gigatoken_chat_template_parity(gigatoken_hf_pair, batched, return_dict,
314338
315339@pytest .mark .parametrize ('side' , ['left' , 'right' ])
316340@pytest .mark .parametrize ('truncation' , [False , True ])
317- def test_gigatoken_chat_padding_and_truncation (gigatoken_hf_pair , side , truncation ):
341+ def test_gigatoken_chat_padding_and_truncation (
342+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
343+ side : str ,
344+ truncation : bool ,
345+ ) -> None :
318346 hf , native = gigatoken_hf_pair
319347 for tokenizer in (hf .tokenizer , native .tokenizer ):
320348 tokenizer .padding_side = tokenizer .truncation_side = side
@@ -325,7 +353,9 @@ def test_gigatoken_chat_padding_and_truncation(gigatoken_hf_pair, side, truncati
325353 ) == hf .apply_chat_template (messages , chat_template = None , ** kwargs )
326354
327355
328- def test_gigatoken_chat_rendering_and_assistant_masks (gigatoken_hf_pair ):
356+ def test_gigatoken_chat_rendering_and_assistant_masks (
357+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
358+ ) -> None :
329359 hf , native = gigatoken_hf_pair
330360 conversation = [
331361 {'role' : 'user' , 'content' : 'Invoice 1234567' },
@@ -336,7 +366,7 @@ def test_gigatoken_chat_rendering_and_assistant_masks(gigatoken_hf_pair):
336366 '{% if m.role == "assistant" %}{% generation %}{{ m.content }}{% endgeneration %}'
337367 '{% else %}{{ m.content }}{% endif %}<eos>{% endfor %}'
338368 )
339- for kwargs in [
369+ cases : list [ dict [ str , Any ]] = [
340370 {'tokenize' : False },
341371 {'tokenize' : True , 'return_dict' : False , 'continue_final_message' : True },
342372 {'tokenize' : True , 'return_dict' : True , 'return_assistant_tokens_mask' : True },
@@ -345,13 +375,16 @@ def test_gigatoken_chat_rendering_and_assistant_masks(gigatoken_hf_pair):
345375 'return_dict' : True ,
346376 'tokenizer_kwargs' : {'return_offsets_mapping' : True },
347377 },
348- ]:
378+ ]
379+ for kwargs in cases :
349380 assert native .apply_chat_template (
350381 conversation , chat_template = template , ** kwargs
351382 ) == hf .apply_chat_template (conversation , chat_template = template , ** kwargs )
352383
353384
354- def test_gigatoken_serving_worker_serialization (gigatoken_hf_pair ):
385+ def test_gigatoken_serving_worker_serialization (
386+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
387+ ) -> None :
355388 hf , native = gigatoken_hf_pair
356389 text = 'Invoice 1234567 <extra> café'
357390 native .tokenizer (
@@ -375,7 +408,9 @@ def test_gigatoken_serving_worker_serialization(gigatoken_hf_pair):
375408 ) == hf .apply_chat_template (messages , chat_template = None , return_dict = False )
376409
377410
378- def test_gigatoken_streaming_preserves_hf_interface (gigatoken_hf_pair ):
411+ def test_gigatoken_streaming_preserves_hf_interface (
412+ gigatoken_hf_pair : tuple [HuggingFaceTokenizer , HuggingFaceTokenizer ],
413+ ) -> None :
379414 from megatron .core .inference .text_generation_server .dynamic_text_gen_server .incremental_detokenizer import (
380415 HuggingFaceFastIncrementalDetokenizer ,
381416 )
0 commit comments