


import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
						    SpeechaTextaDecoder,
						    SpeechaTextaForCausalLM,
						)

@require_torch
class     __snake_case							:
					def __init__(	self					:	Optional[Any]  ,			_lowerCAmelCase					:	Union[str, Any]  ,			_lowerCAmelCase					:	Optional[int]=9_9  ,			_lowerCAmelCase					:	List[str]=1_3  ,			_lowerCAmelCase					:	Dict=1_6  ,			_lowerCAmelCase					:	int=7  ,			_lowerCAmelCase					:	Tuple=True  ,			_lowerCAmelCase					:	Union[str, Any]=True  ,			_lowerCAmelCase					:	Union[str, Any]=True  ,			_lowerCAmelCase					:	Optional[int]=False  ,			_lowerCAmelCase					:	List[str]=True  ,			_lowerCAmelCase					:	Optional[Any]=2  ,			_lowerCAmelCase					:	Optional[Any]=3_2  ,			_lowerCAmelCase					:	Any=4  ,			_lowerCAmelCase					:	List[str]=4  ,			_lowerCAmelCase					:	Optional[Any]=3_0  ,			_lowerCAmelCase					:	int=0  ,			_lowerCAmelCase					:	List[Any]=1  ,			_lowerCAmelCase					:	Optional[int]=2  ,			_lowerCAmelCase					:	Tuple=None  ,			)       ->				Dict:



											'''simple docstring'''





											__magic_name__ 		=			parent
											__magic_name__ 		=			batch_size
											__magic_name__ 		=			decoder_seq_length
											# For common tests
											__magic_name__ 		=			self.decoder_seq_length
											__magic_name__ 		=			is_training
											__magic_name__ 		=			use_attention_mask
											__magic_name__ 		=			use_labels

											__magic_name__ 		=			vocab_size
											__magic_name__ 		=			d_model
											__magic_name__ 		=			d_model
											__magic_name__ 		=			decoder_layers
											__magic_name__ 		=			decoder_layers
											__magic_name__ 		=			decoder_ffn_dim
											__magic_name__ 		=			decoder_attention_heads
											__magic_name__ 		=			decoder_attention_heads
											__magic_name__ 		=			eos_token_id
											__magic_name__ 		=			bos_token_id
											__magic_name__ 		=			pad_token_id
											__magic_name__ 		=			decoder_start_token_id
											__magic_name__ 		=			use_cache
											__magic_name__ 		=			max_position_embeddings

											__magic_name__ 		=			None
											__magic_name__ 		=			decoder_seq_length
											__magic_name__ 		=			2
											__magic_name__ 		=			1
					def   a (	self					:	int     )       ->				int:



											'''simple docstring'''





											__magic_name__ 		=			ids_tensor([self.batch_size, self.decoder_seq_length]  ,			self.vocab_size     )

											__magic_name__ 		=			None
											if self.use_attention_mask:
																	__magic_name__ 		=			ids_tensor([self.batch_size, self.decoder_seq_length]  ,			vocab_size=2     )

											__magic_name__ 		=			None
											if self.use_labels:
																	__magic_name__ 		=			ids_tensor([self.batch_size, self.decoder_seq_length]  ,			self.vocab_size     )

											__magic_name__ 		=			SpeechaTextaConfig(
											    vocab_size=self.vocab_size  ,			d_model=self.d_model  ,			decoder_layers=self.decoder_layers  ,			decoder_ffn_dim=self.decoder_ffn_dim  ,			decoder_attention_heads=self.decoder_attention_heads  ,			eos_token_id=self.eos_token_id  ,			bos_token_id=self.bos_token_id  ,			use_cache=self.use_cache  ,			pad_token_id=self.pad_token_id  ,			decoder_start_token_id=self.decoder_start_token_id  ,			max_position_embeddings=self.max_position_embeddings  ,			)

											return (
											    config,
											    input_ids,
											    attention_mask,
											    lm_labels,
											)
					def   a (	self					:	List[str]  ,			_lowerCAmelCase					:	Optional[Any]  ,			_lowerCAmelCase					:	List[Any]  ,			_lowerCAmelCase					:	int  ,			_lowerCAmelCase					:	Optional[int]  ,			)       ->				Optional[Any]:



											'''simple docstring'''





											__magic_name__ 		=			True
											__magic_name__ 		=			SpeechaTextaDecoder(config=_lowerCAmelCase     ).to(_lowerCAmelCase     ).eval()
											__magic_name__ 		=			input_ids[:2]

											input_ids[input_ids == 0] += 1
											# first forward pass
											__magic_name__ 		=			model(_lowerCAmelCase  ,			use_cache=_lowerCAmelCase     )
											__magic_name__ 		=			model(_lowerCAmelCase     )
											__magic_name__ 		=			model(_lowerCAmelCase  ,			use_cache=_lowerCAmelCase     )

											self.parent.assertTrue(len(_lowerCAmelCase     ) == len(_lowerCAmelCase     )     )
											self.parent.assertTrue(len(_lowerCAmelCase     ) == len(_lowerCAmelCase     ) + 1     )

											__magic_name__ 		=			outputs['past_key_values']

											# create hypothetical next token and extent to next_input_ids
											__magic_name__ 		=			ids_tensor((2, 1)  ,			config.vocab_size - 1     ) + 1

											# append to next input_ids and
											__magic_name__ 		=			torch.cat([input_ids, next_tokens]  ,			dim=-1     )

											print(_lowerCAmelCase     )

											__magic_name__ 		=			model(_lowerCAmelCase     )['last_hidden_state']
											__magic_name__ 		=			model(_lowerCAmelCase  ,			past_key_values=_lowerCAmelCase     )['last_hidden_state']

											# select random slice
											__magic_name__ 		=			ids_tensor((1,)  ,			output_from_past.shape[-1]     ).item()
											__magic_name__ 		=			output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
											__magic_name__ 		=			output_from_past[:, 0, random_slice_idx].detach()

											# test that outputs are equal for slice
											assert torch.allclose(_lowerCAmelCase  ,			_lowerCAmelCase  ,			atol=1E-3     )







					def   a (	self					:	List[Any]     )       ->				str:



											'''simple docstring'''





											__magic_name__ 		=			self.prepare_config_and_inputs()
											(
											    (
											    __magic_name__			
											)		,			(
											    __magic_name__			
											)		,			(
											    __magic_name__			
											)		,			(
											    __magic_name__			
											)		,						
											) 		=			config_and_inputs

											__magic_name__ 		=			{
											    'input_ids': input_ids,
											    'attention_mask': attention_mask,
											}
											return config, inputs_dict







