import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 import (
         MODEL_FOR_PRETRAINING_MAPPING,
         FunnelBaseModel,
         FunnelForMaskedLM,
         FunnelForMultipleChoice,
         FunnelForPreTraining,
         FunnelForQuestionAnswering,
         FunnelForSequenceClassification,
         FunnelForTokenClassification,
         FunnelModel,
     )







class 					UpperCAmelCase_				:




    def __init__(					self  :   str      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[int]=13      ,      lowerCAmelCase  :   List[str]=7      ,      lowerCAmelCase  :   int=True      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   Optional[int]=True      ,      lowerCAmelCase  :   Union[str, Any]=True      ,      lowerCAmelCase  :   Union[str, Any]=99      ,      lowerCAmelCase  :   str=[1, 1, 2]      ,      lowerCAmelCase  :   Optional[Any]=1      ,      lowerCAmelCase  :   int=32      ,      lowerCAmelCase  :   List[Any]=4      ,      lowerCAmelCase  :   Optional[int]=8      ,      lowerCAmelCase  :   int=37      ,      lowerCAmelCase  :   Any="gelu_new"      ,      lowerCAmelCase  :   Optional[Any]=0.1      ,      lowerCAmelCase  :   str=0.1      ,      lowerCAmelCase  :   Tuple=0.0      ,      lowerCAmelCase  :   Optional[Any]=512      ,      lowerCAmelCase  :   str=3      ,      lowerCAmelCase  :   Union[str, Any]=0.02      ,      lowerCAmelCase  :   Union[str, Any]=3      ,      lowerCAmelCase  :   str=4      ,      lowerCAmelCase  :   Dict=None      ,      lowerCAmelCase  :   Dict=False      ,      )	-> int:
      a__     		= parent
      a__     		= batch_size
      a__     		= seq_length
      a__     		= is_training
      a__     		= use_input_mask
      a__     		= use_token_type_ids
      a__     		= use_labels
      a__     		= vocab_size
      a__     		= block_sizes
      a__     		= num_decoder_layers
      a__     		= d_model
      a__     		= n_head
      a__     		= d_head
      a__     		= d_inner
      a__     		= hidden_act
      a__     		= hidden_dropout
      a__     		= attention_dropout
      a__     		= activation_dropout
      a__     		= max_position_embeddings
      a__     		= type_vocab_size
      a__     		= 2
      a__     		= num_labels
      a__     		= num_choices
      a__     		= scope
      a__     		= initializer_std

      # Used in the tests to check the size of the first attention layer
      a__     		= n_head
      # Used in the tests to check the size of the first hidden state
      a__     		= self.d_model
      # Used in the tests to check the number of output hidden states/attentions
      a__     		= 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:
        a__     		= self.num_hidden_layers + 2




    def 						__lowerCamelCase (					self  :   Dict   )	-> Tuple:
      a__     		= ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size   )

      a__     		= None
      if self.use_input_mask:
        a__     		= ids_tensor([self.batch_size, self.seq_length]      ,      vocab_size=2   )

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

      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]      ,      self.num_choices   )
        a__     		= ids_tensor([self.batch_size, self.seq_length]      ,      1   )

      a__     		= self.get_config()

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




    def 						__lowerCamelCase (					self  :   str   )	-> int:
      return 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      ,      )




    def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   Tuple      ,      )	-> int:
      a__     		= FunnelModel(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_   )
      a__     		= model(a_      ,      token_type_ids=a_   )
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.d_model)   )

      a__     		= False
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.d_model)   )

      a__     		= False
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.d_model)   )




    def 						__lowerCamelCase (					self  :   Optional[Any]      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Union[str, Any]      ,      )	-> Dict:
      a__     		= FunnelBaseModel(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_   )
      a__     		= model(a_      ,      token_type_ids=a_   )
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, 2, self.d_model)   )

      a__     		= False
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, 3, self.d_model)   )

      a__     		= False
      a__     		= model(a_   )
      self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, 2, self.d_model)   )




    def 						__lowerCamelCase (					self  :   Optional[Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   int      ,      )	-> Optional[int]:
      a__     		= self.num_labels
      a__     		= FunnelForPreTraining(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      labels=a_   )
      self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length)   )




    def 						__lowerCamelCase (					self  :   List[Any]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   List[str]      ,      )	-> Dict:
      a__     		= FunnelForMaskedLM(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      labels=a_   )
      self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)   )




    def 						__lowerCamelCase (					self  :   List[Any]      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   Tuple      ,      )	-> int:
      a__     		= self.num_labels
      a__     		= FunnelForSequenceClassification(a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      labels=a_   )
      self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)   )




    def 						__lowerCamelCase (					self  :   Optional[int]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Tuple      ,      )	-> Any:
      a__     		= self.num_choices
      a__     		= FunnelForMultipleChoice(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= input_ids.unsqueeze(1   ).expand(-1      ,      self.num_choices      ,      -1   ).contiguous()
      a__     		= token_type_ids.unsqueeze(1   ).expand(-1      ,      self.num_choices      ,      -1   ).contiguous()
      a__     		= input_mask.unsqueeze(1   ).expand(-1      ,      self.num_choices      ,      -1   ).contiguous()
      a__     		= model(
          a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      labels=a_      ,      )
      self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_choices)   )




    def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Dict      ,      )	-> Tuple:
      a__     		= self.num_labels
      a__     		= FunnelForTokenClassification(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      labels=a_   )
      self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)   )




    def 						__lowerCamelCase (					self  :   Dict      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Optional[Any]      ,      )	-> Tuple:
      a__     		= FunnelForQuestionAnswering(config=a_   )
      model.to(a_   )
      model.eval()
      a__     		= model(
          a_      ,      attention_mask=a_      ,      token_type_ids=a_      ,      start_positions=a_      ,      end_positions=a_      ,      )
      self.parent.assertEqual(result.start_logits.shape      ,      (self.batch_size, self.seq_length)   )
      self.parent.assertEqual(result.end_logits.shape      ,      (self.batch_size, self.seq_length)   )




    def 						__lowerCamelCase (					self  :   str   )	-> Tuple:
      a__     		= self.prepare_config_and_inputs()
      (
          (
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,(
          a__		
      )		,		
      )     		= config_and_inputs
      a__     		= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
      return config, inputs_dict







@require_torch
class 					UpperCAmelCase_				(							SCREAMING_SNAKE_CASE__     ,     SCREAMING_SNAKE_CASE__     ,     unittest.TestCase    ):
    UpperCAmelCase_			:       List[str]						 =							False
    UpperCAmelCase_			:       Dict						 =							False
    UpperCAmelCase_			:       Any						 =							(
        (
            FunnelModel,
            FunnelForMaskedLM,
            FunnelForPreTraining,
            FunnelForQuestionAnswering,
            FunnelForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase_			:       Tuple						 =							(
        {
            'feature-extraction': (FunnelBaseModel, FunnelModel),
            'fill-mask': FunnelForMaskedLM,
            'question-answering': FunnelForQuestionAnswering,
            'text-classification': FunnelForSequenceClassification,
            'token-classification': FunnelForTokenClassification,
            'zero-shot': FunnelForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )




    def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   List[Any]=False   )	-> int:
      a__     		= super()._prepare_for_class(a_      ,      a_      ,      return_labels=a_   )

      if return_labels:
        if model_class in get_values(a_   ):
          a__     		= torch.zeros(
              (self.model_tester.batch_size, self.model_tester.seq_length)      ,      dtype=torch.long      ,      device=a_   )
      return inputs_dict




    def 						__lowerCamelCase (					self  :   str   )	-> List[str]:
      a__     		= FunnelModelTester(self   )
      a__     		= ConfigTester(self      ,      config_class=a_   )




    def 						__lowerCamelCase (					self  :   List[str]   )	-> List[str]:
      self.config_tester.run_common_tests()




    def 						__lowerCamelCase (					self  :   List[str]   )	-> Dict:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a_   )




    def 						__lowerCamelCase (					self  :   str   )	-> Any:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*a_   )




    def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Dict:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*a_   )




    def 						__lowerCamelCase (					self  :   Optional[int]   )	-> List[Any]:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*a_   )




    def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Any:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*a_   )




    def 						__lowerCamelCase (					self  :   int      ,      lowerCAmelCase  :   Dict   )	-> int:



      if hasattr(a_      ,      "weight"   ) and module.weight is not None:
        module.weight.data.fill_(3   )
      if hasattr(a_      ,      "bias"   ) and module.bias is not None:
        module.bias.data.fill_(3   )

      for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
        if hasattr(a_      ,      a_   ) and getattr(a_      ,      a_   ) is not None:
          a__     		= getattr(a_      ,      a_   )
          weight.data.fill_(3   )







