




'''simple docstring'''
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     :						Union[str, Any]      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Tuple=3      ,       _lowercase     :						Optional[int]=7      ,       _lowercase     :						Any=True      ,       _lowercase     :						Optional[int]=True      ,       _lowercase     :						Optional[int]=False      ,       _lowercase     :						Tuple=True      ,       _lowercase     :						Dict=99      ,       _lowercase     :						Any=32      ,       _lowercase     :						int=5      ,       _lowercase     :						Any=4      ,       _lowercase     :						int=37      ,       _lowercase     :						Tuple="gelu"      ,       _lowercase     :						int=0.1      ,       _lowercase     :						List[Any]=0.1      ,       _lowercase     :						int=512      ,       _lowercase     :						Optional[int]=16      ,       _lowercase     :						Union[str, Any]=2      ,       _lowercase     :						List[Any]=0.02      ,       _lowercase     :						Optional[int]=3      ,       _lowercase     :						Dict=4      ,       _lowercase     :						Optional[int]=None      ,       )    ->							Tuple:
								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_									= hidden_size
								A_									= num_hidden_layers
								A_									= num_attention_heads
								A_									= intermediate_size
								A_									= hidden_act
								A_									= hidden_dropout_prob
								A_									= attention_probs_dropout_prob
								A_									= max_position_embeddings
								A_									= type_vocab_size
								A_									= type_sequence_label_size
								A_									= initializer_range
								A_									= num_labels
								A_									= num_choices
								A_									= scope



					def     __snake_case						(	self     :						List[str])    ->							Optional[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

								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_									= self.get_config()

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



					def     __snake_case						(	self     :						Optional[Any])    ->							Union[str, Any]:
								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=_lowercase      ,       initializer_range=self.initializer_range      ,       pad_token_id=1      ,       new_decoder_architecture=_lowercase      ,       )



					def     __snake_case						(	self     :						Optional[Any]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						int      ,       _lowercase     :						Any      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						Dict      ,       _lowercase     :						Optional[Any])    ->							Any:
								A_									= FalconModel(config=_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase      ,       attention_mask=_lowercase)
								A_									= model(_lowercase)
								self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size))



					def     __snake_case						(	self     :						int      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						Any      ,       _lowercase     :						List[str]      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						Dict      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						Dict      ,       _lowercase     :						int      ,       )    ->							Optional[Any]:
								A_									= True
								A_									= FalconModel(_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(
								    _lowercase      ,       attention_mask=_lowercase      ,       encoder_hidden_states=_lowercase      ,       encoder_attention_mask=_lowercase      ,       )
								A_									= model(
								    _lowercase      ,       attention_mask=_lowercase      ,       encoder_hidden_states=_lowercase      ,       )
								A_									= model(_lowercase      ,       attention_mask=_lowercase)
								self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size))



					def     __snake_case						(	self     :						Optional[int]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						int      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Tuple      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						int      ,       _lowercase     :						Optional[int]      ,       _lowercase     :						Optional[int]      ,       )    ->							Dict:
								A_									= FalconForCausalLM(config=_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase      ,       attention_mask=_lowercase      ,       labels=_lowercase)
								self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size))



					def     __snake_case						(	self     :						Dict      ,       _lowercase     :						Dict      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Tuple      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						Tuple      ,       _lowercase     :						List[Any]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Optional[int]      ,       )    ->							str:
								A_									= True
								A_									= True
								A_									= FalconForCausalLM(config=_lowercase)
								model.to(_lowercase)
								model.eval()

								# first forward pass
								A_									= model(
								    _lowercase      ,       attention_mask=_lowercase      ,       encoder_hidden_states=_lowercase      ,       encoder_attention_mask=_lowercase      ,       use_cache=_lowercase      ,       )
								A_									= outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								A_									= ids_tensor((self.batch_size, 3)      ,       config.vocab_size)
								A_									= ids_tensor((self.batch_size, 3)      ,       vocab_size=2)

								# append to next input_ids and
								A_									= torch.cat([input_ids, next_tokens]      ,       dim=-1)
								A_									= torch.cat([input_mask, next_mask]      ,       dim=-1)

								A_									= model(
								    _lowercase      ,       attention_mask=_lowercase      ,       encoder_hidden_states=_lowercase      ,       encoder_attention_mask=_lowercase      ,       output_hidden_states=_lowercase      ,       )['hidden_states'][0]
								A_									= model(
								    _lowercase      ,       attention_mask=_lowercase      ,       encoder_hidden_states=_lowercase      ,       encoder_attention_mask=_lowercase      ,       past_key_values=_lowercase      ,       output_hidden_states=_lowercase      ,       )['hidden_states'][0]

								# select random slice
								A_									= ids_tensor((1,)      ,       output_from_past.shape[-1]).item()
								A_									= output_from_no_past[:, -3:, random_slice_idx].detach()
								A_									= 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(_lowercase      ,       _lowercase      ,       atol=1E-3))



					def     __snake_case						(	self     :						List[str])    ->							Dict:
								A_									= self.prepare_config_and_inputs()
								(
								    (
								    A_
								)		, (
								    A_
								)		, (
								    A_
								)		, (
								    A_
								)		, (
								    A_
								)		, (
								    A_
								)		, (
								    A_
								)		, 
								)									= config_and_inputs
								A_									= {'input_ids': input_ids, 'attention_mask': input_mask}
								return config, inputs_dict





