



from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import numpy
				import tensorflow as tf

				from transformers.models.esm.modeling_tf_esm import (
				    TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFEsmForMaskedLM,
				    TFEsmForSequenceClassification,
				    TFEsmForTokenClassification,
				    TFEsmModel,
				)



class        lowerCAmelCase__					:


				def __init__(							self      :  Union[str, Any]						, __UpperCamelCase      :  Union[str, Any]						, )		->     int:
											A      						=    parent
											A      						=    13
											A      						=    7
											A      						=    True
											A      						=    True
											A      						=    True
											A      						=    99
											A      						=    32
											A      						=    2
											A      						=    4
											A      						=    37
											A      						=    'gelu'
											A      						=    0.1
											A      						=    0.1
											A      						=    512
											A      						=    16
											A      						=    2
											A      						=    0.0_2
											A      						=    3
											A      						=    4
											A      						=    None


				def __UpperCamelCase						(							self      :  List[Any]       )		->     Any:
											A      						=    ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size       )

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

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

											A      						=    EsmConfig(
											    vocab_size=self.vocab_size						, hidden_size=self.hidden_size						, num_hidden_layers=self.num_hidden_layers						, pad_token_id=1						, 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						, initializer_range=self.initializer_range						, )

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


				def __UpperCamelCase						(							self      :  Tuple       )		->     Any:
											(
											    (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   
											)      						=    self.prepare_config_and_inputs()

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

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


				def __UpperCamelCase						(							self      :  str						, __UpperCamelCase      :  Optional[Any]						, __UpperCamelCase      :  Dict						, __UpperCamelCase      :  Optional[int]						, __UpperCamelCase      :  List[Any]						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  Optional[Any]       )		->     str:
											A      						=    TFEsmModel(config=__UpperCamelCase       )
											A      						=    {'input_ids': input_ids, 'attention_mask': input_mask}
											A      						=    model(__UpperCamelCase       )

											A      						=    [input_ids, input_mask]
											A      						=    model(__UpperCamelCase       )

											A      						=    model(__UpperCamelCase       )

											self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)       )


				def __UpperCamelCase						(							self      :  int						, __UpperCamelCase      :  str						, __UpperCamelCase      :  str						, __UpperCamelCase      :  List[str]						, __UpperCamelCase      :  Union[str, Any]						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  Tuple						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  str						, )		->     Tuple:
											A      						=    True

											A      						=    TFEsmModel(config=__UpperCamelCase       )
											A      						=    {
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'encoder_hidden_states': encoder_hidden_states,
											    'encoder_attention_mask': encoder_attention_mask,
											}
											A      						=    model(__UpperCamelCase       )

											A      						=    [input_ids, input_mask]
											A      						=    model(__UpperCamelCase						, encoder_hidden_states=__UpperCamelCase       )

											# Also check the case where encoder outputs are not passed
											A      						=    model(__UpperCamelCase						, attention_mask=__UpperCamelCase       )

											self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)       )


				def __UpperCamelCase						(							self      :  str						, __UpperCamelCase      :  int						, __UpperCamelCase      :  Tuple						, __UpperCamelCase      :  Tuple						, __UpperCamelCase      :  Optional[Any]						, __UpperCamelCase      :  str						, __UpperCamelCase      :  Union[str, Any]       )		->     Union[str, Any]:
											A      						=    TFEsmForMaskedLM(config=__UpperCamelCase       )
											A      						=    model([input_ids, input_mask]       )
											self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)       )


				def __UpperCamelCase						(							self      :  int						, __UpperCamelCase      :  Dict						, __UpperCamelCase      :  List[str]						, __UpperCamelCase      :  List[str]						, __UpperCamelCase      :  Union[str, Any]						, __UpperCamelCase      :  Optional[Any]						, __UpperCamelCase      :  Any       )		->     Optional[int]:
											A      						=    self.num_labels
											A      						=    TFEsmForTokenClassification(config=__UpperCamelCase       )
											A      						=    {'input_ids': input_ids, 'attention_mask': input_mask}
											A      						=    model(__UpperCamelCase       )
											self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.num_labels)       )


				def __UpperCamelCase						(							self      :  Optional[Any]       )		->     List[str]:
											A      						=    self.prepare_config_and_inputs()
											(
											    (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   
											)      						=    config_and_inputs
											A      						=    {'input_ids': input_ids, 'attention_mask': input_mask}
											return config, inputs_dict





@require_tf
class        lowerCAmelCase__					(  _lowerCamelCase ,				_lowerCamelCase ,				unittest.TestCase       ):
				A_   :     Tuple								=     (
				    (
				        TFEsmModel,
				        TFEsmForMaskedLM,
				        TFEsmForSequenceClassification,
				        TFEsmForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				A_   :     List[Any]								=     (
				    {
				        'feature-extraction': TFEsmModel,
				        'fill-mask': TFEsmForMaskedLM,
				        'text-classification': TFEsmForSequenceClassification,
				        'token-classification': TFEsmForTokenClassification,
				        'zero-shot': TFEsmForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				A_   :     Any								=     False
				A_   :     str								=     False


				def __UpperCamelCase						(							self      :  int       )		->     List[str]:
											A      						=    TFEsmModelTester(self       )
											A      						=    ConfigTester(self						, config_class=__UpperCamelCase						, hidden_size=37       )


				def __UpperCamelCase						(							self      :  Any       )		->     Union[str, Any]:
											self.config_tester.run_common_tests()


				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Union[str, Any]:
											A      						=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  List[str]       )		->     List[str]:
											A      						=    self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  List[Any]       )		->     int:
											A      						=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  Dict       )		->     str:
											A      						=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase       )


				@slow
				def __UpperCamelCase						(							self      :  Tuple       )		->     str:
											for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		A      						=    TFEsmModel.from_pretrained(__UpperCamelCase       )
																		self.assertIsNotNone(__UpperCamelCase       )


				@unittest.skip('Protein models do not support embedding resizing.'       )
				def __UpperCamelCase						(							self      :  int       )		->     Union[str, Any]:
											pass


				@unittest.skip('Protein models do not support embedding resizing.'       )
				def __UpperCamelCase						(							self      :  List[Any]       )		->     Optional[int]:
											pass


				def __UpperCamelCase						(							self      :  Dict       )		->     Tuple:
											A						,   A      						=    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		A      						=    model_class(__UpperCamelCase       )
																		assert isinstance(model.get_input_embeddings()						, tf.keras.layers.Layer       )
																		if model_class is TFEsmForMaskedLM:
																									# Output embedding test differs from the main test because they're a matrix, not a layer
																									A      						=    model.get_bias()
																									assert isinstance(__UpperCamelCase						, __UpperCamelCase       )
																									for k, v in name.items():
																																assert isinstance(__UpperCamelCase						, tf.Variable       )
																		else:
																									A      						=    model.get_output_embeddings()
																									assert x is None
																									A      						=    model.get_bias()
																									assert name is None





@require_tf
class        lowerCAmelCase__					(  unittest.TestCase       ):


				@slow
				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Optional[int]:
											A      						=    TFEsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'       )

											A      						=    tf.constant([[0, 1, 2, 3, 4, 5]]       )
											A      						=    model(__UpperCamelCase       )[0]
											A      						=    [1, 6, 33]
											self.assertEqual(list(output.numpy().shape       )						, __UpperCamelCase       )
											# compare the actual values for a slice.
											A      						=    tf.constant(
											    [
											        [
											            [8.9_2_1_5_1_8, -1_0.5_8_9_8_1_4, -6.4_6_7_1_3_0_7],
											            [-6.3_9_6_7_1_5_6, -1_3.9_1_1_3_7_7, -1.1_2_1_1_9_1_5],
											            [-7.7_8_1_2_4_7, -1_3.9_5_1_5_5_7, -3.7_4_0_5_9_2],
											        ]
											    ]       )
											self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()						, expected_slice.numpy()						, atol=1e-2       )       )


				@slow
				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     List[Any]:
											A      						=    TFEsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'       )

											A      						=    tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]       )
											A      						=    model(__UpperCamelCase       )[0]
											# compare the actual values for a slice.
											A      						=    tf.constant(
											    [
											        [
											            [0.1_4_4_4_3_0_9_2, 0.5_4_1_2_5_3_2_7, 0.3_2_4_7_7_3_9],
											            [0.3_0_3_4_0_4_8_4, 0.0_0_5_2_6_6_7_6, 0.3_1_0_7_7_7_2_2],
											            [0.3_2_2_7_8_0_4_3, -0.2_4_9_8_7_0_9_6, 0.3_4_1_4_6_2_8],
											        ]
											    ]       )
											self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()						, expected_slice.numpy()						, atol=1e-4       )       )




