






"""simple docstring"""



import argparse

import torch
from datasets import load_dataset
from donut import DonutModel

from transformers import (
    DonutImageProcessor,
    DonutProcessor,
    DonutSwinConfig,
    DonutSwinModel,
    MBartConfig,
    MBartForCausalLM,
    VisionEncoderDecoderModel,
    XLMRobertaTokenizerFast,
)


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : Tuple				)->		str:






     '''simple docstring'''



     UpperCAmelCase__						:    str							=							model.config

     UpperCAmelCase__						:    Tuple							=							DonutSwinConfig(
         image_size=original_config.input_size			,					patch_size=4			,					depths=original_config.encoder_layer			,					num_heads=[4, 8, 16, 32]			,					window_size=original_config.window_size			,					embed_dim=128			,					)
     UpperCAmelCase__						:    Any							=							MBartConfig(
         is_decoder=snake_case__			,					is_encoder_decoder=snake_case__			,					add_cross_attention=snake_case__			,					decoder_layers=original_config.decoder_layer			,					max_position_embeddings=original_config.max_position_embeddings			,					vocab_size=len(
             model.decoder.tokenizer				)			,					scale_embedding=snake_case__			,					add_final_layer_norm=snake_case__			,					)

     return encoder_config, decoder_config


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : str				)->		int:






     '''simple docstring'''



     if "encoder.model" in name:
          UpperCAmelCase__						:    Dict							=							name.replace("encoder.model"			,					"encoder"				)
     if "decoder.model" in name:
          UpperCAmelCase__						:    Any							=							name.replace("decoder.model"			,					"decoder"				)
     if "patch_embed.proj" in name:
          UpperCAmelCase__						:    int							=							name.replace("patch_embed.proj"			,					"embeddings.patch_embeddings.projection"				)
     if "patch_embed.norm" in name:
          UpperCAmelCase__						:    List[Any]							=							name.replace("patch_embed.norm"			,					"embeddings.norm"				)
     if name.startswith("encoder"				):
          if "layers" in name:
               UpperCAmelCase__						:    Union[str, Any]							=							'''encoder.''' + name
          if "attn.proj" in name:
               UpperCAmelCase__						:    Tuple							=							name.replace("attn.proj"			,					"attention.output.dense"				)
          if "attn" in name and "mask" not in name:
               UpperCAmelCase__						:    int							=							name.replace("attn"			,					"attention.self"				)
          if "norm1" in name:
               UpperCAmelCase__						:    Optional[Any]							=							name.replace("norm1"			,					"layernorm_before"				)
          if "norm2" in name:
               UpperCAmelCase__						:    List[Any]							=							name.replace("norm2"			,					"layernorm_after"				)
          if "mlp.fc1" in name:
               UpperCAmelCase__						:    List[str]							=							name.replace("mlp.fc1"			,					"intermediate.dense"				)
          if "mlp.fc2" in name:
               UpperCAmelCase__						:    Optional[int]							=							name.replace("mlp.fc2"			,					"output.dense"				)

          if name == "encoder.norm.weight":
               UpperCAmelCase__						:    List[Any]							=							'''encoder.layernorm.weight'''
          if name == "encoder.norm.bias":
               UpperCAmelCase__						:    Dict							=							'''encoder.layernorm.bias'''

     return name


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : List[str]			,					snake_case       : List[Any]				)->		Union[str, Any]:






     '''simple docstring'''



     for key in orig_state_dict.copy().keys():
          UpperCAmelCase__						:    Dict							=							orig_state_dict.pop(snake_case__				)

          if "qkv" in key:
               UpperCAmelCase__						:    Any							=							key.split("."				)
               UpperCAmelCase__						:    Dict							=							int(key_split[3]				)
               UpperCAmelCase__						:    int							=							int(key_split[5]				)
               UpperCAmelCase__						:    Union[str, Any]							=							model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

               if "weight" in key:
                    UpperCAmelCase__						:    List[str]							=							val[:dim, :]
                    UpperCAmelCase__						:    Optional[int]							=							val[dim : dim * 2, :]
                    UpperCAmelCase__						:    str							=							val[-dim:, :]
               else:
                    UpperCAmelCase__						:    List[str]							=							val[:dim]
                    UpperCAmelCase__						:    Tuple							=							val[dim : dim * 2]
                    UpperCAmelCase__						:    List[str]							=							val[-dim:]
          elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]:
               # HuggingFace implementation doesn't use attn_mask buffer
               # and model doesn't use final LayerNorms for the encoder
               pass
          else:
               UpperCAmelCase__						:    Any							=							val

     return orig_state_dict


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : Any			,					snake_case       : Tuple=None			,					snake_case       : Union[str, Any]=False				)->		str:






     '''simple docstring'''



     UpperCAmelCase__						:    Optional[int]							=							DonutModel.from_pretrained(snake_case__				).eval()

     # load HuggingFace model
     UpperCAmelCase__						:    Union[str, Any]							=							get_configs(snake_case__				)
     UpperCAmelCase__						:    int							=							DonutSwinModel(snake_case__				)
     UpperCAmelCase__						:    Optional[Any]							=							MBartForCausalLM(snake_case__				)
     UpperCAmelCase__						:    Any							=							VisionEncoderDecoderModel(encoder=snake_case__			,					decoder=snake_case__				)
     model.eval()

     UpperCAmelCase__						:    Any							=							original_model.state_dict()
     UpperCAmelCase__						:    Union[str, Any]							=							convert_state_dict(snake_case__			,					snake_case__				)
     model.load_state_dict(snake_case__				)

     # verify results on scanned document
     UpperCAmelCase__						:    List[str]							=							load_dataset("hf-internal-testing/example-documents"				)
     UpperCAmelCase__						:    Optional[Any]							=							dataset['''test'''][0]['''image'''].convert("RGB"				)

     UpperCAmelCase__						:    Union[str, Any]							=							XLMRobertaTokenizerFast.from_pretrained(snake_case__			,					from_slow=snake_case__				)
     UpperCAmelCase__						:    Optional[Any]							=							DonutImageProcessor(
         do_align_long_axis=original_model.config.align_long_axis			,					size=original_model.config.input_size[::-1]				)
     UpperCAmelCase__						:    int							=							DonutProcessor(snake_case__			,					snake_case__				)
     UpperCAmelCase__						:    Dict							=							processor(snake_case__			,					return_tensors="pt"				).pixel_values

     if model_name == "naver-clova-ix/donut-base-finetuned-docvqa":
          UpperCAmelCase__						:    int							=							'''<s_docvqa><s_question>{user_input}</s_question><s_answer>'''
          UpperCAmelCase__						:    Optional[Any]							=							'''When is the coffee break?'''
          UpperCAmelCase__						:    Optional[Any]							=							task_prompt.replace("{user_input}"			,					snake_case__				)
     elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip":
          UpperCAmelCase__						:    Any							=							'''<s_rvlcdip>'''
     elif model_name in [
         "naver-clova-ix/donut-base-finetuned-cord-v1",
         "naver-clova-ix/donut-base-finetuned-cord-v1-2560",
     ]:
          UpperCAmelCase__						:    str							=							'''<s_cord>'''
     elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2":
          UpperCAmelCase__						:    Optional[Any]							=							'''s_cord-v2>'''
     elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket":
          UpperCAmelCase__						:    List[str]							=							'''<s_zhtrainticket>'''
     elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]:
          # use a random prompt
          UpperCAmelCase__						:    List[Any]							=							'''hello world'''
     else:
          raise ValueError("Model name not supported"				)
     UpperCAmelCase__						:    Union[str, Any]							=							original_model.decoder.tokenizer(snake_case__			,					add_special_tokens=snake_case__			,					return_tensors="pt"				)[
         '''input_ids'''
     ]

     UpperCAmelCase__						:    List[str]							=							original_model.encoder.model.patch_embed(snake_case__				)
     UpperCAmelCase__						:    Dict							=							model.encoder.embeddings(snake_case__				)
     assert torch.allclose(snake_case__			,					snake_case__			,					atol=1E-3				)

     # verify encoder hidden states
     UpperCAmelCase__						:    List[str]							=							original_model.encoder(snake_case__				)
     UpperCAmelCase__						:    Optional[Any]							=							model.encoder(snake_case__				).last_hidden_state
     assert torch.allclose(snake_case__			,					snake_case__			,					atol=1E-2				)

     # verify decoder hidden states
     UpperCAmelCase__						:    Optional[int]							=							original_model(snake_case__			,					snake_case__			,					snake_case__				).logits
     UpperCAmelCase__						:    List[Any]							=							model(snake_case__			,					decoder_input_ids=snake_case__				).logits
     assert torch.allclose(snake_case__			,					snake_case__			,					atol=1E-3				)
     print("Looks ok!"				)

     if pytorch_dump_folder_path is not None:
          print(f'Saving model and processor to {pytorch_dump_folder_path}'				)
          model.save_pretrained(snake_case__				)
          processor.save_pretrained(snake_case__				)

     if push_to_hub:
          model.push_to_hub("nielsr/" + model_name.split("/"				)[-1]			,					commit_message="Update model"				)
          processor.push_to_hub("nielsr/" + model_name.split("/"				)[-1]			,					commit_message="Update model"				)


