


"""simple docstring"""





import unittest

from transformers import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


__UpperCAmelCase     					=   get_tests_dir("fixtures/test_sentencepiece.model")

@require_sentencepiece
@require_tokenizers
class 			lowercase_	(			UpperCAmelCase_				,				unittest.TestCase    ):
      __magic_name__  :				List[Any] 							=					XLNetTokenizer
      __magic_name__  :				List[Any] 							=					XLNetTokenizerFast
      __magic_name__  :				Optional[int] 							=					True
      __magic_name__  :				Any 							=					True







      def 					_lowerCAmelCase							(   self			:				str     ):
        super().setUp()

        # We have a SentencePiece fixture for testing
        lowerCAmelCase__     :     Optional[int]										=       XLNetTokenizer(lowerCamelCase__		,	keep_accents=lowerCamelCase__     )
        tokenizer.sanitize_special_tokens()
        tokenizer.save_pretrained(self.tmpdirname     )







      def 					_lowerCAmelCase							(   self			:				int     ):
        lowerCAmelCase__     :     int										=       """<s>"""
        lowerCAmelCase__     :     str										=       1

        self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__     )		,	lowerCamelCase__     )
        self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__     )		,	lowerCamelCase__     )







      def 					_lowerCAmelCase							(   self			:				List[str]     ):
        lowerCAmelCase__     :     Optional[int]										=       list(self.get_tokenizer().get_vocab().keys()     )

        self.assertEqual(vocab_keys[0]		,	"<unk>"     )
        self.assertEqual(vocab_keys[1]		,	"<s>"     )
        self.assertEqual(vocab_keys[-1]		,	"<eod>"     )
        self.assertEqual(len(lowerCamelCase__     )		,	1_0_0_6     )







      def 					_lowerCAmelCase							(   self			:				Any     ):
        self.assertEqual(self.get_tokenizer().vocab_size		,	1_0_0_0     )







      def 					_lowerCAmelCase							(   self			:				Optional[int]     ):
        lowerCAmelCase__     :     List[Any]										=       XLNetTokenizer(lowerCamelCase__		,	keep_accents=lowerCamelCase__     )

        lowerCAmelCase__     :     Union[str, Any]										=       tokenizer.tokenize("This is a test"     )
        self.assertListEqual(lowerCamelCase__		,	["▁This", "▁is", "▁a", "▁t", "est"]     )

        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__     )		,	[2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]     )

        lowerCAmelCase__     :     Union[str, Any]										=       tokenizer.tokenize("I was born in 92000, and this is falsé."     )
        self.assertListEqual(
            lowerCamelCase__		,	[
                SPIECE_UNDERLINE + "I",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "9",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "s",
                "é",
                ".",
            ]		,	)
        lowerCAmelCase__     :     int										=       tokenizer.convert_tokens_to_ids(lowerCamelCase__     )
        self.assertListEqual(lowerCamelCase__		,	[8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 0, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 0, 4]     )

        lowerCAmelCase__     :     Optional[Any]										=       tokenizer.convert_ids_to_tokens(lowerCamelCase__     )
        self.assertListEqual(
            lowerCamelCase__		,	[
                SPIECE_UNDERLINE + "I",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "<unk>",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "s",
                "<unk>",
                ".",
            ]		,	)







      def 					_lowerCAmelCase							(   self			:				Dict     ):
        lowerCAmelCase__     :     List[Any]										=       XLNetTokenizer(lowerCamelCase__		,	do_lower_case=lowerCamelCase__     )
        lowerCAmelCase__     :     List[Any]										=       tokenizer.tokenize("I was born in 92000, and this is falsé."     )
        self.assertListEqual(
            lowerCamelCase__		,	[
                SPIECE_UNDERLINE + "",
                "i",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "9",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "se",
                ".",
            ]		,	)
        self.assertListEqual(tokenizer.tokenize("H\u00E9llo"     )		,	["▁he", "ll", "o"]     )







      def 					_lowerCAmelCase							(   self			:				Dict     ):
        lowerCAmelCase__     :     List[Any]										=       XLNetTokenizer(lowerCamelCase__		,	do_lower_case=lowerCamelCase__     )
        lowerCAmelCase__     :     Dict										=       tokenizer.tokenize("I was born in 92000, and this is falsé."     )
        self.assertListEqual(
            lowerCamelCase__		,	[
                SPIECE_UNDERLINE + "I",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "9",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "se",
                ".",
            ]		,	)







      @slow
      def 					_lowerCAmelCase							(   self			:				Tuple     ):
        lowerCAmelCase__     :     int										=       XLNetTokenizer.from_pretrained("xlnet-base-cased"     )

        lowerCAmelCase__     :     Tuple										=       tokenizer.encode("sequence builders"		,	add_special_tokens=lowerCamelCase__     )
        lowerCAmelCase__     :     Union[str, Any]										=       tokenizer.encode("multi-sequence build"		,	add_special_tokens=lowerCamelCase__     )

        lowerCAmelCase__     :     Any										=       tokenizer.build_inputs_with_special_tokens(lowerCamelCase__     )
        lowerCAmelCase__     :     Tuple										=       tokenizer.build_inputs_with_special_tokens(lowerCamelCase__		,	lowerCamelCase__     )

        assert encoded_sentence == text + [4, 3]
        assert encoded_pair == text + [4] + text_a + [4, 3]







      @slow
      def 					_lowerCAmelCase							(   self			:				Tuple     ):
        lowerCAmelCase__     :     Optional[int]										=       {"""input_ids""": [[1_7, 2_1_4_4_2, 2_7_0, 1_7, 1_0, 1_4_6_4_5, 3_1_8, 3_4, 1_7, 4_5_4_6, 3_1_4_5, 7_8_7, 1_3, 7_7_5_2, 2_2_0_1_8, 2_3, 2_1, 1_7, 4_5_4_6, 3_1_4_5, 7_8_7, 1_3, 3_3_5_2, 1_4_4_3_1, 1_3, 5_5_0_0, 1_1, 1_1_7_6, 5_8_0, 1_3, 1_6_8_1_9, 4_7_9_7, 2_3, 1_7, 1_0, 1_7_1_3_5, 6_5_8, 1_9, 4_5_7, 7_9_3_2, 1_3, 1_8_4, 1_9, 3_1_5_4, 1_7_1_3_5, 6_4_6_8, 1_9, 1_4_0_4, 1_2_2_6_9, 1_9, 4_2_2_9, 5_3_5_6, 1_6_2_6_4, 4_6, 1_9, 1_7, 2_0_5_4_5, 1_0_3_9_5, 9, 9, 9, 1_1, 2_8, 6_4_2_1, 9_5_3_1, 2_0_7_2_9, 1_7, 1_0, 3_5_3, 1_7_0_2_2, 1_1, 2_1, 6_4_2_1, 9_5_3_1, 1_6_9_4_9, 1_7, 1_0, 1_1_5_0_9, 7_5_3, 1_1, 3_3, 9_5, 2_4_2_1, 7_3_8_5, 9_5_6, 1_4_4_3_1, 2_6_2_6, 2_5, 8_4_2, 7_3_8_5, 4_8_3_6, 2_1, 1_4_2_9, 2_2_7_2, 9_8_5_5, 3_1_2_0, 1_6_1, 2_4_7_3_8, 1_9, 1_3_2_0_3, 6_5_8, 2_1_8, 7_8_7, 2_1, 4_3_0, 1_8_4_8_2, 8_4_7, 2_6_3_7, 9, 4, 3], [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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
        # fmt: on

        self.tokenizer_integration_test_util(
            expected_encoding=lowerCamelCase__		,	model_name="xlnet-base-cased"		,	revision="c841166438c31ec7ca9a106dee7bb312b73ae511"		,	)








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			)



