

'''simple docstring'''
from typing import List

import numpy as np


def        _lowerCAmelCase					(						_UpperCamelCase						:     dict				)      ->			int:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    ={key: len(_UpperCamelCase				) for key, value in gen_kwargs.items() if isinstance(_UpperCamelCase							, _UpperCamelCase				)}
     if len(set(lists_lengths.values()				)				) > 1:
          raise RuntimeError(
              (
                  'Sharding is ambiguous for this dataset: '
                  + 'we found several data sources lists of different lengths, and we don\'t know over which list we should parallelize:\n'
                  + '\n'.join(f"\t- key {key} has length {length}" for key, length in lists_lengths.items()				)
                  + '\nTo fix this, check the \'gen_kwargs\' and make sure to use lists only for data sources, '
                  + 'and use tuples otherwise. In the end there should only be one single list, or several lists with the same length.'
              )				)
     _SCREAMING_SNAKE_CASE							    =max(lists_lengths.values()							, default=0				)
     return max(1							, _UpperCamelCase				)


def        _lowerCAmelCase					(						_UpperCamelCase						:     int							, _UpperCamelCase						:     int				)      ->			List[range]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =[]
     for group_idx in range(_UpperCamelCase				):
          _SCREAMING_SNAKE_CASE							    =num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs))
          if num_shards_to_add == 0:
               break
          _SCREAMING_SNAKE_CASE							    =shards_indices_per_group[-1].stop if shards_indices_per_group else 0
          _SCREAMING_SNAKE_CASE							    =range(_UpperCamelCase							, start + num_shards_to_add				)
          shards_indices_per_group.append(_UpperCamelCase				)
     return shards_indices_per_group


def        _lowerCAmelCase					(						_UpperCamelCase						:     dict							, _UpperCamelCase						:     int				)      ->			List[dict]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =_number_of_shards_in_gen_kwargs(_UpperCamelCase				)
     if num_shards == 1:
          return [dict(_UpperCamelCase				)]
     else:
          _SCREAMING_SNAKE_CASE							    =_distribute_shards(num_shards=_UpperCamelCase							, max_num_jobs=_UpperCamelCase				)
          return [
              {
                  key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]]
                  if isinstance(_UpperCamelCase							, _UpperCamelCase				)
                  else value
                  for key, value in gen_kwargs.items()
              }
              for group_idx in range(len(_UpperCamelCase				)				)
          ]


def        _lowerCAmelCase					(						_UpperCamelCase						:     List[dict]				)      ->			dict:
     """simple docstring"""




     return {
         key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]]
         if isinstance(gen_kwargs_list[0][key]							, _UpperCamelCase				)
         else gen_kwargs_list[0][key]
         for key in gen_kwargs_list[0]
     }







