"""simple docstring"""



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 SCREAMING_SNAKE_CASE     :







	"""simple docstring"""


	def __init__(   self		:      List[str]					,   _snake_case		:      List[str]					,   _snake_case		:      int=99					,   _snake_case		:      Union[str, Any]=13					,   _snake_case		:      Union[str, Any]=16					,   _snake_case		:      Union[str, Any]=7					,   _snake_case		:      Dict=True					,   _snake_case		:      List[Any]=True					,   _snake_case		:      Dict=True					,   _snake_case		:      Dict=False					,   _snake_case		:      Any=True					,   _snake_case		:      Tuple=2					,   _snake_case		:      Tuple=32					,   _snake_case		:      List[Any]=4					,   _snake_case		:      List[str]=4					,   _snake_case		:      str=30					,   _snake_case		:      Optional[Any]=0					,   _snake_case		:      Dict=1					,   _snake_case		:      List[str]=2					,   _snake_case		:      Optional[int]=None					,   )				->			Optional[Any]:




				'''simple docstring'''






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

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

				a__     				=		None
				a__     				=		decoder_seq_length
				a__     				=		2
				a__     				=		1


	def        _lowerCAmelCase				(   self		:      Any				)				->			int:




				'''simple docstring'''






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

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

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

				a__     				=		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        _lowerCAmelCase				(   self		:      List[Any]					,   _snake_case		:      List[str]					,   _snake_case		:      str					,   _snake_case		:      int					,   _snake_case		:      Optional[int]					,   )				->			Tuple:




				'''simple docstring'''






				a__     				=		True
				a__     				=		SpeechaTextaDecoder(config=_snake_case				).to(_snake_case				).eval()
				a__     				=		input_ids[:2]

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

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

				a__     				=		outputs['past_key_values']

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

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

				print(_snake_case				)

				a__     				=		model(_snake_case				)['last_hidden_state']
				a__     				=		model(_snake_case					,   past_key_values=_snake_case				)['last_hidden_state']

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

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



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




				'''simple docstring'''






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

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

@require_torch
class SCREAMING_SNAKE_CASE     (    a   ,      a   ,      a   ,      unittest.TestCase	):







	"""simple docstring"""


	a_       :	str  			=(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
	a_       :	str  			=(SpeechaTextaForCausalLM,) if is_torch_available() else ()
	a_       :	Tuple  			={"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
	a_       :	List[str]  			=True
	a_       :	List[Any]  			=False


	def        _lowerCAmelCase				(   self		:      Tuple					,   )				->			Tuple:




				'''simple docstring'''






				a__     				=		SpeechaTextaStandaloneDecoderModelTester(self					,   is_training=_snake_case				)
				a__     				=		ConfigTester(self					,   config_class=_snake_case				)


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




				'''simple docstring'''






				pass


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




				'''simple docstring'''






				pass


	def        _lowerCAmelCase				(   self		:      Tuple				)				->			Any:




				'''simple docstring'''






				pass


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




				'''simple docstring'''






				self.config_tester.run_common_tests()


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




				'''simple docstring'''






				a__     				=		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_decoder_model_past(*_snake_case				)



	def        _lowerCAmelCase				(   self		:      Optional[int]				)				->			Tuple:




				'''simple docstring'''






				return



"""simple docstring"""



import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata





def   _lowerCamelCase ( UpperCAmelCase__					)							->     str:


   '''simple docstring'''


   a__     				=		min(re.search('^ +',UpperCAmelCase__					).end() if t.startswith(' '					) else 0 for t in string.splitlines()					)
   return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(UpperCAmelCase__					)]					)


__magic_name__    				=					"\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n"

__magic_name__    				=					"\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n"


__magic_name__    				=					"\\n# Begin of markdown\n\nSome cool dataset card\n"




class SCREAMING_SNAKE_CASE     (    unittest.TestCase	):







 """simple docstring"""


 def        _lowerCAmelCase				(   self		:      Tuple				)				->			Dict:




    '''simple docstring'''






    with tempfile.TemporaryDirectory() as tmp_dir:
       a__     				=		Path(_snake_case				) / 'README.md'
       with open(_snake_case					,   'w+'				) as readme_file:
          readme_file.write(_snake_case				)
       a__     				=		DatasetMetadata.from_readme(_snake_case				)
       self.assertDictEqual(_snake_case					,   {'language': ['zh', 'en'], 'task_ids': ['sentiment-classification']}				)

       with open(_snake_case					,   'w+'				) as readme_file:
          readme_file.write(_snake_case				)
       a__     				=		DatasetMetadata.from_readme(_snake_case				)
       self.assertDictEqual(_snake_case					,   {}				)

       with open(_snake_case					,   'w+'				) as readme_file:
          readme_file.write(_snake_case				)
       a__     				=		DatasetMetadata.from_readme(_snake_case				)
       self.assertEqual(_snake_case					,   {}				)



 def        _lowerCAmelCase				(   self		:      List[Any]				)				->			List[Any]:




    '''simple docstring'''






    a__     				=		_dedent(
        '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            '				)
    assert DatasetMetadata.from_yaml_string(_snake_case				)

    a__     				=		_dedent(
        '\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            '				)
    with self.assertRaises(_snake_case				):
       DatasetMetadata.from_yaml_string(_snake_case				)

    a__     				=		_dedent(
        '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the "Researcher") has requested permission to use the ImageNet database (the "Database") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            '				)
    assert DatasetMetadata.from_yaml_string(_snake_case				)


