Create a huggingface pretrained tokenizer. Args: name (str): The name of the tokenizer. correct_pad_token (bool): Whether to correct the pad token id. correct_gemma2 (bool): Whether to correct the gemma2 tokenizer. **kwargs: The keyword arguments for the tokenize
(name_or_path, correct_pad_token=True, correct_gemma2=True, **kwargs)
| 33 | |
| 34 | |
| 35 | def hf_tokenizer(name_or_path, correct_pad_token=True, correct_gemma2=True, **kwargs): |
| 36 | """Create a huggingface pretrained tokenizer. |
| 37 | |
| 38 | Args: |
| 39 | name (str): The name of the tokenizer. |
| 40 | correct_pad_token (bool): Whether to correct the pad token id. |
| 41 | correct_gemma2 (bool): Whether to correct the gemma2 tokenizer. |
| 42 | **kwargs: The keyword arguments for the tokenizer. |
| 43 | |
| 44 | Returns: |
| 45 | transformers.PreTrainedTokenizer: The pretrained tokenizer. |
| 46 | |
| 47 | """ |
| 48 | from transformers import AutoTokenizer |
| 49 | if correct_gemma2 and isinstance(name_or_path, str) and 'gemma-2-2b-it' in name_or_path: |
| 50 | # the EOS token in gemma2 is ambiguious, which may worsen RL performance. |
| 51 | # https://huggingface.co/google/gemma-2-2b-it/commit/17a01657f5c87135bcdd0ec7abb4b2dece04408a |
| 52 | warnings.warn('Found gemma-2-2b-it tokenizer. Set eos_token and eos_token_id to <end_of_turn> and 107.') |
| 53 | kwargs['eos_token'] = '<end_of_turn>' |
| 54 | kwargs['eos_token_id'] = 107 |
| 55 | tokenizer = AutoTokenizer.from_pretrained(name_or_path, **kwargs) |
| 56 | if correct_pad_token: |
| 57 | set_pad_token_id(tokenizer) |
| 58 | return tokenizer |
no test coverage detected