



'''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 		lowerCamelCase__      :




					'''simple docstring'''



					def __init__(  self				:	List[Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	int=2     ,  UpperCamelCase_				:	Union[str, Any]=3     ,  UpperCamelCase_				:	Optional[int]=4     ,  UpperCamelCase_				:	int=2     ,  UpperCamelCase_				:	Dict=7     ,  UpperCamelCase_				:	List[str]=True     ,  UpperCamelCase_				:	Union[str, Any]=True     ,  UpperCamelCase_				:	List[Any]=True     ,  UpperCamelCase_				:	Dict=True     ,  UpperCamelCase_				:	int=99     ,  UpperCamelCase_				:	Optional[Any]=36     ,  UpperCamelCase_				:	str=2     ,  UpperCamelCase_				:	int=4     ,  UpperCamelCase_				:	int=37     ,  UpperCamelCase_				:	Any="gelu"     ,  UpperCamelCase_				:	int=0.1     ,  UpperCamelCase_				:	Tuple=0.1     ,  UpperCamelCase_				:	Optional[Any]=512     ,  UpperCamelCase_				:	Optional[Any]=16     ,  UpperCamelCase_				:	Tuple=2     ,  UpperCamelCase_				:	Optional[Any]=0.02     ,  UpperCamelCase_				:	Any=6     ,  UpperCamelCase_				:	str=6     ,  UpperCamelCase_				:	Optional[Any]=3     ,  UpperCamelCase_				:	List[str]=4     ,  UpperCamelCase_				:	List[str]=None     ,  UpperCamelCase_				:	Dict=1000     ,  )  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       int       =		parent
						_lowercase					:       Dict       =		batch_size
						_lowercase					:       str       =		num_channels
						_lowercase					:       Optional[Any]       =		image_size
						_lowercase					:       Tuple       =		patch_size
						_lowercase					:       Optional[int]       =		is_training
						_lowercase					:       List[Any]       =		use_input_mask
						_lowercase					:       Optional[Any]       =		use_token_type_ids
						_lowercase					:       Optional[int]       =		use_labels
						_lowercase					:       List[str]       =		vocab_size
						_lowercase					:       int       =		hidden_size
						_lowercase					:       List[Any]       =		num_hidden_layers
						_lowercase					:       List[Any]       =		num_attention_heads
						_lowercase					:       int       =		intermediate_size
						_lowercase					:       str       =		hidden_act
						_lowercase					:       List[Any]       =		hidden_dropout_prob
						_lowercase					:       str       =		attention_probs_dropout_prob
						_lowercase					:       Union[str, Any]       =		max_position_embeddings
						_lowercase					:       Union[str, Any]       =		type_vocab_size
						_lowercase					:       str       =		type_sequence_label_size
						_lowercase					:       List[Any]       =		initializer_range
						_lowercase					:       Optional[int]       =		coordinate_size
						_lowercase					:       List[str]       =		shape_size
						_lowercase					:       Tuple       =		num_labels
						_lowercase					:       List[str]       =		num_choices
						_lowercase					:       Dict       =		scope
						_lowercase					:       Tuple       =		range_bbox

						# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
						_lowercase					:       List[str]       =		text_seq_length
						_lowercase					:       str       =		(image_size // patch_size) ** 2 + 1
						_lowercase					:       Optional[Any]       =		self.text_seq_length + self.image_seq_length




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       int:



						'''simple docstring'''





						_lowercase					:       int       =		ids_tensor([self.batch_size, self.text_seq_length]     ,  self.vocab_size					)

						_lowercase					:       Dict       =		ids_tensor([self.batch_size, self.text_seq_length, 4]     ,  self.range_bbox					)
						_lowercase					:       Dict       =		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]:
									_lowercase					:       Dict       =		bbox[i, j, 3]
									_lowercase					:       Any       =		bbox[i, j, 1]
									_lowercase					:       str       =		tmp_coordinate
								if bbox[i, j, 2] < bbox[i, j, 0]:
									_lowercase					:       Dict       =		bbox[i, j, 2]
									_lowercase					:       Union[str, Any]       =		bbox[i, j, 0]
									_lowercase					:       str       =		tmp_coordinate
						_lowercase					:       str       =		tf.constant(UpperCamelCase_					)

						_lowercase					:       List[Any]       =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

						_lowercase					:       Dict       =		None
						if self.use_input_mask:
							_lowercase					:       str       =		random_attention_mask([self.batch_size, self.text_seq_length]					)

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

						_lowercase					:       Optional[Any]       =		None
						_lowercase					:       int       =		None
						if self.use_labels:
							_lowercase					:       str       =		ids_tensor([self.batch_size]     ,  self.type_sequence_label_size					)
							_lowercase					:       int       =		ids_tensor([self.batch_size, self.text_seq_length]     ,  self.num_labels					)

						_lowercase					:       str       =		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 						__UpperCAmelCase     (  self				:	Any     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Any					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		TFLayoutLMvaModel(config=UpperCamelCase_					)

						# text + image
						_lowercase					:       Optional[Any]       =		model(UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  training=UpperCamelCase_					)
						_lowercase					:       str       =		model(
						    UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  training=UpperCamelCase_     ,  )
						_lowercase					:       List[str]       =		model(UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  training=UpperCamelCase_					)

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

						# text only
						_lowercase					:       Any       =		model(UpperCamelCase_     ,  training=UpperCamelCase_					)
						self.parent.assertEqual(
						    result.last_hidden_state.shape     ,  (self.batch_size, self.text_seq_length, self.hidden_size)					)

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




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Tuple					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.num_labels
						_lowercase					:       List[Any]       =		TFLayoutLMvaForSequenceClassification(config=UpperCamelCase_					)
						_lowercase					:       Any       =		model(
						    UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_     ,  training=UpperCamelCase_     ,  )
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	List[str]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Any					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		self.num_labels
						_lowercase					:       List[str]       =		TFLayoutLMvaForTokenClassification(config=UpperCamelCase_					)
						_lowercase					:       List[Any]       =		model(
						    UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_     ,  training=UpperCamelCase_     ,  )
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.text_seq_length, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Optional[Any]					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       int       =		2
						_lowercase					:       Tuple       =		TFLayoutLMvaForQuestionAnswering(config=UpperCamelCase_					)
						_lowercase					:       Union[str, Any]       =		model(
						    UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  start_positions=UpperCamelCase_     ,  end_positions=UpperCamelCase_     ,  training=UpperCamelCase_     ,  )
						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 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.prepare_config_and_inputs()
						(_lowercase)					:       Tuple       =		config_and_inputs
						_lowercase					:       int       =		{
						    '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 		lowerCamelCase__      (						A	,   A	,   unittest.TestCase     ):




					'''simple docstring'''



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

					A_						    =						False
					A_						    =						False
					A_						    =						False




					def 						__UpperCAmelCase     (  self				:	Any     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Dict					)  ->       Dict:



						'''simple docstring'''





						return True




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Union[str, Any]=False					)  ->       dict:



						'''simple docstring'''





						_lowercase					:       Dict       =		copy.deepcopy(UpperCamelCase_					)

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

						if return_labels:
							if model_class in get_values(UpperCamelCase_					):
								_lowercase					:       Optional[Any]       =		tf.ones(self.model_tester.batch_size     ,  dtype=tf.intaa					)
							elif model_class in get_values(UpperCamelCase_					):
								_lowercase					:       List[str]       =		tf.zeros(self.model_tester.batch_size     ,  dtype=tf.intaa					)
								_lowercase					:       Union[str, Any]       =		tf.zeros(self.model_tester.batch_size     ,  dtype=tf.intaa					)
							elif model_class in get_values(UpperCamelCase_					):
								_lowercase					:       Dict       =		tf.zeros(self.model_tester.batch_size     ,  dtype=tf.intaa					)
							elif model_class in get_values(UpperCamelCase_					):
								_lowercase					:       List[str]       =		tf.zeros(
								    (self.model_tester.batch_size, self.model_tester.text_seq_length)     ,  dtype=tf.intaa					)

						return inputs_dict




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       str       =		TFLayoutLMvaModelTester(self					)
						_lowercase					:       Optional[Any]       =		ConfigTester(self     ,  config_class=UpperCamelCase_     ,  hidden_size=37					)




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Dict:



						'''simple docstring'''





						self.config_tester.run_common_tests()




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Dict:



						'''simple docstring'''





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

								# Test that model correctly compute the loss with kwargs
								_lowercase					:       List[str]       =		self._prepare_for_class(inputs_dict.copy()     ,  UpperCamelCase_     ,  return_labels=UpperCamelCase_					)
								_lowercase					:       Optional[int]       =		prepared_for_class.pop('input_ids'					)

								_lowercase					:       int       =		model(UpperCamelCase_     ,  **UpperCamelCase_					)[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
								_lowercase					:       int       =		self._prepare_for_class(inputs_dict.copy()     ,  UpperCamelCase_     ,  return_labels=UpperCamelCase_					)
								_lowercase					:       str       =		prepared_for_class.pop('input_ids'					)
								if "labels" in prepared_for_class:
									_lowercase					:       Dict       =		prepared_for_class['labels'].numpy()
									if len(labels.shape					) > 1 and labels.shape[1] != 1:
										_lowercase					:       int       =		-100
										_lowercase					:       List[str]       =		tf.convert_to_tensor(UpperCamelCase_					)
										_lowercase					:       List[str]       =		model(UpperCamelCase_     ,  **UpperCamelCase_					)[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
								_lowercase					:       Dict       =		self._prepare_for_class(inputs_dict.copy()     ,  UpperCamelCase_     ,  return_labels=UpperCamelCase_					)
								_lowercase					:       List[str]       =		model(UpperCamelCase_					)[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
								_lowercase					:       str       =		self._prepare_for_class(inputs_dict.copy()     ,  UpperCamelCase_     ,  return_labels=UpperCamelCase_					)

								# Get keys that were added with the _prepare_for_class function
								_lowercase					:       str       =		prepared_for_class.keys() - inputs_dict.keys()
								_lowercase					:       List[str]       =		inspect.signature(model.call					).parameters
								_lowercase					:       Optional[int]       =		list(signature.keys()					)

								# Create a dictionary holding the location of the tensors in the tuple
								_lowercase					:       Optional[int]       =		{0: 'input_ids'}
								for label_key in label_keys:
									_lowercase					:       Union[str, Any]       =		signature_names.index(UpperCamelCase_					)
									_lowercase					:       List[str]       =		label_key
								_lowercase					:       Tuple       =		sorted(tuple_index_mapping.items()					)
								# Initialize a list with their default values, update the values and convert to a tuple
								_lowercase					:       Optional[Any]       =		[]

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

								for index, value in sorted_tuple_index_mapping:
									_lowercase					:       int       =		prepared_for_class[value]

								_lowercase					:       Any       =		tuple(UpperCamelCase_					)

								# Send to model
								_lowercase					:       List[str]       =		model(tuple_input[:-1]					)[0]

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




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       int:



						'''simple docstring'''





						(
						    _lowercase    
						)					:       int       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       int:



						'''simple docstring'''





						(
						    _lowercase    
						)					:       Tuple       =		self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
							_lowercase					:       Dict       =		type
							self.model_tester.create_and_check_model(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       str:



						'''simple docstring'''





						(
						    _lowercase    
						)					:       Union[str, Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(
						    UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       Tuple:



						'''simple docstring'''





						(
						    _lowercase    
						)					:       Optional[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(
						    UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)




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



						'''simple docstring'''





						(
						    _lowercase    
						)					:       List[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(
						    UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)




					@slow
					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Tuple:



						'''simple docstring'''





						for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_lowercase					:       Optional[int]       =		TFLayoutLMvaModel.from_pretrained(UpperCamelCase_					)
							self.assertIsNotNone(UpperCamelCase_					)







def 						__UpperCamelCase				(							) ->			Optional[int]:

	'''simple docstring'''





	_lowercase					:       List[str]       =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
	return image






@require_tf
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					@cached_property
					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Optional[Any]:



						'''simple docstring'''





						return LayoutLMvaImageProcessor(apply_ocr=UpperCamelCase_					) if is_vision_available() else None




					@slow
					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'					)

						_lowercase					:       Dict       =		self.default_image_processor
						_lowercase					:       List[str]       =		prepare_img()
						_lowercase					:       List[str]       =		image_processor(images=UpperCamelCase_     ,  return_tensors='tf'					).pixel_values

						_lowercase					:       Dict       =		tf.constant([[1, 2]]					)
						_lowercase					:       Union[str, Any]       =		tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]					)     ,  axis=0					)

						# forward pass
						_lowercase					:       Optional[int]       =		model(input_ids=UpperCamelCase_     ,  bbox=UpperCamelCase_     ,  pixel_values=UpperCamelCase_     ,  training=UpperCamelCase_					)

						# verify the logits
						_lowercase					:       Any       =		(1, 199, 768)
						self.assertEqual(outputs.last_hidden_state.shape     ,  UpperCamelCase_					)

						_lowercase					:       Union[str, Any]       =		tf.constant(
						    [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]]					)

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





'''simple docstring'''

import unittest

import numpy as np

from transformers import RoFormerConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
		import jax.numpy as jnp

		from transformers.models.roformer.modeling_flax_roformer import (
		    FlaxRoFormerForMaskedLM,
		    FlaxRoFormerForMultipleChoice,
		    FlaxRoFormerForQuestionAnswering,
		    FlaxRoFormerForSequenceClassification,
		    FlaxRoFormerForTokenClassification,
		    FlaxRoFormerModel,
		)



class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					def __init__(  self				:	Dict     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	List[str]=13     ,  UpperCamelCase_				:	Union[str, Any]=7     ,  UpperCamelCase_				:	Union[str, Any]=True     ,  UpperCamelCase_				:	Any=True     ,  UpperCamelCase_				:	Dict=True     ,  UpperCamelCase_				:	List[str]=True     ,  UpperCamelCase_				:	int=99     ,  UpperCamelCase_				:	Tuple=32     ,  UpperCamelCase_				:	List[str]=5     ,  UpperCamelCase_				:	Dict=4     ,  UpperCamelCase_				:	Tuple=37     ,  UpperCamelCase_				:	int="gelu"     ,  UpperCamelCase_				:	int=0.1     ,  UpperCamelCase_				:	Dict=0.1     ,  UpperCamelCase_				:	Any=512     ,  UpperCamelCase_				:	List[Any]=16     ,  UpperCamelCase_				:	Optional[int]=2     ,  UpperCamelCase_				:	Tuple=0.02     ,  UpperCamelCase_				:	Union[str, Any]=4     ,  )  ->       Tuple:



						'''simple docstring'''





						_lowercase					:       int       =		parent
						_lowercase					:       str       =		batch_size
						_lowercase					:       List[str]       =		seq_length
						_lowercase					:       Dict       =		is_training
						_lowercase					:       Optional[int]       =		use_attention_mask
						_lowercase					:       List[Any]       =		use_token_type_ids
						_lowercase					:       Union[str, Any]       =		use_labels
						_lowercase					:       Dict       =		vocab_size
						_lowercase					:       List[Any]       =		hidden_size
						_lowercase					:       Any       =		num_hidden_layers
						_lowercase					:       int       =		num_attention_heads
						_lowercase					:       Optional[int]       =		intermediate_size
						_lowercase					:       Any       =		hidden_act
						_lowercase					:       List[str]       =		hidden_dropout_prob
						_lowercase					:       Union[str, Any]       =		attention_probs_dropout_prob
						_lowercase					:       Optional[int]       =		max_position_embeddings
						_lowercase					:       int       =		type_vocab_size
						_lowercase					:       Any       =		type_sequence_label_size
						_lowercase					:       Any       =		initializer_range
						_lowercase					:       str       =		num_choices




					def 						__UpperCAmelCase     (  self				:	str					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size					)

						_lowercase					:       int       =		None
						if self.use_attention_mask:
							_lowercase					:       Optional[int]       =		random_attention_mask([self.batch_size, self.seq_length]					)

						_lowercase					:       Any       =		None
						if self.use_token_type_ids:
							_lowercase					:       Optional[Any]       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.type_vocab_size					)

						_lowercase					:       str       =		RoFormerConfig(
						    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     ,  is_decoder=UpperCamelCase_     ,  initializer_range=self.initializer_range     ,  )

						return config, input_ids, token_type_ids, attention_mask




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Dict       =		self.prepare_config_and_inputs()
						_lowercase  ,    _lowercase  ,    _lowercase  ,    _lowercase					:       Union[str, Any]       =		config_and_inputs
						_lowercase					:       Optional[int]       =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
						return config, inputs_dict






@require_flax
class 		lowerCamelCase__      (						A	,   unittest.TestCase     ):




					'''simple docstring'''



					A_						    =						True

					A_						    =						(
					    (
					        FlaxRoFormerModel,
					        FlaxRoFormerForMaskedLM,
					        FlaxRoFormerForSequenceClassification,
					        FlaxRoFormerForTokenClassification,
					        FlaxRoFormerForMultipleChoice,
					        FlaxRoFormerForQuestionAnswering,
					    )
					    if is_flax_available()
					    else ()
					)




					def 						__UpperCAmelCase     (  self				:	str					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Tuple       =		FlaxRoFormerModelTester(self					)




					@slow
					def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       Optional[int]:



						'''simple docstring'''





						for model_class_name in self.all_model_classes:
							_lowercase					:       Optional[int]       =		model_class_name.from_pretrained('junnyu/roformer_chinese_small'     ,  from_pt=UpperCamelCase_					)
							_lowercase					:       str       =		model(np.ones((1, 1)					)					)
							self.assertIsNotNone(UpperCamelCase_					)






@require_flax
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					@slow
					def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Dict       =		FlaxRoFormerForMaskedLM.from_pretrained('junnyu/roformer_chinese_base'					)
						_lowercase					:       Any       =		jnp.array([[0, 1, 2, 3, 4, 5]]					)
						_lowercase					:       int       =		model(UpperCamelCase_					)[0]

						_lowercase					:       Union[str, Any]       =		5_0000

						_lowercase					:       str       =		(1, 6, vocab_size)
						self.assertEqual(output.shape     ,  UpperCamelCase_					)

						_lowercase					:       int       =		jnp.array(
						    [[[-0.12_05, -1.02_65, 0.29_22], [-1.51_34, 0.19_74, 0.15_19], [-5.01_35, -3.90_03, -0.84_04]]]					)

						self.assertTrue(jnp.allclose(output[:, :3, :3]     ,  UpperCamelCase_     ,  atol=1E-4					)					)
