


"""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 						snake_case_:






     def __init__(		self     :    Union[str, Any]    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Tuple=2    , UpperCamelCase_     :    List[str]=3    , UpperCamelCase_     :    Optional[int]=4    , UpperCamelCase_     :    int=2    , UpperCamelCase_     :    List[str]=7    , UpperCamelCase_     :    Dict=True    , UpperCamelCase_     :    Any=True    , UpperCamelCase_     :    List[Any]=True    , UpperCamelCase_     :    Optional[int]=True    , UpperCamelCase_     :    List[Any]=9_9    , UpperCamelCase_     :    Dict=3_6    , UpperCamelCase_     :    Any=2    , UpperCamelCase_     :    int=4    , UpperCamelCase_     :    str=3_7    , UpperCamelCase_     :    Union[str, Any]="gelu"    , UpperCamelCase_     :    List[Any]=0.1    , UpperCamelCase_     :    Tuple=0.1    , UpperCamelCase_     :    Dict=5_1_2    , UpperCamelCase_     :    Optional[Any]=1_6    , UpperCamelCase_     :    int=2    , UpperCamelCase_     :    List[Any]=0.02    , UpperCamelCase_     :    Any=6    , UpperCamelCase_     :    Optional[int]=6    , UpperCamelCase_     :    List[str]=3    , UpperCamelCase_     :    Optional[Any]=4    , UpperCamelCase_     :    int=None    , UpperCamelCase_     :    Any=1_0_0_0    , ):
           lowerCAmelCase    :		int				=      parent
           lowerCAmelCase    :		str				=      batch_size
           lowerCAmelCase    :		List[Any]				=      num_channels
           lowerCAmelCase    :		Any				=      image_size
           lowerCAmelCase    :		int				=      patch_size
           lowerCAmelCase    :		Any				=      is_training
           lowerCAmelCase    :		Union[str, Any]				=      use_input_mask
           lowerCAmelCase    :		List[Any]				=      use_token_type_ids
           lowerCAmelCase    :		Optional[Any]				=      use_labels
           lowerCAmelCase    :		List[str]				=      vocab_size
           lowerCAmelCase    :		Tuple				=      hidden_size
           lowerCAmelCase    :		List[str]				=      num_hidden_layers
           lowerCAmelCase    :		int				=      num_attention_heads
           lowerCAmelCase    :		Tuple				=      intermediate_size
           lowerCAmelCase    :		List[str]				=      hidden_act
           lowerCAmelCase    :		Optional[Any]				=      hidden_dropout_prob
           lowerCAmelCase    :		Optional[int]				=      attention_probs_dropout_prob
           lowerCAmelCase    :		Optional[Any]				=      max_position_embeddings
           lowerCAmelCase    :		Dict				=      type_vocab_size
           lowerCAmelCase    :		Union[str, Any]				=      type_sequence_label_size
           lowerCAmelCase    :		Optional[Any]				=      initializer_range
           lowerCAmelCase    :		Any				=      coordinate_size
           lowerCAmelCase    :		Optional[int]				=      shape_size
           lowerCAmelCase    :		List[Any]				=      num_labels
           lowerCAmelCase    :		Optional[int]				=      num_choices
           lowerCAmelCase    :		Any				=      scope
           lowerCAmelCase    :		Any				=      range_bbox

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






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		List[str]				=      ids_tensor([self.batch_size, self.text_seq_length]    , self.vocab_size )

           lowerCAmelCase    :		str				=      ids_tensor([self.batch_size, self.text_seq_length, 4]    , self.range_bbox )
           lowerCAmelCase    :		List[str]				=      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]:
                             lowerCAmelCase    :		Tuple				=      bbox[i, j, 3]
                             lowerCAmelCase    :		Optional[Any]				=      bbox[i, j, 1]
                             lowerCAmelCase    :		List[Any]				=      tmp_coordinate
                       if bbox[i, j, 2] < bbox[i, j, 0]:
                             lowerCAmelCase    :		Tuple				=      bbox[i, j, 2]
                             lowerCAmelCase    :		List[Any]				=      bbox[i, j, 0]
                             lowerCAmelCase    :		Optional[int]				=      tmp_coordinate
           lowerCAmelCase    :		List[str]				=      tf.constant(__a )

           lowerCAmelCase    :		Union[str, Any]				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

           lowerCAmelCase    :		List[str]				=      None
           if self.use_input_mask:
                 lowerCAmelCase    :		Any				=      random_attention_mask([self.batch_size, self.text_seq_length] )

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

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

           lowerCAmelCase    :		List[Any]				=      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     :    str    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Any    , UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    List[Any]    , UpperCamelCase_     :    Optional[int]    , UpperCamelCase_     :    Union[str, Any] ):
           lowerCAmelCase    :		Union[str, Any]				=      TFLayoutLMvaModel(config=__a )

           # text + image
           lowerCAmelCase    :		Optional[Any]				=      model(__a    , pixel_values=__a    , training=__a )
           lowerCAmelCase    :		int				=      model(
               __a    , bbox=__a    , pixel_values=__a    , attention_mask=__a    , token_type_ids=__a    , training=__a    , )
           lowerCAmelCase    :		Dict				=      model(__a    , bbox=__a    , pixel_values=__a    , training=__a )

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

           # text only
           lowerCAmelCase    :		List[str]				=      model(__a    , training=__a )
           self.parent.assertEqual(
               result.last_hidden_state.shape    , (self.batch_size, self.text_seq_length, self.hidden_size) )

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






     def    lowerCamelCase__   (		self     :    Tuple    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    Dict ):
           lowerCAmelCase    :		Optional[Any]				=      self.num_labels
           lowerCAmelCase    :		Any				=      TFLayoutLMvaForSequenceClassification(config=__a )
           lowerCAmelCase    :		Tuple				=      model(
               __a    , bbox=__a    , pixel_values=__a    , attention_mask=__a    , token_type_ids=__a    , labels=__a    , training=__a    , )
           self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.num_labels) )






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






     def    lowerCamelCase__   (		self     :    int    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    Optional[int]    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    Tuple ):
           lowerCAmelCase    :		List[str]				=      2
           lowerCAmelCase    :		Tuple				=      TFLayoutLMvaForQuestionAnswering(config=__a )
           lowerCAmelCase    :		Dict				=      model(
               __a    , bbox=__a    , pixel_values=__a    , attention_mask=__a    , token_type_ids=__a    , start_positions=__a    , end_positions=__a    , training=__a    , )
           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     :    Tuple ):
           lowerCAmelCase    :		int				=      self.prepare_config_and_inputs()
           ((lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase),					(lowerCAmelCase))    :		Dict				=      config_and_inputs
           lowerCAmelCase    :		List[Any]				=      {
               '''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 						snake_case_(				a__							,    a__							,    unittest.TestCase	):
     __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     :    Optional[int]    , UpperCamelCase_     :    str    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    List[str] ):
           return True






     def    lowerCamelCase__   (		self     :    Tuple    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    int=False ):
           lowerCAmelCase    :		List[str]				=      copy.deepcopy(__a )

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

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

           return inputs_dict






     def    lowerCamelCase__   (		self     :    Any ):
           lowerCAmelCase    :		Tuple				=      TFLayoutLMvaModelTester(self )
           lowerCAmelCase    :		List[Any]				=      ConfigTester(self    , config_class=__a    , hidden_size=3_7 )






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           self.config_tester.run_common_tests()






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

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

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

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

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

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

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

                       lowerCAmelCase    :		List[str]				=      tuple(__a )

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

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






     def    lowerCamelCase__   (		self     :    List[Any] ):
           (
               (
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					  
           )    :		Optional[Any]				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(__a    , __a    , __a    , __a    , __a    , __a )






     def    lowerCamelCase__   (		self     :    str ):
           (
               (
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					  
           )    :		str				=      self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowerCAmelCase    :		Dict				=      type
                 self.model_tester.create_and_check_model(__a    , __a    , __a    , __a    , __a    , __a )






     def    lowerCamelCase__   (		self     :    Tuple ):
           (
               (
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					  
           )    :		Any				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(
               __a    , __a    , __a    , __a    , __a    , __a    , __a )






     def    lowerCamelCase__   (		self     :    int ):
           (
               (
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					  
           )    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(
               __a    , __a    , __a    , __a    , __a    , __a    , __a )






     def    lowerCamelCase__   (		self     :    List[str] ):
           (
               (
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					(
               lowerCAmelCase  
           ),					  
           )    :		Tuple				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(
               __a    , __a    , __a    , __a    , __a    , __a    , __a )






     @slow
     def    lowerCamelCase__   (		self     :    int ):
           for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase    :		Any				=      TFLayoutLMvaModel.from_pretrained(__a )
                 self.assertIsNotNone(__a )



def    _snake_case  (  ):
      lowerCAmelCase    :		Optional[Any]				=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
      return image






@require_tf
class 						snake_case_(				unittest.TestCase	):






     @cached_property
     def    lowerCamelCase__   (		self     :    Dict ):
           return LayoutLMvaImageProcessor(apply_ocr=__a ) if is_vision_available() else None






     @slow
     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase    :		List[str]				=      TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' )

           lowerCAmelCase    :		Any				=      self.default_image_processor
           lowerCAmelCase    :		List[Any]				=      prepare_img()
           lowerCAmelCase    :		str				=      image_processor(images=__a    , return_tensors='''tf''' ).pixel_values

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

           # forward pass
           lowerCAmelCase    :		Union[str, Any]				=      model(input_ids=__a    , bbox=__a    , pixel_values=__a    , training=__a )

           # verify the logits
           lowerCAmelCase    :		str				=      (1, 1_9_9, 7_6_8)
           self.assertEqual(outputs.last_hidden_state.shape    , __a )

           lowerCAmelCase    :		List[Any]				=      tf.constant(
               [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]] )

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




"""simple docstring"""





def       lowerCamelCase__		(			__snake_case	)							->		bool:


					"""simple docstring"""





					return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')	)



def       lowerCamelCase__		(			__snake_case	)							->		bool:


					"""simple docstring"""





					_UpperCamelCase							 =  credit_card_number
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  len(__snake_case	) - 2
					for i in range(__snake_case,     -1,     -2	):
										#  double the value of every second digit
										_UpperCamelCase							 =  int(cc_number[i]	)
										digit *= 2
										# If doubling of a number results in a two digit number
										# i.e greater than 9(e.g., 6 × 2 = 12),
										# then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6),
										# to get a single digit number.
										if digit > 9:
															digit %= 10
															digit += 1
										_UpperCamelCase							 =  cc_number[:i] + str(__snake_case	) + cc_number[i + 1 :]
										total += digit

					# Sum up the remaining digits
					for i in range(len(__snake_case	) - 1,     -1,     -2	):
										total += int(cc_number[i]	)

					return total % 10 == 0



def       lowerCamelCase__		(			__snake_case	)							->		bool:


					"""simple docstring"""





					_UpperCamelCase							 =  F'''{credit_card_number} is an invalid credit card number because'''
					if not credit_card_number.isdigit():
										print(F'''{error_message} it has nonnumerical characters.'''	)
										return False

					if not 13 <= len(__snake_case	) <= 16:
										print(F'''{error_message} of its length.'''	)
										return False

					if not validate_initial_digits(__snake_case	):
										print(F'''{error_message} of its first two digits.'''	)
										return False

					if not luhn_validation(__snake_case	):
										print(F'''{error_message} it fails the Luhn check.'''	)
										return False

					print(F'''{credit_card_number} is a valid credit card number.'''	)
					return True


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				validate_credit_card_number("""4111111111111111""")
				validate_credit_card_number("""32323""")






