

"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 tensorflow as tf

      from transformers import (
          TFFunnelBaseModel,
          TFFunnelForMaskedLM,
          TFFunnelForMultipleChoice,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForSequenceClassification,
          TFFunnelForTokenClassification,
          TFFunnelModel,
      )







class   __lowercase      :

  def __init__(	self		:      int       ,A		:      Tuple       ,A		:      Dict=13       ,A		:      Optional[Any]=7       ,A		:      Optional[int]=True       ,A		:      Union[str, Any]=True       ,A		:      Optional[int]=True       ,A		:      Tuple=True       ,A		:      str=99       ,A		:      int=[1, 1, 2]       ,A		:      Tuple=1       ,A		:      List[Any]=32       ,A		:      List[str]=4       ,A		:      Any=8       ,A		:      Optional[int]=37       ,A		:      Any="gelu_new"       ,A		:      Dict=0.1       ,A		:      Dict=0.1       ,A		:      Any=0.0       ,A		:      Optional[int]=512       ,A		:      List[str]=3       ,A		:      Optional[int]=0.0_2       ,A		:      Optional[Any]=3       ,A		:      Tuple=4       ,A		:      Union[str, Any]=None       ,A		:      List[Any]=False       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       parent
   UpperCAmelCase__       :	int        =       batch_size
   UpperCAmelCase__       :	Any        =       seq_length
   UpperCAmelCase__       :	Any        =       is_training
   UpperCAmelCase__       :	Dict        =       use_input_mask
   UpperCAmelCase__       :	Any        =       use_token_type_ids
   UpperCAmelCase__       :	Union[str, Any]        =       use_labels
   UpperCAmelCase__       :	Any        =       vocab_size
   UpperCAmelCase__       :	int        =       block_sizes
   UpperCAmelCase__       :	Tuple        =       num_decoder_layers
   UpperCAmelCase__       :	List[str]        =       d_model
   UpperCAmelCase__       :	Tuple        =       n_head
   UpperCAmelCase__       :	Dict        =       d_head
   UpperCAmelCase__       :	str        =       d_inner
   UpperCAmelCase__       :	int        =       hidden_act
   UpperCAmelCase__       :	Dict        =       hidden_dropout
   UpperCAmelCase__       :	Any        =       attention_dropout
   UpperCAmelCase__       :	Optional[int]        =       activation_dropout
   UpperCAmelCase__       :	List[Any]        =       max_position_embeddings
   UpperCAmelCase__       :	Union[str, Any]        =       type_vocab_size
   UpperCAmelCase__       :	Tuple        =       2
   UpperCAmelCase__       :	Any        =       num_labels
   UpperCAmelCase__       :	Tuple        =       num_choices
   UpperCAmelCase__       :	List[Any]        =       scope
   UpperCAmelCase__       :	Union[str, Any]        =       initializer_std

   # Used in the tests to check the size of the first attention layer
   UpperCAmelCase__       :	Any        =       n_head
   # Used in the tests to check the size of the first hidden state
   UpperCAmelCase__       :	Optional[Any]        =       self.d_model
   # Used in the tests to check the number of output hidden states/attentions
   UpperCAmelCase__       :	int        =       sum(self.block_sizes     ) + (0 if base else self.num_decoder_layers)
   # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
   # the last hidden state of the first block (which is the first hidden state of the decoder).
   if not base:
    UpperCAmelCase__       :	Tuple        =       self.num_hidden_layers + 2

  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

   UpperCAmelCase__       :	str        =       None
   if self.use_input_mask:
    UpperCAmelCase__       :	Dict        =       random_attention_mask([self.batch_size, self.seq_length]     )

   UpperCAmelCase__       :	Dict        =       None
   if self.use_token_type_ids:
    UpperCAmelCase__       :	Dict        =       ids_tensor([self.batch_size, self.seq_length]       ,self.type_vocab_size     )

   UpperCAmelCase__       :	str        =       None
   UpperCAmelCase__       :	List[str]        =       None
   UpperCAmelCase__       :	int        =       None
   if self.use_labels:
    UpperCAmelCase__       :	List[Any]        =       ids_tensor([self.batch_size]       ,self.type_sequence_label_size     )
    UpperCAmelCase__       :	Tuple        =       ids_tensor([self.batch_size, self.seq_length]       ,self.num_labels     )
    UpperCAmelCase__       :	List[str]        =       ids_tensor([self.batch_size]       ,self.num_choices     )

   UpperCAmelCase__       :	Tuple        =       FunnelConfig(
       vocab_size=self.vocab_size       ,block_sizes=self.block_sizes       ,num_decoder_layers=self.num_decoder_layers       ,d_model=self.d_model       ,n_head=self.n_head       ,d_head=self.d_head       ,d_inner=self.d_inner       ,hidden_act=self.hidden_act       ,hidden_dropout=self.hidden_dropout       ,attention_dropout=self.attention_dropout       ,activation_dropout=self.activation_dropout       ,max_position_embeddings=self.max_position_embeddings       ,type_vocab_size=self.type_vocab_size       ,initializer_std=self.initializer_std       ,)

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

  def  __lowercase							(	self		:      str       ,A		:      int       ,A		:      str       ,A		:      str       ,A		:      int       ,A		:      Dict       ,A		:      Union[str, Any]       ,A		:      Dict       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       TFFunnelModel(config=a__     )
   UpperCAmelCase__       :	Optional[Any]        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	Optional[int]        =       model(a__     )

   UpperCAmelCase__       :	Optional[int]        =       [input_ids, input_mask]
   UpperCAmelCase__       :	str        =       model(a__     )

   UpperCAmelCase__       :	str        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.d_model)     )

   UpperCAmelCase__       :	Dict        =       False
   UpperCAmelCase__       :	Any        =       TFFunnelModel(config=a__     )
   UpperCAmelCase__       :	Union[str, Any]        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.d_model)     )

   UpperCAmelCase__       :	Dict        =       False
   UpperCAmelCase__       :	Tuple        =       TFFunnelModel(config=a__     )
   UpperCAmelCase__       :	int        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.d_model)     )

  def  __lowercase							(	self		:      Union[str, Any]       ,A		:      List[str]       ,A		:      str       ,A		:      List[str]       ,A		:      List[Any]       ,A		:      Tuple       ,A		:      Dict       ,A		:      str       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       TFFunnelBaseModel(config=a__     )
   UpperCAmelCase__       :	int        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	int        =       model(a__     )

   UpperCAmelCase__       :	Tuple        =       [input_ids, input_mask]
   UpperCAmelCase__       :	Optional[int]        =       model(a__     )

   UpperCAmelCase__       :	Optional[int]        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, 2, self.d_model)     )

   UpperCAmelCase__       :	Any        =       False
   UpperCAmelCase__       :	Optional[int]        =       TFFunnelBaseModel(config=a__     )
   UpperCAmelCase__       :	Optional[int]        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, 3, self.d_model)     )

   UpperCAmelCase__       :	List[Any]        =       False
   UpperCAmelCase__       :	Any        =       TFFunnelBaseModel(config=a__     )
   UpperCAmelCase__       :	Any        =       model(a__     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, 2, self.d_model)     )

  def  __lowercase							(	self		:      int       ,A		:      Tuple       ,A		:      Union[str, Any]       ,A		:      List[Any]       ,A		:      Any       ,A		:      Dict       ,A		:      List[Any]       ,A		:      Optional[Any]       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       TFFunnelForPreTraining(config=a__     )
   UpperCAmelCase__       :	List[Any]        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	Dict        =       model(a__     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length)     )

  def  __lowercase							(	self		:      Optional[int]       ,A		:      List[str]       ,A		:      Tuple       ,A		:      Tuple       ,A		:      List[str]       ,A		:      List[Any]       ,A		:      List[Any]       ,A		:      Optional[Any]       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       TFFunnelForMaskedLM(config=a__     )
   UpperCAmelCase__       :	List[str]        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	int        =       model(a__     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)     )

  def  __lowercase							(	self		:      int       ,A		:      List[str]       ,A		:      Tuple       ,A		:      int       ,A		:      Dict       ,A		:      Optional[Any]       ,A		:      Optional[Any]       ,A		:      Dict       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       self.num_labels
   UpperCAmelCase__       :	Any        =       TFFunnelForSequenceClassification(config=a__     )
   UpperCAmelCase__       :	Union[str, Any]        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	Tuple        =       model(a__     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)     )

  def  __lowercase							(	self		:      List[str]       ,A		:      Any       ,A		:      List[Any]       ,A		:      str       ,A		:      List[str]       ,A		:      List[Any]       ,A		:      List[Any]       ,A		:      Tuple       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       self.num_choices
   UpperCAmelCase__       :	Dict        =       TFFunnelForMultipleChoice(config=a__     )
   UpperCAmelCase__       :	Any        =       tf.tile(tf.expand_dims(a__       ,1     )       ,(1, self.num_choices, 1)     )
   UpperCAmelCase__       :	Dict        =       tf.tile(tf.expand_dims(a__       ,1     )       ,(1, self.num_choices, 1)     )
   UpperCAmelCase__       :	List[str]        =       tf.tile(tf.expand_dims(a__       ,1     )       ,(1, self.num_choices, 1)     )
   UpperCAmelCase__       :	Any        =       {
       """input_ids""": multiple_choice_inputs_ids,
       """attention_mask""": multiple_choice_input_mask,
       """token_type_ids""": multiple_choice_token_type_ids,
   }
   UpperCAmelCase__       :	Union[str, Any]        =       model(a__     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_choices)     )

  def  __lowercase							(	self		:      str       ,A		:      int       ,A		:      List[Any]       ,A		:      List[Any]       ,A		:      Optional[Any]       ,A		:      Dict       ,A		:      Union[str, Any]       ,A		:      Dict       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       self.num_labels
   UpperCAmelCase__       :	Optional[int]        =       TFFunnelForTokenClassification(config=a__     )
   UpperCAmelCase__       :	List[str]        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	Dict        =       model(a__     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.num_labels)     )

  def  __lowercase							(	self		:      List[str]       ,A		:      Optional[int]       ,A		:      Dict       ,A		:      Union[str, Any]       ,A		:      Optional[Any]       ,A		:      int       ,A		:      int       ,A		:      Union[str, Any]       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       TFFunnelForQuestionAnswering(config=a__     )
   UpperCAmelCase__       :	Dict        =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
   UpperCAmelCase__       :	Optional[int]        =       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  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.prepare_config_and_inputs()
   (
       UpperCAmelCase__			
   )       :	Dict        =       config_and_inputs
   UpperCAmelCase__       :	List[str]        =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_tf
class   __lowercase      (     lowercase_						,      lowercase_						,      unittest.TestCase       ):
  snake_case_												=							(
      (
          TFFunnelModel,
          TFFunnelForMaskedLM,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  snake_case_												=							(
      {
          "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
          "fill-mask": TFFunnelForMaskedLM,
          "question-answering": TFFunnelForQuestionAnswering,
          "text-classification": TFFunnelForSequenceClassification,
          "token-classification": TFFunnelForTokenClassification,
          "zero-shot": TFFunnelForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  snake_case_												=							False
  snake_case_												=							False

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       TFFunnelModelTester(self     )
   UpperCAmelCase__       :	Tuple        =       ConfigTester(self       ,config_class=a__     )

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*a__     )

  def  __lowercase							(	self		:      Optional[int]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_pretraining(*a__     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*a__     )

  def  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*a__     )




  def  __lowercase							(	self		:      Union[str, Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*a__     )







@require_tf
class   __lowercase      (     lowercase_						,      unittest.TestCase       ):
  snake_case_												=							(
      (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
  )
  snake_case_												=							False
  snake_case_												=							False

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       TFFunnelModelTester(self       ,base=a__     )
   UpperCAmelCase__       :	Any        =       ConfigTester(self       ,config_class=a__     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_base_model(*a__     )

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*a__     )




  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_multiple_choice(*a__     )













import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


lowerCamelCase__       		=  50_0000

lowerCamelCase__		,      lowerCamelCase__       		=  os.path.split(__file__)
lowerCamelCase__       		=  os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json"""))






@get_duration
def 	UpperCamelCase	(							snake_case__  : datasets.Dataset						,**snake_case__  : Any	):




     '''simple docstring'''






     __snake_case      :List[str] 							=    dataset.map(**snake_case__	)






@get_duration
def 	UpperCamelCase	(							snake_case__  : datasets.Dataset						,**snake_case__  : Dict	):




     '''simple docstring'''






     __snake_case      :int 							=    dataset.filter(**snake_case__	)






def 	UpperCamelCase	(							):




     '''simple docstring'''






     __snake_case      :str 							=    {"""num examples""": SPEED_TEST_N_EXAMPLES}
     with tempfile.TemporaryDirectory() as tmp_dir:
          __snake_case      :Dict 							=    datasets.Features({"""text""": datasets.Value("""string"""	), """numbers""": datasets.Value("""float32"""	)}	)
          __snake_case      :Optional[int] 							=    generate_example_dataset(
              os.path.join(snake_case__						,"""dataset.arrow"""	)						,snake_case__						,num_examples=snake_case__	)

          __snake_case      :str 							=    transformers.AutoTokenizer.from_pretrained("""bert-base-cased"""						,use_fast=snake_case__	)

          def tokenize(snake_case__  : Optional[int]	):
               return tokenizer(examples["""text"""]	)

          __snake_case      :Optional[Any] 							=    map(snake_case__	)

          __snake_case      :Optional[Any] 							=    map(snake_case__						,batched=snake_case__	)

          __snake_case      :Optional[Any] 							=    map(snake_case__						,function=lambda snake_case__	: None						,batched=snake_case__	)

          with dataset.formatted_as(type="""numpy"""	):
               __snake_case      :int 							=    map(snake_case__						,function=lambda snake_case__	: None						,batched=snake_case__	)

          with dataset.formatted_as(type="""pandas"""	):
               __snake_case      :List[Any] 							=    map(snake_case__						,function=lambda snake_case__	: None						,batched=snake_case__	)

          with dataset.formatted_as(type="""torch"""						,columns="""numbers"""	):
               __snake_case      :str 							=    map(snake_case__						,function=lambda snake_case__	: None						,batched=snake_case__	)

          with dataset.formatted_as(type="""tensorflow"""						,columns="""numbers"""	):
               __snake_case      :Optional[int] 							=    map(snake_case__						,function=lambda snake_case__	: None						,batched=snake_case__	)

          __snake_case      :Dict 							=    map(snake_case__						,function=snake_case__						,batched=snake_case__	)

          __snake_case      :Tuple 							=    filter(snake_case__	)

          # Activate later when tokenizer support batched inputs
          # with dataset.formatted_as(type='numpy'):
          #     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

     with open(snake_case__						,"""wb"""	) as f:
          f.write(json.dumps(snake_case__	).encode("""utf-8"""	)	)


if __name__ == "__main__":  # useful to run the profiler
     benchmark_map_filter()


