




"""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 _lowerCamelCase    :






   def __init__(		self   ,     UpperCamelCase   ,     UpperCamelCase=3   ,     UpperCamelCase=7   ,     UpperCamelCase=True   ,     UpperCamelCase=True   ,     UpperCamelCase=False   ,     UpperCamelCase=True   ,     UpperCamelCase=99   ,     UpperCamelCase=32   ,     UpperCamelCase=5   ,     UpperCamelCase=4   ,     UpperCamelCase=37   ,     UpperCamelCase="gelu"   ,     UpperCamelCase=0.1   ,     UpperCamelCase=0.1   ,     UpperCamelCase=5_12   ,     UpperCamelCase=16   ,     UpperCamelCase=2   ,     UpperCamelCase=0.02   ,     UpperCamelCase=3   ,     UpperCamelCase=4   ,     UpperCamelCase=None   ,     )			->						str:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	parent
         lowerCAmelCase__      :			Any   =	batch_size
         lowerCAmelCase__      :			List[str]   =	seq_length
         lowerCAmelCase__      :			Union[str, Any]   =	is_training
         lowerCAmelCase__      :			Optional[int]   =	use_input_mask
         lowerCAmelCase__      :			List[str]   =	use_token_type_ids
         lowerCAmelCase__      :			Optional[int]   =	use_labels
         lowerCAmelCase__      :			List[str]   =	vocab_size
         lowerCAmelCase__      :			Tuple   =	hidden_size
         lowerCAmelCase__      :			List[str]   =	num_hidden_layers
         lowerCAmelCase__      :			List[str]   =	num_attention_heads
         lowerCAmelCase__      :			Union[str, Any]   =	intermediate_size
         lowerCAmelCase__      :			Any   =	hidden_act
         lowerCAmelCase__      :			str   =	hidden_dropout_prob
         lowerCAmelCase__      :			Tuple   =	attention_probs_dropout_prob
         lowerCAmelCase__      :			Optional[int]   =	max_position_embeddings
         lowerCAmelCase__      :			Any   =	type_vocab_size
         lowerCAmelCase__      :			str   =	type_sequence_label_size
         lowerCAmelCase__      :			Any   =	initializer_range
         lowerCAmelCase__      :			Optional[int]   =	num_labels
         lowerCAmelCase__      :			str   =	num_choices
         lowerCAmelCase__      :			List[Any]   =	scope






   def 	_lowerCAmelCase   (		self   )			->						List[str]:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size   )

         lowerCAmelCase__      :			List[Any]   =	None
         if self.use_input_mask:
               lowerCAmelCase__      :			List[str]   =	random_attention_mask([self.batch_size, self.seq_length]   )

         lowerCAmelCase__      :			Dict   =	None

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

         lowerCAmelCase__      :			List[Any]   =	self.get_config()

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






   def 	_lowerCAmelCase   (		self   )			->						List[str]:

         """simple docstring"""
         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=UpperCAmelCase__   ,     initializer_range=self.initializer_range   ,     pad_token_id=1   ,     new_decoder_architecture=UpperCAmelCase__   ,     )






   def 	_lowerCAmelCase   (		self   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	FalconModel(config=UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			Any   =	model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   )
         lowerCAmelCase__      :			List[str]   =	model(UpperCAmelCase__   )
         self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)   )






   def 	_lowerCAmelCase   (		self   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	True
         lowerCAmelCase__      :			Optional[Any]   =	FalconModel(UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			Tuple   =	model(
             UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     )
         lowerCAmelCase__      :			Tuple   =	model(
             UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     )
         lowerCAmelCase__      :			Tuple   =	model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   )
         self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)   )






   def 	_lowerCAmelCase   (		self   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     )			->						List[str]:

         """simple docstring"""
         lowerCAmelCase__      :			Any   =	FalconForCausalLM(config=UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			int   =	model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     labels=UpperCAmelCase__   )
         self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)   )






   def 	_lowerCAmelCase   (		self   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     UpperCamelCase   ,     )			->						List[str]:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	True
         lowerCAmelCase__      :			Optional[int]   =	True
         lowerCAmelCase__      :			str   =	FalconForCausalLM(config=UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()

         # first forward pass
         lowerCAmelCase__      :			Optional[Any]   =	model(
             UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     use_cache=UpperCAmelCase__   ,     )
         lowerCAmelCase__      :			Any   =	outputs.past_key_values

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

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

         lowerCAmelCase__      :			Optional[int]   =	model(
             UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     output_hidden_states=UpperCAmelCase__   ,     )["""hidden_states"""][0]
         lowerCAmelCase__      :			str   =	model(
             UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     past_key_values=UpperCAmelCase__   ,     output_hidden_states=UpperCAmelCase__   ,     )["""hidden_states"""][0]

         # select random slice
         lowerCAmelCase__      :			List[str]   =	ids_tensor((1,)   ,     output_from_past.shape[-1]   ).item()
         lowerCAmelCase__      :			Optional[Any]   =	output_from_no_past[:, -3:, random_slice_idx].detach()
         lowerCAmelCase__      :			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(UpperCAmelCase__   ,     UpperCAmelCase__   ,     atol=1E-3   )   )






   def 	_lowerCAmelCase   (		self   )			->						List[str]:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	self.prepare_config_and_inputs()
         (
             (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,   (
             lowerCAmelCase__  
         ) ,     
         )      :			Union[str, Any]   =	config_and_inputs
         lowerCAmelCase__      :			List[Any]   =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
         return config, inputs_dict






@require_torch
class _lowerCamelCase    (					__lowercase       ,	__lowercase       ,	__lowercase       ,	unittest.TestCase						):
   _lowerCamelCase    :Tuple     				=     (
       (
           FalconModel,
           FalconForCausalLM,
           FalconForSequenceClassification,
           FalconForTokenClassification,
           FalconForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _lowerCamelCase    :Dict     				=     (FalconForCausalLM,) if is_torch_available() else ()
   _lowerCamelCase    :int     				=     (
       {
           '''feature-extraction''': FalconModel,
           '''text-classification''': FalconForSequenceClassification,
           '''text-generation''': FalconForCausalLM,
           '''question-answering''': FalconForQuestionAnswering,
           '''token-classification''': FalconForTokenClassification,
           '''zero-shot''': FalconForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _lowerCamelCase    :int     				=     False
   _lowerCamelCase    :Union[str, Any]     				=     False






   def 	_lowerCAmelCase   (		self   )			->						Optional[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	FalconModelTester(self   )
         lowerCAmelCase__      :			Union[str, Any]   =	ConfigTester(self   ,     config_class=UpperCAmelCase__   ,     hidden_size=37   )






   def 	_lowerCAmelCase   (		self   )			->						Optional[Any]:

         """simple docstring"""
         self.config_tester.run_common_tests()






   def 	_lowerCAmelCase   (		self   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCAmelCase__   )






   def 	_lowerCAmelCase   (		self   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__ ,   *lowerCAmelCase__      :			Tuple   =	self.model_tester.prepare_config_and_inputs()
         for alibi in [True, False]:
               lowerCAmelCase__      :			Union[str, Any]   =	alibi
               self.model_tester.create_and_check_model(UpperCAmelCase__   ,     *UpperCAmelCase__   )






   def 	_lowerCAmelCase   (		self   )			->						Union[str, Any]:

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






   def 	_lowerCAmelCase   (		self   )			->						Dict:

         """simple docstring"""
         lowerCAmelCase__ ,   lowerCAmelCase__      :			int   =	self.model_tester.prepare_config_and_inputs_for_common()
         lowerCAmelCase__      :			Optional[Any]   =	3
         lowerCAmelCase__      :			List[Any]   =	"""single_label_classification"""
         lowerCAmelCase__      :			Dict   =	input_dict["""input_ids"""]
         lowerCAmelCase__      :			int   =	input_ids.ne(1   ).to(UpperCAmelCase__   )
         lowerCAmelCase__      :			Tuple   =	ids_tensor([self.model_tester.batch_size]   ,     self.model_tester.type_sequence_label_size   )
         lowerCAmelCase__      :			int   =	FalconForSequenceClassification(UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			List[Any]   =	model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     labels=UpperCAmelCase__   )
         self.assertEqual(result.logits.shape   ,     (self.model_tester.batch_size, self.model_tester.num_labels)   )






   def 	_lowerCAmelCase   (		self   )			->						int:

         """simple docstring"""
         lowerCAmelCase__ ,   lowerCAmelCase__      :			Any   =	self.model_tester.prepare_config_and_inputs_for_common()
         lowerCAmelCase__      :			Optional[int]   =	input_dict["""input_ids"""]
         lowerCAmelCase__      :			Union[str, Any]   =	FalconForCausalLM(UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			Optional[int]   =	model(UpperCAmelCase__   ,     use_cache=UpperCAmelCase__   )
         lowerCAmelCase__      :			int   =	input_ids.shape[0]
         lowerCAmelCase__      :			List[Any]   =	model._convert_to_rw_cache(result.past_key_values   )
         lowerCAmelCase__      :			Optional[int]   =	model._convert_cache_to_standard_format(UpperCAmelCase__   ,     UpperCAmelCase__   )
         for layer in range(len(UpperCAmelCase__   )   ):
               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 	_lowerCAmelCase   (		self   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__ ,   lowerCAmelCase__      :			Optional[Any]   =	self.model_tester.prepare_config_and_inputs_for_common()
         lowerCAmelCase__      :			List[str]   =	3
         lowerCAmelCase__      :			str   =	"""multi_label_classification"""
         lowerCAmelCase__      :			List[Any]   =	input_dict["""input_ids"""]
         lowerCAmelCase__      :			Tuple   =	input_ids.ne(1   ).to(UpperCAmelCase__   )
         lowerCAmelCase__      :			Tuple   =	ids_tensor(
             [self.model_tester.batch_size, config.num_labels]   ,     self.model_tester.type_sequence_label_size   ).to(torch.float   )
         lowerCAmelCase__      :			List[Any]   =	FalconForSequenceClassification(UpperCAmelCase__   )
         model.to(UpperCAmelCase__   )
         model.eval()
         lowerCAmelCase__      :			Any   =	model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     labels=UpperCAmelCase__   )
         self.assertEqual(result.logits.shape   ,     (self.model_tester.batch_size, self.model_tester.num_labels)   )






   def 	_lowerCAmelCase   (		self   )			->						Union[str, Any]:

         """simple docstring"""
         # 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:
               lowerCAmelCase__ ,   lowerCAmelCase__      :			Any   =	self.model_tester.prepare_config_and_inputs_for_common()

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

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

               # 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

               lowerCAmelCase__      :			Optional[Any]   =	(
                   getattr(UpperCAmelCase__   ,     """decoder_layers"""   ,     UpperCAmelCase__   )
                   or getattr(UpperCAmelCase__   ,     """num_decoder_layers"""   ,     UpperCAmelCase__   )
                   or config.num_hidden_layers
               )
               lowerCAmelCase__      :			Optional[int]   =	getattr(UpperCAmelCase__   ,     """num_kv_heads"""   ,     config.num_attention_heads   )
               lowerCAmelCase__      :			Tuple   =	getattr(UpperCAmelCase__   ,     """d_model"""   ,     config.hidden_size   )
               lowerCAmelCase__      :			Any   =	embed_dim // num_attention_heads

               lowerCAmelCase__      :			Tuple   =	outputs["""past_key_values"""]
               self.assertEqual(len(UpperCAmelCase__   )   ,     UpperCAmelCase__   )

               lowerCAmelCase__ ,   lowerCAmelCase__      :			int   =	inputs["""input_ids"""].shape
               for i in range(UpperCAmelCase__   ):
                     if config.new_decoder_architecture:
                           lowerCAmelCase__      :			Tuple   =	config.num_attention_heads
                     elif config.multi_query:
                           lowerCAmelCase__      :			Optional[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 _lowerCamelCase    (					unittest.TestCase						):






   @slow
   def 	_lowerCAmelCase   (		self   )			->						Dict:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""   )
         lowerCAmelCase__      :			List[Any]   =	FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""   )
         model.eval()
         model.to(UpperCAmelCase__   )
         lowerCAmelCase__      :			Tuple   =	tokenizer("""My favorite food is"""   ,     return_tensors="""pt"""   ).to(UpperCAmelCase__   )

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

         lowerCAmelCase__      :			List[str]   =	model.generate(**UpperCAmelCase__   ,     do_sample=UpperCAmelCase__   ,     max_new_tokens=19   )
         lowerCAmelCase__      :			Optional[Any]   =	tokenizer.batch_decode(UpperCAmelCase__   )[0]

         self.assertEqual(UpperCAmelCase__   ,     UpperCAmelCase__   )






   @slow
   def 	_lowerCAmelCase   (		self   )			->						Dict:

         """simple docstring"""
         # 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"]:
               lowerCAmelCase__      :			int   =	AutoTokenizer.from_pretrained(UpperCAmelCase__   )
               lowerCAmelCase__      :			Union[str, Any]   =	FalconForCausalLM.from_pretrained(UpperCAmelCase__   )
               model.eval()
               model.to(UpperCAmelCase__   )
               lowerCAmelCase__      :			List[Any]   =	tokenizer("""My favorite food is"""   ,     return_tensors="""pt"""   ).to(UpperCAmelCase__   )

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






   @slow
   def 	_lowerCAmelCase   (		self   )			->						Dict:

         """simple docstring"""
         # 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",
               ]:
                     lowerCAmelCase__      :			int   =	AutoTokenizer.from_pretrained(UpperCAmelCase__   )
                     lowerCAmelCase__      :			Union[str, Any]   =	FalconForCausalLM.from_pretrained(UpperCAmelCase__   )
                     model.eval()
                     model.to(device=UpperCAmelCase__   )
                     lowerCAmelCase__      :			Tuple   =	tokenizer("""My favorite food is"""   ,     return_tensors="""pt"""   ).to(UpperCAmelCase__   )

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











"""simple docstring"""


import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)


class _lowerCamelCase    (					unittest.TestCase						):






   def 	_lowerCAmelCase   (		self       :		Dict   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         self.assertEqual(x.component(0   )   ,     1   )
         self.assertEqual(x.component(2   )   ,     3   )
         lowerCAmelCase__      :			Union[str, Any]   =	Vector()






   def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Vector([0, 0, 0, 0, 0, 1]   )
         self.assertEqual(str(UpperCamelCase   )   ,     """(0,0,0,0,0,1)"""   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	Vector([1, 2, 3, 4]   )
         self.assertEqual(len(UpperCamelCase   )   ,     4   )






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Vector([1, 2]   )
         lowerCAmelCase__      :			Optional[int]   =	Vector([1, 2, 3, 4, 5]   )
         lowerCAmelCase__      :			Union[str, Any]   =	Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]   )
         lowerCAmelCase__      :			List[Any]   =	Vector([1, -1, 1, -1, 2, -3, 4, -5]   )
         self.assertAlmostEqual(x.euclidean_length()   ,     2.236   ,     3   )
         self.assertAlmostEqual(y.euclidean_length()   ,     7.416   ,     3   )
         self.assertEqual(z.euclidean_length()   ,     0   )
         self.assertAlmostEqual(w.euclidean_length()   ,     7.616   ,     3   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Optional[Any]   =	Vector([1, 1, 1]   )
         self.assertEqual((x + y).component(0   )   ,     2   )
         self.assertEqual((x + y).component(1   )   ,     3   )
         self.assertEqual((x + y).component(2   )   ,     4   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Dict   =	Vector([1, 1, 1]   )
         self.assertEqual((x - y).component(0   )   ,     0   )
         self.assertEqual((x - y).component(1   )   ,     1   )
         self.assertEqual((x - y).component(2   )   ,     2   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Any   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Any   =	Vector([2, -1, 4]   )  # for test of dot product
         lowerCAmelCase__      :			Any   =	Vector([1, -2, -1]   )
         self.assertEqual(str(x * 3.0   )   ,     """(3.0,6.0,9.0)"""   )
         self.assertEqual((a * b)   ,     0   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         self.assertEqual(str(zero_vector(10   )   ).count("""0"""   )   ,     10   )






   def 	_lowerCAmelCase   (		self       :		Tuple   )			->						None:

         """simple docstring"""
         self.assertEqual(str(unit_basis_vector(3   ,     1   )   )   ,     """(0,1,0)"""   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Optional[Any]   =	Vector([1, 0, 1]   )
         self.assertEqual(str(axpy(2   ,     UpperCamelCase   ,     UpperCamelCase   )   )   ,     """(3,4,7)"""   )






   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 0, 0, 0, 0, 0]   )
         lowerCAmelCase__      :			Any   =	x.copy()
         self.assertEqual(str(UpperCamelCase   )   ,     str(UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 0, 0]   )
         x.change_component(0   ,     0   )
         x.change_component(1   ,     1   )
         self.assertEqual(str(UpperCamelCase   )   ,     """(0,1,0)"""   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""   ,     str(UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Dict   =	[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
         for x in range(a.height()   ):
               for y in range(a.width()   ):
                     self.assertEqual(minors[x][y]   ,     a.minor(UpperCamelCase   ,     UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			int   =	[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
         for x in range(a.height()   ):
               for y in range(a.width()   ):
                     self.assertEqual(cofactors[x][y]   ,     a.cofactor(UpperCamelCase   ,     UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual(-5   ,     a.determinant()   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         self.assertEqual("""(14,32,50)"""   ,     str(a * x   )   )
         self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""   ,     str(a * 2   )   )






   def 	_lowerCAmelCase   (		self       :		str   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         a.change_component(0   ,     2   ,     5   )
         self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""   ,     str(UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		Tuple   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual(7   ,     a.component(2   ,     1   )   ,     0.01   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			List[Any]   =	Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]   ,     3   ,     3   )
         self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""   ,     str(a + b   )   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Dict   =	Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]   ,     3   ,     3   )
         self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""   ,     str(a - b   )   )






   def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						None:

         """simple docstring"""
         self.assertEqual(
             """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""   ,     str(square_zero_matrix(5   )   )   ,     )


if __name__ == "__main__":
  unittest.main()





