

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


					'''simple docstring'''






					UpperCAmelCase__:       List[Any]  							=	XGLMConfig
					UpperCAmelCase__:       Any  							=	{}
					UpperCAmelCase__:       str  							=	'''gelu'''






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






					def   __A      (			self      ):
						return XGLMConfig.from_pretrained("""facebook/xglm-564M"""      )






					def   __A      (			self      ):
						A__							:   Optional[int]					       =		tf.clip_by_value(
						    ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size      )   ,	clip_value_min=0   ,	clip_value_max=3      )

						A__							:   List[str]					       =		None
						if self.use_input_mask:
							A__							:   Optional[int]					       =		random_attention_mask([self.batch_size, self.seq_length]      )

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

						A__							:   str					       =		floats_tensor([self.num_hidden_layers, self.num_attention_heads]   ,	2      )

						return (
						    config,
						    input_ids,
						    input_mask,
						    head_mask,
						)






					def   __A      (			self      ):
						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   __A      (			self      ):
						A__							:   List[str]					       =		self.prepare_config_and_inputs()

						(
						    (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       
						)							:   Union[str, Any]					       =		config_and_inputs

						A__							:   Tuple					       =		{
						    """input_ids""": input_ids,
						    """head_mask""": head_mask,
						}

						return config, inputs_dict



@require_tf
class    _a						(__magic_name__		,       __magic_name__		,       unittest.TestCase    ):


					'''simple docstring'''






					UpperCAmelCase__:       Optional[int]  							=	(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
					UpperCAmelCase__:       int  							=	(TFXGLMForCausalLM,) if is_tf_available() else ()
					UpperCAmelCase__:       Any  							=	(
					    {'''feature-extraction''': TFXGLMModel, '''text-generation''': TFXGLMForCausalLM} if is_tf_available() else {}
					)
					UpperCAmelCase__:       Tuple  							=	False
					UpperCAmelCase__:       int  							=	False
					UpperCAmelCase__:       Dict  							=	False






					def   __A      (			self      ):
						A__							:   Tuple					       =		TFXGLMModelTester(self      )
						A__							:   List[Any]					       =		ConfigTester(self   ,	config_class=A__   ,	n_embd=37      )






					def   __A      (			self      ):
						self.config_tester.run_common_tests()






					@slow
					def   __A      (			self      ):
						for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A__							:   int					       =		TFXGLMModel.from_pretrained(A__      )
							self.assertIsNotNone(A__      )






					@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""      )
					def   __A      (			self      ):
						super().test_resize_token_embeddings()



@require_tf
class    _a						(unittest.TestCase    ):


					'''simple docstring'''






					@slow
					def   __A      (			self   ,	A__=True      ):
						A__							:   Tuple					       =		TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""      )
						A__							:   Any					       =		tf.convert_to_tensor([[2, 268, 9865]]   ,	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__							:   Tuple					       =		[2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581]
						# fmt: on
						A__							:   Dict					       =		model.generate(A__   ,	do_sample=A__   ,	num_beams=1      )
						if verify_outputs:
							self.assertListEqual(output_ids[0].numpy().tolist()   ,	A__      )






					@slow
					def   __A      (			self      ):
						A__							:   List[str]					       =		XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""      )
						A__							:   List[Any]					       =		TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""      )

						tf.random.set_seed(0      )
						A__							:   str					       =		tokenizer("""Today is a nice day and"""   ,	return_tensors="""tf"""      )
						A__							:   int					       =		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__							:   List[str]					       =		model.generate(A__   ,	do_sample=A__   ,	seed=[7, 0]      )
						A__							:   Any					       =		tokenizer.decode(output_ids[0]   ,	skip_special_tokens=A__      )

						A__							:   List[Any]					       =		(
						    """Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"""
						)
						self.assertEqual(A__   ,	A__      )






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

						A__							:   List[str]					       =		"""left"""

						# use different length sentences to test batching
						A__							:   Union[str, Any]					       =		[
						    """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__							:   Union[str, Any]					       =		tokenizer(A__   ,	return_tensors="""tf"""   ,	padding=A__      )
						A__							:   int					       =		inputs["""input_ids"""]

						A__							:   Dict					       =		model.generate(input_ids=A__   ,	attention_mask=inputs["""attention_mask"""]   ,	max_new_tokens=12      )

						A__							:   str					       =		tokenizer(sentences[0]   ,	return_tensors="""tf"""      ).input_ids
						A__							:   Tuple					       =		model.generate(input_ids=A__   ,	max_new_tokens=12      )

						A__							:   str					       =		tokenizer(sentences[1]   ,	return_tensors="""tf"""      ).input_ids
						A__							:   int					       =		model.generate(input_ids=A__   ,	max_new_tokens=12      )

						A__							:   Any					       =		tokenizer.batch_decode(A__   ,	skip_special_tokens=A__      )
						A__							:   Any					       =		tokenizer.decode(output_non_padded[0]   ,	skip_special_tokens=A__      )
						A__							:   List[Any]					       =		tokenizer.decode(output_padded[0]   ,	skip_special_tokens=A__      )

						A__							:   Optional[Any]					       =		[
						    """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]      )






