




import unittest
from unittest.util import safe_repr

from transformers import AutoTokenizer, RwkvConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
							    RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
							    RwkvForCausalLM,
							    RwkvModel,
							)
							from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
							UpperCamelCase__  =		False


class        __SCREAMING_SNAKE_CASE							:

					def __init__(       self				, __lowercase				, __lowercase=14				, __lowercase=7				, __lowercase=True				, __lowercase=False				, __lowercase=True				, __lowercase=True				, __lowercase=True				, __lowercase=99				, __lowercase=32				, __lowercase=5				, __lowercase=37				, __lowercase="gelu"				, __lowercase=0.1				, __lowercase=0.1				, __lowercase=512				, __lowercase=16				, __lowercase=2				, __lowercase=3				, __lowercase=4				, __lowercase=None				, ):







						'''simple docstring'''




						lowerCamelCase_							:	Any							     =							parent
						lowerCamelCase_							:	int							     =							batch_size
						lowerCamelCase_							:	Dict							     =							seq_length
						lowerCamelCase_							:	List[str]							     =							is_training
						lowerCamelCase_							:	Any							     =							use_token_type_ids
						lowerCamelCase_							:	Optional[Any]							     =							use_input_mask
						lowerCamelCase_							:	List[str]							     =							use_labels
						lowerCamelCase_							:	str							     =							use_mc_token_ids
						lowerCamelCase_							:	Any							     =							vocab_size
						lowerCamelCase_							:	int							     =							hidden_size
						lowerCamelCase_							:	Optional[Any]							     =							num_hidden_layers
						lowerCamelCase_							:	Union[str, Any]							     =							intermediate_size
						lowerCamelCase_							:	str							     =							hidden_act
						lowerCamelCase_							:	List[str]							     =							hidden_dropout_prob
						lowerCamelCase_							:	Dict							     =							attention_probs_dropout_prob
						lowerCamelCase_							:	Union[str, Any]							     =							max_position_embeddings
						lowerCamelCase_							:	Dict							     =							type_vocab_size
						lowerCamelCase_							:	Optional[int]							     =							type_sequence_label_size
						lowerCamelCase_							:	Optional[Any]							     =							num_labels
						lowerCamelCase_							:	int							     =							num_choices
						lowerCamelCase_							:	Tuple							     =							scope
						lowerCamelCase_							:	Optional[int]							     =							vocab_size - 1
						lowerCamelCase_							:	Optional[int]							     =							vocab_size - 1
						lowerCamelCase_							:	Any							     =							vocab_size - 1

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						return RwkvConfig.from_pretrained('sgugger/rwkv-4-pile-7b'      )

					def 			lowercase_							(       self				, __lowercase=False				, __lowercase=False				, __lowercase=False      ):







						'''simple docstring'''




						lowerCamelCase_							:	Dict							     =							ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size      )

						lowerCamelCase_							:	List[str]							     =							None
						if self.use_input_mask:
							lowerCamelCase_							:	Tuple							     =							random_attention_mask([self.batch_size, self.seq_length]      )

						lowerCamelCase_							:	List[Any]							     =							None
						if self.use_token_type_ids:
							lowerCamelCase_							:	int							     =							ids_tensor([self.batch_size, self.seq_length]				, self.type_vocab_size      )

						lowerCamelCase_							:	Any							     =							None
						if self.use_mc_token_ids:
							lowerCamelCase_							:	Union[str, Any]							     =							ids_tensor([self.batch_size, self.num_choices]				, self.seq_length      )

						lowerCamelCase_							:	Optional[int]							     =							None
						lowerCamelCase_							:	List[str]							     =							None
						lowerCamelCase_							:	Any							     =							None
						if self.use_labels:
							lowerCamelCase_							:	Union[str, Any]							     =							ids_tensor([self.batch_size]				, self.type_sequence_label_size      )
							lowerCamelCase_							:	Optional[Any]							     =							ids_tensor([self.batch_size, self.seq_length]				, self.num_labels      )
							lowerCamelCase_							:	List[str]							     =							ids_tensor([self.batch_size]				, self.num_choices      )

						lowerCamelCase_							:	Optional[int]							     =							self.get_config(
						    gradient_checkpointing=__lowercase				, scale_attn_by_inverse_layer_idx=__lowercase				, reorder_and_upcast_attn=__lowercase				, )

						return (
						    config,
						    input_ids,
						    input_mask,
						    None,
						    token_type_ids,
						    mc_token_ids,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						)

					def 			lowercase_							(       self				, __lowercase=False				, __lowercase=False				, __lowercase=False      ):







						'''simple docstring'''




						return RwkvConfig(
						    vocab_size=self.vocab_size				, hidden_size=self.hidden_size				, num_hidden_layers=self.num_hidden_layers				, intermediate_size=self.intermediate_size				, activation_function=self.hidden_act				, resid_pdrop=self.hidden_dropout_prob				, attn_pdrop=self.attention_probs_dropout_prob				, n_positions=self.max_position_embeddings				, type_vocab_size=self.type_vocab_size				, use_cache=__lowercase				, bos_token_id=self.bos_token_id				, eos_token_id=self.eos_token_id				, pad_token_id=self.pad_token_id				, gradient_checkpointing=__lowercase				, scale_attn_by_inverse_layer_idx=__lowercase				, reorder_and_upcast_attn=__lowercase				, )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Dict							     =							self.get_config()
						lowerCamelCase_							:	Optional[Any]							     =							300
						return config

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						(
						    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    
						)							:	int							     =							self.prepare_config_and_inputs()

						lowerCamelCase_							:	int							     =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
						lowerCamelCase_							:	Optional[Any]							     =							ids_tensor([self.batch_size, self.seq_length]				, vocab_size=2      )

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

					def 			lowercase_							(       self				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, *__lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	Dict							     =							True
						lowerCamelCase_							:	Any							     =							RwkvModel(config=__lowercase      )
						model.to(__lowercase      )
						model.eval()

						lowerCamelCase_							:	Union[str, Any]							     =							model(__lowercase      )

						self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size)      )
						self.parent.assertEqual(len(result.hidden_states      )				, config.num_hidden_layers + 1      )

					def 			lowercase_							(       self				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, *__lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[Any]							     =							RwkvForCausalLM(__lowercase      )
						model.to(__lowercase      )
						model.eval()

						lowerCamelCase_							:	Dict							     =							model(__lowercase				, labels=__lowercase      )
						self.parent.assertEqual(result.loss.shape				, ()      )
						self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)      )

					def 			lowercase_							(       self				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, *__lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	int							     =							RwkvModel(config=__lowercase      )
						model.to(__lowercase      )
						model.eval()

						lowerCamelCase_							:	str							     =							model(__lowercase      )
						lowerCamelCase_							:	Dict							     =							outputs.last_hidden_state

						lowerCamelCase_							:	List[Any]							     =							model(input_ids[:, :2]      )
						lowerCamelCase_							:	str							     =							outputs.last_hidden_state

						# Using the state computed on the first inputs, we will get the same output
						lowerCamelCase_							:	Any							     =							model(input_ids[:, 2:]				, state=outputs.state      )
						lowerCamelCase_							:	int							     =							outputs.last_hidden_state

						self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]				, dim=1      )				, __lowercase				, atol=1E-5      )      )

					def 			lowercase_							(       self				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, __lowercase				, *__lowercase				, __lowercase=False      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[str]							     =							RwkvForCausalLM(__lowercase      )
						model.to(__lowercase      )
						if gradient_checkpointing:
							model.gradient_checkpointing_enable()

						lowerCamelCase_							:	List[str]							     =							model(__lowercase				, labels=__lowercase      )
						self.parent.assertEqual(result.loss.shape				, ()      )
						self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)      )
						result.loss.backward()





					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Any							     =							self.prepare_config_and_inputs()

						(
						    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    (
						    lowerCamelCase_
						)     ,    
						)							:	Dict							     =							config_and_inputs

						lowerCamelCase_							:	Any							     =							{'input_ids': input_ids}

						return config, inputs_dict


