




'''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	:  str							,      lowerCAmelCase_	:  str							,      )      ->							str:


														"""simple docstring"""


														_a     					=    parent
														_a     					=    13
														_a     					=    7
														_a     					=    True
														_a     					=    True
														_a     					=    True
														_a     					=    True
														_a     					=    True
														_a     					=    False
														_a     					=    False
														_a     					=    False
														_a     					=    2
														_a     					=    99
														_a     					=    0
														_a     					=    32
														_a     					=    2
														_a     					=    4
														_a     					=    0.1
														_a     					=    0.1
														_a     					=    5_12
														_a     					=    16
														_a     					=    2
														_a     					=    0.0_2
														_a     					=    3
														_a     					=    4
														_a     					=    '''last'''
														_a     					=    True
														_a     					=    None
														_a     					=    0







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


														"""simple docstring"""


														_a     					=    ids_tensor([self.batch_size, self.seq_length]							,      self.vocab_size )
														_a     					=    random_attention_mask([self.batch_size, self.seq_length]							,      dtype=tf.floataa )

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

														_a     					=    None
														if self.use_token_type_ids:
																					_a     					=    ids_tensor([self.batch_size, self.seq_length]							,      self.n_langs )

														_a     					=    None
														_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.seq_length]							,      self.num_labels )
																					_a     					=    ids_tensor([self.batch_size]							,      2							,      dtype=tf.floataa )
																					_a     					=    ids_tensor([self.batch_size]							,      self.num_choices )

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


														"""simple docstring"""


														_a     					=    TFFlaubertModel(config=lowerCAmelCase_ )
														_a     					=    {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
														_a     					=    model(lowerCAmelCase_ )

														_a     					=    [input_ids, input_mask]
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.last_hidden_state.shape							,      (self.batch_size, self.seq_length, self.hidden_size) )







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


														"""simple docstring"""


														_a     					=    TFFlaubertWithLMHeadModel(lowerCAmelCase_ )

														_a     					=    {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
														_a     					=    model(lowerCAmelCase_ )

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







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


														"""simple docstring"""


														_a     					=    TFFlaubertForQuestionAnsweringSimple(lowerCAmelCase_ )

														_a     					=    {'''input_ids''': input_ids, '''lengths''': input_lengths}

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


														"""simple docstring"""


														_a     					=    TFFlaubertForSequenceClassification(lowerCAmelCase_ )

														_a     					=    {'''input_ids''': input_ids, '''lengths''': input_lengths}

														_a     					=    model(lowerCAmelCase_ )

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







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


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    TFFlaubertForTokenClassification(config=lowerCAmelCase_ )
														_a     					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.num_labels) )







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


														"""simple docstring"""


														_a     					=    self.num_choices
														_a     					=    TFFlaubertForMultipleChoice(config=lowerCAmelCase_ )
														_a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      1 )							,      (1, self.num_choices, 1) )
														_a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      1 )							,      (1, self.num_choices, 1) )
														_a     					=    tf.tile(tf.expand_dims(lowerCAmelCase_							,      1 )							,      (1, self.num_choices, 1) )
														_a     					=    {
														    '''input_ids''': multiple_choice_inputs_ids,
														    '''attention_mask''': multiple_choice_input_mask,
														    '''token_type_ids''': multiple_choice_token_type_ids,
														}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_choices) )







							def        __lowerCAmelCase      (  self	:  Dict )      ->							str:


														"""simple docstring"""


														_a     					=    self.prepare_config_and_inputs()
														(
														    (
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							(
														    _a
														)					,							
														)     					=    config_and_inputs
														_a     					=    {
														    '''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   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (
							        TFFlaubertModel,
							        TFFlaubertWithLMHeadModel,
							        TFFlaubertForSequenceClassification,
							        TFFlaubertForQuestionAnsweringSimple,
							        TFFlaubertForTokenClassification,
							        TFFlaubertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase_          =    (
							    (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Check other models whether language generation is also applicable
							lowercase_          =    (
							    {
							        'feature-extraction': TFFlaubertModel,
							        'fill-mask': TFFlaubertWithLMHeadModel,
							        'question-answering': TFFlaubertForQuestionAnsweringSimple,
							        'text-classification': TFFlaubertForSequenceClassification,
							        'token-classification': TFFlaubertForTokenClassification,
							        'zero-shot': TFFlaubertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowercase_          =    False
							lowercase_          =    False







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


														"""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	:  Union[str, Any] )      ->							str:


														"""simple docstring"""


														_a     					=    TFFlaubertModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      emb_dim=37 )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							int:


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_model(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_lm_head(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_qa(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							int:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCAmelCase_ )







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


														"""simple docstring"""


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



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







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


														"""simple docstring"""


														_a     					=    TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased''' )

														_a     					=    tf.convert_to_tensor(
														    [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]]							,      dtype=tf.intaa							,      )  # "J'aime flaubert !"

														_a     					=    model(lowerCAmelCase_ )[0]
														_a     					=    tf.TensorShape((1, 8, 5_12) )
														self.assertEqual(output.shape							,      lowerCAmelCase_ )
														# compare the actual values for a slice.
														_a     					=    tf.convert_to_tensor(
														    [
														        [
														            [-1.8_7_6_8_7_7_3, -1.5_6_6_5_5_5, 0.2_7_0_7_2_4_1_8],
														            [-1.6_9_2_0_0_3_8, -0.5_8_7_3_5_0_5, 1.9_3_2_9_5_9_9],
														            [-2.9_5_6_3_9_8_5, -1.6_9_9_3_8_3_5, 1.7_9_7_2_0_5_2],
														        ]
														    ]							,      dtype=tf.floataa							,      )

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







'''simple docstring'''


import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM

@require_torch
class       A   :







							def __init__(  self	:  Union[str, Any]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Union[str, Any]=99							,      lowerCAmelCase_	:  Optional[int]=13							,      lowerCAmelCase_	:  Tuple=16							,      lowerCAmelCase_	:  Optional[int]=7							,      lowerCAmelCase_	:  Optional[Any]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Any=True							,      lowerCAmelCase_	:  Union[str, Any]=False							,      lowerCAmelCase_	:  int=True							,      lowerCAmelCase_	:  Tuple=2							,      lowerCAmelCase_	:  Any=32							,      lowerCAmelCase_	:  str=4							,      lowerCAmelCase_	:  Optional[Any]=4							,      lowerCAmelCase_	:  Optional[Any]=30							,      lowerCAmelCase_	:  int=0							,      lowerCAmelCase_	:  Any=1							,      lowerCAmelCase_	:  str=2							,      lowerCAmelCase_	:  Union[str, Any]=None							,      )      ->							Any:


														"""simple docstring"""


														_a     					=    parent
														_a     					=    batch_size
														_a     					=    decoder_seq_length
														# For common tests
														_a     					=    self.decoder_seq_length
														_a     					=    is_training
														_a     					=    use_attention_mask
														_a     					=    use_labels

														_a     					=    vocab_size
														_a     					=    d_model
														_a     					=    d_model
														_a     					=    decoder_layers
														_a     					=    decoder_layers
														_a     					=    decoder_ffn_dim
														_a     					=    decoder_attention_heads
														_a     					=    decoder_attention_heads
														_a     					=    eos_token_id
														_a     					=    bos_token_id
														_a     					=    pad_token_id
														_a     					=    decoder_start_token_id
														_a     					=    use_cache
														_a     					=    max_position_embeddings

														_a     					=    None
														_a     					=    decoder_seq_length
														_a     					=    2
														_a     					=    1







							def        __lowerCAmelCase      (  self	:  str )      ->							str:


														"""simple docstring"""


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

														_a     					=    None
														if self.use_attention_mask:
																					_a     					=    ids_tensor([self.batch_size, self.decoder_seq_length]							,      vocab_size=2 )

														_a     					=    None
														if self.use_labels:
																					_a     					=    ids_tensor([self.batch_size, self.decoder_seq_length]							,      self.vocab_size )

														_a     					=    TrOCRConfig(
														    vocab_size=self.vocab_size							,      d_model=self.d_model							,      decoder_layers=self.decoder_layers							,      decoder_ffn_dim=self.decoder_ffn_dim							,      decoder_attention_heads=self.decoder_attention_heads							,      eos_token_id=self.eos_token_id							,      bos_token_id=self.bos_token_id							,      use_cache=self.use_cache							,      pad_token_id=self.pad_token_id							,      decoder_start_token_id=self.decoder_start_token_id							,      max_position_embeddings=self.max_position_embeddings							,      )

														return (config, input_ids, attention_mask, lm_labels)







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


														"""simple docstring"""


														_a     					=    True
														_a     					=    TrOCRDecoder(config=lowerCAmelCase_ ).to(lowerCAmelCase_ ).eval()
														_a     					=    input_ids[:2]

														input_ids[input_ids == 0] += 1
														# first forward pass
														_a     					=    model(lowerCAmelCase_							,      use_cache=lowerCAmelCase_ )
														_a     					=    model(lowerCAmelCase_ )
														_a     					=    model(lowerCAmelCase_							,      use_cache=lowerCAmelCase_ )

														self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) )
														self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) + 1 )

														_a     					=    outputs['''past_key_values''']

														# create hypothetical next token and extent to next_input_ids
														_a     					=    ids_tensor((2, 1)							,      config.vocab_size - 1 ) + 1

														# append to next input_ids and
														_a     					=    torch.cat([input_ids, next_tokens]							,      dim=-1 )

														_a     					=    model(lowerCAmelCase_ )['''last_hidden_state''']
														_a     					=    model(lowerCAmelCase_							,      past_key_values=lowerCAmelCase_ )['''last_hidden_state''']

														# select random slice
														_a     					=    ids_tensor((1,)							,      output_from_past.shape[-1] ).item()
														_a     					=    output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
														_a     					=    output_from_past[:, 0, random_slice_idx].detach()

														# test that outputs are equal for slice
														assert torch.allclose(lowerCAmelCase_							,      lowerCAmelCase_							,      atol=1e-3 )







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


														"""simple docstring"""


														_a     					=    self.prepare_config_and_inputs()
														_a					,							_a					,							_a					,							_a     					=    config_and_inputs

														_a     					=    {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
														return config, inputs_dict



@require_torch
class       A   (		_a					,_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
							lowercase_          =    (TrOCRForCausalLM,) if is_torch_available() else ()
							lowercase_          =    {'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
							lowercase_          =    True
							lowercase_          =    False







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


														"""simple docstring"""


														_a     					=    TrOCRStandaloneDecoderModelTester(self							,      is_training=lowerCAmelCase_ )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_ )







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


														"""simple docstring"""


														pass







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Dict:


														"""simple docstring"""


														pass







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


														"""simple docstring"""


														pass







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past(*lowerCAmelCase_ )







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


														"""simple docstring"""


														return







							@unittest.skip('''The model doesn\'t support left padding''' )  # and it's not used enough to be worth fixing :)
							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


														pass

