





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__:






       '''simple docstring'''


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




       def     UpperCamelCase_			(      self							)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size							)

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

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							None

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

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

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




       def     UpperCamelCase_			(      self							)			->     Dict:
            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     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)			->     List[Any]:
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							FalconModel(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							model(__lowerCamelCase							)
            self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size)							)




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




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						)			->     Optional[int]:
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							FalconForCausalLM(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						labels=__lowerCamelCase							)
            self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)							)




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							True
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							True
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							FalconForCausalLM(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()

            # first forward pass
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							model(
                __lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						encoder_hidden_states=__lowerCamelCase    ,						encoder_attention_mask=__lowerCamelCase    ,						use_cache=__lowerCamelCase    ,						)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							outputs.past_key_values

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

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

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





@require_torch
class 			lowerCAmelCase__( __lowercase  ,     __lowercase  ,     __lowercase  ,     unittest.TestCase    ):






       '''simple docstring'''


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




       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	str						    =							FalconModelTester(self							)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							ConfigTester(self    ,						config_class=__lowerCamelCase    ,						hidden_size=3_7							)




       def     UpperCamelCase_			(      self							)			->     Dict:
            self.config_tester.run_common_tests()




       def     UpperCamelCase_			(      self							)			->     Optional[int]:
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     int:
            _SCREAMING_SNAKE_CASE    ,			*_SCREAMING_SNAKE_CASE       :	Dict						    =							self.model_tester.prepare_config_and_inputs()
            for alibi in [True, False]:
                 _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							alibi
                 self.model_tester.create_and_check_model(__lowerCamelCase    ,						*__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     str:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	int						    =							self.model_tester.prepare_config_and_inputs_for_common()
            _SCREAMING_SNAKE_CASE       :	int						    =							3
            _SCREAMING_SNAKE_CASE       :	str						    =							input_dict["input_ids"]
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							input_ids.ne(1							).to(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Any						    =							ids_tensor([self.model_tester.batch_size]    ,						self.model_tester.type_sequence_label_size							)
            _SCREAMING_SNAKE_CASE       :	Dict						    =							FalconForSequenceClassification(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						labels=__lowerCamelCase							)
            self.assertEqual(result.logits.shape    ,						(self.model_tester.batch_size, self.model_tester.num_labels)							)




       def     UpperCamelCase_			(      self							)			->     List[str]:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	List[str]						    =							self.model_tester.prepare_config_and_inputs_for_common()
            _SCREAMING_SNAKE_CASE       :	str						    =							3
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							"single_label_classification"
            _SCREAMING_SNAKE_CASE       :	str						    =							input_dict["input_ids"]
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							input_ids.ne(1							).to(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Dict						    =							ids_tensor([self.model_tester.batch_size]    ,						self.model_tester.type_sequence_label_size							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							FalconForSequenceClassification(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						labels=__lowerCamelCase							)
            self.assertEqual(result.logits.shape    ,						(self.model_tester.batch_size, self.model_tester.num_labels)							)




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







       def     UpperCamelCase_			(      self							)			->     List[str]:
            # Falcon can have different numbers of KV-heads than the number of query heads, so we need
            # to override this test to use the right head counts.
            for model_class in self.all_generative_model_classes:
                 _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	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

                 _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							model_class(__lowerCamelCase							).to(__lowerCamelCase							)
                 if "use_cache" not in inputs:
                      _SCREAMING_SNAKE_CASE       :	List[str]						    =							True
                 _SCREAMING_SNAKE_CASE       :	Union[str, 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

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

                 _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							outputs["past_key_values"]
                 self.assertEqual(len(__lowerCamelCase							)    ,						__lowerCamelCase							)

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






       '''simple docstring'''


       @slow
       def     UpperCamelCase_			(      self							)			->     Any:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b"							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b"							)
            model.eval()
            model.to(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							tokenizer("My favorite food is"    ,						return_tensors="pt"							).to(__lowerCamelCase							)

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

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

            self.assertEqual(__lowerCamelCase    ,						__lowerCamelCase							)




       @slow
       def     UpperCamelCase_			(      self							)			->     List[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
            for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
                 _SCREAMING_SNAKE_CASE       :	Tuple						    =							AutoTokenizer.from_pretrained(__lowerCamelCase							)
                 _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							FalconForCausalLM.from_pretrained(__lowerCamelCase							)
                 model.eval()
                 model.to(__lowerCamelCase							)
                 _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							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     UpperCamelCase_			(      self							)			->     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",
                 ]:
                      _SCREAMING_SNAKE_CASE       :	Dict						    =							AutoTokenizer.from_pretrained(__lowerCamelCase							)
                      _SCREAMING_SNAKE_CASE       :	List[str]						    =							FalconForCausalLM.from_pretrained(__lowerCamelCase							)
                      model.eval()
                      model.to(device=__lowerCamelCase							)
                      _SCREAMING_SNAKE_CASE       :	Dict						    =							tokenizer("My favorite food is"    ,						return_tensors="pt"							).to(__lowerCamelCase							)

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






import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import AutoTokenizer, BarkProcessor
from transformers.testing_utils import require_torch, slow





@require_torch
class 			lowerCAmelCase__( unittest.TestCase    ):






       '''simple docstring'''


       def     UpperCamelCase_			(      self							)			->     Dict:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							"ylacombe/bark-small"
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							tempfile.mkdtemp()
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							"en_speaker_1"
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							"This is a test string"
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							"speaker_embeddings_path.json"
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							"speaker_embeddings"




       def     UpperCamelCase_			(      self    ,						**__lowerCamelCase							)			->     str:
            return AutoTokenizer.from_pretrained(self.checkpoint    ,						**__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            shutil.rmtree(self.tmpdirname							)




       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	Any						    =							self.get_tokenizer()

            _SCREAMING_SNAKE_CASE       :	Dict						    =							BarkProcessor(tokenizer=__lowerCamelCase							)

            processor.save_pretrained(self.tmpdirname							)
            _SCREAMING_SNAKE_CASE       :	Any						    =							BarkProcessor.from_pretrained(self.tmpdirname							)

            self.assertEqual(processor.tokenizer.get_vocab()    ,						tokenizer.get_vocab()							)




       @slow
       def     UpperCamelCase_			(      self							)			->     Optional[Any]:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							BarkProcessor.from_pretrained(
                pretrained_processor_name_or_path=self.checkpoint    ,						speaker_embeddings_dict_path=self.speaker_embeddings_dict_path    ,						)
            processor.save_pretrained(
                self.tmpdirname    ,						speaker_embeddings_dict_path=self.speaker_embeddings_dict_path    ,						speaker_embeddings_directory=self.speaker_embeddings_directory    ,						)

            _SCREAMING_SNAKE_CASE       :	Dict						    =							self.get_tokenizer(bos_token="(BOS)"    ,						eos_token="(EOS)"							)

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							BarkProcessor.from_pretrained(
                self.tmpdirname    ,						self.speaker_embeddings_dict_path    ,						bos_token="(BOS)"    ,						eos_token="(EOS)"    ,						)

            self.assertEqual(processor.tokenizer.get_vocab()    ,						tokenizer_add_kwargs.get_vocab()							)




       def     UpperCamelCase_			(      self							)			->     str:
            _SCREAMING_SNAKE_CASE       :	Any						    =							BarkProcessor.from_pretrained(
                pretrained_processor_name_or_path=self.checkpoint    ,						speaker_embeddings_dict_path=self.speaker_embeddings_dict_path    ,						)

            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							3_5
            _SCREAMING_SNAKE_CASE       :	Any						    =							2
            _SCREAMING_SNAKE_CASE       :	int						    =							8

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							{
                "semantic_prompt": np.ones(__lowerCamelCase							),
                "coarse_prompt": np.ones((nb_codebooks_coarse, seq_len)							),
                "fine_prompt": np.ones((nb_codebooks_total, seq_len)							),
            }

            # test providing already loaded voice_preset
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							processor(text=self.input_string    ,						voice_preset=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							inputs["history_prompt"]
            for key in voice_preset:
                 self.assertListEqual(voice_preset[key].tolist()    ,						processed_voice_preset.get(__lowerCamelCase    ,						np.array([]							)							).tolist()							)

            # test loading voice preset from npz file
            _SCREAMING_SNAKE_CASE       :	Any						    =							os.path.join(self.tmpdirname    ,						"file.npz"							)
            np.savez(__lowerCamelCase    ,						**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							processor(text=self.input_string    ,						voice_preset=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							inputs["history_prompt"]

            for key in voice_preset:
                 self.assertListEqual(voice_preset[key].tolist()    ,						processed_voice_preset.get(__lowerCamelCase    ,						np.array([]							)							).tolist()							)

            # test loading voice preset from the hub
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							processor(text=self.input_string    ,						voice_preset=self.voice_preset							)







       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							self.get_tokenizer()

            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							BarkProcessor(tokenizer=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	str						    =							processor(text=self.input_string							)

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							tokenizer(
                self.input_string    ,						padding="max_length"    ,						max_length=2_5_6    ,						add_special_tokens=__lowerCamelCase    ,						return_attention_mask=__lowerCamelCase    ,						return_token_type_ids=__lowerCamelCase    ,						)

            for key in encoded_tok.keys():
                 self.assertListEqual(encoded_tok[key]    ,						encoded_processor[key].squeeze().tolist()							)