



"""simple docstring"""


import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    YosoForMaskedLM,
				    YosoForMultipleChoice,
				    YosoForQuestionAnswering,
				    YosoForSequenceClassification,
				    YosoForTokenClassification,
				    YosoModel,
				)
				from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST







class  A_     :



						"""simple docstring"""






						def __init__(   self					:Union[str, Any]  ,    lowercase_					:Any  ,    lowercase_					:Optional[Any]=13  ,    lowercase_					:Union[str, Any]=7  ,    lowercase_					:Tuple=True  ,    lowercase_					:int=True  ,    lowercase_					:Tuple=True  ,    lowercase_					:str=True  ,    lowercase_					:List[Any]=99  ,    lowercase_					:Any=32  ,    lowercase_					:Any=5  ,    lowercase_					:Any=4  ,    lowercase_					:int=37  ,    lowercase_					:Optional[Any]="gelu"  ,    lowercase_					:List[Any]=0.1  ,    lowercase_					:Any=0.1  ,    lowercase_					:Union[str, Any]=5_12  ,    lowercase_					:Optional[Any]=16  ,    lowercase_					:Dict=2  ,    lowercase_					:Optional[Any]=0.02  ,    lowercase_					:Optional[int]=3  ,    lowercase_					:Dict=4  ,    lowercase_					:Dict=None  ,    )							->     str:
												UpperCAmelCase			 =					parent
												UpperCAmelCase			 =					batch_size
												UpperCAmelCase			 =					seq_length
												UpperCAmelCase			 =					is_training
												UpperCAmelCase			 =					use_input_mask
												UpperCAmelCase			 =					use_token_type_ids
												UpperCAmelCase			 =					use_labels
												UpperCAmelCase			 =					vocab_size
												UpperCAmelCase			 =					hidden_size
												UpperCAmelCase			 =					num_hidden_layers
												UpperCAmelCase			 =					num_attention_heads
												UpperCAmelCase			 =					intermediate_size
												UpperCAmelCase			 =					hidden_act
												UpperCAmelCase			 =					hidden_dropout_prob
												UpperCAmelCase			 =					attention_probs_dropout_prob
												UpperCAmelCase			 =					max_position_embeddings
												UpperCAmelCase			 =					type_vocab_size
												UpperCAmelCase			 =					type_sequence_label_size
												UpperCAmelCase			 =					initializer_range
												UpperCAmelCase			 =					num_labels
												UpperCAmelCase			 =					num_choices
												UpperCAmelCase			 =					scope






						def       UpperCAmelCase__	(   self					:Tuple   )							->     int:
												UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size   )

												UpperCAmelCase			 =					None
												if self.use_input_mask:
																		UpperCAmelCase			 =					random_attention_mask([self.batch_size, self.seq_length]   )

												UpperCAmelCase			 =					None
												if self.use_token_type_ids:
																		UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.type_vocab_size   )

												UpperCAmelCase			 =					None
												UpperCAmelCase			 =					None
												UpperCAmelCase			 =					None
												if self.use_labels:
																		UpperCAmelCase			 =					ids_tensor([self.batch_size]  ,    self.type_sequence_label_size   )
																		UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.num_labels   )
																		UpperCAmelCase			 =					ids_tensor([self.batch_size]  ,    self.num_choices   )

												UpperCAmelCase			 =					self.get_config()

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






						def       UpperCAmelCase__	(   self					:Optional[int]   )							->     List[Any]:
												return YosoConfig(
												    vocab_size=self.vocab_size  ,    hidden_size=self.hidden_size  ,    num_hidden_layers=self.num_hidden_layers  ,    num_attention_heads=self.num_attention_heads  ,    intermediate_size=self.intermediate_size  ,    hidden_act=self.hidden_act  ,    hidden_dropout_prob=self.hidden_dropout_prob  ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,    max_position_embeddings=self.max_position_embeddings  ,    type_vocab_size=self.type_vocab_size  ,    is_decoder=lowercase_  ,    initializer_range=self.initializer_range  ,    )






						def       UpperCAmelCase__	(   self					:int   )							->     Union[str, Any]:
												UpperCAmelCase			 =					self.get_config()
												UpperCAmelCase			 =					3_00
												return config






						def       UpperCAmelCase__	(   self					:str   )							->     Optional[Any]:
												(
												    (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       
												)			 =					self.prepare_config_and_inputs()

												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
												UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    vocab_size=2   )

												return (
												    config,
												    input_ids,
												    token_type_ids,
												    input_mask,
												    sequence_labels,
												    token_labels,
												    choice_labels,
												    encoder_hidden_states,
												    encoder_attention_mask,
												)






						def       UpperCAmelCase__	(   self					:List[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Dict  ,    lowercase_					:Optional[int]  ,    lowercase_					:Dict  ,    lowercase_					:str  ,    lowercase_					:Union[str, Any]   )							->     Tuple:
												UpperCAmelCase			 =					YosoModel(config=lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_   )
												UpperCAmelCase			 =					model(lowercase_  ,    token_type_ids=lowercase_   )
												UpperCAmelCase			 =					model(lowercase_   )
												self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






						def       UpperCAmelCase__	(   self					:int  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Dict  ,    lowercase_					:List[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Tuple  ,    lowercase_					:str  ,    lowercase_					:Tuple  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[int]  ,    )							->     Tuple:
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					YosoModel(lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(
												    lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    encoder_hidden_states=lowercase_  ,    encoder_attention_mask=lowercase_  ,    )
												UpperCAmelCase			 =					model(
												    lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    encoder_hidden_states=lowercase_  ,    )
												UpperCAmelCase			 =					model(lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_   )
												self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






						def       UpperCAmelCase__	(   self					:Optional[Any]  ,    lowercase_					:List[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Optional[Any]  ,    lowercase_					:List[str]  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:int   )							->     Optional[int]:
												UpperCAmelCase			 =					YosoForMaskedLM(config=lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    labels=lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.vocab_size)   )






						def       UpperCAmelCase__	(   self					:str  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Dict  ,    lowercase_					:int  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[int]  ,    lowercase_					:Optional[int]   )							->     int:
												UpperCAmelCase			 =					YosoForQuestionAnswering(config=lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(
												    lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    start_positions=lowercase_  ,    end_positions=lowercase_  ,    )
												self.parent.assertEqual(result.start_logits.shape  ,    (self.batch_size, self.seq_length)   )
												self.parent.assertEqual(result.end_logits.shape  ,    (self.batch_size, self.seq_length)   )






						def       UpperCAmelCase__	(   self					:Dict  ,    lowercase_					:Optional[int]  ,    lowercase_					:List[Any]  ,    lowercase_					:int  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Dict   )							->     Union[str, Any]:
												UpperCAmelCase			 =					self.num_labels
												UpperCAmelCase			 =					YosoForSequenceClassification(lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    labels=lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_labels)   )






						def       UpperCAmelCase__	(   self					:Optional[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Optional[int]  ,    lowercase_					:int  ,    lowercase_					:int  ,    lowercase_					:str  ,    lowercase_					:int  ,    lowercase_					:Optional[Any]   )							->     Optional[Any]:
												UpperCAmelCase			 =					self.num_labels
												UpperCAmelCase			 =					YosoForTokenClassification(config=lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					model(lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    labels=lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.num_labels)   )






						def       UpperCAmelCase__	(   self					:str  ,    lowercase_					:Tuple  ,    lowercase_					:List[str]  ,    lowercase_					:List[Any]  ,    lowercase_					:Tuple  ,    lowercase_					:int  ,    lowercase_					:List[str]  ,    lowercase_					:str   )							->     Tuple:
												UpperCAmelCase			 =					self.num_choices
												UpperCAmelCase			 =					YosoForMultipleChoice(config=lowercase_   )
												model.to(lowercase_   )
												model.eval()
												UpperCAmelCase			 =					input_ids.unsqueeze(1   ).expand(-1  ,    self.num_choices  ,    -1   ).contiguous()
												UpperCAmelCase			 =					token_type_ids.unsqueeze(1   ).expand(-1  ,    self.num_choices  ,    -1   ).contiguous()
												UpperCAmelCase			 =					input_mask.unsqueeze(1   ).expand(-1  ,    self.num_choices  ,    -1   ).contiguous()
												UpperCAmelCase			 =					model(
												    lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    labels=lowercase_  ,    )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_choices)   )






						def       UpperCAmelCase__	(   self					:Dict   )							->     str:
												UpperCAmelCase			 =					self.prepare_config_and_inputs()
												(
												    (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       
												)			 =					config_and_inputs
												UpperCAmelCase			 =					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
												return config, inputs_dict







@require_torch
class  A_     (	SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_      ,  unittest.TestCase							):



						"""simple docstring"""
						__UpperCamelCase													=				(
						    (
						        YosoModel,
						        YosoForMaskedLM,
						        YosoForMultipleChoice,
						        YosoForQuestionAnswering,
						        YosoForSequenceClassification,
						        YosoForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False

						__UpperCamelCase													=				()
						__UpperCamelCase													=				(
						    {
						        """feature-extraction""": YosoModel,
						        """fill-mask""": YosoForMaskedLM,
						        """question-answering""": YosoForQuestionAnswering,
						        """text-classification""": YosoForSequenceClassification,
						        """token-classification""": YosoForTokenClassification,
						        """zero-shot""": YosoForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)






						def       UpperCAmelCase__	(   self					:Union[str, Any]   )							->     str:
												UpperCAmelCase			 =					YosoModelTester(self   )
												UpperCAmelCase			 =					ConfigTester(self  ,    config_class=lowercase_  ,    hidden_size=37   )






						def       UpperCAmelCase__	(   self					:int   )							->     List[str]:
												self.config_tester.run_common_tests()






						def       UpperCAmelCase__	(   self					:Any   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*lowercase_   )






						def       UpperCAmelCase__	(   self					:str   )							->     str:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																		UpperCAmelCase			 =					type
																		self.model_tester.create_and_check_model(*lowercase_   )






						def       UpperCAmelCase__	(   self					:Union[str, Any]   )							->     Optional[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*lowercase_   )






						def       UpperCAmelCase__	(   self					:str   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*lowercase_   )






						def       UpperCAmelCase__	(   self					:Optional[int]   )							->     List[str]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*lowercase_   )






						def       UpperCAmelCase__	(   self					:int   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*lowercase_   )






						def       UpperCAmelCase__	(   self					:Optional[Any]   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*lowercase_   )






						@slow
						def       UpperCAmelCase__	(   self					:int   )							->     Union[str, Any]:
												for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCAmelCase			 =					YosoModel.from_pretrained(lowercase_   )
																		self.assertIsNotNone(lowercase_   )






						def       UpperCAmelCase__	(   self					:Optional[int]   )							->     Dict:
												return







@require_torch
class  A_     (	unittest.TestCase							):



						"""simple docstring"""






						@slow
						def       UpperCAmelCase__	(   self					:int   )							->     str:
												UpperCAmelCase			 =					YosoModel.from_pretrained('uw-madison/yoso-4096'   )
												UpperCAmelCase			 =					torch.tensor([[0, 1, 2, 3, 4, 5]]   )

												with torch.no_grad():
																		UpperCAmelCase			 =					model(lowercase_   )[0]

												UpperCAmelCase			 =					torch.Size((1, 6, 7_68)   )
												self.assertEqual(output.shape  ,    lowercase_   )

												UpperCAmelCase			 =					torch.tensor(
												    [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]   )

												self.assertTrue(torch.allclose(output[:, :3, :3]  ,    lowercase_  ,    atol=1E-4   )   )






						@slow
						def       UpperCAmelCase__	(   self					:Dict   )							->     Optional[Any]:
												UpperCAmelCase			 =					YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'   )
												UpperCAmelCase			 =					torch.tensor([[0, 1, 2, 3, 4, 5]]   )

												with torch.no_grad():
																		UpperCAmelCase			 =					model(lowercase_   )[0]

												UpperCAmelCase			 =					5_02_65

												UpperCAmelCase			 =					torch.Size((1, 6, vocab_size)   )
												self.assertEqual(output.shape  ,    lowercase_   )

												UpperCAmelCase			 =					torch.tensor(
												    [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]   )

												self.assertTrue(torch.allclose(output[:, :3, :3]  ,    lowercase_  ,    atol=1E-4   )   )






						@slow
						def       UpperCAmelCase__	(   self					:List[str]   )							->     str:
												UpperCAmelCase			 =					YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'   )
												UpperCAmelCase			 =					torch.arange(40_96   ).unsqueeze(0   )

												with torch.no_grad():
																		UpperCAmelCase			 =					model(lowercase_   )[0]

												UpperCAmelCase			 =					5_02_65

												UpperCAmelCase			 =					torch.Size((1, 40_96, vocab_size)   )
												self.assertEqual(output.shape  ,    lowercase_   )

												UpperCAmelCase			 =					torch.tensor(
												    [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]   )

												self.assertTrue(torch.allclose(output[:, :3, :3]  ,    lowercase_  ,    atol=1E-4   )   )













"""simple docstring"""

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     A_				(   unittest.TestCase	):
				UpperCAmelCase__										=					EsmTokenizer







				def      _snake_case       (				self				:    List[str]						)					->       Dict:
										super().setUp()
										__magic_name__      =  tempfile.mkdtemp()
										# fmt: off
										__magic_name__      =  ["<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
										__magic_name__      =  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      _snake_case       (				self				:    Optional[Any]    ,     **__lowerCamelCase				:    str						)					->       List[PreTrainedTokenizerBase]:
										return [self.get_tokenizer(**__lowerCamelCase						)]







				def      _snake_case       (				self				:    Dict    ,     **__lowerCamelCase				:    int						)					->       PreTrainedTokenizer:
										return self.tokenizer_class.from_pretrained(self.tmpdirname    ,     **__lowerCamelCase						)







				def      _snake_case       (				self				:    str						)					->       Optional[int]:
										__magic_name__      =  self.tokenizer_class(self.vocab_file						)

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







				def      _snake_case       (				self				:    int						)					->       Dict:
										__magic_name__      =  self.tokenizer_class(self.vocab_file						)

										__magic_name__      =  "LAGVS"
										self.assertListEqual(tokenizer.encode(__lowerCamelCase						)    ,     [0, 4, 5, 6, 7, 8, 2]						)







				def      _snake_case       (				self				:    List[Any]						)					->       Tuple:
										__magic_name__      =  self.tokenizer_class(self.vocab_file						)

										__magic_name__      =  ["LAGVS", "WCB"]
										__magic_name__      =  tokenizer(__lowerCamelCase    ,     padding=__lowerCamelCase						)["input_ids"]

										self.assertListEqual(__lowerCamelCase    ,     [[0, 4, 5, 6, 7, 8, 2], [0, 2_2, 2_3, 2_5, 2]]						)







				def      _snake_case       (				self				:    Union[str, Any]						)					->       List[str]:
										__magic_name__      =  self.tokenizer_class(self.vocab_file						)

										__magic_name__      =  ["LAGVS", "WCB"]
										__magic_name__      =  tokenizer(__lowerCamelCase    ,     padding=__lowerCamelCase						)["input_ids"]

										self.assertListEqual(__lowerCamelCase    ,     [[0, 4, 5, 6, 7, 8, 2], [0, 2_2, 2_3, 2_5, 2, 1, 1]]						)







				def      _snake_case       (				self				:    Union[str, Any]						)					->       Dict:
										__magic_name__      =  self.get_tokenizers(fast=__lowerCamelCase						)
										for tokenizer in tokenizers:
																with self.subTest(f'''{tokenizer.__class__.__name__}'''						):
																						__magic_name__      =  "<unk>"
																						__magic_name__      =  "<mask>"

																						__magic_name__      =  tokenizer.tokenize(__lowerCamelCase						)
																						__magic_name__      =  tokenizer.tokenize(__lowerCamelCase						)

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



