


import json
import os
import unittest

from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast
from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class  SCREAMING_SNAKE_CASE__  (				snake_case_						,    unittest.TestCase			):
      snake_case__				:     Union[str, Any]        =						GPTaTokenizer
      snake_case__				:     Optional[int]        =						GPTaTokenizerFast
      snake_case__				:     Tuple        =						True
      snake_case__				:     Any        =						{'''add_prefix_space''': True}
      snake_case__				:     str        =						False
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			List[Any]:
             super().setUp()

             # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
             a_					:		List[str]							=  [
                 'l',
                 'o',
                 'w',
                 'e',
                 'r',
                 's',
                 't',
                 'i',
                 'd',
                 'n',
                 '\u0120',
                 '\u0120l',
                 '\u0120n',
                 '\u0120lo',
                 '\u0120low',
                 'er',
                 '\u0120lowest',
                 '\u0120newer',
                 '\u0120wider',
                 '<unk>',
                 '<|endoftext|>',
             ]
             a_					:		Optional[int]							=  dict(zip(SCREAMING_SNAKE_CASE__     ,							range(len(SCREAMING_SNAKE_CASE__     )     )     )     )
             a_					:		int							=  ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
             a_					:		Optional[int]							=  {'unk_token': '<unk>'}

             a_					:		Dict							=  os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES['vocab_file']     )
             a_					:		List[str]							=  os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES['merges_file']     )
             with open(self.vocab_file     ,							'w'     ,							encoding='utf-8'     ) as fp:
                    fp.write(json.dumps(SCREAMING_SNAKE_CASE__     ) + '\n'     )
             with open(self.merges_file     ,							'w'     ,							encoding='utf-8'     ) as fp:
                    fp.write('\n'.join(SCREAMING_SNAKE_CASE__     )     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							**SCREAMING_SNAKE_CASE__				:       Union[str, Any]     )		->			Union[str, Any]:
             kwargs.update(self.special_tokens_map     )
             return GPTaTokenizer.from_pretrained(self.tmpdirname     ,							**SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							**SCREAMING_SNAKE_CASE__				:       str     )		->			Tuple:
             kwargs.update(self.special_tokens_map     )
             return GPTaTokenizerFast.from_pretrained(self.tmpdirname     ,							**SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			int:
             a_					:		int							=  'lower newer'
             a_					:		Any							=  'lower newer'
             return input_text, output_text
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			str:
             a_					:		str							=  GPTaTokenizer(self.vocab_file     ,							self.merges_file     ,							**self.special_tokens_map     )
             a_					:		Optional[Any]							=  'lower newer'
             a_					:		List[str]							=  ['\u0120low', 'er', '\u0120', 'n', 'e', 'w', 'er']
             a_					:		List[Any]							=  tokenizer.tokenize(SCREAMING_SNAKE_CASE__     ,							add_prefix_space=SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             a_					:		Tuple							=  tokens + [tokenizer.unk_token]
             a_					:		Optional[Any]							=  [1_4, 1_5, 1_0, 9, 3, 2, 1_5, 1_9]
             self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Optional[Any]:





             if not self.test_rust_tokenizer:
                    return

             a_					:		Optional[int]							=  self.get_tokenizer()
             a_					:		Any							=  self.get_rust_tokenizer(add_prefix_space=SCREAMING_SNAKE_CASE__     )

             a_					:		Optional[Any]							=  'lower newer'

             # Testing tokenization
             a_					:		Optional[int]							=  tokenizer.tokenize(SCREAMING_SNAKE_CASE__     ,							add_prefix_space=SCREAMING_SNAKE_CASE__     )
             a_					:		Optional[int]							=  rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             # Testing conversion to ids without special tokens
             a_					:		Dict							=  tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     ,							add_prefix_space=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  rust_tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             # Testing conversion to ids with special tokens
             a_					:		Optional[int]							=  self.get_rust_tokenizer(add_prefix_space=SCREAMING_SNAKE_CASE__     )
             a_					:		int							=  tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_prefix_space=SCREAMING_SNAKE_CASE__     )
             a_					:		Union[str, Any]							=  rust_tokenizer.encode(SCREAMING_SNAKE_CASE__     )
             self.assertListEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

             # Testing the unknown token
             a_					:		Optional[int]							=  tokens + [rust_tokenizer.unk_token]
             a_					:		Dict							=  [1_4, 1_5, 1_0, 9, 3, 2, 1_5, 1_9]
             self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__     )     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							*SCREAMING_SNAKE_CASE__				:       int     ,							**SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Union[str, Any]:
             pass
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       str=1_5     )		->			List[str]:





             for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                    with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
                           a_					:		str							=  self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

                           # Simple input
                           a_					:		Optional[int]							=  'This is a simple input'
                           a_					:		Tuple							=  ['This is a simple input 1', 'This is a simple input 2']
                           a_					:		Optional[Any]							=  ('This is a simple input', 'This is a pair')
                           a_					:		int							=  [
                               ('This is a simple input 1', 'This is a simple input 2'),
                               ('This is a simple pair 1', 'This is a simple pair 2'),
                           ]

                           # Simple input tests
                           self.assertRaises(SCREAMING_SNAKE_CASE__     ,							tokenizer_r.encode     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     )

                           # Simple input
                           self.assertRaises(SCREAMING_SNAKE_CASE__     ,							tokenizer_r.encode_plus     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     )

                           # Simple input
                           self.assertRaises(
                               SCREAMING_SNAKE_CASE__     ,							tokenizer_r.batch_encode_plus     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     ,							)

                           # Pair input
                           self.assertRaises(SCREAMING_SNAKE_CASE__     ,							tokenizer_r.encode     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     )

                           # Pair input
                           self.assertRaises(SCREAMING_SNAKE_CASE__     ,							tokenizer_r.encode_plus     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     )

                           # Pair input
                           self.assertRaises(
                               SCREAMING_SNAKE_CASE__     ,							tokenizer_r.batch_encode_plus     ,							SCREAMING_SNAKE_CASE__     ,							max_length=SCREAMING_SNAKE_CASE__     ,							padding='max_length'     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			Union[str, Any]:
             a_					:		Dict							=  GPTaTokenizer.from_pretrained(self.tmpdirname     ,							pad_token='<pad>'     )

             # Simple input
             a_					:		Tuple							=  'This is a simple input'
             a_					:		List[str]							=  ['This is a simple input looooooooong', 'This is a simple input']
             a_					:		str							=  ('This is a simple input', 'This is a pair')
             a_					:		Union[str, Any]							=  [
                 ('This is a simple input loooooong', 'This is a simple input'),
                 ('This is a simple pair loooooong', 'This is a simple pair'),
             ]

             a_					:		Any							=  tokenizer.pad_token_id

             a_					:		List[Any]							=  tokenizer(SCREAMING_SNAKE_CASE__     ,							padding='max_length'     ,							max_length=3_0     ,							return_tensors='np'     )
             a_					:		List[Any]							=  tokenizer(SCREAMING_SNAKE_CASE__     ,							padding=SCREAMING_SNAKE_CASE__     ,							truncate=SCREAMING_SNAKE_CASE__     ,							return_tensors='np'     )
             a_					:		List[str]							=  tokenizer(*SCREAMING_SNAKE_CASE__     ,							padding='max_length'     ,							max_length=6_0     ,							return_tensors='np'     )
             a_					:		str							=  tokenizer(SCREAMING_SNAKE_CASE__     ,							padding=SCREAMING_SNAKE_CASE__     ,							truncate=SCREAMING_SNAKE_CASE__     ,							return_tensors='np'     )

             # s
             # test single string max_length padding
             self.assertEqual(out_s['input_ids'].shape[-1]     ,							3_0     )
             self.assertTrue(pad_token_id in out_s['input_ids']     )
             self.assertTrue(0 in out_s['attention_mask']     )

             # s2
             # test automatic padding
             self.assertEqual(out_sa['input_ids'].shape[-1]     ,							3_3     )
             # long slice doesn't have padding
             self.assertFalse(pad_token_id in out_sa['input_ids'][0]     )
             self.assertFalse(0 in out_sa['attention_mask'][0]     )
             # short slice does have padding
             self.assertTrue(pad_token_id in out_sa['input_ids'][1]     )
             self.assertTrue(0 in out_sa['attention_mask'][1]     )

             # p
             # test single pair max_length padding
             self.assertEqual(out_p['input_ids'].shape[-1]     ,							6_0     )
             self.assertTrue(pad_token_id in out_p['input_ids']     )
             self.assertTrue(0 in out_p['attention_mask']     )

             # p2
             # test automatic padding pair
             self.assertEqual(out_pa['input_ids'].shape[-1]     ,							5_2     )
             # long slice pair doesn't have padding
             self.assertFalse(pad_token_id in out_pa['input_ids'][0]     )
             self.assertFalse(0 in out_pa['attention_mask'][0]     )
             # short slice pair does have padding
             self.assertTrue(pad_token_id in out_pa['input_ids'][1]     )
             self.assertTrue(0 in out_pa['attention_mask'][1]     )
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			List[Any]:
             a_					:		Optional[int]							=  '$$$'
             a_					:		Any							=  GPTaTokenizer.from_pretrained(self.tmpdirname     ,							bos_token=SCREAMING_SNAKE_CASE__     ,							add_bos_token=SCREAMING_SNAKE_CASE__     )

             a_					:		int							=  'This is a simple input'
             a_					:		str							=  ['This is a simple input 1', 'This is a simple input 2']

             a_					:		Union[str, Any]							=  tokenizer.bos_token_id

             a_					:		List[Any]							=  tokenizer(SCREAMING_SNAKE_CASE__     )
             a_					:		List[str]							=  tokenizer(SCREAMING_SNAKE_CASE__     )

             self.assertEqual(out_s.input_ids[0]     ,							SCREAMING_SNAKE_CASE__     )
             self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids     )     )

             a_					:		str							=  tokenizer.decode(out_s.input_ids     )
             a_					:		Dict							=  tokenizer.batch_decode(out_sa.input_ids     )

             self.assertEqual(decode_s.split()[0]     ,							SCREAMING_SNAKE_CASE__     )
             self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa     )     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Optional[int]:
             pass

      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			str:
             a_					:		Tuple							=  [self.get_tokenizer(do_lower_case=SCREAMING_SNAKE_CASE__     ,							add_bos_token=SCREAMING_SNAKE_CASE__     )]
             for tokenizer in tokenizers:
                    with self.subTest(F"""{tokenizer.__class__.__name__}"""     ):
                           a_					:		Optional[int]							=  'Encode this.'
                           a_					:		Optional[int]							=  'This one too please.'
                           a_					:		Tuple							=  tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
                           encoded_sequence += tokenizer.encode(SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     )
                           a_					:		Dict							=  tokenizer.encode_plus(
                               SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							add_special_tokens=SCREAMING_SNAKE_CASE__     ,							return_special_tokens_mask=SCREAMING_SNAKE_CASE__     ,							)
                           a_					:		List[Any]							=  encoded_sequence_dict['input_ids']
                           a_					:		List[Any]							=  encoded_sequence_dict['special_tokens_mask']
                           self.assertEqual(len(SCREAMING_SNAKE_CASE__     )     ,							len(SCREAMING_SNAKE_CASE__     )     )

                           a_					:		Optional[int]							=  [
                               (x if not special_tokens_mask[i] else None) for i, x in enumerate(SCREAMING_SNAKE_CASE__     )
                           ]
                           a_					:		Optional[int]							=  [x for x in filtered_sequence if x is not None]
                           self.assertEqual(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
@require_tokenizers
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Optional[Any]:
             a_					:		Optional[int]							=  AutoTokenizer.from_pretrained('facebook/opt-350m'     ,							from_slow=SCREAMING_SNAKE_CASE__     )
             a_					:		Dict							=  'A photo of a cat'

             a_					:		Tuple							=  tokenizer.encode(
                 SCREAMING_SNAKE_CASE__     ,							)
             self.assertEqual(SCREAMING_SNAKE_CASE__     ,							[2, 2_5_0, 1_3_4_5, 9, 1_0, 4_7_5_8]     )
             tokenizer.save_pretrained('test_opt'     )

             a_					:		int							=  AutoTokenizer.from_pretrained('./test_opt'     )
             a_					:		Union[str, Any]							=  tokenizer.encode(
                 SCREAMING_SNAKE_CASE__     ,							)
             self.assertEqual(SCREAMING_SNAKE_CASE__     ,							[2, 2_5_0, 1_3_4_5, 9, 1_0, 4_7_5_8]     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			List[str]:
             a_					:		Dict							=  AutoTokenizer.from_pretrained('facebook/opt-350m'     ,							use_slow=SCREAMING_SNAKE_CASE__     )
             a_					:		Union[str, Any]							=  'A photo of a cat'

             a_					:		Optional[int]							=  tokenizer.encode(
                 SCREAMING_SNAKE_CASE__     ,							)
             # Same as above
             self.assertEqual(SCREAMING_SNAKE_CASE__     ,							[2, 2_5_0, 1_3_4_5, 9, 1_0, 4_7_5_8]     )

      @unittest.skip('This test is failing because of a bug in the fast tokenizer'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Union[str, Any]:
             a_					:		List[Any]							=  AutoTokenizer.from_pretrained('facebook/opt-350m'     ,							from_slow=SCREAMING_SNAKE_CASE__     )

             a_					:		Union[str, Any]							=  'bos'
             a_					:		Optional[int]							=  tokenizer.get_vocab()['bos']

             a_					:		Dict							=  'A photo of a cat'
             a_					:		Dict							=  tokenizer.encode(
                 SCREAMING_SNAKE_CASE__     ,							)
             # We changed the bos token
             self.assertEqual(SCREAMING_SNAKE_CASE__     ,							[3_1_9_5_7, 2_5_0, 1_3_4_5, 9, 1_0, 4_7_5_8]     )
             tokenizer.save_pretrained('./tok'     )
             a_					:		Dict							=  AutoTokenizer.from_pretrained('./tok'     )
             self.assertTrue(tokenizer.is_fast     )
             a_					:		int							=  tokenizer.encode(
                 SCREAMING_SNAKE_CASE__     ,							)
             self.assertEqual(SCREAMING_SNAKE_CASE__     ,							[3_1_9_5_7, 2_5_0, 1_3_4_5, 9, 1_0, 4_7_5_8]     )



import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


a_    					=       False
try:
   a_    					=       _is_package_available('google.colab')
except ModuleNotFoundError:
   pass





@input.register
class 		_lowercase						:

 def __init__( self	:							Dict   ,      snake_case	:							str = None   ,      snake_case	:							list = []			)   ->						Union[str, Any]:

  """simple docstring"""





  UpperCamelCase_					:						Any =					0
  UpperCamelCase_					:						Optional[Any] =					choices
  UpperCamelCase_					:						Any =					prompt
  if sys.platform == "win32":
   UpperCamelCase_					:						Optional[Any] =					'*'
  else:
   UpperCamelCase_					:						str =					'➔ '

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[int]   ,      snake_case	:							List[str]   ,      snake_case	:							str = ""			)   ->						List[Any]:

  """simple docstring"""





  if sys.platform != "win32":
   writeColor(self.choices[index]   ,      3_2   ,      snake_case			)
  else:
   forceWrite(self.choices[index]   ,      snake_case			)

 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[str]   ,      snake_case	:							int			)   ->						List[Any]:

  """simple docstring"""





  if index == self.position:
   forceWrite(f" {self.arrow_char} "			)
   self.write_choice(snake_case			)
  else:
   forceWrite(f"    {self.choices[index]}"			)
  reset_cursor()

 def 						SCREAMING_SNAKE_CASE__   ( self	:							Any   ,      snake_case	:							Direction   ,      snake_case	:							int = 1			)   ->						List[str]:

  """simple docstring"""





  UpperCamelCase_					:						Any =					self.position
  if direction == Direction.DOWN:
   if self.position + 1 >= len(self.choices			):
    return
   self.position += num_spaces
  else:
   if self.position - 1 < 0:
    return
   self.position -= num_spaces
  clear_line()
  self.print_choice(snake_case			)
  move_cursor(snake_case   ,      direction.name			)
  self.print_choice(self.position			)

 @input.mark(KEYMAP['up']			)
 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[int]			)   ->						List[Any]:

  """simple docstring"""





  self.move_direction(Direction.UP			)

 @input.mark(KEYMAP['down']			)
 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[int]			)   ->						Optional[Any]:

  """simple docstring"""





  self.move_direction(Direction.DOWN			)

 @input.mark(KEYMAP['newline']			)
 def 						SCREAMING_SNAKE_CASE__   ( self	:							str			)   ->						Optional[int]:

  """simple docstring"""





  move_cursor(len(self.choices			) - self.position   ,      'DOWN'			)
  return self.position

 @input.mark(KEYMAP['interrupt']			)
 def 						SCREAMING_SNAKE_CASE__   ( self	:							Optional[Any]			)   ->						Optional[Any]:

  """simple docstring"""





  move_cursor(len(self.choices			) - self.position   ,      'DOWN'			)
  raise KeyboardInterrupt

 @input.mark_multiple(*[KEYMAP[str(snake_case			)] for number in range(1_0			)]			)
 def 						SCREAMING_SNAKE_CASE__   ( self	:							int			)   ->						Dict:

  """simple docstring"""





  UpperCamelCase_					:						List[Any] =					int(chr(self.current_selection			)			)
  UpperCamelCase_					:						Optional[int] =					index - self.position
  if index == self.position:
   return
  if index < len(self.choices			):
   if self.position > index:
    self.move_direction(Direction.UP   ,      -movement			)
   elif self.position < index:
    self.move_direction(Direction.DOWN   ,      snake_case			)
   else:
    return
  else:
   return


 def 						SCREAMING_SNAKE_CASE__   ( self	:							List[Any]   ,      snake_case	:							int = 0			)   ->						Union[str, Any]:

  """simple docstring"""





  if self.prompt:
   linebreak()
   forceWrite(self.prompt   ,      '\n'			)
   if in_colab:
    forceWrite('Please input a choice index (starting from 0), and press enter'   ,      '\n'			)
   else:
    forceWrite('Please select a choice using the arrow or number keys, and selecting with enter'   ,      '\n'			)
  UpperCamelCase_					:						Optional[int] =					default_choice
  for i in range(len(self.choices			)			):
   self.print_choice(snake_case			)
   forceWrite('\n'			)
  move_cursor(len(self.choices			) - self.position   ,      'UP'			)
  with cursor.hide():
   while True:
    if in_colab:
     try:
      UpperCamelCase_					:						Tuple =					int(builtins.input()			)
     except ValueError:
      UpperCamelCase_					:						Tuple =					default_choice
    else:
     UpperCamelCase_					:						Optional[Any] =					self.handle_input()
    if choice is not None:
     reset_cursor()
     for _ in range(len(self.choices			) + 1			):
      move_cursor(1   ,      'UP'			)
      clear_line()
     self.write_choice(snake_case   ,      '\n'			)
     return choice



