






'''simple docstring'''







from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


UpperCamelCase     :   str	      =		logging.get_logger(__name__)




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		_UpperCamelCase						=       ['audio_values', 'audio_mask']






		def __init__(   self ,_lowerCAmelCase=20_48 ,_lowerCAmelCase=1 ,_lowerCAmelCase=[16, 16] ,_lowerCAmelCase=1_28 ,_lowerCAmelCase=4_41_00 ,_lowerCAmelCase=86 ,_lowerCAmelCase=20_48 ,_lowerCAmelCase=0.0 ,**_lowerCAmelCase ,):
								super().__init__(
								    feature_size=_lowerCAmelCase ,sampling_rate=_lowerCAmelCase ,padding_value=_lowerCAmelCase ,**_lowerCAmelCase ,)

								lowerCamelCase__								=	spectrogram_length
								lowerCamelCase__								=	num_channels
								lowerCamelCase__								=	patch_size
								lowerCamelCase__								=	feature_size // self.patch_size[1]
								lowerCamelCase__								=	n_fft
								lowerCamelCase__								=	sampling_rate // hop_length_to_sampling_rate
								lowerCamelCase__								=	sampling_rate
								lowerCamelCase__								=	padding_value
								lowerCamelCase__								=	mel_filter_bank(
								    num_frequency_bins=1 + n_fft // 2 ,num_mel_filters=_lowerCAmelCase ,min_frequency=0.0 ,max_frequency=2_2050.0 ,sampling_rate=_lowerCAmelCase ,norm="""slaney""" ,mel_scale="""slaney""" ,).T






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	spectrogram(
								    _lowerCAmelCase ,window_function(self.n_fft ,"""hann"""							) ,frame_length=self.n_fft ,hop_length=self.hop_length ,power=2.0 ,mel_filters=self.mel_filters.T ,log_mel="""dB""" ,db_range=80.0 ,)
								lowerCamelCase__								=	log_spec[:, :-1]
								lowerCamelCase__								=	log_spec - 20.0
								lowerCamelCase__								=	np.clip(log_spec / 40.0 ,-2.0 ,0.0							) + 1.0
								return log_spec






		def __call__(   self ,_lowerCAmelCase ,_lowerCAmelCase = None ,_lowerCAmelCase = True ,_lowerCAmelCase = None ,_lowerCAmelCase = False ,_lowerCAmelCase = False ,**_lowerCAmelCase ,):

								if sampling_rate is not None:
														if sampling_rate != self.sampling_rate:
																				raise ValueError(
																				    """This feature extractor is set to support sampling rate"""
																				    F''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'''
																				    F''' with {self.sampling_rate} and not {sampling_rate}.'''							)
								else:
														logger.warning(
														    """It is strongly recommended to pass the `sampling_rate` argument to this function. """
														    """Failing to do so can result in silent errors that might be hard to debug."""							)

								lowerCamelCase__								=	isinstance(_lowerCAmelCase ,np.ndarray							) and len(raw_speech.shape							) > 1
								if is_batched_numpy and len(raw_speech.shape							) > 2:
														raise ValueError(F'''Only mono-channel audio is supported for input to {self}'''							)
								lowerCamelCase__								=	is_batched_numpy or (
								    isinstance(_lowerCAmelCase ,(list, tuple)							) and (isinstance(raw_speech[0] ,(np.ndarray, tuple, list)							))
								)
								if is_batched:
														lowerCamelCase__								=	[np.asarray([speech] ,dtype=np.floataa							).T for speech in raw_speech]
								elif not is_batched and not isinstance(_lowerCAmelCase ,np.ndarray							):
														lowerCamelCase__								=	np.asarray(_lowerCAmelCase ,dtype=np.floataa							)
								elif isinstance(_lowerCAmelCase ,np.ndarray							) and raw_speech.dtype is np.dtype(np.floataa							):
														lowerCamelCase__								=	raw_speech.astype(np.floataa							)
								# always return batch
								if not is_batched:
														lowerCamelCase__								=	[np.asarray([raw_speech]							).T]

								# Convert audio signals to log mel spectrograms, truncate by time axis
								lowerCamelCase__								=	[
								    self._np_extract_fbank_features(waveform.squeeze()							).T[: self.spectrogram_length] for waveform in raw_speech
								]
								if isinstance(audio_features[0] ,_lowerCAmelCase							):
														lowerCamelCase__								=	[np.asarray(_lowerCAmelCase ,dtype=np.floataa							) for feature in audio_features]

								# Create audio attention mask
								lowerCamelCase__								=	max(
								    [ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len for feature in audio_features]							)  # The maximum number of audio patches in a batch
								if return_attention_mask:
														lowerCamelCase__								=	[
														    (ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [1]
														    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [0]
														    for feature in audio_features
														]
														lowerCamelCase__								=	np.array(_lowerCAmelCase							).astype(np.floataa							)

								# convert into correct format for padding
								lowerCamelCase__								=	max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
								lowerCamelCase__								=	np.ones([len(_lowerCAmelCase							), 1, max_time_len, self.feature_size]							).astype(np.floataa							)
								lowerCamelCase__								=	padded_audio_features * self.padding_value
								for i in range(len(_lowerCAmelCase							)							):
														lowerCamelCase__								=	audio_features[i]
														lowerCamelCase__								=	feature

								# return as BatchFeature
								if return_attention_mask:
														lowerCamelCase__								=	{"""audio_values""": padded_audio_features, """audio_mask""": audio_mask}
								else:
														lowerCamelCase__								=	{"""audio_values""": padded_audio_features}

								lowerCamelCase__								=	BatchFeature(data=_lowerCAmelCase ,tensor_type=_lowerCAmelCase							)
								return encoded_inputs








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

     lowerCamelCase__       : str   					=						None
     if self.use_input_mask:
         lowerCamelCase__       : Tuple   					=						random_attention_mask([self.batch_size, self.seq_length]      )

     lowerCamelCase__       : Any   					=						None

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

     lowerCamelCase__       : Optional[int]   					=						self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 	A_      ( self    : Any      )     ->      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=UpperCAmelCase     ,	initializer_range=self.initializer_range     ,	pad_token_id=1     ,	new_decoder_architecture=UpperCAmelCase     ,	)
 def 	A_      ( self    : Tuple     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Union[str, Any]      )     ->      Tuple:
     lowerCamelCase__       : Tuple   					=						FalconModel(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase      )
     lowerCamelCase__       : List[Any]   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : List[Any]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Union[str, Any]     ,	)     ->      Optional[Any]:
     lowerCamelCase__       : Optional[int]   					=						True
     lowerCamelCase__       : Optional[Any]   					=						FalconModel(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : str   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	)
     lowerCamelCase__       : Union[str, Any]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	)
     lowerCamelCase__       : Dict   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : str     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : str     ,	)     ->      List[str]:
     lowerCamelCase__       : Optional[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 	A_      ( self    : Union[str, Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[Any]     ,	)     ->      List[str]:
     lowerCamelCase__       : int   					=						True
     lowerCamelCase__       : Optional[int]   					=						True
     lowerCamelCase__       : str   					=						FalconForCausalLM(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

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

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

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

     lowerCamelCase__       : Union[str, Any]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	output_hidden_states=UpperCAmelCase     ,	)['hidden_states'][0]
     lowerCamelCase__       : Tuple   					=						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__       : Dict   					=						ids_tensor((1,)     ,	output_from_past.shape[-1]      ).item()
     lowerCamelCase__       : List[Any]   					=						output_from_no_past[:, -3:, random_slice_idx].detach()
     lowerCamelCase__       : Dict   					=						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 	A_      ( self    : Optional[Any]      )     ->      Optional[int]:
     lowerCamelCase__       : Any   					=						self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				
     )       : Union[str, Any]   					=						config_and_inputs
     lowerCamelCase__       : List[str]   					=						{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict


@require_torch
class   lowerCAmelCase				(   __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    unittest.TestCase     ):
 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 	A_      ( self    : List[str]      )     ->      List[Any]:
     lowerCamelCase__       : Union[str, Any]   					=						FalconModelTester(self      )
     lowerCamelCase__       : List[Any]   					=						ConfigTester(self     ,	config_class=UpperCAmelCase     ,	hidden_size=37      )
 def 	A_      ( self    : Optional[int]      )     ->      int:
     self.config_tester.run_common_tests()
 def 	A_      ( self    : Dict      )     ->      Dict:
     lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase      )
 def 	A_      ( self    : List[str]      )     ->      int:
     lowerCamelCase__					,				*lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
         lowerCamelCase__       : List[Any]   					=						alibi
         self.model_tester.create_and_check_model(UpperCAmelCase     ,	*UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]      )     ->      Optional[int]:
     lowerCamelCase__					,				lowerCamelCase__       : Any   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : int   					=						3
     lowerCamelCase__       : List[str]   					=						input_dict['input_ids']
     lowerCamelCase__       : Dict   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : Optional[Any]   					=						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[str]   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.assertEqual(result.logits.shape     ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )
 def 	A_      ( self    : Optional[Any]      )     ->      Optional[Any]:
     lowerCamelCase__					,				lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : Tuple   					=						3
     lowerCamelCase__       : List[str]   					=						'single_label_classification'
     lowerCamelCase__       : Optional[int]   					=						input_dict['input_ids']
     lowerCamelCase__       : Union[str, Any]   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : List[Any]   					=						ids_tensor([self.model_tester.batch_size]     ,	self.model_tester.type_sequence_label_size      )
     lowerCamelCase__       : Union[str, 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 	A_      ( self    : Any      )     ->      Union[str, Any]:
     lowerCamelCase__					,				lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : List[str]   					=						input_dict['input_ids']
     lowerCamelCase__       : Optional[Any]   					=						FalconForCausalLM(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : Any   					=						model(UpperCAmelCase     ,	use_cache=UpperCAmelCase      )
     lowerCamelCase__       : int   					=						input_ids.shape[0]
     lowerCamelCase__       : str   					=						model._convert_to_rw_cache(result.past_key_values      )
     lowerCamelCase__       : Optional[Any]   					=						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 	A_      ( self    : List[str]      )     ->      str:
     lowerCamelCase__					,				lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : Optional[Any]   					=						3
     lowerCamelCase__       : List[str]   					=						'multi_label_classification'
     lowerCamelCase__       : Optional[int]   					=						input_dict['input_ids']
     lowerCamelCase__       : Tuple   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : int   					=						ids_tensor(
         [self.model_tester.batch_size, config.num_labels]     ,	self.model_tester.type_sequence_label_size      ).to(torch.float      )
     lowerCamelCase__       : 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 	A_      ( self    : Any      )     ->      Tuple:
     # 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__       : Dict   					=						model_class(UpperCAmelCase      ).to(UpperCAmelCase      )
         if "use_cache" not in inputs:
             lowerCamelCase__       : Optional[Any]   					=						True
         lowerCamelCase__       : Any   					=						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__       : Any   					=						(
             getattr(UpperCAmelCase     ,	'decoder_layers'     ,	UpperCAmelCase      )
             or getattr(UpperCAmelCase     ,	'num_decoder_layers'     ,	UpperCAmelCase      )
             or config.num_hidden_layers
         )
         lowerCamelCase__       : Dict   					=						getattr(UpperCAmelCase     ,	'num_kv_heads'     ,	config.num_attention_heads      )
         lowerCamelCase__       : Dict   					=						getattr(UpperCAmelCase     ,	'd_model'     ,	config.hidden_size      )
         lowerCamelCase__       : str   					=						embed_dim // num_attention_heads

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

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

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

     lowerCamelCase__       : Optional[Any]   					=						model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=19      )
     lowerCamelCase__       : Union[str, Any]   					=						tokenizer.batch_decode(UpperCAmelCase      )[0]

     self.assertEqual(UpperCAmelCase     ,	UpperCAmelCase      )
 @slow
 def 	A_      ( self    : Any      )     ->      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"]:
         lowerCamelCase__       : str   					=						AutoTokenizer.from_pretrained(UpperCAmelCase      )
         lowerCamelCase__       : int   					=						FalconForCausalLM.from_pretrained(UpperCAmelCase      )
         model.eval()
         model.to(UpperCAmelCase      )
         lowerCamelCase__       : Tuple   					=						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 	A_      ( self    : Union[str, Any]      )     ->      str:
     # 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__       : Dict   					=						AutoTokenizer.from_pretrained(UpperCAmelCase      )
             lowerCamelCase__       : Tuple   					=						FalconForCausalLM.from_pretrained(UpperCAmelCase      )
             model.eval()
             model.to(device=UpperCAmelCase      )
             lowerCamelCase__       : Any   					=						tokenizer('My favorite food is'     ,	return_tensors='pt'      ).to(UpperCAmelCase      )

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