@require_torch
class   __UpperCAmelCase				(     lowerCAmelCase			,lowerCAmelCase			,lowerCAmelCase			,unittest.TestCase				):



					'''simple docstring'''





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



					def     __snake_case						(	self     :						Optional[int])    ->							str:
								A_									= FalconModelTester(self)
								A_									= ConfigTester(self      ,       config_class=_lowercase      ,       hidden_size=37)



					def     __snake_case						(	self     :						Optional[int])    ->							Any:
								self.config_tester.run_common_tests()



					def     __snake_case						(	self     :						str)    ->							str:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowercase)



					def     __snake_case						(	self     :						Any)    ->							Optional[int]:
								A_		, *A_									= self.model_tester.prepare_config_and_inputs()
								for alibi in [True, False]:
											A_									= alibi
											self.model_tester.create_and_check_model(_lowercase      ,       *_lowercase)



					def     __snake_case						(	self     :						List[str])    ->							int:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()
								A_									= 3
								A_									= input_dict['input_ids']
								A_									= input_ids.ne(1).to(_lowercase)
								A_									= ids_tensor([self.model_tester.batch_size]      ,       self.model_tester.type_sequence_label_size)
								A_									= FalconForSequenceClassification(_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase      ,       attention_mask=_lowercase      ,       labels=_lowercase)
								self.assertEqual(result.logits.shape      ,       (self.model_tester.batch_size, self.model_tester.num_labels))



					def     __snake_case						(	self     :						int)    ->							Optional[int]:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()
								A_									= 3
								A_									= 'single_label_classification'
								A_									= input_dict['input_ids']
								A_									= input_ids.ne(1).to(_lowercase)
								A_									= ids_tensor([self.model_tester.batch_size]      ,       self.model_tester.type_sequence_label_size)
								A_									= FalconForSequenceClassification(_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase      ,       attention_mask=_lowercase      ,       labels=_lowercase)
								self.assertEqual(result.logits.shape      ,       (self.model_tester.batch_size, self.model_tester.num_labels))



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



					def     __snake_case						(	self     :						int)    ->							Dict:
								# 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_									= self.model_tester.prepare_config_and_inputs_for_common()

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

											A_									= model_class(_lowercase).to(_lowercase)
											if "use_cache" not in inputs:
														A_									= True
											A_									= model(**_lowercase)

											# 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_									= (
											    getattr(_lowercase      ,       'decoder_layers'      ,       _lowercase)
											    or getattr(_lowercase      ,       'num_decoder_layers'      ,       _lowercase)
											    or config.num_hidden_layers
											)
											A_									= getattr(_lowercase      ,       'num_kv_heads'      ,       config.num_attention_heads)
											A_									= getattr(_lowercase      ,       'd_model'      ,       config.hidden_size)
											A_									= embed_dim // num_attention_heads

											A_									= outputs['past_key_values']
											self.assertEqual(len(_lowercase)      ,       _lowercase)

											A_		, A_									= inputs['input_ids'].shape
											for i in range(_lowercase):
														if config.new_decoder_architecture:
																	A_									= config.num_attention_heads
														elif config.multi_query:
																	A_									= 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     __snake_case						(	self     :						List[str])    ->							str:
								A_									= AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b')
								A_									= FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b')
								model.eval()
								model.to(_lowercase)
								A_									= tokenizer('My favorite food is'      ,       return_tensors='pt').to(_lowercase)

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

								A_									= model.generate(**_lowercase      ,       do_sample=_lowercase      ,       max_new_tokens=19)
								A_									= tokenizer.batch_decode(_lowercase)[0]

								self.assertEqual(_lowercase      ,       _lowercase)



					@slow
					def     __snake_case						(	self     :						Union[str, Any])    ->							Union[str, Any]:
								# 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_									= AutoTokenizer.from_pretrained(_lowercase)
											A_									= FalconForCausalLM.from_pretrained(_lowercase)
											model.eval()
											model.to(_lowercase)
											A_									= tokenizer('My favorite food is'      ,       return_tensors='pt').to(_lowercase)

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



					@slow
					def     __snake_case						(	self     :						Any)    ->							List[Any]:
								# 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_									= AutoTokenizer.from_pretrained(_lowercase)
														A_									= FalconForCausalLM.from_pretrained(_lowercase)
														model.eval()
														model.to(device=_lowercase)
														A_									= tokenizer('My favorite food is'      ,       return_tensors='pt').to(_lowercase)

														# Test results are the same with and without cache
														A_									= model.generate(**_lowercase      ,       do_sample=_lowercase      ,       max_new_tokens=20      ,       use_cache=_lowercase)
														A_									= model.generate(**_lowercase      ,       do_sample=_lowercase      ,       max_new_tokens=20      ,       use_cache=_lowercase)
														self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0)










'''simple docstring'''
from PIL import Image





def 			lowerCamelCase(							SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_     )		->		Image:

			def brightness(SCREAMING_SNAKE_CASE_     ) -> float:
						return 128 + level + (c - 128)

			if not -255.0 <= level <= 255.0:
						raise ValueError('level must be between -255.0 (black) and 255.0 (white)'     )
			return img.point(SCREAMING_SNAKE_CASE_     )


if __name__ == "__main__":
							# Load image
							with Image.open("image_data/lena.jpg") as img:
														# Change brightness to 100
														_SCREAMING_SNAKE_CASE			=						change_brightness(img, 1_00)
														brigt_img.save("image_data/lena_brightness.png", format="png")