@require_torch
class 					UpperCAmelCase_				(							SCREAMING_SNAKE_CASE__     ,     unittest.TestCase    ):
    UpperCAmelCase_			:       Optional[int]						 =							False
    UpperCAmelCase_			:       Union[str, Any]						 =							False
    UpperCAmelCase_			:       List[str]						 =							(
        (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
    )




    def 						__lowerCamelCase (					self  :   str   )	-> Union[str, Any]:
      a__     		= FunnelModelTester(self      ,      base=a_   )
      a__     		= ConfigTester(self      ,      config_class=a_   )




    def 						__lowerCamelCase (					self  :   Dict   )	-> Union[str, Any]:
      self.config_tester.run_common_tests()




    def 						__lowerCamelCase (					self  :   Dict   )	-> List[str]:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_base_model(*a_   )




    def 						__lowerCamelCase (					self  :   Any   )	-> List[Any]:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*a_   )




    def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> str:
      a__     		= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*a_   )




    def 						__lowerCamelCase (					self  :   Dict   )	-> Dict:
      a__		,a__     		= self.model_tester.prepare_config_and_inputs_for_common()
      a__     		= True

      for model_class in self.all_model_classes:
        if model_class.__name__ == "FunnelBaseModel":
          continue
        a__     		= model_class(a_   )
        model.to(a_   )
        model.train()
        a__     		= self._prepare_for_class(a_      ,      a_      ,      return_labels=a_   )
        a__     		= model(**a_   ).loss
        loss.backward()




    def 						__lowerCamelCase (					self  :   Optional[Any]      ,      lowerCAmelCase  :   Tuple   )	-> Optional[Any]:



      if hasattr(a_      ,      "weight"   ) and module.weight is not None:
        module.weight.data.fill_(3   )
      if hasattr(a_      ,      "bias"   ) and module.bias is not None:
        module.bias.data.fill_(3   )

      for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
        if hasattr(a_      ,      a_   ) and getattr(a_      ,      a_   ) is not None:
          a__     		= getattr(a_      ,      a_   )
          weight.data.fill_(3   )







