
from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.xglm.modeling_tf_xglm import (
					    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFXGLMForCausalLM,
					    TFXGLMModel,
					)





@require_tf
class 				UpperCAmelCase :




	'''simple docstring'''





	snake_case_          =	XGLMConfig
	snake_case_          =	{}
	snake_case_          =	"gelu"






	def __init__(			self   :					str				,A   :					Optional[int]				,A   :					List[str]=14				,A   :					List[str]=7				,A   :					str=True				,A   :					int=True				,A   :					Optional[Any]=True				,A   :					List[Any]=99				,A   :					Dict=32				,A   :					Optional[int]=2				,A   :					Any=4				,A   :					Optional[Any]=37				,A   :					Optional[Any]="gelu"				,A   :					List[Any]=0.1				,A   :					Tuple=0.1				,A   :					int=5_12				,A   :					Optional[Any]=0.02				,):
					__A       =					parent
					__A       =					batch_size
					__A       =					seq_length
					__A       =					is_training
					__A       =					use_input_mask
					__A       =					use_labels
					__A       =					vocab_size
					__A       =					d_model
					__A       =					num_hidden_layers
					__A       =					num_attention_heads
					__A       =					ffn_dim
					__A       =					activation_function
					__A       =					activation_dropout
					__A       =					attention_dropout
					__A       =					max_position_embeddings
					__A       =					initializer_range
					__A       =					None
					__A       =					0
					__A       =					2
					__A       =					1






	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					return XGLMConfig.from_pretrained("facebook/xglm-564M"					)






	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					tf.clip_by_value(
					    ids_tensor([self.batch_size, self.seq_length]				,self.vocab_size					)				,clip_value_min=0				,clip_value_max=3					)

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

					__A       =					self.get_config()

					__A       =					floats_tensor([self.num_hidden_layers, self.num_attention_heads]				,2					)

					return (
					    config,
					    input_ids,
					    input_mask,
					    head_mask,
					)






	def 							UpperCamelCase_    (			self   :					List[str]					):
					return XGLMConfig(
					    vocab_size=self.vocab_size				,d_model=self.hidden_size				,num_layers=self.num_hidden_layers				,attention_heads=self.num_attention_heads				,ffn_dim=self.ffn_dim				,activation_function=self.activation_function				,activation_dropout=self.activation_dropout				,attention_dropout=self.attention_dropout				,max_position_embeddings=self.max_position_embeddings				,initializer_range=self.initializer_range				,use_cache=_a				,bos_token_id=self.bos_token_id				,eos_token_id=self.eos_token_id				,pad_token_id=self.pad_token_id				,return_dict=_a				,)







	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.prepare_config_and_inputs()

					(
					    __A   
					)       =					config_and_inputs

					__A       =					{
					    '''input_ids''': input_ids,
					    '''head_mask''': head_mask,
					}

					return config, inputs_dict





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




	'''simple docstring'''





	snake_case_          =	(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
	snake_case_          =	(TFXGLMForCausalLM,) if is_tf_available() else ()
	snake_case_          =	(
	    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
	)
	snake_case_          =	False
	snake_case_          =	False
	snake_case_          =	False






	def 							UpperCamelCase_    (			self   :					str					):
					__A       =					TFXGLMModelTester(self					)
					__A       =					ConfigTester(self				,config_class=_a				,n_embd=37					)






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






	@slow
	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A       =					TFXGLMModel.from_pretrained(_a					)
									self.assertIsNotNone(_a					)







	@unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."					)
	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					super().test_resize_token_embeddings()





@require_tf
class 				UpperCAmelCase (    unittest.TestCase    ):




	'''simple docstring'''





	@slow
	def 							UpperCamelCase_    (			self   :					Optional[Any]				,A   :					int=True					):
					__A       =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"					)
					__A       =					tf.convert_to_tensor([[2, 2_68, 98_65]]				,dtype=tf.intaa					)  # The dog
					# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
					# fmt: off
					__A       =					[2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
					# fmt: on
					__A       =					model.generate(_a				,do_sample=_a				,num_beams=1					)
					if verify_outputs:
									self.assertListEqual(output_ids[0].numpy().tolist()				,_a					)






	@slow
	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					XGLMTokenizer.from_pretrained("facebook/xglm-564M"					)
					__A       =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"					)

					tf.random.set_seed(0					)
					__A       =					tokenizer("Today is a nice day and"				,return_tensors="tf"					)
					__A       =					tokenized.input_ids
					# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
					with tf.device(":/CPU:0"					):
									__A       =					model.generate(_a				,do_sample=_a				,seed=[7, 0]					)
					__A       =					tokenizer.decode(output_ids[0]				,skip_special_tokens=_a					)

					__A       =					(
					    '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'''
					)
					self.assertEqual(_a				,_a					)







	@slow
	def 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"					)
					__A       =					XGLMTokenizer.from_pretrained("facebook/xglm-564M"					)

					__A       =					'''left'''

					# use different length sentences to test batching
					__A       =					[
					    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
					    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
					    '''regardless of whether left padding is applied or not. When''',
					    '''Hello, my dog is a little''',
					]

					__A       =					tokenizer(_a				,return_tensors="tf"				,padding=_a					)
					__A       =					inputs['''input_ids''']

					__A       =					model.generate(input_ids=_a				,attention_mask=inputs["attention_mask"]				,max_new_tokens=12					)

					__A       =					tokenizer(sentences[0]				,return_tensors="tf"					).input_ids
					__A       =					model.generate(input_ids=_a				,max_new_tokens=12					)

					__A       =					tokenizer(sentences[1]				,return_tensors="tf"					).input_ids
					__A       =					model.generate(input_ids=_a				,max_new_tokens=12					)

					__A       =					tokenizer.batch_decode(_a				,skip_special_tokens=_a					)
					__A       =					tokenizer.decode(output_non_padded[0]				,skip_special_tokens=_a					)
					__A       =					tokenizer.decode(output_padded[0]				,skip_special_tokens=_a					)

					__A       =					[
					    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
					    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
					    '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '''
					    '''a single''',
					    '''Hello, my dog is a little bit of a shy one, but he is very friendly''',
					]
					self.assertListEqual(_a				,_a					)
					self.assertListEqual(_a				,[non_padded_sentence, padded_sentence]					)



from ...configuration_utils import PretrainedConfig
from ...utils import logging


a__												=						logging.get_logger(__name__)

a__												=						{
    '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''',
    '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''',
    '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''',
    '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''',
    '''funnel-transformer/intermediate''': (
        '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json'''
    ),
    '''funnel-transformer/intermediate-base''': (
        '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json'''
    ),
    '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''',
    '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''',
    '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''',
    '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''',
}

class 							UpperCAmelCase_						(     __lowercase  ):
							"""simple docstring"""







							UpperCAmelCase__			:		Optional[int]             =       "funnel"
							UpperCAmelCase__			:		Tuple             =       {
							    "hidden_size": "d_model",
							    "num_attention_heads": "n_head",
							}


							def __init__(     self			,				_a=3_0_5_2_2			,				_a=[4, 4, 4]			,				_a=None			,				_a=2			,				_a=7_6_8			,				_a=1_2			,				_a=6_4			,				_a=3_0_7_2			,				_a="gelu_new"			,				_a=0.1			,				_a=0.1			,				_a=0.0			,				_a=0.1			,				_a=None			,				_a=1e-9			,				_a="mean"			,				_a="relative_shift"			,				_a=True			,				_a=True			,				_a=True			,				**_a			,				)       ->       List[Any]:
												_a      :       Optional[int]   =						vocab_size
												_a      :       Dict   =						block_sizes
												_a      :       Optional[int]   =						[1] * len(_a			) if block_repeats is None else block_repeats
												assert len(_a			) == len(
												    self.block_repeats			), "`block_sizes` and `block_repeats` should have the same length."
												_a      :       int   =						num_decoder_layers
												_a      :       List[str]   =						d_model
												_a      :       Optional[Any]   =						n_head
												_a      :       Tuple   =						d_head
												_a      :       Dict   =						d_inner
												_a      :       List[str]   =						hidden_act
												_a      :       int   =						hidden_dropout
												_a      :       Union[str, Any]   =						attention_dropout
												_a      :       Tuple   =						activation_dropout
												_a      :       Optional[Any]   =						initializer_range
												_a      :       Dict   =						initializer_std
												_a      :       Union[str, Any]   =						layer_norm_eps
												assert pooling_type in [
												    "mean",
												    "max",
												], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported."""
												_a      :       Any   =						pooling_type
												assert attention_type in [
												    "relative_shift",
												    "factorized",
												], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."""
												_a      :       Optional[Any]   =						attention_type
												_a      :       int   =						separate_cls
												_a      :       Tuple   =						truncate_seq
												_a      :       List[Any]   =						pool_q_only

												super().__init__(**_a			)


							@property
							def 							__lowercase							(     self			)       ->       Tuple:
												return sum(self.block_sizes			)


							@num_hidden_layers.setter
							def 							__lowercase							(     self			,				_a			)       ->       List[str]:
												raise NotImplementedError(
												    '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.'''			)


							@property
							def 							__lowercase							(     self			)       ->       Optional[int]:
												return len(self.block_sizes			)




							@num_blocks.setter
							def 							__lowercase							(     self			,				_a			)       ->       Dict:
												raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.'''			)



