






import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import 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 (
		    CpmAntConfig,
		    CpmAntForCausalLM,
		    CpmAntModel,
		    CpmAntTokenizer,
		)





@require_torch
class  snake_case__      :
	def __init__(       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__=2    ,     lowerCAmelCase__=8    ,     lowerCAmelCase__=True    ,     lowerCAmelCase__=False    ,     lowerCAmelCase__=False    ,     lowerCAmelCase__=False    ,     lowerCAmelCase__=False    ,     lowerCAmelCase__=99    ,     lowerCAmelCase__=32    ,     lowerCAmelCase__=3    ,     lowerCAmelCase__=4    ,     lowerCAmelCase__=37    ,     lowerCAmelCase__=32    ,     lowerCAmelCase__=1_28    ,     lowerCAmelCase__=8    ,     lowerCAmelCase__=8    ,     lowerCAmelCase__=8    ,     lowerCAmelCase__=1.0    ,     lowerCAmelCase__=True    ,     )							->   Optional[Any]:
							__magic_name__    :     List[Any]     		=      parent
							__magic_name__    :     Optional[Any]     		=      batch_size
							__magic_name__    :     Dict     		=      seq_length
							__magic_name__    :     List[Any]     		=      is_training
							__magic_name__    :     Tuple     		=      use_token_type_ids
							__magic_name__    :     Optional[int]     		=      use_input_mask
							__magic_name__    :     Optional[Any]     		=      use_labels
							__magic_name__    :     int     		=      use_mc_token_ids
							__magic_name__    :     Union[str, Any]     		=      vocab_size
							__magic_name__    :     str     		=      hidden_size
							__magic_name__    :     Union[str, Any]     		=      num_hidden_layers
							__magic_name__    :     List[str]     		=      num_attention_heads
							__magic_name__    :     Optional[Any]     		=      intermediate_size
							__magic_name__    :     Tuple     		=      num_buckets
							__magic_name__    :     Any     		=      max_distance
							__magic_name__    :     List[Any]     		=      prompt_length
							__magic_name__    :     int     		=      prompt_types
							__magic_name__    :     Optional[Any]     		=      segment_types
							__magic_name__    :     Optional[int]     		=      init_std
							__magic_name__    :     str     		=      return_dict
	def     __magic_name__       (       self				)							->   Tuple:
							__magic_name__    :     List[str]     		=      {}
							__magic_name__    :     Optional[int]     		=      ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size				).type(torch.intaa				)
							__magic_name__    :     Tuple     		=      False

							__magic_name__    :     Union[str, Any]     		=      self.get_config()

							return (config, input_ids)
	def     __magic_name__       (       self				)							->   Dict:
							return CpmAntConfig(
							    vocab_size=self.vocab_size    ,     hidden_size=self.hidden_size    ,     num_hidden_layers=self.num_hidden_layers    ,     num_attention_heads=self.num_attention_heads    ,     dim_ff=self.intermediate_size    ,     position_bias_num_buckets=self.num_buckets    ,     position_bias_max_distance=self.max_distance    ,     prompt_types=self.prompt_types    ,     prompt_length=self.prompt_length    ,     segment_types=self.segment_types    ,     use_cache=lowerCAmelCase__    ,     init_std=self.init_std    ,     return_dict=self.return_dict    ,     )
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     *lowerCAmelCase__				)							->   str:
							__magic_name__    :     Dict     		=      CpmAntModel(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()

							__magic_name__    :     str     		=      model(**lowerCAmelCase__				).last_hidden_state

							self.parent.assertEqual(hidden_states.shape    ,     (self.batch_size, self.seq_length, config.hidden_size)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     *lowerCAmelCase__				)							->   Tuple:
							__magic_name__    :     Any     		=      CpmAntForCausalLM(lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							__magic_name__    :     Any     		=      input_ids["""input_ids"""].to(lowerCAmelCase__				)
							model.eval()

							__magic_name__    :     Union[str, Any]     		=      model(**lowerCAmelCase__				)
							self.parent.assertEqual(
							    model_output.logits.shape    ,     (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)    ,     )







	def     __magic_name__       (       self				)							->   Union[str, Any]:
							__magic_name__    :     Optional[int]     		=      self.prepare_config_and_inputs()
							return config, inputs_dict





@require_torch
class  snake_case__      (							_lowerCAmelCase		,						_lowerCAmelCase		,						unittest.TestCase   ):
	lowercase__		:  Dict							   =					(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
	lowercase__		:  Tuple							   =					(
	    {'''feature-extraction''': CpmAntModel, '''text-generation''': CpmAntForCausalLM} if is_torch_available() else {}
	)

	lowercase__		:  Dict							   =					False
	lowercase__		:  Optional[Any]							   =					False
	lowercase__		:  Tuple							   =					False
	lowercase__		:  str							   =					False
	lowercase__		:  int							   =					False
	def     __magic_name__       (       self				)							->   int:
							__magic_name__    :     int     		=      CpmAntModelTester(self				)
							__magic_name__    :     List[str]     		=      ConfigTester(self    ,     config_class=lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Tuple:
							self.config_tester.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()
	def     __magic_name__       (       self				)							->   Dict:
							unittest.skip("""CPMAnt doesn't support input_embeds."""				)(self.test_inputs_embeds				)
	def     __magic_name__       (       self				)							->   Dict:
							unittest.skip(
							    """CPMAnt doesn't support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model."""				)(self.test_retain_grad_hidden_states_attentions				)
	def     __magic_name__       (       self				)							->   Optional[Any]:
							__magic_name__    :     Dict     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_cpmant_model(lowerCAmelCase__    ,     lowerCAmelCase__				)







	def     __magic_name__       (       self				)							->   Union[str, Any]:
							__magic_name__    :     List[Any]     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(lowerCAmelCase__    ,     lowerCAmelCase__				)





@require_torch
class  snake_case__      (							unittest.TestCase   ):
	@tooslow
	def     __magic_name__       (       self				)							->   int:
							__magic_name__    :     Optional[Any]     		=      """今天天气真好！"""
							__magic_name__    :     str     		=      """openbmb/cpm-ant-10b"""
							__magic_name__    :     List[str]     		=      CpmAntModel.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Tuple     		=      CpmAntTokenizer.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Optional[int]     		=      tokenizer(lowerCAmelCase__    ,     return_tensors="""pt"""				)
							__magic_name__    :     Any     		=      model(**lowerCAmelCase__				).last_hidden_state

							__magic_name__    :     Tuple     		=      torch.tensor(
							    [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -11.33_24], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]    ,     )
							self.assertTrue(torch.allclose(hidden_states[:, :3, :3]    ,     lowerCAmelCase__    ,     atol=1e-2				)				)





@require_torch
class  snake_case__      (							unittest.TestCase   ):
	@tooslow
	def     __magic_name__       (       self				)							->   List[Any]:
							__magic_name__    :     List[str]     		=      """今天天气真好！"""
							__magic_name__    :     Any     		=      """openbmb/cpm-ant-10b"""
							__magic_name__    :     Optional[Any]     		=      CpmAntForCausalLM.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     int     		=      CpmAntTokenizer.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Optional[Any]     		=      tokenizer(lowerCAmelCase__    ,     return_tensors="""pt"""				)
							__magic_name__    :     Optional[Any]     		=      model(**lowerCAmelCase__				).logits

							__magic_name__    :     List[str]     		=      torch.tensor(
							    [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]    ,     )
							self.assertTrue(torch.allclose(hidden_states[:, :3, :3]    ,     lowerCAmelCase__    ,     atol=1e-2				)				)
	@tooslow
	def     __magic_name__       (       self				)							->   int:
							__magic_name__    :     List[Any]     		=      """openbmb/cpm-ant-10b"""
							__magic_name__    :     Tuple     		=      CpmAntForCausalLM.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Optional[int]     		=      CpmAntTokenizer.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Dict     		=      """今天天气不错，"""
							__magic_name__    :     Union[str, Any]     		=      """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
							__magic_name__    :     int     		=      tokenizer(lowerCAmelCase__    ,     return_tensors="""pt"""				)
							__magic_name__    :     Union[str, Any]     		=      model.generate(**lowerCAmelCase__				)
							__magic_name__    :     Optional[int]     		=      tokenizer.batch_decode(lowerCAmelCase__				)
							self.assertEqual(lowerCAmelCase__    ,     lowerCAmelCase__				)







	@tooslow
	def     __magic_name__       (       self				)							->   Tuple:
							__magic_name__    :     Optional[Any]     		=      """openbmb/cpm-ant-10b"""
							__magic_name__    :     List[str]     		=      CpmAntForCausalLM.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Tuple     		=      CpmAntTokenizer.from_pretrained(lowerCAmelCase__				)
							__magic_name__    :     Dict     		=      ["""今天天气不错，""", """新年快乐，万事如意！"""]
							__magic_name__    :     Tuple     		=      [
							    """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的""",
							    """新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的""",
							]
							__magic_name__    :     Union[str, Any]     		=      tokenizer(lowerCAmelCase__    ,     return_tensors="""pt"""    ,     padding=lowerCAmelCase__				)
							__magic_name__    :     Union[str, Any]     		=      model.generate(**lowerCAmelCase__				)
							__magic_name__    :     List[str]     		=      tokenizer.batch_decode(lowerCAmelCase__				)
							self.assertEqual(lowerCAmelCase__    ,     lowerCAmelCase__				)








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

							# Used in the tests to check the size of the first attention layer
							__magic_name__    :     List[Any]     		=      n_head
							# Used in the tests to check the size of the first hidden state
							__magic_name__    :     int     		=      self.d_model
							# Used in the tests to check the number of output hidden states/attentions
							__magic_name__    :     Tuple     		=      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:
													__magic_name__    :     Optional[int]     		=      self.num_hidden_layers + 2
	def     __magic_name__       (       self				)							->   Union[str, Any]:
							__magic_name__    :     Tuple     		=      ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size				)

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

							__magic_name__    :     int     		=      None
							if self.use_token_type_ids:
													__magic_name__    :     Optional[Any]     		=      ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size				)

							__magic_name__    :     Tuple     		=      None
							__magic_name__    :     Optional[Any]     		=      None
							__magic_name__    :     Optional[int]     		=      None
							if self.use_labels:
													__magic_name__    :     Optional[int]     		=      ids_tensor([self.batch_size]    ,     self.type_sequence_label_size				)
													__magic_name__    :     Optional[int]     		=      ids_tensor([self.batch_size, self.seq_length]    ,     self.num_labels				)
													__magic_name__    :     Dict     		=      ids_tensor([self.batch_size]    ,     self.num_choices				)
													__magic_name__    :     Any     		=      ids_tensor([self.batch_size, self.seq_length]    ,     1				)

							__magic_name__    :     List[Any]     		=      self.get_config()

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_mask,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							    fake_token_labels,
							)
	def     __magic_name__       (       self				)							->   Optional[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     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   str:
							__magic_name__    :     Optional[int]     		=      FunnelModel(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Optional[Any]     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)
							__magic_name__    :     str     		=      model(lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)
							__magic_name__    :     Dict     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.d_model)				)

							__magic_name__    :     Union[str, Any]     		=      False
							__magic_name__    :     Tuple     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.d_model)				)

							__magic_name__    :     Optional[int]     		=      False
							__magic_name__    :     Dict     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.d_model)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   List[Any]:
							__magic_name__    :     List[str]     		=      FunnelBaseModel(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Dict     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)
							__magic_name__    :     Optional[Any]     		=      model(lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)
							__magic_name__    :     List[str]     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, 2, self.d_model)				)

							__magic_name__    :     str     		=      False
							__magic_name__    :     Optional[int]     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, 3, self.d_model)				)

							__magic_name__    :     List[Any]     		=      False
							__magic_name__    :     Optional[int]     		=      model(lowerCAmelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, 2, self.d_model)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   int:
							__magic_name__    :     Any     		=      self.num_labels
							__magic_name__    :     Tuple     		=      FunnelForPreTraining(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Optional[int]     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     labels=lowerCAmelCase__				)
							self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   List[str]:
							__magic_name__    :     int     		=      FunnelForMaskedLM(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Dict     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     labels=lowerCAmelCase__				)
							self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.vocab_size)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   Any:
							__magic_name__    :     Tuple     		=      self.num_labels
							__magic_name__    :     str     		=      FunnelForSequenceClassification(lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     int     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     labels=lowerCAmelCase__				)
							self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_labels)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   str:
							__magic_name__    :     str     		=      self.num_choices
							__magic_name__    :     int     		=      FunnelForMultipleChoice(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Union[str, Any]     		=      input_ids.unsqueeze(1				).expand(-1    ,     self.num_choices    ,     -1				).contiguous()
							__magic_name__    :     List[Any]     		=      token_type_ids.unsqueeze(1				).expand(-1    ,     self.num_choices    ,     -1				).contiguous()
							__magic_name__    :     Optional[Any]     		=      input_mask.unsqueeze(1				).expand(-1    ,     self.num_choices    ,     -1				).contiguous()
							__magic_name__    :     Union[str, Any]     		=      model(
							    lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     labels=lowerCAmelCase__    ,     )
							self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_choices)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   Tuple:
							__magic_name__    :     Tuple     		=      self.num_labels
							__magic_name__    :     int     		=      FunnelForTokenClassification(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Union[str, Any]     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     labels=lowerCAmelCase__				)
							self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.num_labels)				)
	def     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     )							->   Optional[int]:
							__magic_name__    :     Union[str, Any]     		=      FunnelForQuestionAnswering(config=lowerCAmelCase__				)
							model.to(lowerCAmelCase__				)
							model.eval()
							__magic_name__    :     Optional[int]     		=      model(
							    lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__    ,     start_positions=lowerCAmelCase__    ,     end_positions=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     __magic_name__       (       self				)							->   List[Any]:
							__magic_name__    :     Tuple     		=      self.prepare_config_and_inputs()
							(
							    (
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,(
							    __magic_name__
							)		,
							)    :     str     		=      config_and_inputs
							__magic_name__    :     Optional[Any]     		=      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
							return config, inputs_dict





@require_torch
class  snake_case__      (							_lowerCAmelCase		,						_lowerCAmelCase		,						unittest.TestCase   ):
	lowercase__		:  Optional[int]							   =					False
	lowercase__		:  Dict							   =					False
	lowercase__		:  int							   =					(
	    (
	        FunnelModel,
	        FunnelForMaskedLM,
	        FunnelForPreTraining,
	        FunnelForQuestionAnswering,
	        FunnelForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase__		:  Optional[int]							   =					(
	    {
	        '''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     __magic_name__       (       self    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__=False				)							->   Any:
							__magic_name__    :     Union[str, Any]     		=      super()._prepare_for_class(lowerCAmelCase__    ,     lowerCAmelCase__    ,     return_labels=lowerCAmelCase__				)

							if return_labels:
													if model_class in get_values(lowerCAmelCase__				):
																			__magic_name__    :     List[Any]     		=      torch.zeros(
																			    (self.model_tester.batch_size, self.model_tester.seq_length)    ,     dtype=torch.long    ,     device=lowerCAmelCase__				)
							return inputs_dict
	def     __magic_name__       (       self				)							->   Optional[Any]:
							__magic_name__    :     Any     		=      FunnelModelTester(self				)
							__magic_name__    :     Union[str, Any]     		=      ConfigTester(self    ,     config_class=lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   List[Any]:
							self.config_tester.run_common_tests()
	def     __magic_name__       (       self				)							->   Any:
							__magic_name__    :     Optional[Any]     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Any:
							__magic_name__    :     Optional[Any]     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Tuple:
							__magic_name__    :     Optional[int]     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Tuple:
							__magic_name__    :     Tuple     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Union[str, Any]:
							__magic_name__    :     int     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__				)







	def     __magic_name__       (       self    ,     lowerCAmelCase__				)							->   int:
							if hasattr(lowerCAmelCase__    ,     """weight"""				) and module.weight is not None:
													module.weight.data.fill_(3				)
							if hasattr(lowerCAmelCase__    ,     """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(lowerCAmelCase__    ,     lowerCAmelCase__				) and getattr(lowerCAmelCase__    ,     lowerCAmelCase__				) is not None:
																			__magic_name__    :     Optional[int]     		=      getattr(lowerCAmelCase__    ,     lowerCAmelCase__				)
																			weight.data.fill_(3				)





@require_torch
class  snake_case__      (							_lowerCAmelCase		,						unittest.TestCase   ):
	lowercase__		:  Tuple							   =					False
	lowercase__		:  int							   =					False
	lowercase__		:  Optional[int]							   =					(
	    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
	)
	def     __magic_name__       (       self				)							->   Optional[Any]:
							__magic_name__    :     str     		=      FunnelModelTester(self    ,     base=lowerCAmelCase__				)
							__magic_name__    :     Tuple     		=      ConfigTester(self    ,     config_class=lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   str:
							self.config_tester.run_common_tests()
	def     __magic_name__       (       self				)							->   Optional[int]:
							__magic_name__    :     List[str]     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   str:
							__magic_name__    :     Any     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   Tuple:
							__magic_name__    :     Any     		=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__				)
	def     __magic_name__       (       self				)							->   List[str]:
							__magic_name__		,__magic_name__    :     Tuple     		=      self.model_tester.prepare_config_and_inputs_for_common()
							__magic_name__    :     int     		=      True

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







	def     __magic_name__       (       self    ,     lowerCAmelCase__				)							->   Optional[int]:
							if hasattr(lowerCAmelCase__    ,     """weight"""				) and module.weight is not None:
													module.weight.data.fill_(3				)
							if hasattr(lowerCAmelCase__    ,     """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(lowerCAmelCase__    ,     lowerCAmelCase__				) and getattr(lowerCAmelCase__    ,     lowerCAmelCase__				) is not None:
																			__magic_name__    :     Tuple     		=      getattr(lowerCAmelCase__    ,     lowerCAmelCase__				)
																			weight.data.fill_(3				)





@require_torch
@require_sentencepiece
@require_tokenizers
class  snake_case__      (							unittest.TestCase   ):
	def     __magic_name__       (       self				)							->   List[Any]:
							__magic_name__    :     Union[str, Any]     		=      13
							__magic_name__    :     str     		=      7
							__magic_name__    :     Optional[int]     		=      torch.arange(0    ,     batch_size * sequence_length				).long().reshape(lowerCAmelCase__    ,     lowerCAmelCase__				)
							__magic_name__    :     str     		=      [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
							__magic_name__    :     List[Any]     		=      torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]				)

							__magic_name__    :     Optional[int]     		=      FunnelModel.from_pretrained("""sgugger/funnel-random-tiny"""				)
							__magic_name__    :     List[str]     		=      model(lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)[0].abs()

							__magic_name__    :     Tuple     		=      torch.tensor(2_3_4_4.8_3_5_2				)
							__magic_name__    :     Optional[Any]     		=      torch.tensor(0.8_0_5_2				)
							self.assertTrue(torch.allclose(output.sum()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)
							self.assertTrue(torch.allclose(output.mean()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)

							__magic_name__    :     int     		=      torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]				)
							__magic_name__    :     Tuple     		=      model(lowerCAmelCase__    ,     attention_mask=lowerCAmelCase__    ,     token_type_ids=lowerCAmelCase__				)[0].abs()

							__magic_name__    :     List[Any]     		=      torch.tensor(2_3_4_3.8_4_2_5				)
							__magic_name__    :     Dict     		=      torch.tensor(0.8_0_4_9				)
							self.assertTrue(torch.allclose(output.sum()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)
							self.assertTrue(torch.allclose(output.mean()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)







	@slow
	def     __magic_name__       (       self				)							->   List[Any]:
							__magic_name__    :     Tuple     		=      FunnelTokenizer.from_pretrained("""huggingface/funnel-small"""				)
							__magic_name__    :     Union[str, Any]     		=      FunnelModel.from_pretrained("""huggingface/funnel-small"""				)
							__magic_name__    :     str     		=      tokenizer("""Hello! I am the Funnel Transformer model."""    ,     return_tensors="""pt"""				)
							__magic_name__    :     Optional[Any]     		=      model(**lowerCAmelCase__				)[0]

							__magic_name__    :     List[Any]     		=      torch.tensor(2_3_5.7_2_4_6				)
							__magic_name__    :     List[Any]     		=      torch.tensor(0.0_2_5_6				)
							self.assertTrue(torch.allclose(output.sum()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)
							self.assertTrue(torch.allclose(output.mean()    ,     lowerCAmelCase__    ,     atol=1e-4				)				)