@require_torch
@require_sentencepiece
@require_tokenizers
class 					UpperCAmelCase_				(							unittest.TestCase    ):




    def 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> Any:
      a__     		= 13
      a__     		= 7
      a__     		= torch.arange(0      ,      batch_size * sequence_length   ).long().reshape(a_      ,      a_   )
      a__     		= [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
      a__     		= torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]   )

      a__     		= FunnelModel.from_pretrained("sgugger/funnel-random-tiny"   )
      a__     		= model(a_      ,      token_type_ids=a_   )[0].abs()

      a__     		= torch.tensor(2344.8352   )
      a__     		= torch.tensor(0.8_052   )
      self.assertTrue(torch.allclose(output.sum()      ,      a_      ,      atol=1e-4   )   )
      self.assertTrue(torch.allclose(output.mean()      ,      a_      ,      atol=1e-4   )   )

      a__     		= torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]   )
      a__     		= model(a_      ,      attention_mask=a_      ,      token_type_ids=a_   )[0].abs()

      a__     		= torch.tensor(2343.8425   )
      a__     		= torch.tensor(0.8_049   )
      self.assertTrue(torch.allclose(output.sum()      ,      a_      ,      atol=1e-4   )   )
      self.assertTrue(torch.allclose(output.mean()      ,      a_      ,      atol=1e-4   )   )




    @slow
    def 						__lowerCamelCase (					self  :   int   )	-> Dict:
      a__     		= FunnelTokenizer.from_pretrained("huggingface/funnel-small"   )
      a__     		= FunnelModel.from_pretrained("huggingface/funnel-small"   )
      a__     		= tokenizer("Hello! I am the Funnel Transformer model."      ,      return_tensors="pt"   )
      a__     		= model(**a_   )[0]

      a__     		= torch.tensor(235.7_246   )
      a__     		= torch.tensor(0.0_256   )
      self.assertTrue(torch.allclose(output.sum()      ,      a_      ,      atol=1e-4   )   )
      self.assertTrue(torch.allclose(output.mean()      ,      a_      ,      atol=1e-4   )   )


def 				__lowercase	(						lowerCamelCase__ = "mytext.txt" ,				lowerCamelCase__ = False					)				->None:

  '''simple docstring'''




  import socket

  a__     		=     1_23_12  # Reserve a port for your service.
  a__     		=     socket.socket()  # Create a socket object
  a__     		=     socket.gethostname()  # Get local machine name
  sock.bind((host, port)					)  # Bind to the port
  sock.listen(5					)  # Now wait for client connection.

  print("Server listening...."					)

  while True:
    a__		,a__     		=     sock.accept()  # Establish connection with client.
    print(f"Got connection from {addr}"					)
    a__     		=     conn.recv(10_24					)
    print(f"Server received: {data = }"					)

    with open(lowerCamelCase__ ,				"rb"					) as in_file:
      a__     		=     in_file.read(10_24					)
      while data:
        conn.send(lowerCamelCase__					)
        print(f"Sent {data!r}"					)
        a__     		=     in_file.read(10_24					)

    print("Done sending"					)
    conn.close()
    if testing:  # Allow the test to complete
      break

  sock.shutdown(1					)
  sock.close()


if __name__ == "__main__":
     send_file()

