

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 				UpperCAmelCase_		:
							"""simple docstring"""







							def __init__(			self       ,							_a       ,							_a=3       ,							_a=7       ,							_a=True       ,							_a=True       ,							_a=False       ,							_a=True       ,							_a=9_9       ,							_a=3_2       ,							_a=5       ,							_a=4       ,							_a=3_7       ,							_a="gelu"       ,							_a=0.1       ,							_a=0.1       ,							_a=5_1_2       ,							_a=1_6       ,							_a=2       ,							_a=0.02       ,							_a=3       ,							_a=4       ,							_a=None       ,							)    ->    Tuple:
												_a	:    Any         =		parent
												_a	:    List[str]         =		batch_size
												_a	:    Tuple         =		seq_length
												_a	:    List[str]         =		is_training
												_a	:    int         =		use_input_mask
												_a	:    Optional[int]         =		use_token_type_ids
												_a	:    Tuple         =		use_labels
												_a	:    Tuple         =		vocab_size
												_a	:    Tuple         =		hidden_size
												_a	:    str         =		num_hidden_layers
												_a	:    Tuple         =		num_attention_heads
												_a	:    Dict         =		intermediate_size
												_a	:    Dict         =		hidden_act
												_a	:    Any         =		hidden_dropout_prob
												_a	:    List[str]         =		attention_probs_dropout_prob
												_a	:    Dict         =		max_position_embeddings
												_a	:    Optional[Any]         =		type_vocab_size
												_a	:    Dict         =		type_sequence_label_size
												_a	:    Dict         =		initializer_range
												_a	:    Optional[Any]         =		num_labels
												_a	:    List[str]         =		num_choices
												_a	:    int         =		scope


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    Any         =		ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size       )

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

												_a	:    Any         =		None

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

												_a	:    Tuple         =		self.get_config()

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


							def 				__lowercase				(			self       )    ->    str:
												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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    str:
												_a	:    Any         =		FalconModel(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    str         =		model(_a       ,							attention_mask=_a       )
												_a	:    str         =		model(_a       )
												self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							)    ->    int:
												_a	:    Any         =		True
												_a	:    Optional[Any]         =		FalconModel(_a       )
												model.to(_a       )
												model.eval()
												_a	:    Any         =		model(
												    _a       ,							attention_mask=_a       ,							encoder_hidden_states=_a       ,							encoder_attention_mask=_a       ,							)
												_a	:    Dict         =		model(
												    _a       ,							attention_mask=_a       ,							encoder_hidden_states=_a       ,							)
												_a	:    Tuple         =		model(_a       ,							attention_mask=_a       )
												self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							)    ->    str:
												_a	:    List[Any]         =		FalconForCausalLM(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    int         =		model(_a       ,							attention_mask=_a       ,							labels=_a       )
												self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.vocab_size)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							)    ->    List[Any]:
												_a	:    Optional[int]         =		True
												_a	:    str         =		True
												_a	:    Any         =		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	:    int         =		outputs.past_key_values

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

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

												_a	:    Optional[Any]         =		model(
												    _a       ,							attention_mask=_a       ,							encoder_hidden_states=_a       ,							encoder_attention_mask=_a       ,							output_hidden_states=_a       ,							)['''hidden_states'''][0]
												_a	:    int         =		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	:    Union[str, Any]         =		output_from_no_past[:, -3:, random_slice_idx].detach()
												_a	:    str         =		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 				__lowercase				(			self       )    ->    Union[str, Any]:
												_a	:    Optional[int]         =		self.prepare_config_and_inputs()
												(
												    (
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					
												)	:    List[Any]         =		config_and_inputs
												_a	:    Dict         =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict



@require_torch
class 				UpperCAmelCase_		(			__lowercase				,       __lowercase				,       __lowercase				,       unittest.TestCase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						Tuple	    =    (
							    (
							        FalconModel,
							        FalconForCausalLM,
							        FalconForSequenceClassification,
							        FalconForTokenClassification,
							        FalconForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase__       :						Tuple	    =    (FalconForCausalLM,) if is_torch_available() else ()
							UpperCAmelCase__       :						Tuple	    =    (
							    {
							        "feature-extraction": FalconModel,
							        "text-classification": FalconForSequenceClassification,
							        "text-generation": FalconForCausalLM,
							        "question-answering": FalconForQuestionAnswering,
							        "token-classification": FalconForTokenClassification,
							        "zero-shot": FalconForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							UpperCAmelCase__       :						Tuple	    =    False
							UpperCAmelCase__       :						Tuple	    =    False


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    Tuple         =		FalconModelTester(self       )
												_a	:    List[Any]         =		ConfigTester(self       ,							config_class=_a       ,							hidden_size=3_7       )


							def 				__lowercase				(			self       )    ->    List[Any]:
												self.config_tester.run_common_tests()


							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Optional[Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_a       )


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a      ,					*_a	:    Union[str, Any]         =		self.model_tester.prepare_config_and_inputs()
												for alibi in [True, False]:
																	_a	:    Dict         =		alibi
																	self.model_tester.create_and_check_model(_a       ,							*_a       )


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a      ,					_a	:    Optional[Any]         =		self.model_tester.prepare_config_and_inputs_for_common()
												_a	:    Optional[Any]         =		3
												_a	:    str         =		input_dict['''input_ids''']
												_a	:    Optional[int]         =		input_ids.ne(1       ).to(_a       )
												_a	:    List[str]         =		ids_tensor([self.model_tester.batch_size]       ,							self.model_tester.type_sequence_label_size       )
												_a	:    Optional[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 				__lowercase				(			self       )    ->    int:
												_a      ,					_a	:    Optional[int]         =		self.model_tester.prepare_config_and_inputs_for_common()
												_a	:    str         =		3
												_a	:    List[str]         =		'''single_label_classification'''
												_a	:    Union[str, Any]         =		input_dict['''input_ids''']
												_a	:    List[Any]         =		input_ids.ne(1       ).to(_a       )
												_a	:    List[Any]         =		ids_tensor([self.model_tester.batch_size]       ,							self.model_tester.type_sequence_label_size       )
												_a	:    str         =		FalconForSequenceClassification(_a       )
												model.to(_a       )
												model.eval()
												_a	:    Union[str, Any]         =		model(_a       ,							attention_mask=_a       ,							labels=_a       )
												self.assertEqual(result.logits.shape       ,							(self.model_tester.batch_size, self.model_tester.num_labels)       )


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a      ,					_a	:    int         =		self.model_tester.prepare_config_and_inputs_for_common()
												_a	:    Tuple         =		input_dict['''input_ids''']
												_a	:    Union[str, Any]         =		FalconForCausalLM(_a       )
												model.to(_a       )
												model.eval()
												_a	:    int         =		model(_a       ,							use_cache=_a       )
												_a	:    Dict         =		input_ids.shape[0]
												_a	:    Optional[int]         =		model._convert_to_rw_cache(result.past_key_values       )
												_a	:    Dict         =		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 				__lowercase				(			self       )    ->    Any:
												_a      ,					_a	:    Union[str, Any]         =		self.model_tester.prepare_config_and_inputs_for_common()
												_a	:    Dict         =		3
												_a	:    Any         =		'''multi_label_classification'''
												_a	:    Dict         =		input_dict['''input_ids''']
												_a	:    Union[str, Any]         =		input_ids.ne(1       ).to(_a       )
												_a	:    Any         =		ids_tensor(
												    [self.model_tester.batch_size, config.num_labels]       ,							self.model_tester.type_sequence_label_size       ).to(torch.float       )
												_a	:    str         =		FalconForSequenceClassification(_a       )
												model.to(_a       )
												model.eval()
												_a	:    Optional[int]         =		model(_a       ,							attention_mask=_a       ,							labels=_a       )
												self.assertEqual(result.logits.shape       ,							(self.model_tester.batch_size, self.model_tester.num_labels)       )




							def 				__lowercase				(			self       )    ->    str:
												# 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	:    int         =		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	:    Dict         =		model_class(_a       ).to(_a       )
																	if "use_cache" not in inputs:
																						_a	:    List[str]         =		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	:    List[Any]         =		(
																	    getattr(_a       ,							'''decoder_layers'''       ,							_a       )
																	    or getattr(_a       ,							'''num_decoder_layers'''       ,							_a       )
																	    or config.num_hidden_layers
																	)
																	_a	:    str         =		getattr(_a       ,							'''num_kv_heads'''       ,							config.num_attention_heads       )
																	_a	:    Dict         =		getattr(_a       ,							'''d_model'''       ,							config.hidden_size       )
																	_a	:    int         =		embed_dim // num_attention_heads

																	_a	:    Union[str, Any]         =		outputs['''past_key_values''']
																	self.assertEqual(len(_a       )       ,							_a       )

																	_a      ,					_a	:    Any         =		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	:    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 				UpperCAmelCase_		(			unittest.TestCase   ):
							"""simple docstring"""







							@slow
							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Union[str, Any]         =		AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''       )
												_a	:    Optional[Any]         =		FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''       )
												model.eval()
												model.to(_a       )
												_a	:    Union[str, Any]         =		tokenizer('''My favorite food is'''       ,							return_tensors='''pt'''       ).to(_a       )

												_a	:    Any         =		(
												    '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
												)

												_a	:    List[Any]         =		model.generate(**_a       ,							do_sample=_a       ,							max_new_tokens=1_9       )
												_a	:    str         =		tokenizer.batch_decode(_a       )[0]

												self.assertEqual(_a       ,							_a       )


							@slow
							def 				__lowercase				(			self       )    ->    int:
												# 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	:    Optional[Any]         =		AutoTokenizer.from_pretrained(_a       )
																	_a	:    Tuple         =		FalconForCausalLM.from_pretrained(_a       )
																	model.eval()
																	model.to(_a       )
																	_a	:    str         =		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 				__lowercase				(			self       )    ->    int:
												# 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	:    Tuple         =		AutoTokenizer.from_pretrained(_a       )
																						_a	:    Tuple         =		FalconForCausalLM.from_pretrained(_a       )
																						model.eval()
																						model.to(device=_a       )
																						_a	:    Any         =		tokenizer('''My favorite food is'''       ,							return_tensors='''pt'''       ).to(_a       )

																						# Test results are the same with and without cache
																						_a	:    Dict         =		model.generate(**_a       ,							do_sample=_a       ,							max_new_tokens=2_0       ,							use_cache=_a       )
																						_a	:    List[Any]         =		model.generate(**_a       ,							do_sample=_a       ,							max_new_tokens=2_0       ,							use_cache=_a       )
																						self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0       )






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

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







							UpperCAmelCase__       :						str	    =    ["image_processor", "tokenizer"]
							UpperCAmelCase__       :						Optional[int]	    =    "CLIPImageProcessor"
							UpperCAmelCase__       :						Tuple	    =    ("XLMRobertaTokenizer", "XLMRobertaTokenizerFast")


							def __init__(			self       ,							_a=None       ,							_a=None       ,							**_a       )    ->    Any:
												_a	:    int         =		None
												if "feature_extractor" in kwargs:
																	warnings.warn(
																	    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																	    ''' instead.'''       ,							_a       ,							)
																	_a	:    int         =		kwargs.pop('''feature_extractor'''       )

												_a	:    int         =		image_processor if image_processor is not None else feature_extractor
												if image_processor is None:
																	raise ValueError('''You need to specify an `image_processor`.'''       )
												if tokenizer is None:
																	raise ValueError('''You need to specify a `tokenizer`.'''       )

												super().__init__(_a       ,							_a       )


							def __call__(			self       ,							_a=None       ,							_a=None       ,							_a=None       ,							**_a       )    ->    List[Any]:

												if text is None and images is None:
																	raise ValueError('''You have to specify either text or images. Both cannot be none.'''       )

												if text is not None:
																	_a	:    str         =		self.tokenizer(_a       ,							return_tensors=_a       ,							**_a       )

												if images is not None:
																	_a	:    Optional[Any]         =		self.image_processor(_a       ,							return_tensors=_a       ,							**_a       )

												if text is not None and images is not None:
																	_a	:    List[Any]         =		image_features.pixel_values
																	return encoding
												elif text is not None:
																	return encoding
												else:
																	return BatchEncoding(data=dict(**_a       )       ,							tensor_type=_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Optional[Any]:
												return self.tokenizer.batch_decode(*_a       ,							**_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Optional[int]:
												return self.tokenizer.decode(*_a       ,							**_a       )




							@property
							def 				__lowercase				(			self       )    ->    Optional[int]:
												_a	:    Dict         =		self.tokenizer.model_input_names
												_a	:    Dict         =		self.image_processor.model_input_names
												return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )



