


from __future__ import annotations

from typing import Any


class     __UpperCAmelCase							:


     """simple docstring"""


     def __init__(	self  ,       __A  ,       __A  ,       __A = 0    ):
        __a	,    __a					=  row, column
        __a					=  [[default_value for c in range(__A    )] for r in range(__A    )]


     def __str__(	self    ):
        __a					=  f'''Matrix consist of {self.row} rows and {self.column} columns\n'''

        # Make string identifier
        __a					=  0
        for row_vector in self.array:
           for obj in row_vector:
              __a					=  max(__A  ,       len(str(__A    )    )    )
        __a					=  f'''%{max_element_length}s'''

        # Make string and return
        def single_line(__A    ) -> str:
           nonlocal string_format_identifier
           __a					=  """["""
           line += ", ".join(string_format_identifier % (obj,) for obj in row_vector    )
           line += "]"
           return line

        s += "\n".join(single_line(__A    ) for row_vector in self.array    )
        return s


     def __repr__(	self    ):
        return str(self    )


     def 	snake_case_  (	self  ,       __A    ):
        if not (isinstance(__A  ,       (list, tuple)    ) and len(__A    ) == 2):
           return False
        elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column):
           return False
        else:
           return True


     def __getitem__(	self  ,       __A    ):
        assert self.validate_indicies(__A    )
        return self.array[loc[0]][loc[1]]


     def __setitem__(	self  ,       __A  ,       __A    ):
        assert self.validate_indicies(__A    )
        __a					=  value


     def __add__(	self  ,       __A    ):
        assert isinstance(__A  ,       __A    )
        assert self.row == another.row and self.column == another.column

        # Add
        __a					=  Matrix(self.row  ,       self.column    )
        for r in range(self.row    ):
           for c in range(self.column    ):
              __a					=  self[r, c] + another[r, c]
        return result


     def __neg__(	self    ):
        __a					=  Matrix(self.row  ,       self.column    )
        for r in range(self.row    ):
           for c in range(self.column    ):
              __a					=  -self[r, c]
        return result


     def __sub__(	self  ,       __A    ):
        return self + (-another)


     def __mul__(	self  ,       __A    ):

        if isinstance(__A  ,       (int, float)    ):  # Scalar multiplication
           __a					=  Matrix(self.row  ,       self.column    )
           for r in range(self.row    ):
              for c in range(self.column    ):
                 __a					=  self[r, c] * another
           return result
        elif isinstance(__A  ,       __A    ):  # Matrix multiplication
           assert self.column == another.row
           __a					=  Matrix(self.row  ,       another.column    )
           for r in range(self.row    ):
              for c in range(another.column    ):
                 for i in range(self.column    ):
                    result[r, c] += self[r, i] * another[i, c]
           return result
        else:
           __a					=  f'''Unsupported type given for another ({type(__A    )})'''
           raise TypeError(__A    )


     def 	snake_case_  (	self    ):
        __a					=  Matrix(self.column  ,       self.row    )
        for r in range(self.row    ):
           for c in range(self.column    ):
              __a					=  self[r, c]
        return result


     def 	snake_case_  (	self  ,       __A  ,       __A    ):
        assert isinstance(__A  ,       __A    ) and isinstance(__A  ,       __A    )
        assert self.row == self.column == u.row == v.row  # u, v should be column vector
        assert u.column == v.column == 1  # u, v should be column vector

        # Calculate
        __a					=  v.transpose()
        __a					=  (v_t * self * u)[0, 0] + 1
        if numerator_factor == 0:
           return None  # It's not invertable
        return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))


