






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





	"""simple docstring"""






	def __init__(							self	,   UpperCAmelCase	,   UpperCAmelCase=99	,   UpperCAmelCase=13	,   UpperCAmelCase=16	,   UpperCAmelCase=7	,   UpperCAmelCase=True	,   UpperCAmelCase=True	,   UpperCAmelCase=True	,   UpperCAmelCase=False	,   UpperCAmelCase=True	,   UpperCAmelCase=2	,   UpperCAmelCase=32	,   UpperCAmelCase=4	,   UpperCAmelCase=4	,   UpperCAmelCase=30	,   UpperCAmelCase=0	,   UpperCAmelCase=1	,   UpperCAmelCase=2	,   UpperCAmelCase=None	,   ):
							"""simple docstring"""







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

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

							_UpperCAmelCase     =      None
							_UpperCAmelCase     =      decoder_seq_length
							_UpperCAmelCase     =      2
							_UpperCAmelCase     =      1

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

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

							_UpperCAmelCase     =      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 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   ):
							"""simple docstring"""







							_UpperCAmelCase     =      True
							_UpperCAmelCase     =      SpeechaTextaDecoder(config=UpperCAmelCase   ).to(UpperCAmelCase   ).eval()
							_UpperCAmelCase     =      input_ids[:2]

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

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

							_UpperCAmelCase     =      outputs['past_key_values']

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

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

							print(UpperCAmelCase   )

							_UpperCAmelCase     =      model(UpperCAmelCase   )['last_hidden_state']
							_UpperCAmelCase     =      model(UpperCAmelCase	,   past_key_values=UpperCAmelCase   )['last_hidden_state']

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

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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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





@require_torch
class __lowerCamelCase			(				snake_case__					,	snake_case__					,	snake_case__					,	unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=				(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
	UpperCamelCase__								=				(SpeechaTextaForCausalLM,) if is_torch_available() else ()
	UpperCamelCase__								=				{"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
	UpperCamelCase__								=				True
	UpperCamelCase__								=				False

	def 	UpperCamelCase   (							self	,   ):
							"""simple docstring"""







							_UpperCAmelCase     =      SpeechaTextaStandaloneDecoderModelTester(self	,   is_training=UpperCAmelCase   )
							_UpperCAmelCase     =      ConfigTester(self	,   config_class=UpperCAmelCase   )

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							pass

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							pass

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							pass

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							self.config_tester.run_common_tests()

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase   )

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							return








import unittest

import pytest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    MBartaaTokenizerFast,
    MBartConfig,
    MBartForConditionalGeneration,
    TranslationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch, slow

from .test_pipelines_common import ANY





@is_pipeline_test
class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=				MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
	UpperCamelCase__								=				TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							if isinstance(model.config	,   UpperCAmelCase   ):
													_UpperCAmelCase ,			_UpperCAmelCase     =      list(tokenizer.lang_code_to_id.keys()   )[:2]
													_UpperCAmelCase     =      TranslationPipeline(model=UpperCAmelCase	,   tokenizer=UpperCAmelCase	,   src_lang=UpperCAmelCase	,   tgt_lang=UpperCAmelCase   )
							else:
													_UpperCAmelCase     =      TranslationPipeline(model=UpperCAmelCase	,   tokenizer=UpperCAmelCase   )
							return translator, ["Some string", "Some other text"]

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							_UpperCAmelCase     =      translator('Some string'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}]   )

							_UpperCAmelCase     =      translator(['Some string']   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}]   )

							_UpperCAmelCase     =      translator(['Some string', 'other string']   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}, {'translation_text': ANY(UpperCAmelCase   )}]   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_ro'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='pt'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
							                ' Beide Beide'
							            )
							        }
							    ]	,   )

	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_ro'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='tf'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
							                ' Beide Beide'
							            )
							        }
							    ]	,   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_de'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='pt'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
							                ' urine urine urine urine urine urine urine'
							            )
							        }
							    ]	,   )

	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_de'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='tf'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
							                ' urine urine urine urine urine urine urine'
							            )
							        }
							    ]	,   )





class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	@require_torch
	@slow
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							with self.assertRaises(UpperCAmelCase   ):
													pipeline(task='translation_cn_to_ar'   )

							# but we do for this one
							_UpperCAmelCase     =      pipeline(task='translation_en_to_de'   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'en'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'de'   )

	@require_torch
	@slow
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      MBartForConditionalGeneration.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'   )
							_UpperCAmelCase     =      MBartaaTokenizerFast.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'   )

							_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase	,   tokenizer=UpperCAmelCase   )
							# Missing src_lang, tgt_lang
							with self.assertRaises(UpperCAmelCase   ):
													translator('This is a test'   )

							_UpperCAmelCase     =      translator('This is a test'	,   src_lang='en_XX'	,   tgt_lang='ar_AR'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'هذا إختبار'}]   )

							_UpperCAmelCase     =      translator('This is a test'	,   src_lang='en_XX'	,   tgt_lang='hi_IN'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'यह एक परीक्षण है'}]   )

							# src_lang, tgt_lang can be defined at pipeline call time
							_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase	,   tokenizer=UpperCAmelCase	,   src_lang='en_XX'	,   tgt_lang='ar_AR'   )
							_UpperCAmelCase     =      translator('This is a test'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'هذا إختبار'}]   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      'patrickvonplaten/t5-tiny-random'
							_UpperCAmelCase     =      pipeline(task='translation_cn_to_ar'	,   model=UpperCAmelCase   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'cn'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'ar'   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      'patrickvonplaten/t5-tiny-random'
							with pytest.warns(UpperCAmelCase	,   match=R'.*translation_en_to_de.*'   ):
													_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase   )
							self.assertEqual(translator.task	,   'translation_en_to_de'   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'en'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'de'   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							with self.assertRaises(UpperCAmelCase   ):
													pipeline(task='translation'   )
