




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







							def __init__(    self			:    Optional[int]				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    int=2				,     lowerCAmelCase_			:    List[Any]=3				,     lowerCAmelCase_			:    Optional[int]=4				,     lowerCAmelCase_			:    Any=2				,     lowerCAmelCase_			:    Union[str, Any]=7				,     lowerCAmelCase_			:    Union[str, Any]=True				,     lowerCAmelCase_			:    Optional[int]=True				,     lowerCAmelCase_			:    List[Any]=True				,     lowerCAmelCase_			:    Any=True				,     lowerCAmelCase_			:    Any=99				,     lowerCAmelCase_			:    Any=36				,     lowerCAmelCase_			:    Optional[int]=2				,     lowerCAmelCase_			:    int=4				,     lowerCAmelCase_			:    int=37				,     lowerCAmelCase_			:    Tuple="gelu"				,     lowerCAmelCase_			:    str=0.1				,     lowerCAmelCase_			:    Optional[int]=0.1				,     lowerCAmelCase_			:    Tuple=5_12				,     lowerCAmelCase_			:    List[Any]=16				,     lowerCAmelCase_			:    int=2				,     lowerCAmelCase_			:    Optional[int]=0.0_2				,     lowerCAmelCase_			:    List[str]=6				,     lowerCAmelCase_			:    Dict=6				,     lowerCAmelCase_			:    List[Any]=3				,     lowerCAmelCase_			:    Union[str, Any]=4				,     lowerCAmelCase_			:    Dict=None				,     lowerCAmelCase_			:    Optional[int]=10_00				,     )     ->				List[Any]:


														"""simple docstring"""


														_a      	=							parent
														_a      	=							batch_size
														_a      	=							num_channels
														_a      	=							image_size
														_a      	=							patch_size
														_a      	=							is_training
														_a      	=							use_input_mask
														_a      	=							use_token_type_ids
														_a      	=							use_labels
														_a      	=							vocab_size
														_a      	=							hidden_size
														_a      	=							num_hidden_layers
														_a      	=							num_attention_heads
														_a      	=							intermediate_size
														_a      	=							hidden_act
														_a      	=							hidden_dropout_prob
														_a      	=							attention_probs_dropout_prob
														_a      	=							max_position_embeddings
														_a      	=							type_vocab_size
														_a      	=							type_sequence_label_size
														_a      	=							initializer_range
														_a      	=							coordinate_size
														_a      	=							shape_size
														_a      	=							num_labels
														_a      	=							num_choices
														_a      	=							scope
														_a      	=							range_bbox

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







							def   __lowerCAmelCase     (    self			:    Optional[Any]   )     ->				Tuple:


														"""simple docstring"""


														_a      	=							ids_tensor([self.batch_size, self.text_seq_length]				,     self.vocab_size   )

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

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

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

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

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

														_a      	=							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			:    List[Any]				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Union[str, Any]   )     ->				str:


														"""simple docstring"""


														_a      	=							TFLayoutLMvaModel(config=lowerCAmelCase_   )

														# text + image
														_a      	=							model(lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     training=lowerCAmelCase_   )
														_a      	=							model(
														    lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     token_type_ids=lowerCAmelCase_				,     training=lowerCAmelCase_				,     )
														_a      	=							model(lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     training=lowerCAmelCase_   )

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

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

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







							def   __lowerCAmelCase     (    self			:    Any				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    str				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Union[str, Any]   )     ->				List[Any]:


														"""simple docstring"""


														_a      	=							self.num_labels
														_a      	=							TFLayoutLMvaForSequenceClassification(config=lowerCAmelCase_   )
														_a      	=							model(
														    lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     token_type_ids=lowerCAmelCase_				,     labels=lowerCAmelCase_				,     training=lowerCAmelCase_				,     )
														self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)   )







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


														"""simple docstring"""


														_a      	=							self.num_labels
														_a      	=							TFLayoutLMvaForTokenClassification(config=lowerCAmelCase_   )
														_a      	=							model(
														    lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     token_type_ids=lowerCAmelCase_				,     labels=lowerCAmelCase_				,     training=lowerCAmelCase_				,     )
														self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.text_seq_length, self.num_labels)   )







							def   __lowerCAmelCase     (    self			:    int				,     lowerCAmelCase_			:    str				,     lowerCAmelCase_			:    Union[str, Any]				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Union[str, Any]				,     lowerCAmelCase_			:    List[str]   )     ->				int:


														"""simple docstring"""


														_a      	=							2
														_a      	=							TFLayoutLMvaForQuestionAnswering(config=lowerCAmelCase_   )
														_a      	=							model(
														    lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     token_type_ids=lowerCAmelCase_				,     start_positions=lowerCAmelCase_				,     end_positions=lowerCAmelCase_				,     training=lowerCAmelCase_				,     )
														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			:    List[Any]   )     ->				Any:


														"""simple docstring"""


														_a      	=							self.prepare_config_and_inputs()
														((_a)	,				(_a)	,				(_a)	,				(_a)	,				(_a)	,				(_a)	,				(_a)	,				(_a))      	=							config_and_inputs
														_a      	=							{
														    '''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      A       (						_a	,_a	,unittest.TestCase       ):
							lowercase_							     =							(
							    (
							        TFLayoutLMvaModel,
							        TFLayoutLMvaForQuestionAnswering,
							        TFLayoutLMvaForSequenceClassification,
							        TFLayoutLMvaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase_							     =							(
							    {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel}
							    if is_tf_available()
							    else {}
							)

							lowercase_							     =							False
							lowercase_							     =							False
							lowercase_							     =							False







							def   __lowerCAmelCase     (    self			:    Any				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    Tuple   )     ->				Optional[Any]:


														"""simple docstring"""


														return True







							def   __lowerCAmelCase     (    self			:    Optional[int]				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    str=False   )     ->				dict:


														"""simple docstring"""


														_a      	=							copy.deepcopy(lowerCAmelCase_   )

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

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

														return inputs_dict







							def   __lowerCAmelCase     (    self			:    Dict   )     ->				Union[str, Any]:


														"""simple docstring"""


														_a      	=							TFLayoutLMvaModelTester(self   )
														_a      	=							ConfigTester(self				,     config_class=lowerCAmelCase_				,     hidden_size=37   )







							def   __lowerCAmelCase     (    self			:    int   )     ->				Tuple:


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def   __lowerCAmelCase     (    self			:    List[str]   )     ->				List[str]:


														"""simple docstring"""


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

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

																												_a      	=							model(lowerCAmelCase_				,     **lowerCAmelCase_   )[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
																												_a      	=							self._prepare_for_class(inputs_dict.copy()				,     lowerCAmelCase_				,     return_labels=lowerCAmelCase_   )
																												_a      	=							prepared_for_class.pop('''input_ids'''   )
																												if "labels" in prepared_for_class:
																																			_a      	=							prepared_for_class['''labels'''].numpy()
																																			if len(labels.shape   ) > 1 and labels.shape[1] != 1:
																																										_a      	=							-1_00
																																										_a      	=							tf.convert_to_tensor(lowerCAmelCase_   )
																																										_a      	=							model(lowerCAmelCase_				,     **lowerCAmelCase_   )[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
																												_a      	=							self._prepare_for_class(inputs_dict.copy()				,     lowerCAmelCase_				,     return_labels=lowerCAmelCase_   )
																												_a      	=							model(lowerCAmelCase_   )[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
																												_a      	=							self._prepare_for_class(inputs_dict.copy()				,     lowerCAmelCase_				,     return_labels=lowerCAmelCase_   )

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

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

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

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

																												_a      	=							tuple(lowerCAmelCase_   )

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

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







							def   __lowerCAmelCase     (    self			:    Optional[Any]   )     ->				Dict:


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







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


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					_a      	=							type
																					self.model_tester.create_and_check_model(lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    Tuple   )     ->				Dict:


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(
														    lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    Tuple   )     ->				Tuple:


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(
														    lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    Optional[Any]   )     ->				Optional[Any]:


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(
														    lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







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


														"""simple docstring"""


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



def 						snake_case_       ():





							'''simple docstring'''


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

@require_tf
class      A       (						unittest.TestCase       ):







							@cached_property
							def   __lowerCAmelCase     (    self			:    Dict   )     ->				Dict:


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a      	=							TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''   )

														_a      	=							self.default_image_processor
														_a      	=							prepare_img()
														_a      	=							image_processor(images=lowerCAmelCase_				,     return_tensors='''tf'''   ).pixel_values

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

														# forward pass
														_a      	=							model(input_ids=lowerCAmelCase_				,     bbox=lowerCAmelCase_				,     pixel_values=lowerCAmelCase_				,     training=lowerCAmelCase_   )

														# verify the logits
														_a      	=							(1, 1_99, 7_68)
														self.assertEqual(outputs.last_hidden_state.shape				,     lowerCAmelCase_   )

														_a      	=							tf.constant(
														    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]   )

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





'''simple docstring'''
import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp


def       _lowercase							(					):
 __A	:		List[Any]	  =      ArgumentParser(
     description=(
         'PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'
     )      )

 # Optional arguments for the launch helper
 parser.add_argument('--num_cores',					type=UpperCamelCase__,					default=1,					help='Number of TPU cores to use (1 or 8).'      )

 # positional
 parser.add_argument(
     'training_script',					type=UpperCamelCase__,					help=(
         'The full path to the single TPU training '
         'program/script to be launched in parallel, '
         'followed by all the arguments for the '
         'training script'
     ),					)

 # rest from the training program
 parser.add_argument('training_script_args',					nargs=UpperCamelCase__      )

 return parser.parse_args()


def       _lowercase							(					):
 __A	:		str	  =      parse_args()

 # Import training_script as a module.
 __A	:		Tuple	  =      Path(args.training_script      )
 sys.path.append(str(script_fpath.parent.resolve()      )      )
 __A	:		str	  =      script_fpath.stem
 __A	:		Dict	  =      importlib.import_module(UpperCamelCase__      )

 # Patch sys.argv
 __A	:		Optional[Any]	  =      [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores      )]

 xmp.spawn(mod._mp_fn,					args=(),					nprocs=args.num_cores      )


if __name__ == "__main__":
 main()