@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0     ,  reason="""See https://github.com/huggingface/transformers/pull/24204"""						)
@require_torch
class        __SCREAMING_SNAKE_CASE							(	lowerCamelCase     ,  lowerCamelCase     ,  lowerCamelCase     ,  unittest.TestCase						):
					_lowerCamelCase							=				(RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
					_lowerCamelCase							=				(
					    {"""feature-extraction""": RwkvModel, """text-generation""": RwkvForCausalLM} if is_torch_available() else {}
					)
					# all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
					_lowerCamelCase							=				False
					_lowerCamelCase							=				False
					_lowerCamelCase							=				False
					_lowerCamelCase							=				False
					_lowerCamelCase							=				False  # Rwkv does not support head masking

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Dict							     =							RwkvModelTester(self      )
						lowerCamelCase_							:	str							     =							ConfigTester(
						    self				, config_class=__lowercase				, n_embd=37				, common_properties=['hidden_size', 'num_hidden_layers']      )

					def 			lowercase_							(       self				, __lowercase				, __lowercase				, __lowercase=None      ):







						'''simple docstring'''




						if isinstance(__lowercase				, torch.Tensor      ):
							lowerCamelCase_     ,    lowerCamelCase_							:	Tuple							     =							member.max().item(), member.min().item()
						elif isinstance(__lowercase				, __lowercase      ) or isinstance(__lowercase				, __lowercase      ):
							lowerCamelCase_     ,    lowerCamelCase_							:	List[str]							     =							max(__lowercase      ), min(__lowercase      )

						if not isinstance(__lowercase				, __lowercase      ):
							raise TypeError('container should be a list or tuple'      )
						elif len(__lowercase      ) != 2:
							raise ValueError('container should have 2 elements'      )

						lowerCamelCase_     ,    lowerCamelCase_							:	Tuple							     =							container

						lowerCamelCase_							:	Tuple							     =							(min_value >= expected_min) and (max_value <= expected_max)

						if not is_inside_interval:
							lowerCamelCase_							:	Dict							     =							'%s not found in %s' % (safe_repr(__lowercase      ), safe_repr(__lowercase      ))
							self.fail(self._formatMessage(__lowercase				, __lowercase      )      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						self.config_tester.run_common_tests()

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Optional[Any]							     =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_rwkv_model(*__lowercase      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	int							     =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_causl_lm(*__lowercase      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	str							     =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_state_equivalency(*__lowercase      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_     ,    lowerCamelCase_							:	List[str]							     =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:	Dict							     =							model_class(config=__lowercase      )
							for name, param in model.named_parameters():
								if "time_decay" in name:
									if param.requires_grad:
										self.assertTrue(param.data.max().item() == 3.0      )
										self.assertTrue(param.data.min().item() == -5.0      )
								elif "time_first" in name:
									if param.requires_grad:
										# check if it's a ones like
										self.assertTrue(torch.allclose(param.data				, torch.ones_like(param.data      )				, atol=1E-5				, rtol=1E-5      )      )
								elif any(x in name for x in ['time_mix_key', 'time_mix_receptance']      ):
									if param.requires_grad:
										self.assertInterval(
										    param.data				, [0.0, 1.0]				, msg=f"Parameter {name} of model {model_class} seems not properly initialized"				, )
								elif "time_mix_value" in name:
									if param.requires_grad:
										self.assertInterval(
										    param.data				, [0.0, 1.3]				, msg=f"Parameter {name} of model {model_class} seems not properly initialized"				, )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_     ,    lowerCamelCase_							:	Optional[int]							     =							self.model_tester.prepare_config_and_inputs_for_common()
						lowerCamelCase_							:	Optional[Any]							     =							True

						lowerCamelCase_							:	Union[str, Any]							     =							getattr(self.model_tester				, 'seq_length'				, __lowercase      )

						for model_class in self.all_model_classes:
							lowerCamelCase_							:	Tuple							     =							True
							lowerCamelCase_							:	Optional[int]							     =							False
							lowerCamelCase_							:	Union[str, Any]							     =							True
							lowerCamelCase_							:	Optional[Any]							     =							model_class(__lowercase      )
							model.to(__lowercase      )
							model.eval()

							lowerCamelCase_							:	Any							     =							self._prepare_for_class(__lowercase				, __lowercase      )
							lowerCamelCase_							:	Tuple							     =							inputs['input_ids'].shape[0]
							with torch.no_grad():
								lowerCamelCase_							:	Optional[Any]							     =							model(**__lowercase      )
							lowerCamelCase_							:	str							     =							outputs.attentions
							self.assertEqual(len(__lowercase      )				, self.model_tester.num_hidden_layers      )

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							lowerCamelCase_							:	int							     =							True
							lowerCamelCase_							:	str							     =							model_class(__lowercase      )
							model.to(__lowercase      )
							model.eval()

							lowerCamelCase_							:	Dict							     =							self._prepare_for_class(__lowercase				, __lowercase      )
							lowerCamelCase_							:	Optional[Any]							     =							inputs['input_ids'].shape[0]
							with torch.no_grad():
								lowerCamelCase_							:	Union[str, Any]							     =							model(**__lowercase      )
							lowerCamelCase_							:	int							     =							outputs.attentions
							self.assertEqual(len(__lowercase      )				, self.model_tester.num_hidden_layers      )

							self.assertListEqual(
							    list(attentions[0].shape[-3:]      )				, [batch_size, seq_len, config.hidden_size]				, )
							lowerCamelCase_							:	str							     =							len(__lowercase      )

							# Check attention is always last and order is fine
							lowerCamelCase_							:	int							     =							True
							lowerCamelCase_							:	Optional[Any]							     =							True
							lowerCamelCase_							:	Tuple							     =							model_class(__lowercase      )
							model.to(__lowercase      )
							model.eval()

							lowerCamelCase_							:	Dict							     =							self._prepare_for_class(__lowercase				, __lowercase      )
							lowerCamelCase_							:	Optional[int]							     =							inputs['input_ids'].shape[0]
							with torch.no_grad():
								lowerCamelCase_							:	Any							     =							model(**__lowercase      )

							lowerCamelCase_							:	Union[str, Any]							     =							1
							self.assertEqual(out_len + added_hidden_states				, len(__lowercase      )      )

							lowerCamelCase_							:	List[str]							     =							outputs.attentions

							self.assertEqual(len(__lowercase      )				, self.model_tester.num_hidden_layers      )
							self.assertListEqual(
							    list(self_attentions[0].shape[-3:]      )				, [batch_size, seq_len, config.hidden_size]				, )





					@slow
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:	List[Any]							     =							RwkvModel.from_pretrained(__lowercase      )
							self.assertIsNotNone(__lowercase      )


@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0     ,  reason="""See https://github.com/huggingface/transformers/pull/24204"""						)
@slow
class        __SCREAMING_SNAKE_CASE							(	unittest.TestCase						):

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[Any]							     =							'RWKV/rwkv-4-169m-pile'
						lowerCamelCase_							:	int							     =							AutoTokenizer.from_pretrained(self.model_id      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[Any]							     =							'Hello my name is Jasmine and I am a newbie to the'
						lowerCamelCase_							:	Optional[Any]							     =							RwkvForCausalLM.from_pretrained(self.model_id      ).to(__lowercase      )

						lowerCamelCase_							:	Tuple							     =							self.tokenizer('Hello my name is'				, return_tensors='pt'      ).input_ids.to(__lowercase      )
						lowerCamelCase_							:	List[str]							     =							model.generate(__lowercase				, max_new_tokens=10      )
						lowerCamelCase_							:	List[str]							     =							self.tokenizer.decode(output[0].tolist()      )

						self.assertEqual(__lowercase				, __lowercase      )





					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[Any]							     =							'Hello my name is Jasmine and I am a newbie to the'

						lowerCamelCase_							:	Dict							     =							self.tokenizer('Hello my name is'				, return_tensors='pt'      ).input_ids.to(__lowercase      )
						lowerCamelCase_							:	Optional[Any]							     =							RwkvForCausalLM.from_pretrained(self.model_id				, torch_dtype=torch.bfloataa      ).to(__lowercase      )

						lowerCamelCase_							:	str							     =							model.generate(__lowercase				, max_new_tokens=10      )
						lowerCamelCase_							:	Dict							     =							self.tokenizer.decode(output[0].tolist()      )

						self.assertEqual(__lowercase				, __lowercase      )






from __future__ import annotations

import unittest
import warnings

from transformers import AutoTokenizer, MarianConfig, MarianTokenizer, TranslationPipeline, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

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


if is_tf_available():
							import tensorflow as tf

							from transformers import TFAutoModelForSeqaSeqLM, TFMarianModel, TFMarianMTModel


@require_tf
class        __SCREAMING_SNAKE_CASE							:
					_lowerCamelCase							=				MarianConfig
					_lowerCamelCase							=				{}
					_lowerCamelCase							=				"""gelu"""

					def __init__(       self				, __lowercase				, __lowercase=13				, __lowercase=7				, __lowercase=True				, __lowercase=False				, __lowercase=99				, __lowercase=32				, __lowercase=2				, __lowercase=4				, __lowercase=37				, __lowercase=0.1				, __lowercase=0.1				, __lowercase=20				, __lowercase=2				, __lowercase=1				, __lowercase=0				, ):







						'''simple docstring'''




						lowerCamelCase_							:	Optional[Any]							     =							parent
						lowerCamelCase_							:	List[str]							     =							batch_size
						lowerCamelCase_							:	List[str]							     =							seq_length
						lowerCamelCase_							:	Optional[int]							     =							is_training
						lowerCamelCase_							:	List[str]							     =							use_labels
						lowerCamelCase_							:	Tuple							     =							vocab_size
						lowerCamelCase_							:	Dict							     =							hidden_size
						lowerCamelCase_							:	List[str]							     =							num_hidden_layers
						lowerCamelCase_							:	List[str]							     =							num_attention_heads
						lowerCamelCase_							:	Tuple							     =							intermediate_size

						lowerCamelCase_							:	Tuple							     =							hidden_dropout_prob
						lowerCamelCase_							:	Dict							     =							attention_probs_dropout_prob
						lowerCamelCase_							:	List[str]							     =							max_position_embeddings
						lowerCamelCase_							:	Optional[int]							     =							eos_token_id
						lowerCamelCase_							:	List[Any]							     =							pad_token_id
						lowerCamelCase_							:	Optional[int]							     =							bos_token_id

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Union[str, Any]							     =							ids_tensor([self.batch_size, self.seq_length - 1]				, self.vocab_size      )
						lowerCamelCase_							:	Tuple							     =							tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size      )				, 1      )
						lowerCamelCase_							:	Optional[int]							     =							tf.concat([input_ids, eos_tensor]				, axis=1      )

						lowerCamelCase_							:	List[Any]							     =							ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size      )

						lowerCamelCase_							:	Optional[Any]							     =							self.config_cls(
						    vocab_size=self.vocab_size				, d_model=self.hidden_size				, encoder_layers=self.num_hidden_layers				, decoder_layers=self.num_hidden_layers				, encoder_attention_heads=self.num_attention_heads				, decoder_attention_heads=self.num_attention_heads				, encoder_ffn_dim=self.intermediate_size				, decoder_ffn_dim=self.intermediate_size				, dropout=self.hidden_dropout_prob				, attention_dropout=self.attention_probs_dropout_prob				, max_position_embeddings=self.max_position_embeddings				, eos_token_ids=[2]				, bos_token_id=self.bos_token_id				, pad_token_id=self.pad_token_id				, decoder_start_token_id=self.pad_token_id				, **self.config_updates				, )
						lowerCamelCase_							:	int							     =							prepare_marian_inputs_dict(__lowercase				, __lowercase				, __lowercase      )
						return config, inputs_dict





					def 			lowercase_							(       self				, __lowercase				, __lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	Union[str, Any]							     =							TFMarianModel(config=__lowercase      ).get_decoder()
						lowerCamelCase_							:	Tuple							     =							inputs_dict['input_ids']

						lowerCamelCase_							:	Union[str, Any]							     =							input_ids[:1, :]
						lowerCamelCase_							:	List[str]							     =							inputs_dict['attention_mask'][:1, :]
						lowerCamelCase_							:	List[str]							     =							inputs_dict['head_mask']
						lowerCamelCase_							:	List[str]							     =							1

						# first forward pass
						lowerCamelCase_							:	List[str]							     =							model(__lowercase				, attention_mask=__lowercase				, head_mask=__lowercase				, use_cache=__lowercase      )

						lowerCamelCase_     ,    lowerCamelCase_							:	Any							     =							outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						lowerCamelCase_							:	Any							     =							ids_tensor((self.batch_size, 3)				, config.vocab_size      )
						lowerCamelCase_							:	Optional[Any]							     =							tf.cast(ids_tensor((self.batch_size, 3)				, 2      )				, tf.inta      )

						# append to next input_ids and
						lowerCamelCase_							:	str							     =							tf.concat([input_ids, next_tokens]				, axis=-1      )
						lowerCamelCase_							:	List[str]							     =							tf.concat([attention_mask, next_attn_mask]				, axis=-1      )

						lowerCamelCase_							:	str							     =							model(__lowercase				, attention_mask=__lowercase      )[0]
						lowerCamelCase_							:	Optional[Any]							     =							model(__lowercase				, attention_mask=__lowercase				, past_key_values=__lowercase      )[0]

						self.parent.assertEqual(next_tokens.shape[1]				, output_from_past.shape[1]      )

						# select random slice
						lowerCamelCase_							:	Any							     =							int(ids_tensor((1,)				, output_from_past.shape[-1]      )      )
						lowerCamelCase_							:	Union[str, Any]							     =							output_from_no_past[:, -3:, random_slice_idx]
						lowerCamelCase_							:	int							     =							output_from_past[:, :, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(__lowercase				, __lowercase				, rtol=1E-3      )






def       snake_case      (							A		,     A		,     A		,     A=None		,     A=None		,     A=None		,     A=None		,     A=None		,     )							->							Dict:




	'''simple docstring'''


	if attention_mask is None:
		lowerCamelCase_							:	Tuple							     =							tf.cast(tf.math.not_equal(A		,     config.pad_token_id    )		,     tf.inta    )
	if decoder_attention_mask is None:
		lowerCamelCase_							:	Union[str, Any]							     =							tf.concat(
		    [
		        tf.ones(decoder_input_ids[:, :1].shape		,     dtype=tf.inta    ),
		        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]		,     config.pad_token_id    )		,     tf.inta    ),
		    ]		,     axis=-1		,     )
	if head_mask is None:
		lowerCamelCase_							:	Optional[Any]							     =							tf.ones((config.encoder_layers, config.encoder_attention_heads)    )
	if decoder_head_mask is None:
		lowerCamelCase_							:	Tuple							     =							tf.ones((config.decoder_layers, config.decoder_attention_heads)    )
	if cross_attn_head_mask is None:
		lowerCamelCase_							:	Dict							     =							tf.ones((config.decoder_layers, config.decoder_attention_heads)    )
	return {
	    "input_ids": input_ids,
	    "decoder_input_ids": decoder_input_ids,
	    "attention_mask": attention_mask,
	    "decoder_attention_mask": decoder_attention_mask,
	    "head_mask": head_mask,
	    "decoder_head_mask": decoder_head_mask,
	    "cross_attn_head_mask": cross_attn_head_mask,
	}


@require_tf
class        __SCREAMING_SNAKE_CASE							(	lowerCamelCase     ,  lowerCamelCase     ,  unittest.TestCase						):
					_lowerCamelCase							=				(TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
					_lowerCamelCase							=				(TFMarianMTModel,) if is_tf_available() else ()
					_lowerCamelCase							=				(
					    {
					        """conversational""": TFMarianMTModel,
					        """feature-extraction""": TFMarianModel,
					        """summarization""": TFMarianMTModel,
					        """text2text-generation""": TFMarianMTModel,
					        """translation""": TFMarianMTModel,
					    }
					    if is_tf_available()
					    else {}
					)
					_lowerCamelCase							=				True
					_lowerCamelCase							=				False
					_lowerCamelCase							=				False

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Union[str, Any]							     =							TFMarianModelTester(self      )
						lowerCamelCase_							:	str							     =							ConfigTester(self				, config_class=__lowercase      )

					def 			lowercase_							(       self      ):







						'''simple docstring'''




						self.config_tester.run_common_tests()





					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Optional[Any]							     =							self.model_tester.prepare_config_and_inputs_for_common()
						self.model_tester.check_decoder_model_past_large_inputs(*__lowercase      )


@require_tf
class        __SCREAMING_SNAKE_CASE							(	unittest.TestCase						):
					_lowerCamelCase							=				10_00  # show more chars for failing integration tests

					@classmethod
					def 			lowercase_							(       cls      ):







						'''simple docstring'''




						lowerCamelCase_							:	Union[str, Any]							     =							f"Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}"
						return cls

					@cached_property
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						return AutoTokenizer.from_pretrained(self.model_name      )

					@property
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						return self.tokenizer.eos_token_id

					@cached_property
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						warnings.simplefilter('error'      )
						lowerCamelCase_							:	TFMarianMTModel							     =							TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name      )
						assert isinstance(__lowercase				, __lowercase      )
						lowerCamelCase_							:	Optional[Any]							     =							model.config
						self.assertListEqual(c.bad_words_ids				, [[c.pad_token_id]]      )
						self.assertEqual(c.max_length				, 512      )
						self.assertEqual(c.decoder_start_token_id				, c.pad_token_id      )
						return model

					def 			lowercase_							(       self				, **__lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	List[str]							     =							self.translate_src_text(**__lowercase      )
						self.assertListEqual(self.expected_text				, __lowercase      )





					def 			lowercase_							(       self				, **__lowercase      ):







						'''simple docstring'''




						lowerCamelCase_							:	str							     =							self.tokenizer(self.src_text				, **__lowercase				, padding=__lowercase				, return_tensors='tf'      )
						lowerCamelCase_							:	Optional[int]							     =							self.model.generate(
						    model_inputs.input_ids				, attention_mask=model_inputs.attention_mask				, num_beams=2				, max_length=128      )
						lowerCamelCase_							:	Optional[int]							     =							self.tokenizer.batch_decode(generated_ids.numpy()				, skip_special_tokens=__lowercase      )
						return generated_words


@require_sentencepiece
@require_tokenizers
@require_tf
class        __SCREAMING_SNAKE_CASE							(	lowerCamelCase						):
					_lowerCamelCase							=				"""mt"""
					_lowerCamelCase							=				"""en"""
					_lowerCamelCase							=				["""Billi messu b'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem."""]
					_lowerCamelCase							=				["""Touching gently, Jesus healed a man who was affected by the sad disease of leprosy."""]

					@unittest.skip('Skipping until #12647 is resolved.'      )
					@slow
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						self._assert_generated_batch_equal_expected()


@require_sentencepiece
@require_tokenizers
@require_tf
class        __SCREAMING_SNAKE_CASE							(	lowerCamelCase						):
					_lowerCamelCase							=				"""en"""
					_lowerCamelCase							=				"""zh"""
					_lowerCamelCase							=				["""My name is Wolfgang and I live in Berlin"""]
					_lowerCamelCase							=				["""我叫沃尔夫冈 我住在柏林"""]

					@unittest.skip('Skipping until #12647 is resolved.'      )
					@slow
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						self._assert_generated_batch_equal_expected()


@require_sentencepiece
@require_tokenizers
@require_tf
class        __SCREAMING_SNAKE_CASE							(	lowerCamelCase						):
					_lowerCamelCase							=				"""en"""
					_lowerCamelCase							=				"""ROMANCE"""
					_lowerCamelCase							=				[
					    """>>fr<< Don't spend so much time watching TV.""",
					    """>>pt<< Your message has been sent.""",
					    """>>es<< He's two years older than me.""",
					]
					_lowerCamelCase							=				[
					    """Ne passez pas autant de temps à regarder la télé.""",
					    """A sua mensagem foi enviada.""",
					    """Es dos años más viejo que yo.""",
					]

					@unittest.skip('Skipping until #12647 is resolved.'      )
					@slow
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						self._assert_generated_batch_equal_expected()





					@unittest.skip('Skipping until #12647 is resolved.'      )
					@slow
					def 			lowercase_							(       self      ):







						'''simple docstring'''




						lowerCamelCase_							:	Optional[int]							     =							TranslationPipeline(self.model				, self.tokenizer				, framework='tf'      )
						lowerCamelCase_							:	Optional[int]							     =							pipeline(self.src_text      )
						self.assertEqual(self.expected_text				, [x['translation_text'] for x in output]      )
