




"""simple docstring"""

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 			lowerCamelCase__    :


			def __init__(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=13				, SCREAMING_SNAKE_CASE=7				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE=99				, SCREAMING_SNAKE_CASE=[1, 1, 2]				, SCREAMING_SNAKE_CASE=1				, SCREAMING_SNAKE_CASE=32				, SCREAMING_SNAKE_CASE=4				, SCREAMING_SNAKE_CASE=8				, SCREAMING_SNAKE_CASE=37				, SCREAMING_SNAKE_CASE="gelu_new"				, SCREAMING_SNAKE_CASE=0.1				, SCREAMING_SNAKE_CASE=0.1				, SCREAMING_SNAKE_CASE=0.0				, SCREAMING_SNAKE_CASE=512				, SCREAMING_SNAKE_CASE=3				, SCREAMING_SNAKE_CASE=0.02				, SCREAMING_SNAKE_CASE=3				, SCREAMING_SNAKE_CASE=4				, SCREAMING_SNAKE_CASE=None				, SCREAMING_SNAKE_CASE=False				, ):



					"""simple docstring"""







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

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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Optional[int]								=				ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size							)

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

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

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

					snake_case		:						Dict								=				self.get_config()

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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					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				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						Dict								=				FunnelModel(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						List[str]								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)
					snake_case		:						int								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.d_model)							)

					snake_case		:						Dict								=				False
					snake_case		:						List[str]								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.d_model)							)

					snake_case		:						Optional[int]								=				False
					snake_case		:						Dict								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.d_model)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						int								=				FunnelBaseModel(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)
					snake_case		:						List[str]								=				model(SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)
					snake_case		:						int								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, 2, self.d_model)							)

					snake_case		:						List[str]								=				False
					snake_case		:						Tuple								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, 3, self.d_model)							)

					snake_case		:						List[str]								=				False
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, 2, self.d_model)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						Optional[Any]								=				self.num_labels
					snake_case		:						str								=				FunnelForPreTraining(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						Any								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE				, labels=SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						List[Any]								=				FunnelForMaskedLM(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE				, labels=SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						Optional[int]								=				self.num_labels
					snake_case		:						Any								=				FunnelForSequenceClassification(SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						List[Any]								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE				, labels=SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_labels)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







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


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						Dict								=				self.num_labels
					snake_case		:						List[Any]								=				FunnelForTokenClassification(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE				, labels=SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.num_labels)							)


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, ):



					"""simple docstring"""







					snake_case		:						Optional[int]								=				FunnelForQuestionAnswering(config=SCREAMING_SNAKE_CASE							)
					model.to(SCREAMING_SNAKE_CASE							)
					model.eval()
					snake_case		:						Any								=				model(
					    SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE				, start_positions=SCREAMING_SNAKE_CASE				, end_positions=SCREAMING_SNAKE_CASE				, )
					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							):



					"""simple docstring"""







					snake_case		:						Union[str, Any]								=				self.prepare_config_and_inputs()
					(
					    (
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				(
					    snake_case 
					)			,				 
					)		:						List[str]								=				config_and_inputs
					snake_case		:						Union[str, Any]								=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict







@require_torch
class 			lowerCamelCase__    (  snake_case_				,  snake_case_				,  unittest.TestCase			):
			a__ :      Any								=     False
			a__ :      List[Any]								=     False
			a__ :      Dict								=     (
			    (
			        FunnelModel,
			        FunnelForMaskedLM,
			        FunnelForPreTraining,
			        FunnelForQuestionAnswering,
			        FunnelForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			a__ :      str								=     (
			    {
			        """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				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=False							):



					"""simple docstring"""







					snake_case		:						Union[str, Any]								=				super()._prepare_for_class(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, return_labels=SCREAMING_SNAKE_CASE							)

					if return_labels:
							if model_class in get_values(SCREAMING_SNAKE_CASE							):
									snake_case		:						List[Any]								=				torch.zeros(
									    (self.model_tester.batch_size, self.model_tester.seq_length)				, dtype=torch.long				, device=SCREAMING_SNAKE_CASE							)
					return inputs_dict


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[Any]								=				FunnelModelTester(self							)
					snake_case		:						List[Any]								=				ConfigTester(self				, config_class=SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					self.config_tester.run_common_tests()


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Dict								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Union[str, Any]								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Tuple								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[str]								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[Any]								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE							)



			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					if hasattr(SCREAMING_SNAKE_CASE				, "weight"							) and module.weight is not None:
							module.weight.data.fill_(3							)
					if hasattr(SCREAMING_SNAKE_CASE				, "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(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							) and getattr(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							) is not None:
									snake_case		:						int								=				getattr(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
									weight.data.fill_(3							)







@require_torch
class 			lowerCamelCase__    (  snake_case_				,  unittest.TestCase			):
			a__ :      str								=     False
			a__ :      Dict								=     False
			a__ :      str								=     (
			    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
			)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						int								=				FunnelModelTester(self				, base=SCREAMING_SNAKE_CASE							)
					snake_case		:						Tuple								=				ConfigTester(self				, config_class=SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					self.config_tester.run_common_tests()


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Tuple								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[Any]								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						int								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case			,				snake_case		:						Any								=				self.model_tester.prepare_config_and_inputs_for_common()
					snake_case		:						Union[str, Any]								=				True

					for model_class in self.all_model_classes:
							if model_class.__name__ == "FunnelBaseModel":
									continue
							snake_case		:						Optional[Any]								=				model_class(SCREAMING_SNAKE_CASE							)
							model.to(SCREAMING_SNAKE_CASE							)
							model.train()
							snake_case		:						List[str]								=				self._prepare_for_class(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, return_labels=SCREAMING_SNAKE_CASE							)
							snake_case		:						List[Any]								=				model(**SCREAMING_SNAKE_CASE							).loss
							loss.backward()



			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					if hasattr(SCREAMING_SNAKE_CASE				, "weight"							) and module.weight is not None:
							module.weight.data.fill_(3							)
					if hasattr(SCREAMING_SNAKE_CASE				, "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(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							) and getattr(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							) is not None:
									snake_case		:						Optional[Any]								=				getattr(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
									weight.data.fill_(3							)







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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[Any]								=				13
					snake_case		:						Union[str, Any]								=				7
					snake_case		:						Dict								=				torch.arange(0				, batch_size * sequence_length							).long().reshape(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
					snake_case		:						List[Any]								=				[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
					snake_case		:						Optional[Any]								=				torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]							)

					snake_case		:						Dict								=				FunnelModel.from_pretrained("sgugger/funnel-random-tiny"							)
					snake_case		:						Union[str, Any]								=				model(SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)[0].abs()

					snake_case		:						Any								=				torch.tensor(23_44.83_52							)
					snake_case		:						Tuple								=				torch.tensor(0.80_52							)
					self.assertTrue(torch.allclose(output.sum()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)
					self.assertTrue(torch.allclose(output.mean()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)

					snake_case		:						Dict								=				torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]							)
					snake_case		:						Tuple								=				model(SCREAMING_SNAKE_CASE				, attention_mask=SCREAMING_SNAKE_CASE				, token_type_ids=SCREAMING_SNAKE_CASE							)[0].abs()

					snake_case		:						int								=				torch.tensor(23_43.84_25							)
					snake_case		:						Optional[int]								=				torch.tensor(0.80_49							)
					self.assertTrue(torch.allclose(output.sum()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)
					self.assertTrue(torch.allclose(output.mean()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)



			@slow
			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Tuple								=				FunnelTokenizer.from_pretrained("huggingface/funnel-small"							)
					snake_case		:						Optional[Any]								=				FunnelModel.from_pretrained("huggingface/funnel-small"							)
					snake_case		:						Dict								=				tokenizer("Hello! I am the Funnel Transformer model."				, return_tensors="pt"							)
					snake_case		:						Any								=				model(**SCREAMING_SNAKE_CASE							)[0]

					snake_case		:						Tuple								=				torch.tensor(2_35.72_46							)
					snake_case		:						Dict								=				torch.tensor(0.02_56							)
					self.assertTrue(torch.allclose(output.sum()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)
					self.assertTrue(torch.allclose(output.mean()				, SCREAMING_SNAKE_CASE				, atol=1E-4							)							)







"""simple docstring"""

import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Image
from datasets.packaged_modules.text.text import Text

from ..utils import require_pil



@pytest.fixture
def   UpperCamelCase__     (		lowercase__  :     str   ):
		snake_case		:						List[str]								=				tmp_path / "text.txt"
		snake_case		:						Tuple								=				textwrap.dedent(
		    "\\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n        Second paragraph:\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n        "   )
		with open(lowercase__       ,     "w"       ,     encoding="utf-8"   ) as f:
				f.write(lowercase__   )
		return str(lowercase__   )



@pytest.fixture
def   UpperCamelCase__     (		lowercase__  :     str       ,     lowercase__  :     Dict   ):
		snake_case		:						Any								=				tmp_path / "text_with_image.txt"
		with open(lowercase__       ,     "w"       ,     encoding="utf-8"   ) as f:
				f.write(lowercase__   )
		return str(lowercase__   )



@pytest.mark.parametrize("keep_linebreaks"       ,     [True, False]   )
def   UpperCamelCase__     (		lowercase__  :     Union[str, Any]       ,     lowercase__  :     Tuple   ):
		with open(lowercase__       ,     encoding="utf-8"   ) as f:
				snake_case		:						List[Any]								=				f.read().splitlines(keepends=lowercase__   )
		snake_case		:						List[Any]								=				Text(keep_linebreaks=lowercase__       ,     encoding="utf-8"   )
		snake_case		:						Optional[int]								=				text._generate_tables([[text_file]]   )
		snake_case		:						int								=				pa.concat_tables([table for _, table in generator]   ).to_pydict()["text"]
		assert generated_content == expected_content



@require_pil
def   UpperCamelCase__     (		lowercase__  :     Optional[Any]   ):
		with open(lowercase__       ,     encoding="utf-8"   ) as f:
				snake_case		:						Union[str, Any]								=				f.read().splitlines()[0]
		snake_case		:						List[Any]								=				Text(encoding="utf-8"       ,     features=Features({"image": Image()}   )   )
		snake_case		:						Tuple								=				text._generate_tables([[text_file_with_image]]   )
		snake_case		:						List[str]								=				pa.concat_tables([table for _, table in generator]   )
		assert pa_table.schema.field("image"   ).type == Image()()
		snake_case		:						Optional[Any]								=				pa_table.to_pydict()["image"]
		assert generated_content == [{"path": image_file, "bytes": None}]



@pytest.mark.parametrize("sample_by"       ,     ["line", "paragraph", "document"]   )
def   UpperCamelCase__     (		lowercase__  :     Union[str, Any]       ,     lowercase__  :     List[str]   ):
		with open(lowercase__       ,     encoding="utf-8"   ) as f:
				snake_case		:						Tuple								=				f.read()
		if sample_by == "line":
				snake_case		:						Optional[int]								=				expected_content.splitlines()
		elif sample_by == "paragraph":
				snake_case		:						str								=				expected_content.split("\n\n"   )
		elif sample_by == "document":
				snake_case		:						List[Any]								=				[expected_content]
		snake_case		:						str								=				Text(sample_by=lowercase__       ,     encoding="utf-8"       ,     chunksize=100   )
		snake_case		:						Optional[int]								=				text._generate_tables([[text_file]]   )
		snake_case		:						Optional[Any]								=				pa.concat_tables([table for _, table in generator]   ).to_pydict()["text"]
		assert generated_content == expected_content