def        _lowerCAmelCase					(						_UpperCamelCase						:     np.random.Generator							, _UpperCamelCase						:     dict				)      ->			dict:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    ={len(_UpperCamelCase				) for value in gen_kwargs.values() if isinstance(_UpperCamelCase							, _UpperCamelCase				)}
     _SCREAMING_SNAKE_CASE							    ={}
     for size in list_sizes:
          _SCREAMING_SNAKE_CASE							    =list(range(_UpperCamelCase				)				)
          rng.shuffle(indices_per_size[size]				)
     # Now let's copy the gen_kwargs and shuffle the lists based on their sizes
     _SCREAMING_SNAKE_CASE							    =dict(_UpperCamelCase				)
     for key, value in shuffled_kwargs.items():
          if isinstance(_UpperCamelCase							, _UpperCamelCase				):
               _SCREAMING_SNAKE_CASE							    =[value[i] for i in indices_per_size[len(_UpperCamelCase				)]]
     return shuffled_kwargs






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:



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_		     =    parent
       lowerCamelCase_		     =    13
       lowerCamelCase_		     =    7
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    99
       lowerCamelCase_		     =    0
       lowerCamelCase_		     =    32
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    4
       lowerCamelCase_		     =    0.1
       lowerCamelCase_		     =    0.1
       lowerCamelCase_		     =    512
       lowerCamelCase_		     =    16
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    0.02
       lowerCamelCase_		     =    3
       lowerCamelCase_		     =    4
       lowerCamelCase_		     =    'last'
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    0

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

       """simple docstring"""






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

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

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

       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    None
       if self.use_labels:
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							self.type_sequence_label_size  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size, self.seq_length]						,							self.num_labels  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							2						,							dtype=tf.floataa  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							self.num_choices  )

       lowerCamelCase_		     =    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 				a__ (				self					:  int						,							A_					:  List[str]						,							A_					:  List[Any]						,							A_					:  str						,							A_					:  List[Any]						,							A_					:  int						,							A_					:  Tuple						,							A_					:  Optional[int]						,							A_					:  Optional[int]						,							A_					:  str						,							)   ->	Union[str, Any]:

       """simple docstring"""






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

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

 def 				a__ (				self					:  Tuple						,							A_					:  List[str]						,							A_					:  int						,							A_					:  List[Any]						,							A_					:  Any						,							A_					:  Any						,							A_					:  Dict						,							A_					:  str						,							A_					:  List[Any]						,							A_					:  Union[str, Any]						,							)   ->	List[str]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertWithLMHeadModel(A_  )

       lowerCamelCase_		     =    {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
       lowerCamelCase_		     =    model(A_  )

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

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

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertForQuestionAnsweringSimple(A_  )

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

       lowerCamelCase_		     =    model(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 				a__ (				self					:  Optional[int]						,							A_					:  List[Any]						,							A_					:  str						,							A_					:  List[str]						,							A_					:  Dict						,							A_					:  Optional[Any]						,							A_					:  Tuple						,							A_					:  str						,							A_					:  Optional[int]						,							A_					:  Tuple						,							)   ->	List[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertForSequenceClassification(A_  )

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

       lowerCamelCase_		     =    model(A_  )

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

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

       """simple docstring"""






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

 def 				a__ (				self					:  List[Any]						,							A_					:  Optional[int]						,							A_					:  List[Any]						,							A_					:  Optional[int]						,							A_					:  Tuple						,							A_					:  Union[str, Any]						,							A_					:  int						,							A_					:  str						,							A_					:  Tuple						,							A_					:  str						,							)   ->	Optional[int]:

       """simple docstring"""






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





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

       """simple docstring"""






       lowerCamelCase_		     =    self.prepare_config_and_inputs()
       (
           (
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				
       )		     =    config_and_inputs
       lowerCamelCase_		     =    {
           '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(						UpperCamelCase							,      UpperCamelCase							,      unittest.TestCase   ):



 '''simple docstring'''


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

 def 				a__ (				self					:  Union[str, Any]						,							A_					:  Any						,							A_					:  List[Any]						,							A_					:  Union[str, Any]						,							A_					:  str						,							A_					:  List[str]  )   ->	Optional[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 				a__ (				self					:  List[str]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertModelTester(self  )
       lowerCamelCase_		     =    ConfigTester(self						,							config_class=A_						,							emb_dim=37  )

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

       """simple docstring"""






       self.config_tester.run_common_tests()

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*A_  )

 def 				a__ (				self					:  Dict  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*A_  )

 def 				a__ (				self					:  List[Any]  )   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_for_token_classification(*A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_for_multiple_choice(*A_  )





 @slow
 def 				a__ (				self					:  Union[str, Any]  )   ->	Optional[int]:

       """simple docstring"""






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







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



 '''simple docstring'''


 @slow
 def 				a__ (				self					:  Dict  )   ->	str:

       """simple docstring"""






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

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

       lowerCamelCase_		     =    model(A_  )[0]
       lowerCamelCase_		     =    tf.TensorShape((1, 8, 512)  )
       self.assertEqual(output.shape						,							A_  )
       # compare the actual values for a slice.
       lowerCamelCase_		     =    tf.convert_to_tensor(
           [
               [
                   [-1.8768773, -1.566555, 0.27072418],
                   [-1.6920038, -0.5873505, 1.9329599],
                   [-2.9563985, -1.6993835, 1.7972052],
               ]
           ]						,							dtype=tf.floataa						,							)

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



