



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




	'''simple docstring'''





	def __init__(	self :			Tuple							,A :			Any							,A :			Any=13							,A :			Any=7							,A :			List[str]=True							,A :			Optional[int]=True							,A :			int=True							,A :			int=True							,A :			int=99							,A :			Union[str, Any]=[1, 1, 2]							,A :			Union[str, Any]=1							,A :			Optional[int]=32							,A :			Tuple=4							,A :			Optional[int]=8							,A :			str=37							,A :			Union[str, Any]="gelu_new"							,A :			int=0.1							,A :			List[Any]=0.1							,A :			str=0.0							,A :			int=5_12							,A :			Optional[Any]=3							,A :			Any=0.02							,A :			List[str]=3							,A :			Tuple=4							,A :			Any=None							,A :			int=False							,):
					__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     UpperCamelCase_  (	self :			List[Any]			):
					__A             =			ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size			)

					__A             =			None
					if self.use_input_mask:
									__A             =			random_attention_mask([self.batch_size, self.seq_length]			)

					__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             =			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     UpperCamelCase_  (	self :			List[str]							,A :			List[str]							,A :			str							,A :			int							,A :			Tuple							,A :			Dict							,A :			Dict							,A :			Tuple							,):
					__A             =			TFFunnelModel(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)

					__A             =			[input_ids, input_mask]
					__A             =			model(A			)

					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.d_model)			)

					__A             =			False
					__A             =			TFFunnelModel(config=A			)
					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.d_model)			)

					__A             =			False
					__A             =			TFFunnelModel(config=A			)
					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.d_model)			)






	def     UpperCamelCase_  (	self :			Tuple							,A :			Union[str, Any]							,A :			Optional[int]							,A :			List[Any]							,A :			Optional[Any]							,A :			Optional[Any]							,A :			Tuple							,A :			Tuple							,):
					__A             =			TFFunnelBaseModel(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)

					__A             =			[input_ids, input_mask]
					__A             =			model(A			)

					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, 2, self.d_model)			)

					__A             =			False
					__A             =			TFFunnelBaseModel(config=A			)
					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, 3, self.d_model)			)

					__A             =			False
					__A             =			TFFunnelBaseModel(config=A			)
					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, 2, self.d_model)			)






	def     UpperCamelCase_  (	self :			Optional[int]							,A :			List[str]							,A :			Any							,A :			Optional[Any]							,A :			Union[str, Any]							,A :			Union[str, Any]							,A :			Optional[int]							,A :			List[str]							,):
					__A             =			TFFunnelForPreTraining(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length)			)






	def     UpperCamelCase_  (	self :			Optional[int]							,A :			Optional[int]							,A :			List[str]							,A :			Dict							,A :			List[str]							,A :			Union[str, Any]							,A :			List[str]							,A :			int							,):
					__A             =			TFFunnelForMaskedLM(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)			)






	def     UpperCamelCase_  (	self :			Union[str, Any]							,A :			Dict							,A :			Optional[int]							,A :			Optional[int]							,A :			Any							,A :			Optional[Any]							,A :			Tuple							,A :			Optional[int]							,):
					__A             =			self.num_labels
					__A             =			TFFunnelForSequenceClassification(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)			)






	def     UpperCamelCase_  (	self :			str							,A :			Union[str, Any]							,A :			Optional[Any]							,A :			Dict							,A :			Union[str, Any]							,A :			List[str]							,A :			Dict							,A :			List[Any]							,):
					__A             =			self.num_choices
					__A             =			TFFunnelForMultipleChoice(config=A			)
					__A             =			tf.tile(tf.expand_dims(A							,1			)							,(1, self.num_choices, 1)			)
					__A             =			tf.tile(tf.expand_dims(A							,1			)							,(1, self.num_choices, 1)			)
					__A             =			tf.tile(tf.expand_dims(A							,1			)							,(1, self.num_choices, 1)			)
					__A             =			{
					    "input_ids": multiple_choice_inputs_ids,
					    "attention_mask": multiple_choice_input_mask,
					    "token_type_ids": multiple_choice_token_type_ids,
					}
					__A             =			model(A			)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)			)






	def     UpperCamelCase_  (	self :			Optional[Any]							,A :			Tuple							,A :			Optional[int]							,A :			Tuple							,A :			Dict							,A :			Union[str, Any]							,A :			str							,A :			Any							,):
					__A             =			self.num_labels
					__A             =			TFFunnelForTokenClassification(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			model(A			)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)			)






	def     UpperCamelCase_  (	self :			Tuple							,A :			List[str]							,A :			Union[str, Any]							,A :			Optional[Any]							,A :			int							,A :			Tuple							,A :			Any							,A :			Any							,):
					__A             =			TFFunnelForQuestionAnswering(config=A			)
					__A             =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					__A             =			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     UpperCamelCase_  (	self :			str			):
					__A             =			self.prepare_config_and_inputs()
					(
					    (
					    __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_tf
class 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      unittest.TestCase  ):




	'''simple docstring'''





	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     UpperCamelCase_  (	self :			List[Any]			):
					__A             =			TFFunnelModelTester(self			)
					__A             =			ConfigTester(self							,config_class=A			)






	def     UpperCamelCase_  (	self :			str			):
					self.config_tester.run_common_tests()






	def     UpperCamelCase_  (	self :			List[Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A			)






	def     UpperCamelCase_  (	self :			Dict			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*A			)






	def     UpperCamelCase_  (	self :			Union[str, Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*A			)






	def     UpperCamelCase_  (	self :			List[Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*A			)







	def     UpperCamelCase_  (	self :			Union[str, Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*A			)





@require_tf
class 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE      ,      unittest.TestCase  ):




	'''simple docstring'''





	snake_case_    =					(
	    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
	)
	snake_case_    =					False
	snake_case_    =					False






	def     UpperCamelCase_  (	self :			List[str]			):
					__A             =			TFFunnelModelTester(self							,base=A			)
					__A             =			ConfigTester(self							,config_class=A			)






	def     UpperCamelCase_  (	self :			Tuple			):
					self.config_tester.run_common_tests()






	def     UpperCamelCase_  (	self :			Tuple			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*A			)






	def     UpperCamelCase_  (	self :			Optional[Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*A			)







	def     UpperCamelCase_  (	self :			str			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*A			)


import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin





class 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE      ,      unittest.TestCase  ):




	'''simple docstring'''





	snake_case_    =					CTRLTokenizer
	snake_case_    =					False
	snake_case_    =					False






	def     UpperCamelCase_  (	self :			Dict			):
					super().setUp()

					# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
					__A             =			["adapt", "re@@", "a@@", "apt", "c@@", "t", "<unk>"]
					__A             =			dict(zip(A							,range(len(A			)			)			)			)
					__A             =			["#version: 0.2", "a p", "ap t</w>", "r e", "a d", "ad apt</w>", ""]
					__A             =			{"unk_token": "<unk>"}

					__A             =			os.path.join(self.tmpdirname							,VOCAB_FILES_NAMES["vocab_file"]			)
					__A             =			os.path.join(self.tmpdirname							,VOCAB_FILES_NAMES["merges_file"]			)
					with open(self.vocab_file							,"w"							,encoding="utf-8"			) as fp:
									fp.write(json.dumps(A			) + "\n"			)
					with open(self.merges_file							,"w"							,encoding="utf-8"			) as fp:
									fp.write("\n".join(A			)			)






	def     UpperCamelCase_  (	self :			List[str]							,**A :			List[str]			):
					kwargs.update(self.special_tokens_map			)
					return CTRLTokenizer.from_pretrained(self.tmpdirname							,**A			)






	def     UpperCamelCase_  (	self :			Optional[int]							,A :			Tuple			):
					__A             =			"adapt react readapt apt"
					__A             =			"adapt react readapt apt"
					return input_text, output_text







	def     UpperCamelCase_  (	self :			Any			):
					__A             =			CTRLTokenizer(self.vocab_file							,self.merges_file							,**self.special_tokens_map			)
					__A             =			"adapt react readapt apt"
					__A             =			"adapt re@@ a@@ c@@ t re@@ adapt apt".split()
					__A             =			tokenizer.tokenize(A			)
					self.assertListEqual(A							,A			)

					__A             =			tokens + [tokenizer.unk_token]

					__A             =			[0, 1, 2, 4, 5, 1, 0, 3, 6]
					self.assertListEqual(tokenizer.convert_tokens_to_ids(A			)							,A			)