# Testing
if __name__ == "__main__":




  def  a   ():
     # a^(-1)
     __a					=  Matrix(3		,				3		,				0   )
     for i in range(3   ):
        __a					=  1
     print(f'''a^(-1) is {ainv}'''   )
     # u, v
     __a					=  Matrix(3		,				1		,				0   )
     __a	,    __a	,    __a					=  1, 2, -3
     __a					=  Matrix(3		,				1		,				0   )
     __a	,    __a	,    __a					=  4, -2, 5
     print(f'''u is {u}'''   )
     print(f'''v is {v}'''   )
     print(f'''uv^T is {u * v.transpose()}'''   )
     # Sherman Morrison
     print(f'''(a + uv^T)^(-1) is {ainv.sherman_morrison(lowerCAmelCase__		,				lowerCAmelCase__   )}'''   )




  def  a   ():
     import doctest

     doctest.testmod()

  testa()











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						:

  def __init__(    self						:	int   ,				lowerCamelCase__						:	List[str]   ,				lowerCamelCase__						:	Union[str, Any]=3   ,				lowerCamelCase__						:	Optional[int]=7   ,				lowerCamelCase__						:	Optional[int]=True   ,				lowerCamelCase__						:	str=True   ,				lowerCamelCase__						:	Optional[int]=False   ,				lowerCamelCase__						:	Any=True   ,				lowerCamelCase__						:	int=99   ,				lowerCamelCase__						:	Optional[Any]=32   ,				lowerCamelCase__						:	Dict=5   ,				lowerCamelCase__						:	str=4   ,				lowerCamelCase__						:	Dict=37   ,				lowerCamelCase__						:	Union[str, Any]="gelu"   ,				lowerCamelCase__						:	Tuple=0.1   ,				lowerCamelCase__						:	str=0.1   ,				lowerCamelCase__						:	str=5_12   ,				lowerCamelCase__						:	int=16   ,				lowerCamelCase__						:	Optional[int]=2   ,				lowerCamelCase__						:	Any=0.02   ,				lowerCamelCase__						:	Any=3   ,				lowerCamelCase__						:	Tuple=4   ,				lowerCamelCase__						:	Any=None   ,				):
     """simple docstring"""

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

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size			)

     __UpperCamelCase	:			Tuple					=					None
     if self.use_input_mask:
        __UpperCamelCase	:			Optional[int]					=					random_attention_mask([self.batch_size, self.seq_length]			)

     __UpperCamelCase	:			Optional[Any]					=					None

     __UpperCamelCase	:			Optional[Any]					=					None
     __UpperCamelCase	:			List[Any]					=					None
     __UpperCamelCase	:			Dict					=					None
     if self.use_labels:
        __UpperCamelCase	:			List[Any]					=					ids_tensor([self.batch_size]   ,				self.type_sequence_label_size			)
        __UpperCamelCase	:			Tuple					=					ids_tensor([self.batch_size, self.seq_length]   ,				self.num_labels			)
        __UpperCamelCase	:			List[Any]					=					ids_tensor([self.batch_size]   ,				self.num_choices			)

     __UpperCamelCase	:			str					=					self.get_config()

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

  def    a					(    self						:	Dict			):
     """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=lowerCamelCase__   ,				initializer_range=self.initializer_range   ,				pad_token_id=1   ,				new_decoder_architecture=lowerCamelCase__   ,				)

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	int			):
     """simple docstring"""

     __UpperCamelCase	:			str					=					FalconModel(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			List[Any]					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			)
     __UpperCamelCase	:			Tuple					=					model(lowerCamelCase__			)
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)			)

  def    a					(    self						:	Any   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Optional[Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	int   ,				):
     """simple docstring"""

     __UpperCamelCase	:			str					=					True
     __UpperCamelCase	:			List[str]					=					FalconModel(lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			Dict					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				)
     __UpperCamelCase	:			Any					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				)
     __UpperCamelCase	:			Any					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			)
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)			)

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	List[str]   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	int   ,				):
     """simple docstring"""

     __UpperCamelCase	:			int					=					FalconForCausalLM(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			Optional[Any]					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				labels=lowerCamelCase__			)
     self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)			)

  def    a					(    self						:	Optional[Any]   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	int   ,				lowerCamelCase__						:	List[str]   ,				):
     """simple docstring"""

     __UpperCamelCase	:			List[str]					=					True
     __UpperCamelCase	:			List[Any]					=					True
     __UpperCamelCase	:			Dict					=					FalconForCausalLM(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()

     # first forward pass
     __UpperCamelCase	:			Union[str, Any]					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				use_cache=lowerCamelCase__   ,				)
     __UpperCamelCase	:			List[Any]					=					outputs.past_key_values

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

     # append to next input_ids and
     __UpperCamelCase	:			Tuple					=					torch.cat([input_ids, next_tokens]   ,				dim=-1			)
     __UpperCamelCase	:			Union[str, Any]					=					torch.cat([input_mask, next_mask]   ,				dim=-1			)

     __UpperCamelCase	:			Optional[int]					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				output_hidden_states=lowerCamelCase__   ,				)["""hidden_states"""][0]
     __UpperCamelCase	:			Union[str, Any]					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				past_key_values=lowerCamelCase__   ,				output_hidden_states=lowerCamelCase__   ,				)["""hidden_states"""][0]

     # select random slice
     __UpperCamelCase	:			Dict					=					ids_tensor((1,)   ,				output_from_past.shape[-1]			).item()
     __UpperCamelCase	:			Dict					=					output_from_no_past[:, -3:, random_slice_idx].detach()
     __UpperCamelCase	:			Union[str, 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(lowerCamelCase__   ,				lowerCamelCase__   ,				atol=1e-3			)			)

  def    a					(    self						:	Union[str, Any]			):
     """simple docstring"""

     __UpperCamelCase	:			List[Any]					=					self.prepare_config_and_inputs()
     (
         (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     (
         __UpperCamelCase
     )	,     
     )	:			Optional[Any]					=					config_and_inputs
     __UpperCamelCase	:			Any					=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
     return config, inputs_dict







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

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     __UpperCamelCase	:			Dict					=					FalconModelTester(self			)
     __UpperCamelCase	:			Dict					=					ConfigTester(self   ,				config_class=lowerCamelCase__   ,				hidden_size=37			)

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     self.config_tester.run_common_tests()

  def    a					(    self						:	Union[str, Any]			):
     """simple docstring"""

     __UpperCamelCase	:			List[Any]					=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCamelCase__			)

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     *__UpperCamelCase	:			List[Any]					=					self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
        __UpperCamelCase	:			List[str]					=					alibi
        self.model_tester.create_and_check_model(lowerCamelCase__   ,				*lowerCamelCase__			)

  def    a					(    self						:	int			):
     """simple docstring"""

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

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

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

  def    a					(    self						:	List[Any]			):
     """simple docstring"""

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

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

  def    a					(    self						:	int			):
     """simple docstring"""

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	,     __UpperCamelCase	:			Union[str, Any]					=					self.model_tester.prepare_config_and_inputs_for_common()

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

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

        # 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

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

        __UpperCamelCase	:			List[Any]					=					outputs["""past_key_values"""]
        self.assertEqual(len(lowerCamelCase__			)   ,				lowerCamelCase__			)

        __UpperCamelCase	,     __UpperCamelCase	:			Dict					=					inputs["""input_ids"""].shape
        for i in range(lowerCamelCase__			):
           if config.new_decoder_architecture:
              __UpperCamelCase	:			List[Any]					=					config.num_attention_heads
           elif config.multi_query:
              __UpperCamelCase	:			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 ):

  @slow
  def    a					(    self						:	Optional[int]			):
     """simple docstring"""

     __UpperCamelCase	:			int					=					AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""			)
     __UpperCamelCase	:			Optional[int]					=					FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""			)
     model.eval()
     model.to(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

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

     __UpperCamelCase	:			Tuple					=					model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=19			)
     __UpperCamelCase	:			Dict					=					tokenizer.batch_decode(lowerCamelCase__			)[0]

     self.assertEqual(lowerCamelCase__   ,				lowerCamelCase__			)

  @slow
  def    a					(    self						:	Dict			):
     """simple docstring"""

     for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
        __UpperCamelCase	:			Dict					=					AutoTokenizer.from_pretrained(lowerCamelCase__			)
        __UpperCamelCase	:			Union[str, Any]					=					FalconForCausalLM.from_pretrained(lowerCamelCase__			)
        model.eval()
        model.to(lowerCamelCase__			)
        __UpperCamelCase	:			Dict					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

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

  @slow
  def    a					(    self						:	Tuple			):
     """simple docstring"""

     with torch.no_grad():
        for repo in [
            "Rocketknight1/falcon-rw-1b",
            "Rocketknight1/tiny-random-falcon-7b",
            "Rocketknight1/tiny-random-falcon-40b",
        ]:
           __UpperCamelCase	:			List[str]					=					AutoTokenizer.from_pretrained(lowerCamelCase__			)
           __UpperCamelCase	:			List[str]					=					FalconForCausalLM.from_pretrained(lowerCamelCase__			)
           model.eval()
           model.to(device=lowerCamelCase__			)
           __UpperCamelCase	:			Optional[int]					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

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



