



"""simple docstring"""





from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

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 tensorflow as tf

					from transformers import (
					    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
					    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    LayoutLMvaConfig,
					    TFLayoutLMvaForQuestionAnswering,
					    TFLayoutLMvaForSequenceClassification,
					    TFLayoutLMvaForTokenClassification,
					    TFLayoutLMvaModel,
					)

if is_vision_available():
					from PIL import Image

					from transformers import LayoutLMvaImageProcessor




class 			lowercase__			:
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case=2	,       snake_case=3	,       snake_case=4	,       snake_case=2	,       snake_case=7	,       snake_case=True	,       snake_case=True	,       snake_case=True	,       snake_case=True	,       snake_case=99	,       snake_case=36	,       snake_case=2	,       snake_case=4	,       snake_case=37	,       snake_case="gelu"	,       snake_case=0.1	,       snake_case=0.1	,       snake_case=512	,       snake_case=16	,       snake_case=2	,       snake_case=0.02	,       snake_case=6	,       snake_case=6	,       snake_case=3	,       snake_case=4	,       snake_case=None	,       snake_case=1000	,       )			->							List[Any]:
									_UpperCAmelCase							 =		parent
									_UpperCAmelCase							 =		batch_size
									_UpperCAmelCase							 =		num_channels
									_UpperCAmelCase							 =		image_size
									_UpperCAmelCase							 =		patch_size
									_UpperCAmelCase							 =		is_training
									_UpperCAmelCase							 =		use_input_mask
									_UpperCAmelCase							 =		use_token_type_ids
									_UpperCAmelCase							 =		use_labels
									_UpperCAmelCase							 =		vocab_size
									_UpperCAmelCase							 =		hidden_size
									_UpperCAmelCase							 =		num_hidden_layers
									_UpperCAmelCase							 =		num_attention_heads
									_UpperCAmelCase							 =		intermediate_size
									_UpperCAmelCase							 =		hidden_act
									_UpperCAmelCase							 =		hidden_dropout_prob
									_UpperCAmelCase							 =		attention_probs_dropout_prob
									_UpperCAmelCase							 =		max_position_embeddings
									_UpperCAmelCase							 =		type_vocab_size
									_UpperCAmelCase							 =		type_sequence_label_size
									_UpperCAmelCase							 =		initializer_range
									_UpperCAmelCase							 =		coordinate_size
									_UpperCAmelCase							 =		shape_size
									_UpperCAmelCase							 =		num_labels
									_UpperCAmelCase							 =		num_choices
									_UpperCAmelCase							 =		scope
									_UpperCAmelCase							 =		range_bbox

									# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
									_UpperCAmelCase							 =		text_seq_length
									_UpperCAmelCase							 =		(image_size // patch_size) ** 2 + 1
									_UpperCAmelCase							 =		self.text_seq_length + self.image_seq_length






							def        lowerCamelCase_   (  self						)			->							int:
									_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.text_seq_length]	,       self.vocab_size						)

									_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.text_seq_length, 4]	,       self.range_bbox						)
									_UpperCAmelCase							 =		bbox.numpy()
									# Ensure that bbox is legal
									for i in range(bbox.shape[0]						):
											for j in range(bbox.shape[1]						):
													if bbox[i, j, 3] < bbox[i, j, 1]:
															_UpperCAmelCase							 =		bbox[i, j, 3]
															_UpperCAmelCase							 =		bbox[i, j, 1]
															_UpperCAmelCase							 =		tmp_coordinate
													if bbox[i, j, 2] < bbox[i, j, 0]:
															_UpperCAmelCase							 =		bbox[i, j, 2]
															_UpperCAmelCase							 =		bbox[i, j, 0]
															_UpperCAmelCase							 =		tmp_coordinate
									_UpperCAmelCase							 =		tf.constant(lowercase_						)

									_UpperCAmelCase							 =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

									_UpperCAmelCase							 =		None
									if self.use_input_mask:
											_UpperCAmelCase							 =		random_attention_mask([self.batch_size, self.text_seq_length]						)

									_UpperCAmelCase							 =		None
									if self.use_token_type_ids:
											_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.text_seq_length]	,       self.type_vocab_size						)

									_UpperCAmelCase							 =		None
									_UpperCAmelCase							 =		None
									if self.use_labels:
											_UpperCAmelCase							 =		ids_tensor([self.batch_size]	,       self.type_sequence_label_size						)
											_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.text_seq_length]	,       self.num_labels						)

									_UpperCAmelCase							 =		LayoutLMvaConfig(
									    vocab_size=self.vocab_size	,       hidden_size=self.hidden_size	,       num_hidden_layers=self.num_hidden_layers	,       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	,       coordinate_size=self.coordinate_size	,       shape_size=self.shape_size	,       input_size=self.image_size	,       patch_size=self.patch_size	,       )

									return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		TFLayoutLMvaModel(config=lowercase_						)

									# text + image
									_UpperCAmelCase							 =		model(lowercase_	,       pixel_values=lowercase_	,       training=lowercase_						)
									_UpperCAmelCase							 =		model(
									    lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       attention_mask=lowercase_	,       token_type_ids=lowercase_	,       training=lowercase_	,       )
									_UpperCAmelCase							 =		model(lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       training=lowercase_						)

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

									# text only
									_UpperCAmelCase							 =		model(lowercase_	,       training=lowercase_						)
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,       (self.batch_size, self.text_seq_length, self.hidden_size)						)

									# image only
									_UpperCAmelCase							 =		model({'pixel_values': pixel_values}	,       training=lowercase_						)
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,       (self.batch_size, self.image_seq_length, self.hidden_size)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		self.num_labels
									_UpperCAmelCase							 =		TFLayoutLMvaForSequenceClassification(config=lowercase_						)
									_UpperCAmelCase							 =		model(
									    lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       attention_mask=lowercase_	,       token_type_ids=lowercase_	,       labels=lowercase_	,       training=lowercase_	,       )
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.num_labels)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							str:
									_UpperCAmelCase							 =		self.num_labels
									_UpperCAmelCase							 =		TFLayoutLMvaForTokenClassification(config=lowercase_						)
									_UpperCAmelCase							 =		model(
									    lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       attention_mask=lowercase_	,       token_type_ids=lowercase_	,       labels=lowercase_	,       training=lowercase_	,       )
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.text_seq_length, self.num_labels)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Optional[int]:
									_UpperCAmelCase							 =		2
									_UpperCAmelCase							 =		TFLayoutLMvaForQuestionAnswering(config=lowercase_						)
									_UpperCAmelCase							 =		model(
									    lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       attention_mask=lowercase_	,       token_type_ids=lowercase_	,       start_positions=lowercase_	,       end_positions=lowercase_	,       training=lowercase_	,       )
									self.parent.assertEqual(result.start_logits.shape	,       (self.batch_size, self.seq_length)						)
									self.parent.assertEqual(result.end_logits.shape	,       (self.batch_size, self.seq_length)						)






							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		self.prepare_config_and_inputs()
									((_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase) ,					(_UpperCAmelCase))							 =		config_and_inputs
									_UpperCAmelCase							 =		{
									    'input_ids': input_ids,
									    'bbox': bbox,
									    'pixel_values': pixel_values,
									    'token_type_ids': token_type_ids,
									    'attention_mask': input_mask,
									}
									return config, inputs_dict