@require_torch
class     __snake_case							(      _UpperCAmelCase  ,			_UpperCAmelCase  ,			_UpperCAmelCase  ,			unittest.TestCase):
					SCREAMING_SNAKE_CASE_		:					Any 				= (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE_		:					List[str] 				= (SpeechaTextaForCausalLM,) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE_		:					int 				= {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
					SCREAMING_SNAKE_CASE_		:					Tuple 				= True
					SCREAMING_SNAKE_CASE_		:					int 				= False
					def   a (	self					:	Optional[Any]  ,			)       ->				List[str]:



											'''simple docstring'''





											__magic_name__ 		=			SpeechaTextaStandaloneDecoderModelTester(self  ,			is_training=_lowerCAmelCase     )
											__magic_name__ 		=			ConfigTester(self  ,			config_class=_lowerCAmelCase     )
					def   a (	self					:	Optional[int]     )       ->				List[str]:



											'''simple docstring'''





											pass
					def   a (	self					:	List[str]     )       ->				str:



											'''simple docstring'''





											pass
					def   a (	self					:	Optional[Any]     )       ->				Optional[int]:



											'''simple docstring'''





											pass
					def   a (	self					:	Union[str, Any]     )       ->				Tuple:



											'''simple docstring'''





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



											'''simple docstring'''





											__magic_name__ 		=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_decoder_model_past(*_lowerCAmelCase     )







					def   a (	self					:	List[str]     )       ->				int:



											'''simple docstring'''





											return





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     __snake_case							(      unittest.TestCase):
					SCREAMING_SNAKE_CASE_		:					Tuple 				= EsmTokenizer
					def   a (	self					:	List[Any]     )       ->				Dict:



											'''simple docstring'''





											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   a (	self					:	Optional[int]  ,			**_lowerCAmelCase					:	int     )       ->				List[PreTrainedTokenizerBase]:



											'''simple docstring'''





											return [self.get_tokenizer(**_lowerCAmelCase     )]
					def   a (	self					:	List[str]  ,			**_lowerCAmelCase					:	int     )       ->				PreTrainedTokenizer:



											'''simple docstring'''





											return self.tokenizer_class.from_pretrained(self.tmpdirname  ,			**_lowerCAmelCase     )
					def   a (	self					:	int     )       ->				int:



											'''simple docstring'''





											__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   a (	self					:	str     )       ->				str:



											'''simple docstring'''





											__magic_name__ 		=			self.tokenizer_class(self.vocab_file     )

											__magic_name__ 		=			'LAGVS'
											self.assertListEqual(tokenizer.encode(_lowerCAmelCase     )  ,			[0, 4, 5, 6, 7, 8, 2]     )
					def   a (	self					:	int     )       ->				List[Any]:



											'''simple docstring'''





											__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   a (	self					:	Dict     )       ->				List[Any]:



											'''simple docstring'''





											__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   a (	self					:	Optional[int]     )       ->				str:



											'''simple docstring'''





											__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     )

