




import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase




@require_tokenizers
class 	UpperCamelCase__			(unittest.TestCase  ):

				'''simple docstring'''



				lowerCamelCase_   :			Union[str, Any]			  =   EsmTokenizer







				def 					_lowercase  (					self  )  ->   str:
											super().setUp()
											lowerCamelCase     :   str    				=      tempfile.mkdtemp()
											# fmt: off
											lowerCamelCase     :   List[str]    				=      ["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
											# fmt: on
											lowerCamelCase     :   Union[str, Any]    				=      os.path.join(self.tmpdirname  ,						VOCAB_FILES_NAMES["vocab_file"]  )
											with open(self.vocab_file  ,						"w"  ,						encoding="utf-8"  ) as vocab_writer:
																		vocab_writer.write("".join([x + "\n" for x in vocab_tokens]  )  )







				def 					_lowercase  (					self  ,						**UpperCamelCase__  )  ->   Optional[Any]:
											return [self.get_tokenizer(**UpperCAmelCase_  )]







				def 					_lowercase  (					self  ,						**UpperCamelCase__  )  ->   Union[str, Any]:
											return self.tokenizer_class.from_pretrained(self.tmpdirname  ,						**UpperCAmelCase_  )







				def 					_lowercase  (					self  )  ->   Tuple:
											lowerCamelCase     :   Optional[Any]    				=      self.tokenizer_class(self.vocab_file  )

											lowerCamelCase     :   Tuple    				=      tokenizer.tokenize("LAGVS"  )
											self.assertListEqual(UpperCAmelCase_  ,						["L", "A", "G", "V", "S"]  )
											self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_  )  ,						[4, 5, 6, 7, 8]  )







				def 					_lowercase  (					self  )  ->   Optional[int]:
											lowerCamelCase     :   Union[str, Any]    				=      self.tokenizer_class(self.vocab_file  )

											lowerCamelCase     :   int    				=      'LAGVS'
											self.assertListEqual(tokenizer.encode(UpperCAmelCase_  )  ,						[0, 4, 5, 6, 7, 8, 2]  )







				def 					_lowercase  (					self  )  ->   List[str]:
											lowerCamelCase     :   Any    				=      self.tokenizer_class(self.vocab_file  )

											lowerCamelCase     :   Optional[Any]    				=      ['LAGVS', 'WCB']
											lowerCamelCase     :   List[Any]    				=      tokenizer(UpperCAmelCase_  ,						padding=UpperCAmelCase_  )['input_ids']

											self.assertListEqual(UpperCAmelCase_  ,						[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]  )







				def 					_lowercase  (					self  )  ->   List[str]:
											lowerCamelCase     :   Tuple    				=      self.tokenizer_class(self.vocab_file  )

											lowerCamelCase     :   Optional[Any]    				=      ['LAGVS', 'WCB']
											lowerCamelCase     :   Tuple    				=      tokenizer(UpperCAmelCase_  ,						padding=UpperCAmelCase_  )['input_ids']

											self.assertListEqual(UpperCAmelCase_  ,						[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]  )







				def 					_lowercase  (					self  )  ->   Union[str, Any]:
											lowerCamelCase     :   Union[str, Any]    				=      self.get_tokenizers(fast=UpperCAmelCase_  )
											for tokenizer in tokenizers:
																		with self.subTest(F'''{tokenizer.__class__.__name__}'''  ):
																									lowerCamelCase     :   int    				=      '<unk>'
																									lowerCamelCase     :   Any    				=      '<mask>'

																									lowerCamelCase     :   Optional[Any]    				=      tokenizer.tokenize(UpperCAmelCase_  )
																									lowerCamelCase     :   Dict    				=      tokenizer.tokenize(UpperCAmelCase_  )

																									self.assertEqual(len(UpperCAmelCase_  )  ,						1  )
																									self.assertEqual(len(UpperCAmelCase_  )  ,						1  )
																									self.assertEqual(token_a[0]  ,						UpperCAmelCase_  )
																									self.assertEqual(token_a[0]  ,						UpperCAmelCase_  )












import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
       import torch

       from transformers import GPTaLMHeadModel




@require_torch
@require_sentencepiece
@require_tokenizers
class 	UpperCamelCase__			(unittest.TestCase  ):

    '''simple docstring'''



    @slow
    @unittest.skip("Model is not available."  )
    def 					_lowercase  (					self  )  ->   List[Any]:
           lowerCamelCase     :   List[Any]    				=      "nvidia/megatron-gpt2-345m/"
           if "MYDIR" in os.environ:
                  lowerCamelCase     :   Union[str, Any]    				=      os.path.join(os.environ["MYDIR"]  ,						UpperCamelCase__  )
           lowerCamelCase     :   Any    				=      GPTaLMHeadModel.from_pretrained(UpperCamelCase__  )
           model.to(UpperCamelCase__  )
           model.half()

           lowerCamelCase     :   Dict    				=      torch.tensor(
               [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  ,						device=UpperCamelCase__  ,						dtype=torch.long  ,						)

           with torch.no_grad():
                  lowerCamelCase     :   Any    				=      model(UpperCamelCase__  ).logits

           lowerCamelCase     :   Union[str, Any]    				=      torch.Size((1, 9, 5_0257)  )
           self.assertEqual(output.shape  ,						UpperCamelCase__  )

           lowerCamelCase     :   List[str]    				=      torch.tensor(
               [
                   4.9414,
                   -0.2920,
                   -1.2148,
                   -4.0273,
                   -0.5161,
                   -5.2109,
                   -1.2412,
                   -1.8301,
                   -1.7734,
                   -4.7148,
                   -0.2317,
                   -1.0811,
                   -2.1777,
                   0.4141,
                   -3.7969,
                   -4.0586,
                   -2.5332,
                   -3.3809,
                   4.3867,
               ]  ,						device=UpperCamelCase__  ,						dtype=torch.half  ,						)

           for i in range(19  ):
                  lowerCamelCase				,   lowerCamelCase     :   Optional[int]    				=      8 * i // 17, 2792 * i  # along the diagonal
                  lowerCamelCase				,   lowerCamelCase     :   Tuple    				=      output[0, r, c], expected_diag[i]
                  lowerCamelCase     :   List[str]    				=      F'''row={r} col={c} computed={computed} expected={expected}'''
                  self.assertAlmostEqual(UpperCamelCase__  ,						UpperCamelCase__  ,						delta=1e-4  ,						msg=UpperCamelCase__  )