@require_tf
class 			lowercase__			(      A,					A,					unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					(
							    (
							        TFLayoutLMvaModel,
							        TFLayoutLMvaForQuestionAnswering,
							        TFLayoutLMvaForSequenceClassification,
							        TFLayoutLMvaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							_UpperCAmelCase      				=					(
							    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
							    if is_tf_available()
							    else {}
							)

							_UpperCAmelCase      				=					False
							_UpperCAmelCase      				=					False
							_UpperCAmelCase      				=					False






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Optional[int]:
									return True






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case=False						)			->							dict:
									_UpperCAmelCase							 =		copy.deepcopy(lowercase_						)

									if model_class in get_values(lowercase_						):
											_UpperCAmelCase							 =		{
											    k: tf.tile(tf.expand_dims(lowercase_	,       1						)	,       (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)						)
											    if isinstance(lowercase_	,       tf.Tensor						) and v.ndim > 0
											    else v
											    for k, v in inputs_dict.items()
											}

									if return_labels:
											if model_class in get_values(lowercase_						):
													_UpperCAmelCase							 =		tf.ones(self.model_tester.batch_size	,       dtype=tf.intaa						)
											elif model_class in get_values(lowercase_						):
													_UpperCAmelCase							 =		tf.zeros(self.model_tester.batch_size	,       dtype=tf.intaa						)
													_UpperCAmelCase							 =		tf.zeros(self.model_tester.batch_size	,       dtype=tf.intaa						)
											elif model_class in get_values(lowercase_						):
													_UpperCAmelCase							 =		tf.zeros(self.model_tester.batch_size	,       dtype=tf.intaa						)
											elif model_class in get_values(lowercase_						):
													_UpperCAmelCase							 =		tf.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length)	,       dtype=tf.intaa						)

									return inputs_dict






							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase							 =		TFLayoutLMvaModelTester(self						)
									_UpperCAmelCase							 =		ConfigTester(self	,       config_class=lowercase_	,       hidden_size=37						)






							def        lowerCamelCase_   (  self						)			->							List[Any]:
									self.config_tester.run_common_tests()






							def        lowerCamelCase_   (  self						)			->							List[Any]:
									_UpperCAmelCase ,					_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs_for_common()
									for model_class in self.all_model_classes:
											_UpperCAmelCase							 =		model_class(lowercase_						)
											if getattr(lowercase_	,       'hf_compute_loss'	,       lowercase_						):
													# The number of elements in the loss should be the same as the number of elements in the label
													_UpperCAmelCase							 =		self._prepare_for_class(inputs_dict.copy()	,       lowercase_	,       return_labels=lowercase_						)
													_UpperCAmelCase							 =		prepared_for_class[
													    sorted(prepared_for_class.keys() - inputs_dict.keys()	,       reverse=lowercase_						)[0]
													]
													_UpperCAmelCase							 =		added_label.shape.as_list()[:1]

													# Test that model correctly compute the loss with kwargs
													_UpperCAmelCase							 =		self._prepare_for_class(inputs_dict.copy()	,       lowercase_	,       return_labels=lowercase_						)
													_UpperCAmelCase							 =		prepared_for_class.pop('input_ids'						)

													_UpperCAmelCase							 =		model(lowercase_	,       **lowercase_						)[0]
													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)

													# Test that model correctly compute the loss when we mask some positions
													_UpperCAmelCase							 =		self._prepare_for_class(inputs_dict.copy()	,       lowercase_	,       return_labels=lowercase_						)
													_UpperCAmelCase							 =		prepared_for_class.pop('input_ids'						)
													if "labels" in prepared_for_class:
															_UpperCAmelCase							 =		prepared_for_class['labels'].numpy()
															if len(labels.shape						) > 1 and labels.shape[1] != 1:
																	_UpperCAmelCase							 =		-100
																	_UpperCAmelCase							 =		tf.convert_to_tensor(lowercase_						)
																	_UpperCAmelCase							 =		model(lowercase_	,       **lowercase_						)[0]
																	self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)
																	self.assertTrue(not np.any(np.isnan(loss.numpy()						)						)						)

																	     # Test that model correctly compute the loss with a dict
													_UpperCAmelCase							 =		self._prepare_for_class(inputs_dict.copy()	,       lowercase_	,       return_labels=lowercase_						)
													_UpperCAmelCase							 =		model(lowercase_						)[0]
													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)

													# Test that model correctly compute the loss with a tuple
													_UpperCAmelCase							 =		self._prepare_for_class(inputs_dict.copy()	,       lowercase_	,       return_labels=lowercase_						)

													# Get keys that were added with the _prepare_for_class function
													_UpperCAmelCase							 =		prepared_for_class.keys() - inputs_dict.keys()
													_UpperCAmelCase							 =		inspect.signature(model.call						).parameters
													_UpperCAmelCase							 =		list(signature.keys()						)

													# Create a dictionary holding the location of the tensors in the tuple
													_UpperCAmelCase							 =		{0: 'input_ids'}
													for label_key in label_keys:
															_UpperCAmelCase							 =		signature_names.index(lowercase_						)
															_UpperCAmelCase							 =		label_key
													_UpperCAmelCase							 =		sorted(tuple_index_mapping.items()						)
													# Initialize a list with their default values, update the values and convert to a tuple
													_UpperCAmelCase							 =		[]

													for name in signature_names:
															if name != "kwargs":
																	list_input.append(signature[name].default						)

													for index, value in sorted_tuple_index_mapping:
															_UpperCAmelCase							 =		prepared_for_class[value]

													_UpperCAmelCase							 =		tuple(lowercase_						)

													# Send to model
													_UpperCAmelCase							 =		model(tuple_input[:-1]						)[0]

													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									(
									    (
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,						
									)							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_						)






							def        lowerCamelCase_   (  self						)			->							Dict:
									(
									    (
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,						
									)							 =		self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
											_UpperCAmelCase							 =		type
											self.model_tester.create_and_check_model(lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_						)






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									(
									    (
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,						
									)							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(
									    lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_						)






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									(
									    (
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,						
									)							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(
									    lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_						)






							def        lowerCamelCase_   (  self						)			->							int:
									(
									    (
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,					(
									    _UpperCAmelCase	
									) ,						
									)							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(
									    lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_	,       lowercase_						)






							@slow
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:







									for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_UpperCAmelCase							 =		TFLayoutLMvaModel.from_pretrained(lowercase_						)
											self.assertIsNotNone(lowercase_						)







def 	UpperCAmelCase     (		):

		'''simple docstring'''


		_UpperCAmelCase							 =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
		return image





@require_tf
class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							@cached_property
							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									return LayoutLMvaImageProcessor(apply_ocr=lowercase_						) if is_vision_available() else None






							@slow
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'						)

									_UpperCAmelCase							 =		self.default_image_processor
									_UpperCAmelCase							 =		prepare_img()
									_UpperCAmelCase							 =		image_processor(images=lowercase_	,       return_tensors='tf'						).pixel_values

									_UpperCAmelCase							 =		tf.constant([[1, 2]]						)
									_UpperCAmelCase							 =		tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]						)	,       axis=0						)

									# forward pass
									_UpperCAmelCase							 =		model(input_ids=lowercase_	,       bbox=lowercase_	,       pixel_values=lowercase_	,       training=lowercase_						)

									# verify the logits
									_UpperCAmelCase							 =		(1, 199, 768)
									self.assertEqual(outputs.last_hidden_state.shape	,       lowercase_						)

									_UpperCAmelCase							 =		tf.constant(
									    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]						)

									self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3]	,       lowercase_	,       atol=1E-4						)						)







