"""simple docstring"""




import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        FalconForCausalLM,
        FalconForQuestionAnswering,
        FalconForSequenceClassification,
        FalconForTokenClassification,
        FalconModel,
    )


class      lowerCamelCase_					:






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase=3			,			__lowercase=7			,			__lowercase=True			,			__lowercase=True			,			__lowercase=False			,			__lowercase=True			,			__lowercase=99			,			__lowercase=32			,			__lowercase=5			,			__lowercase=4			,			__lowercase=37			,			__lowercase="gelu"			,			__lowercase=0.1			,			__lowercase=0.1			,			__lowercase=512			,			__lowercase=16			,			__lowercase=2			,			__lowercase=0.02			,			__lowercase=3			,			__lowercase=4			,			__lowercase=None			,			)	->     List[Any]:
        __UpperCamelCase					:Dict        =		parent
        __UpperCamelCase					:Dict        =		batch_size
        __UpperCamelCase					:Optional[int]        =		seq_length
        __UpperCamelCase					:Tuple        =		is_training
        __UpperCamelCase					:List[str]        =		use_input_mask
        __UpperCamelCase					:Union[str, Any]        =		use_token_type_ids
        __UpperCamelCase					:Optional[Any]        =		use_labels
        __UpperCamelCase					:str        =		vocab_size
        __UpperCamelCase					:Dict        =		hidden_size
        __UpperCamelCase					:Optional[int]        =		num_hidden_layers
        __UpperCamelCase					:Tuple        =		num_attention_heads
        __UpperCamelCase					:Optional[int]        =		intermediate_size
        __UpperCamelCase					:List[str]        =		hidden_act
        __UpperCamelCase					:Optional[Any]        =		hidden_dropout_prob
        __UpperCamelCase					:Any        =		attention_probs_dropout_prob
        __UpperCamelCase					:Tuple        =		max_position_embeddings
        __UpperCamelCase					:int        =		type_vocab_size
        __UpperCamelCase					:List[Any]        =		type_sequence_label_size
        __UpperCamelCase					:Tuple        =		initializer_range
        __UpperCamelCase					:Tuple        =		num_labels
        __UpperCamelCase					:Dict        =		num_choices
        __UpperCamelCase					:List[Any]        =		scope







    def       UpperCamelCase__       (     self)	->     int:
        __UpperCamelCase					:str        =		ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size)

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

        __UpperCamelCase					:List[str]        =		None

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

        __UpperCamelCase					:Any        =		self.get_config()

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







    def       UpperCamelCase__       (     self)	->     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=_A			,			initializer_range=self.initializer_range			,			pad_token_id=1			,			new_decoder_architecture=_A			,			)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     str:
        __UpperCamelCase					:Union[str, Any]        =		FalconModel(config=_A)
        model.to(_A)
        model.eval()
        __UpperCamelCase					:int        =		model(_A			,			attention_mask=_A)
        __UpperCamelCase					:Optional[int]        =		model(_A)
        self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size))







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			)	->     Union[str, Any]:
        __UpperCamelCase					:str        =		True
        __UpperCamelCase					:Optional[int]        =		FalconModel(_A)
        model.to(_A)
        model.eval()
        __UpperCamelCase					:Tuple        =		model(
            _A			,			attention_mask=_A			,			encoder_hidden_states=_A			,			encoder_attention_mask=_A			,			)
        __UpperCamelCase					:int        =		model(
            _A			,			attention_mask=_A			,			encoder_hidden_states=_A			,			)
        __UpperCamelCase					:Union[str, Any]        =		model(_A			,			attention_mask=_A)
        self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size))







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			)	->     Optional[Any]:
        __UpperCamelCase					:str        =		FalconForCausalLM(config=_A)
        model.to(_A)
        model.eval()
        __UpperCamelCase					:List[Any]        =		model(_A			,			attention_mask=_A			,			labels=_A)
        self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.vocab_size))







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			)	->     Optional[int]:
        __UpperCamelCase					:str        =		True
        __UpperCamelCase					:Any        =		True
        __UpperCamelCase					:int        =		FalconForCausalLM(config=_A)
        model.to(_A)
        model.eval()

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

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

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

        __UpperCamelCase					:str        =		model(
            _A			,			attention_mask=_A			,			encoder_hidden_states=_A			,			encoder_attention_mask=_A			,			output_hidden_states=_A			,			)['hidden_states'][0]
        __UpperCamelCase					:Optional[Any]        =		model(
            _A			,			attention_mask=_A			,			encoder_hidden_states=_A			,			encoder_attention_mask=_A			,			past_key_values=_A			,			output_hidden_states=_A			,			)['hidden_states'][0]

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







    def       UpperCamelCase__       (     self)	->     Optional[int]:
        __UpperCamelCase					:Tuple        =		self.prepare_config_and_inputs()
        (
            __UpperCamelCase    
        )					:Union[str, Any]        =		config_and_inputs
        __UpperCamelCase					:Any        =		{'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict


@require_torch
class      lowerCamelCase_					(       UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     unittest.TestCase			):






    '''simple docstring'''






    a__     :						List[str]      =				(
        (
            FalconModel,
            FalconForCausalLM,
            FalconForSequenceClassification,
            FalconForTokenClassification,
            FalconForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    a__     :						Union[str, Any]      =				(FalconForCausalLM,) if is_torch_available() else ()
    a__     :						Optional[int]      =				(
        {
            """feature-extraction""": FalconModel,
            """text-classification""": FalconForSequenceClassification,
            """text-generation""": FalconForCausalLM,
            """question-answering""": FalconForQuestionAnswering,
            """token-classification""": FalconForTokenClassification,
            """zero-shot""": FalconForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    a__     :						str      =				False
    a__     :						List[Any]      =				False







    def       UpperCamelCase__       (     self)	->     Union[str, Any]:
        __UpperCamelCase					:Optional[int]        =		FalconModelTester(self)
        __UpperCamelCase					:Union[str, Any]        =		ConfigTester(self			,			config_class=_A			,			hidden_size=37)







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







    def       UpperCamelCase__       (     self)	->     Any:
        __UpperCamelCase					:List[str]        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_A)







    def       UpperCamelCase__       (     self)	->     Any:
        __UpperCamelCase					:Tuple        =		self.model_tester.prepare_config_and_inputs()
        for alibi in [True, False]:
            __UpperCamelCase					:int        =		alibi
            self.model_tester.create_and_check_model(_A			,			*_A)







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







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







    def       UpperCamelCase__       (     self)	->     List[Any]:
        __UpperCamelCase					:Dict        =		self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCamelCase					:Tuple        =		input_dict['input_ids']
        __UpperCamelCase					:Optional[Any]        =		FalconForCausalLM(_A)
        model.to(_A)
        model.eval()
        __UpperCamelCase					:Dict        =		model(_A			,			use_cache=_A)
        __UpperCamelCase					:Optional[Any]        =		input_ids.shape[0]
        __UpperCamelCase					:List[str]        =		model._convert_to_rw_cache(result.past_key_values)
        __UpperCamelCase					:List[str]        =		model._convert_cache_to_standard_format(_A			,			_A)
        for layer in range(len(_A)):
            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]:
        __UpperCamelCase					:Optional[int]        =		self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCamelCase					:Union[str, Any]        =		3
        __UpperCamelCase					:Optional[int]        =		'multi_label_classification'
        __UpperCamelCase					:List[str]        =		input_dict['input_ids']
        __UpperCamelCase					:List[Any]        =		input_ids.ne(1).to(_A)
        __UpperCamelCase					:int        =		ids_tensor(
            [self.model_tester.batch_size, config.num_labels]			,			self.model_tester.type_sequence_label_size).to(torch.float)
        __UpperCamelCase					:List[Any]        =		FalconForSequenceClassification(_A)
        model.to(_A)
        model.eval()
        __UpperCamelCase					:Union[str, Any]        =		model(_A			,			attention_mask=_A			,			labels=_A)
        self.assertEqual(result.logits.shape			,			(self.model_tester.batch_size, self.model_tester.num_labels))







    def       UpperCamelCase__       (     self)	->     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:
            __UpperCamelCase					:Optional[Any]        =		self.model_tester.prepare_config_and_inputs_for_common()

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

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

            # 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					:Union[str, Any]        =		(
                getattr(_A			,			'''decoder_layers'''			,			_A)
                or getattr(_A			,			'''num_decoder_layers'''			,			_A)
                or config.num_hidden_layers
            )
            __UpperCamelCase					:Optional[int]        =		getattr(_A			,			'''num_kv_heads'''			,			config.num_attention_heads)
            __UpperCamelCase					:Optional[int]        =		getattr(_A			,			'''d_model'''			,			config.hidden_size)
            __UpperCamelCase					:List[str]        =		embed_dim // num_attention_heads

            __UpperCamelCase					:Any        =		outputs['past_key_values']
            self.assertEqual(len(_A)			,			_A)

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

        __UpperCamelCase					:Dict        =		(
            '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(**_A			,			do_sample=_A			,			max_new_tokens=19)
        __UpperCamelCase					:Optional[int]        =		tokenizer.batch_decode(_A)[0]

        self.assertEqual(_A			,			_A)







    @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
        for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
            __UpperCamelCase					:Any        =		AutoTokenizer.from_pretrained(_A)
            __UpperCamelCase					:Optional[int]        =		FalconForCausalLM.from_pretrained(_A)
            model.eval()
            model.to(_A)
            __UpperCamelCase					:Union[str, Any]        =		tokenizer('''My favorite food is'''			,			return_tensors='''pt''').to(_A)

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







    @slow
    def       UpperCamelCase__       (     self)	->     Optional[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",
            ]:
                __UpperCamelCase					:List[str]        =		AutoTokenizer.from_pretrained(_A)
                __UpperCamelCase					:int        =		FalconForCausalLM.from_pretrained(_A)
                model.eval()
                model.to(device=_A)
                __UpperCamelCase					:List[str]        =		tokenizer('''My favorite food is'''			,			return_tensors='''pt''').to(_A)

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






from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_herbert import HerbertTokenizer


__lowercase										= logging.get_logger(__name__)

__lowercase										= {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__lowercase										= {
    '''vocab_file''': {
        '''allegro/herbert-base-cased''': '''https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json'''
    },
    '''merges_file''': {
        '''allegro/herbert-base-cased''': '''https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt'''
    },
}

__lowercase										= {'''allegro/herbert-base-cased''': 514}
__lowercase										= {}


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						Tuple      =				VOCAB_FILES_NAMES
    a__     :						Dict      =				PRETRAINED_VOCAB_FILES_MAP
    a__     :						Tuple      =				PRETRAINED_INIT_CONFIGURATION
    a__     :						Optional[Any]      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    a__     :						Union[str, Any]      =				HerbertTokenizer







    def __init__(     self			,			__lowercase=None			,			__lowercase=None			,			__lowercase=None			,			__lowercase="<s>"			,			__lowercase="<unk>"			,			__lowercase="<pad>"			,			__lowercase="<mask>"			,			__lowercase="</s>"			,			**__lowercase			,			)	->     Optional[Any]:
        super().__init__(
            __lowercase			,			__lowercase			,			tokenizer_file=__lowercase			,			cls_token=__lowercase			,			unk_token=__lowercase			,			pad_token=__lowercase			,			mask_token=__lowercase			,			sep_token=__lowercase			,			**__lowercase			,			)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None)	->     List[int]:
        __UpperCamelCase					:List[str]        =		[self.cls_token_id]
        __UpperCamelCase					:Tuple        =		[self.sep_token_id]
        if token_ids_a is None:
            return cls + token_ids_a + sep

        return cls + token_ids_a + sep + token_ids_a + sep







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None			,			__lowercase = False)	->     List[int]:
        if already_has_special_tokens:
            return super().get_special_tokens_mask(
                token_ids_a=__lowercase			,			token_ids_a=__lowercase			,			already_has_special_tokens=__lowercase)

        if token_ids_a is None:
            return [1] + ([0] * len(__lowercase)) + [1]
        return [1] + ([0] * len(__lowercase)) + [1] + ([0] * len(__lowercase)) + [1]







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None)	->     List[int]:
        __UpperCamelCase					:Optional[Any]        =		[self.sep_token_id]
        __UpperCamelCase					:int        =		[self.cls_token_id]

        if token_ids_a is None:
            return len(cls + token_ids_a + sep) * [0]
        return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None)	->     Tuple[str]:
        __UpperCamelCase					:Optional[int]        =		self._tokenizer.model.save(__lowercase			,			name=__lowercase)
        return tuple(__lowercase)