import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    FalconForCausalLM,
	    FalconForQuestionAnswering,
	    FalconForSequenceClassification,
	    FalconForTokenClassification,
	    FalconModel,
	)



class    _a						:


					'''simple docstring'''






					def __init__(			self   ,	A__   ,	A__=3   ,	A__=7   ,	A__=True   ,	A__=True   ,	A__=False   ,	A__=True   ,	A__=99   ,	A__=32   ,	A__=5   ,	A__=4   ,	A__=37   ,	A__="gelu"   ,	A__=0.1   ,	A__=0.1   ,	A__=512   ,	A__=16   ,	A__=2   ,	A__=0.0_2   ,	A__=3   ,	A__=4   ,	A__=None   ,	):
						A__							:   List[Any]					       =		parent
						A__							:   List[str]					       =		batch_size
						A__							:   Optional[int]					       =		seq_length
						A__							:   Optional[int]					       =		is_training
						A__							:   Any					       =		use_input_mask
						A__							:   Tuple					       =		use_token_type_ids
						A__							:   str					       =		use_labels
						A__							:   Tuple					       =		vocab_size
						A__							:   Any					       =		hidden_size
						A__							:   List[str]					       =		num_hidden_layers
						A__							:   Optional[int]					       =		num_attention_heads
						A__							:   Optional[Any]					       =		intermediate_size
						A__							:   Optional[Any]					       =		hidden_act
						A__							:   Tuple					       =		hidden_dropout_prob
						A__							:   Union[str, Any]					       =		attention_probs_dropout_prob
						A__							:   List[str]					       =		max_position_embeddings
						A__							:   Union[str, Any]					       =		type_vocab_size
						A__							:   str					       =		type_sequence_label_size
						A__							:   Tuple					       =		initializer_range
						A__							:   Tuple					       =		num_labels
						A__							:   Dict					       =		num_choices
						A__							:   List[str]					       =		scope






					def   __A      (			self      ):
						A__							:   Any					       =		ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size      )

						A__							:   Any					       =		None
						if self.use_input_mask:
							A__							:   int					       =		random_attention_mask([self.batch_size, self.seq_length]      )

						A__							:   str					       =		None

						A__							:   Union[str, Any]					       =		None
						A__							:   List[str]					       =		None
						A__							:   Optional[Any]					       =		None
						if self.use_labels:
							A__							:   Any					       =		ids_tensor([self.batch_size]   ,	self.type_sequence_label_size      )
							A__							:   List[str]					       =		ids_tensor([self.batch_size, self.seq_length]   ,	self.num_labels      )
							A__							:   List[Any]					       =		ids_tensor([self.batch_size]   ,	self.num_choices      )

						A__							:   Dict					       =		self.get_config()

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






					def   __A      (			self      ):
						return FalconConfig(
						    vocab_size=self.vocab_size   ,	hidden_size=self.hidden_size   ,	num_hidden_layers=self.num_hidden_layers   ,	num_attention_heads=self.num_attention_heads   ,	intermediate_size=self.intermediate_size   ,	hidden_act=self.hidden_act   ,	hidden_dropout_prob=self.hidden_dropout_prob   ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,	max_position_embeddings=self.max_position_embeddings   ,	type_vocab_size=self.type_vocab_size   ,	is_decoder=A__   ,	initializer_range=self.initializer_range   ,	pad_token_id=1   ,	new_decoder_architecture=A__   ,	)






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__      ):
						A__							:   List[str]					       =		FalconModel(config=A__      )
						model.to(A__      )
						model.eval()
						A__							:   int					       =		model(A__   ,	attention_mask=A__      )
						A__							:   Union[str, Any]					       =		model(A__      )
						self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Union[str, Any]					       =		True
						A__							:   Union[str, Any]					       =		FalconModel(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Tuple					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	)
						A__							:   Union[str, Any]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	)
						A__							:   List[str]					       =		model(A__   ,	attention_mask=A__      )
						self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Any					       =		FalconForCausalLM(config=A__      )
						model.to(A__      )
						model.eval()
						A__							:   Tuple					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.vocab_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Optional[Any]					       =		True
						A__							:   Union[str, Any]					       =		True
						A__							:   int					       =		FalconForCausalLM(config=A__      )
						model.to(A__      )
						model.eval()

						# first forward pass
						A__							:   List[Any]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	use_cache=A__   ,	)
						A__							:   Tuple					       =		outputs.past_key_values

						# create hypothetical multiple next token and extent to next_input_ids
						A__							:   Tuple					       =		ids_tensor((self.batch_size, 3)   ,	config.vocab_size      )
						A__							:   List[Any]					       =		ids_tensor((self.batch_size, 3)   ,	vocab_size=2      )

						# append to next input_ids and
						A__							:   Optional[int]					       =		torch.cat([input_ids, next_tokens]   ,	dim=-1      )
						A__							:   List[str]					       =		torch.cat([input_mask, next_mask]   ,	dim=-1      )

						A__							:   Optional[int]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	output_hidden_states=A__   ,	)["""hidden_states"""][0]
						A__							:   Any					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	past_key_values=A__   ,	output_hidden_states=A__   ,	)["""hidden_states"""][0]

						# select random slice
						A__							:   List[Any]					       =		ids_tensor((1,)   ,	output_from_past.shape[-1]      ).item()
						A__							:   List[str]					       =		output_from_no_past[:, -3:, random_slice_idx].detach()
						A__							:   Optional[Any]					       =		output_from_past[:, :, random_slice_idx].detach()

						self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]      )

						# test that outputs are equal for slice
						self.parent.assertTrue(torch.allclose(A__   ,	A__   ,	atol=1e-3      )      )






					def   __A      (			self      ):
						A__							:   List[str]					       =		self.prepare_config_and_inputs()
						(
						    (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       (
						    A__
						)       ,       
						)							:   Tuple					       =		config_and_inputs
						A__							:   List[str]					       =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
						return config, inputs_dict



@require_torch
class    _a						(__magic_name__		,       __magic_name__		,       __magic_name__		,       unittest.TestCase    ):


					'''simple docstring'''






					UpperCAmelCase__:       List[Any]  							=	(
					    (
					        FalconModel,
					        FalconForCausalLM,
					        FalconForSequenceClassification,
					        FalconForTokenClassification,
					        FalconForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__:       Tuple  							=	(FalconForCausalLM,) if is_torch_available() else ()
					UpperCAmelCase__:       Optional[int]  							=	(
					    {
					        '''feature-extraction''': FalconModel,
					        '''text-classification''': FalconForSequenceClassification,
					        '''text-generation''': FalconForCausalLM,
					        '''question-answering''': FalconForQuestionAnswering,
					        '''token-classification''': FalconForTokenClassification,
					        '''zero-shot''': FalconForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					UpperCAmelCase__:       str  							=	False
					UpperCAmelCase__:       int  							=	False






					def   __A      (			self      ):
						A__							:   List[Any]					       =		FalconModelTester(self      )
						A__							:   Union[str, Any]					       =		ConfigTester(self   ,	config_class=A__   ,	hidden_size=37      )






					def   __A      (			self      ):
						self.config_tester.run_common_tests()






					def   __A      (			self      ):
						A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A__      )






					def   __A      (			self      ):
						A__       ,       *A__							:   List[Any]					       =		self.model_tester.prepare_config_and_inputs()
						for alibi in [True, False]:
							A__							:   Tuple					       =		alibi
							self.model_tester.create_and_check_model(A__   ,	*A__      )






					def   __A      (			self      ):
						A__       ,       A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Optional[int]					       =		3
						A__							:   int					       =		input_dict["""input_ids"""]
						A__							:   int					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Union[str, Any]					       =		ids_tensor([self.model_tester.batch_size]   ,	self.model_tester.type_sequence_label_size      )
						A__							:   Optional[int]					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   int					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						A__       ,       A__							:   str					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Dict					       =		3
						A__							:   Tuple					       =		"""single_label_classification"""
						A__							:   List[Any]					       =		input_dict["""input_ids"""]
						A__							:   Dict					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Dict					       =		ids_tensor([self.model_tester.batch_size]   ,	self.model_tester.type_sequence_label_size      )
						A__							:   Any					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Any					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						A__       ,       A__							:   Optional[Any]					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   List[str]					       =		input_dict["""input_ids"""]
						A__							:   List[str]					       =		FalconForCausalLM(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Any					       =		model(A__   ,	use_cache=A__      )
						A__							:   Any					       =		input_ids.shape[0]
						A__							:   Union[str, Any]					       =		model._convert_to_rw_cache(result.past_key_values      )
						A__							:   int					       =		model._convert_cache_to_standard_format(A__   ,	A__      )
						for layer in range(len(A__      )      ):
							for tensor_idx in range(2      ):
								self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3      )
								self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4      )
								self.assertTrue(
								    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]      )      )






					def   __A      (			self      ):
						A__       ,       A__							:   Dict					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Optional[Any]					       =		3
						A__							:   List[Any]					       =		"""multi_label_classification"""
						A__							:   Tuple					       =		input_dict["""input_ids"""]
						A__							:   List[Any]					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Optional[Any]					       =		ids_tensor(
						    [self.model_tester.batch_size, config.num_labels]   ,	self.model_tester.type_sequence_label_size      ).to(torch.float      )
						A__							:   Optional[int]					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   List[Any]					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						# Falcon can have different numbers of KV-heads than the number of query heads, so we need
						# to override this test to use the right head counts.
						for model_class in self.all_generative_model_classes:
							A__       ,       A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs_for_common()

							# If it doesn't support cache, pass the test
							if not hasattr(A__   ,	"""use_cache"""      ):
								return

							A__							:   Optional[Any]					       =		model_class(A__      ).to(A__      )
							if "use_cache" not in inputs:
								A__							:   Optional[int]					       =		True
							A__							:   List[Any]					       =		model(**A__      )

							# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
							if "past_key_values" not in outputs:
								return

							A__							:   str					       =		(
							    getattr(A__   ,	"""decoder_layers"""   ,	A__      )
							    or getattr(A__   ,	"""num_decoder_layers"""   ,	A__      )
							    or config.num_hidden_layers
							)
							A__							:   Dict					       =		getattr(A__   ,	"""num_kv_heads"""   ,	config.num_attention_heads      )
							A__							:   List[str]					       =		getattr(A__   ,	"""d_model"""   ,	config.hidden_size      )
							A__							:   Union[str, Any]					       =		embed_dim // num_attention_heads

							A__							:   str					       =		outputs["""past_key_values"""]
							self.assertEqual(len(A__      )   ,	A__      )

							A__       ,       A__							:   int					       =		inputs["""input_ids"""].shape
							for i in range(A__      ):
								if config.new_decoder_architecture:
									A__							:   Any					       =		config.num_attention_heads
								elif config.multi_query:
									A__							:   List[Any]					       =		1
								self.assertEqual(len(past_kv[0]      )   ,	2      )  # K V for the decoder = 2
								self.assertEqual(
								    past_kv[i][0].shape   ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )
								self.assertEqual(
								    past_kv[i][1].shape   ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )



@require_torch
class    _a						(unittest.TestCase    ):


					'''simple docstring'''






					@slow
					def   __A      (			self      ):
						A__							:   Dict					       =		AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""      )
						A__							:   List[Any]					       =		FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""      )
						model.eval()
						model.to(A__      )
						A__							:   Optional[int]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

						A__							:   Optional[Any]					       =		(
						    """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
						)

						A__							:   Any					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=19      )
						A__							:   Optional[int]					       =		tokenizer.batch_decode(A__      )[0]

						self.assertEqual(A__   ,	A__      )






					@slow
					def   __A      (			self      ):
						# The big models are way too big for the CI, so we use tiny random models that resemble their
						# architectures but with much smaller and fewer layers
						for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
							A__							:   Dict					       =		AutoTokenizer.from_pretrained(A__      )
							A__							:   List[str]					       =		FalconForCausalLM.from_pretrained(A__      )
							model.eval()
							model.to(A__      )
							A__							:   Union[str, Any]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

							# We just test that these run without errors - the models are randomly initialized
							# and so the actual text outputs will be garbage
							model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=4      )
							model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=4      )
							model.generate(**A__   ,	num_beams=2   ,	max_new_tokens=4      )






					@slow
					def   __A      (			self      ):
						# The big models are way too big for the CI, so we use tiny random models that resemble their
						# architectures but with much smaller and fewer layers
						with torch.no_grad():
							for repo in [
							    "Rocketknight1/falcon-rw-1b",
							    "Rocketknight1/tiny-random-falcon-7b",
							    "Rocketknight1/tiny-random-falcon-40b",
							]:
								A__							:   Dict					       =		AutoTokenizer.from_pretrained(A__      )
								A__							:   Any					       =		FalconForCausalLM.from_pretrained(A__      )
								model.eval()
								model.to(device=A__      )
								A__							:   List[str]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

								# Test results are the same with and without cache
								A__							:   Tuple					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=20   ,	use_cache=A__      )
								A__							:   Optional[Any]					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=20   ,	use_cache=A__      )
								self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0      )