if __name__ == "__main__":
 _lowerCAmelCase	:	List[str]				=      argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     """--model_name""",
     default="""naver-clova-ix/donut-base-finetuned-docvqa""",
     required=False,
     type=str,
     help="""Name of the original model you\'d like to convert.""",
 )
 parser.add_argument(
     """--pytorch_dump_folder_path""",
     default=None,
     required=False,
     type=str,
     help="""Path to the output PyTorch model directory.""",
 )
 parser.add_argument(
     """--push_to_hub""",
     action="""store_true""",
     help="""Whether or not to push the converted model and processor to the 🤗 hub.""",
 )

 _lowerCAmelCase	:	Optional[Any]				=      parser.parse_args()
 convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)




'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

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


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import (
          TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          FlaubertConfig,
          TFFlaubertForMultipleChoice,
          TFFlaubertForQuestionAnsweringSimple,
          TFFlaubertForSequenceClassification,
          TFFlaubertForTokenClassification,
          TFFlaubertModel,
          TFFlaubertWithLMHeadModel,
      )





class 					A							:





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     )					->							Optional[Any]:






         """simple docstring"""






         A			:  List[str]   =		parent
         A			:  int   =		13
         A			:  Dict   =		7
         A			:  str   =		True
         A			:  Dict   =		True
         A			:  Tuple   =		True
         A			:  Union[str, Any]   =		True
         A			:  Dict   =		True
         A			:  str   =		False
         A			:  Union[str, Any]   =		False
         A			:  Union[str, Any]   =		False
         A			:  List[str]   =		2
         A			:  Optional[int]   =		99
         A			:  List[str]   =		0
         A			:  int   =		32
         A			:  Any   =		2
         A			:  Optional[Any]   =		4
         A			:  List[Any]   =		0.1
         A			:  List[str]   =		0.1
         A			:  Tuple   =		512
         A			:  Optional[Any]   =		16
         A			:  List[str]   =		2
         A			:  Tuple   =		0.02
         A			:  List[str]   =		3
         A			:  List[Any]   =		4
         A			:  Any   =		'''last'''
         A			:  int   =		True
         A			:  Union[str, Any]   =		None
         A			:  Dict   =		0





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






         """simple docstring"""






         A			:  int   =		ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )
         A			:  Any   =		random_attention_mask([self.batch_size, self.seq_length]		,     dtype=tf.floataa      )

         A			:  int   =		None
         if self.use_input_lengths:
             A			:  int   =		(
                 ids_tensor([self.batch_size]		,     vocab_size=2      ) + self.seq_length - 2
             )  # small variation of seq_length

         A			:  Tuple   =		None
         if self.use_token_type_ids:
             A			:  Union[str, Any]   =		ids_tensor([self.batch_size, self.seq_length]		,     self.n_langs      )

         A			:  List[Any]   =		None
         A			:  List[Any]   =		None
         A			:  Tuple   =		None
         if self.use_labels:
             A			:  Dict   =		ids_tensor([self.batch_size]		,     self.type_sequence_label_size      )
             A			:  Dict   =		ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels      )
             A			:  List[Any]   =		ids_tensor([self.batch_size]		,     2		,     dtype=tf.floataa      )
             A			:  Optional[Any]   =		ids_tensor([self.batch_size]		,     self.num_choices      )

         A			:  Dict   =		FlaubertConfig(
             vocab_size=self.vocab_size		,     n_special=self.n_special		,     emb_dim=self.hidden_size		,     n_layers=self.num_hidden_layers		,     n_heads=self.num_attention_heads		,     dropout=self.hidden_dropout_prob		,     attention_dropout=self.attention_probs_dropout_prob		,     gelu_activation=self.gelu_activation		,     sinusoidal_embeddings=self.sinusoidal_embeddings		,     asm=self.asm		,     causal=self.causal		,     n_langs=self.n_langs		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     summary_type=self.summary_type		,     use_proj=self.use_proj		,     bos_token_id=self.bos_token_id		,     )

         return (
             config,
             input_ids,
             token_type_ids,
             input_lengths,
             sequence_labels,
             token_labels,
             is_impossible_labels,
             choice_labels,
             input_mask,
         )





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






         """simple docstring"""






         A			:  int   =		TFFlaubertModel(config=SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		[input_ids, input_mask]
         A			:  str   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )





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






         """simple docstring"""






         A			:  List[str]   =		TFFlaubertWithLMHeadModel(SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE      )

         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )





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






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForQuestionAnsweringSimple(SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths}

         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE      )

         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		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForSequenceClassification(SCREAMING_SNAKE_CASE      )

         A			:  Dict   =		{'''input_ids''': input_ids, '''lengths''': input_lengths}

         A			:  Optional[Any]   =		model(SCREAMING_SNAKE_CASE      )

         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							str:






         """simple docstring"""






         A			:  Optional[Any]   =		self.num_labels
         A			:  str   =		TFFlaubertForTokenClassification(config=SCREAMING_SNAKE_CASE      )
         A			:  List[str]   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  str   =		self.num_choices
         A			:  int   =		TFFlaubertForMultipleChoice(config=SCREAMING_SNAKE_CASE      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  Optional[Any]   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  List[str]   =		{
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
             '''token_type_ids''': multiple_choice_token_type_ids,
         }
         A			:  List[Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.prepare_config_and_inputs()
         (
             (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    
         )			:  Any   =		config_and_inputs
         A			:  List[str]   =		{
             '''input_ids''': input_ids,
             '''token_type_ids''': token_type_ids,
             '''langs''': token_type_ids,
             '''lengths''': input_lengths,
         }
         return config, inputs_dict





@require_tf
class 					A							(				__snake_case       ,     __snake_case       ,     unittest.TestCase		):
     __magic_name__          =						(
         (
             TFFlaubertModel,
             TFFlaubertWithLMHeadModel,
             TFFlaubertForSequenceClassification,
             TFFlaubertForQuestionAnsweringSimple,
             TFFlaubertForTokenClassification,
             TFFlaubertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     __magic_name__          =						(
         (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     __magic_name__          =						(
         {
             '''feature-extraction''': TFFlaubertModel,
             '''fill-mask''': TFFlaubertWithLMHeadModel,
             '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
             '''text-classification''': TFFlaubertForSequenceClassification,
             '''token-classification''': TFFlaubertForTokenClassification,
             '''zero-shot''': TFFlaubertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __magic_name__          =						False
     __magic_name__          =						False





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         if (
             pipeline_test_casse_name == "QAPipelineTests"
             and tokenizer_name is not None
             and not tokenizer_name.endswith('''Fast'''      )
         ):
             # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
             # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
             # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
             return True

         return False





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  int   =		TFFlaubertModelTester(self      )
         A			:  Dict   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     emb_dim=37      )





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






         """simple docstring"""






         self.config_tester.run_common_tests()





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






         """simple docstring"""






         A			:  List[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  int   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_lm_head(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_qa(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         A			:  Optional[int]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_sequence_classif(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_token_classification(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_multiple_choice(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A			:  Union[str, Any]   =		TFFlaubertModel.from_pretrained(SCREAMING_SNAKE_CASE      )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE      )





@require_tf
@require_sentencepiece
@require_tokenizers
class 					A							(				unittest.TestCase		):





     @slow
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Tuple   =		TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased'''      )

         A			:  List[str]   =		tf.convert_to_tensor(
             [[0, 158, 735, 2592, 1424, 6727, 82, 1]]		,     dtype=tf.intaa		,     )  # "J'aime flaubert !"

         A			:  int   =		model(SCREAMING_SNAKE_CASE      )[0]
         A			:  str   =		tf.TensorShape((1, 8, 512)      )
         self.assertEqual(output.shape		,     SCREAMING_SNAKE_CASE      )
         # compare the actual values for a slice.
         A			:  Union[str, Any]   =		tf.convert_to_tensor(
             [
                 [
                     [-1.8_768_773, -1.566_555, 0.27_072_418],
                     [-1.6_920_038, -0.5_873_505, 1.9_329_599],
                     [-2.9_563_985, -1.6_993_835, 1.7_972_052],
                 ]
             ]		,     dtype=tf.floataa		,     )

         self.assertTrue(np.allclose(output[:, :3, :3].numpy()		,     expected_slice.numpy()		,     atol=1e-4      )      )