"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
					import torch




@is_pipeline_test
class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							_UpperCAmelCase      				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		TextaTextGenerationPipeline(model=snake_case	,       tokenizer=snake_case						)
									return generator, ["Something to write", "Something else"]






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		generator('Something there'						)
									self.assertEqual(snake_case	,       [{'generated_text': ANY(snake_case						)}]						)
									# These are encoder decoder, they don't just append to incoming string
									self.assertFalse(outputs[0]['generated_text'].startswith('Something there'						)						)

									_UpperCAmelCase							 =		generator(['This is great !', 'Something else']	,       num_return_sequences=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									_UpperCAmelCase							 =		generator(
									    ['This is great !', 'Something else']	,       num_return_sequences=2	,       batch_size=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									with self.assertRaises(snake_case						):
											generator(4						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='pt'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)

									_UpperCAmelCase							 =		3
									_UpperCAmelCase							 =		generator(
									    'Something there'	,       num_return_sequences=snake_case	,       num_beams=snake_case	,       )
									_UpperCAmelCase							 =		[
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': ''},
									]
									self.assertEqual(snake_case	,       snake_case						)

									_UpperCAmelCase							 =		generator('This is a test'	,       do_sample=snake_case	,       num_return_sequences=2	,       return_tensors=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        {'generated_token_ids': ANY(torch.Tensor						)},
									        {'generated_token_ids': ANY(torch.Tensor						)},
									    ]	,       )
									_UpperCAmelCase							 =		generator.model.config.eos_token_id
									_UpperCAmelCase							 =		'<pad>'
									_UpperCAmelCase							 =		generator(
									    ['This is a test', 'This is a second test']	,       do_sample=snake_case	,       num_return_sequences=2	,       batch_size=2	,       return_tensors=snake_case	,       )
									self.assertEqual(
									    snake_case	,       [
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									    ]	,       )






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Any:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='tf'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)