import json
import os
import unittest

from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast
from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, require_torch
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class        lowerCAmelCase__					(  _lowerCamelCase ,				unittest.TestCase       ):
				A_   :     Dict								=     LEDTokenizer
				A_   :     Union[str, Any]								=     LEDTokenizerFast
				A_   :     Tuple								=     True


				def __UpperCamelCase						(							self      :  str       )		->     str:
											super().setUp()
											A      						=    [
											    'l',
											    'o',
											    'w',
											    'e',
											    'r',
											    's',
											    't',
											    'i',
											    'd',
											    'n',
											    '\u0120',
											    '\u0120l',
											    '\u0120n',
											    '\u0120lo',
											    '\u0120low',
											    'er',
											    '\u0120lowest',
											    '\u0120newer',
											    '\u0120wider',
											    '<unk>',
											]
											A      						=    dict(zip(__UpperCamelCase						, range(len(__UpperCamelCase       )       )       )       )
											A      						=    ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
											A      						=    {'unk_token': '<unk>'}

											A      						=    os.path.join(self.tmpdirname						, VOCAB_FILES_NAMES['vocab_file']       )
											A      						=    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(__UpperCamelCase       ) + '\n'       )
											with open(self.merges_file						, 'w'						, encoding='utf-8'       ) as fp:
																		fp.write('\n'.join(__UpperCamelCase       )       )


				def __UpperCamelCase						(							self      :  str						, **__UpperCamelCase      :  str       )		->     Tuple:
											kwargs.update(self.special_tokens_map       )
											return self.tokenizer_class.from_pretrained(self.tmpdirname						, **__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  List[str]						, **__UpperCamelCase      :  List[Any]       )		->     str:
											kwargs.update(self.special_tokens_map       )
											return self.rust_tokenizer_class.from_pretrained(self.tmpdirname						, **__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  Optional[int]       )		->     Dict:
											return "lower newer", "lower newer"


				@cached_property
				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Any:
											return LEDTokenizer.from_pretrained('allenai/led-base-16384'       )


				@cached_property
				def __UpperCamelCase						(							self      :  str       )		->     str:
											return LEDTokenizerFast.from_pretrained('allenai/led-base-16384'       )


				@require_torch
				def __UpperCamelCase						(							self      :  str       )		->     Optional[Any]:
											A      						=    ['A long paragraph for summarization.', 'Another paragraph for summarization.']
											A      						=    [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2]

											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    tokenizer(__UpperCamelCase						, max_length=len(__UpperCamelCase       )						, padding=__UpperCamelCase						, return_tensors='pt'       )
																		self.assertIsInstance(__UpperCamelCase						, __UpperCamelCase       )

																		self.assertEqual((2, 9)						, batch.input_ids.shape       )
																		self.assertEqual((2, 9)						, batch.attention_mask.shape       )
																		A      						=    batch.input_ids.tolist()[0]
																		self.assertListEqual(__UpperCamelCase						, __UpperCamelCase       )


				@require_torch
				def __UpperCamelCase						(							self      :  Optional[int]       )		->     str:
											A      						=    ['A long paragraph for summarization.', 'Another paragraph for summarization.']
											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    tokenizer(__UpperCamelCase						, padding=__UpperCamelCase						, return_tensors='pt'       )
																		self.assertIn('input_ids'						, __UpperCamelCase       )
																		self.assertIn('attention_mask'						, __UpperCamelCase       )
																		self.assertNotIn('labels'						, __UpperCamelCase       )
																		self.assertNotIn('decoder_attention_mask'						, __UpperCamelCase       )


				@require_torch
				def __UpperCamelCase						(							self      :  List[Any]       )		->     Union[str, Any]:
											A      						=    [
											    'Summary of the text.',
											    'Another summary.',
											]
											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    tokenizer(text_target=__UpperCamelCase						, max_length=32						, padding='max_length'						, return_tensors='pt'       )
																		self.assertEqual(32						, targets['input_ids'].shape[1]       )


				@require_torch
				def __UpperCamelCase						(							self      :  List[str]       )		->     Any:
											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    tokenizer(
																		    ['I am a small frog' * 1_024, 'I am a small frog']						, padding=__UpperCamelCase						, truncation=__UpperCamelCase						, return_tensors='pt'       )
																		self.assertIsInstance(__UpperCamelCase						, __UpperCamelCase       )
																		self.assertEqual(batch.input_ids.shape						, (2, 5_122)       )


				@require_torch
				def __UpperCamelCase						(							self      :  int       )		->     Optional[Any]:
											A      						=    ['A long paragraph for summarization.']
											A      						=    [
											    'Summary of the text.',
											]
											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    tokenizer(__UpperCamelCase						, return_tensors='pt'       )
																		A      						=    tokenizer(text_target=__UpperCamelCase						, return_tensors='pt'       )
																		A      						=    inputs['input_ids']
																		A      						=    targets['input_ids']
																		self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item()       )
																		self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item()       )
																		self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item()       )
																		self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item()       )


				@require_torch
				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Tuple:
											for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
																		A      						=    ['Summary of the text.', 'Another summary.']
																		A      						=    [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]]

																		A      						=    tokenizer(__UpperCamelCase						, padding=__UpperCamelCase       )
																		A      						=    [[0] * len(__UpperCamelCase       ) for x in encoded_output['input_ids']]
																		A      						=    tokenizer.pad(__UpperCamelCase       )
																		self.assertSequenceEqual(outputs['global_attention_mask']						, __UpperCamelCase       )


				def __UpperCamelCase						(							self      :  str       )		->     int:
											pass


				def __UpperCamelCase						(							self      :  Dict       )		->     Any:
											for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																		with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
																									A      						=    self.rust_tokenizer_class.from_pretrained(__UpperCamelCase						, **__UpperCamelCase       )
																									A      						=    self.tokenizer_class.from_pretrained(__UpperCamelCase						, **__UpperCamelCase       )
																									A      						=    'A, <mask> AllenNLP sentence.'
																									A      						=    tokenizer_r.encode_plus(__UpperCamelCase						, add_special_tokens=__UpperCamelCase						, return_token_type_ids=__UpperCamelCase       )
																									A      						=    tokenizer_p.encode_plus(__UpperCamelCase						, add_special_tokens=__UpperCamelCase						, return_token_type_ids=__UpperCamelCase       )
																									self.assertEqual(sum(tokens_r['token_type_ids']       )						, sum(tokens_p['token_type_ids']       )       )
																									self.assertEqual(
																									    sum(tokens_r['attention_mask']       ) / len(tokens_r['attention_mask']       )						, sum(tokens_p['attention_mask']       ) / len(tokens_p['attention_mask']       )						, )

																									A      						=    tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids']       )
																									A      						=    tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids']       )
																									self.assertSequenceEqual(tokens_p['input_ids']						, [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]       )
																									self.assertSequenceEqual(tokens_r['input_ids']						, [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]       )

																									self.assertSequenceEqual(
																									    __UpperCamelCase						, ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>']       )
																									self.assertSequenceEqual(
																									    __UpperCamelCase						, ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>']       )