


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





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
        __lowerCamelCase:				List[str]     =	ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size							)

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

        __lowerCamelCase:				Any     =	None

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

        __lowerCamelCase:				int     =	self.get_config()

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





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[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=SCREAMING_SNAKE_CASE_    ,	initializer_range=self.initializer_range    ,	pad_token_id=1    ,	new_decoder_architecture=SCREAMING_SNAKE_CASE_    ,	)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				int							):
        __lowerCamelCase:				List[str]     =	FalconModel(config=SCREAMING_SNAKE_CASE_							)
        model.to(SCREAMING_SNAKE_CASE_							)
        model.eval()
        __lowerCamelCase:				Union[str, Any]     =	model(SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				List[str]     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	):
        __lowerCamelCase:				List[Any]     =	True
        __lowerCamelCase:				List[str]     =	FalconModel(SCREAMING_SNAKE_CASE_							)
        model.to(SCREAMING_SNAKE_CASE_							)
        model.eval()
        __lowerCamelCase:				Any     =	model(
            SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_    ,	encoder_attention_mask=SCREAMING_SNAKE_CASE_    ,	)
        __lowerCamelCase:				Union[str, Any]     =	model(
            SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_    ,	)
        __lowerCamelCase:				Any     =	model(SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				str    ,	):
        __lowerCamelCase:				str     =	FalconForCausalLM(config=SCREAMING_SNAKE_CASE_							)
        model.to(SCREAMING_SNAKE_CASE_							)
        model.eval()
        __lowerCamelCase:				Tuple     =	model(SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	labels=SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	):
        __lowerCamelCase:				str     =	True
        __lowerCamelCase:				int     =	True
        __lowerCamelCase:				Optional[int]     =	FalconForCausalLM(config=SCREAMING_SNAKE_CASE_							)
        model.to(SCREAMING_SNAKE_CASE_							)
        model.eval()

        # first forward pass
        __lowerCamelCase:				Any     =	model(
            SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_    ,	encoder_attention_mask=SCREAMING_SNAKE_CASE_    ,	use_cache=SCREAMING_SNAKE_CASE_    ,	)
        __lowerCamelCase:				List[Any]     =	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:				List[Any]     =	torch.cat([input_ids, next_tokens]    ,	dim=-1							)
        __lowerCamelCase:				Dict     =	torch.cat([input_mask, next_mask]    ,	dim=-1							)

        __lowerCamelCase:				Optional[Any]     =	model(
            SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_    ,	encoder_attention_mask=SCREAMING_SNAKE_CASE_    ,	output_hidden_states=SCREAMING_SNAKE_CASE_    ,	)["""hidden_states"""][0]
        __lowerCamelCase:				Any     =	model(
            SCREAMING_SNAKE_CASE_    ,	attention_mask=SCREAMING_SNAKE_CASE_    ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_    ,	encoder_attention_mask=SCREAMING_SNAKE_CASE_    ,	past_key_values=SCREAMING_SNAKE_CASE_    ,	output_hidden_states=SCREAMING_SNAKE_CASE_    ,	)["""hidden_states"""][0]

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







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



@require_torch
class        a					(			_UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase     ,unittest.TestCase   ):
     UpperCAmelCase__ :  Tuple						      =	(
         (
             FalconModel,
             FalconForCausalLM,
             FalconForSequenceClassification,
             FalconForTokenClassification,
             FalconForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase__ :  Union[str, Any]						      =	(FalconForCausalLM,) if is_torch_available() else ()
     UpperCAmelCase__ :  Tuple						      =	(
         {
             "feature-extraction": FalconModel,
             "text-classification": FalconForSequenceClassification,
             "text-generation": FalconForCausalLM,
             "question-answering": FalconForQuestionAnswering,
             "token-classification": FalconForTokenClassification,
             "zero-shot": FalconForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     UpperCAmelCase__ :  Dict						      =	False
     UpperCAmelCase__ :  Optional[Any]						      =	False





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict							):
        __lowerCamelCase:				Optional[Any]     =	FalconModelTester(self							)
        __lowerCamelCase:				Tuple     =	ConfigTester(self    ,	config_class=SCREAMING_SNAKE_CASE_    ,	hidden_size=37							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
        self.config_tester.run_common_tests()





     def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]							):
        __lowerCamelCase:				List[Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict							):
        __lowerCamelCase   ,     *__lowerCamelCase:				str     =	self.model_tester.prepare_config_and_inputs()
        for alibi in [True, False]:
           __lowerCamelCase:				Dict     =	alibi
           self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)





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





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





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







     def  SCREAMING_SNAKE_CASE__       (    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:
           __lowerCamelCase   ,     __lowerCamelCase:				int     =	self.model_tester.prepare_config_and_inputs_for_common()

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

           __lowerCamelCase:				str     =	model_class(SCREAMING_SNAKE_CASE_							).to(SCREAMING_SNAKE_CASE_							)
           if "use_cache" not in inputs:
              __lowerCamelCase:				Dict     =	True
           __lowerCamelCase:				Dict     =	model(**SCREAMING_SNAKE_CASE_							)

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

           __lowerCamelCase:				List[str]     =	outputs["""past_key_values"""]
           self.assertEqual(len(SCREAMING_SNAKE_CASE_							)    ,	SCREAMING_SNAKE_CASE_							)

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

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

        __lowerCamelCase:				Dict     =	model.generate(**SCREAMING_SNAKE_CASE_    ,	do_sample=SCREAMING_SNAKE_CASE_    ,	max_new_tokens=19							)
        __lowerCamelCase:				int     =	tokenizer.batch_decode(SCREAMING_SNAKE_CASE_							)[0]

        self.assertEqual(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)





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

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







     @slow
     def  SCREAMING_SNAKE_CASE__       (    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
        with torch.no_grad():
           for repo in [
               "Rocketknight1/falcon-rw-1b",
               "Rocketknight1/tiny-random-falcon-7b",
               "Rocketknight1/tiny-random-falcon-40b",
           ]:
              __lowerCamelCase:				str     =	AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_							)
              __lowerCamelCase:				List[Any]     =	FalconForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_							)
              model.eval()
              model.to(device=SCREAMING_SNAKE_CASE_							)
              __lowerCamelCase:				List[Any]     =	tokenizer("""My favorite food is"""    ,	return_tensors="""pt"""							).to(SCREAMING_SNAKE_CASE_							)

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






from manim import *



class        a					(			_UpperCAmelCase   ):





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	Rectangle(height=0.5    ,	width=0.5							)
        __lowerCamelCase:				List[str]     =	Rectangle(height=0.25    ,	width=0.25							)
        __lowerCamelCase:				Optional[int]     =	Rectangle(height=0.46    ,	width=0.46							).set_stroke(width=0							)

        __lowerCamelCase:				str     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Union[str, Any]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Tuple     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""CPU"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        cpu.move_to([-2.5, -0.5, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	[mem.copy() for i in range(4							)]
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""GPU"""    ,	font_size=24							)
        __lowerCamelCase:				Dict     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        gpu.move_to([-1, -1, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Any     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				List[Any]     =	Text("""Model"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        model.move_to([3, -1.0, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	[]
        __lowerCamelCase:				Any     =	[]
        __lowerCamelCase:				int     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           rect.set_stroke(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				int     =	Rectangle(height=0.46 / 4    ,	width=0.46 / 3							).set_stroke(width=0.0							).set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)

           if i == 0:
              cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)    ,	buff=0.02    ,	direction=SCREAMING_SNAKE_CASE_							)
              cpu_target.set_x(cpu_target.get_x() + 0.1							)
           elif i == 3:
              cpu_target.next_to(model_cpu_arr[0]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           else:
              cpu_target.next_to(model_cpu_arr[i - 1]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           self.add(SCREAMING_SNAKE_CASE_							)
           model_cpu_arr.append(SCREAMING_SNAKE_CASE_							)

        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Any     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				Tuple     =	Text("""Loaded Checkpoint"""    ,	font_size=24							)
        __lowerCamelCase:				Tuple     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        checkpoint.move_to([3, 0.5, 0]							)

        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[]
        __lowerCamelCase:				Optional[int]     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	fill.copy().set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)
           target.move_to(SCREAMING_SNAKE_CASE_							)
           ckpt_arr.append(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				List[Any]     =	target.copy()
           if i < 5:
              cpu_target.move_to(cpu_left_col_base[i + 1]							)
           else:
              cpu_target.move_to(cpu_right_col_base[i - 5]							)
           ckpt_cpu_arr.append(SCREAMING_SNAKE_CASE_							)
        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[Any]     =	Square(side_length=2.2							)
        key.move_to([-5, 2, 0]							)

        __lowerCamelCase:				Dict     =	MarkupText(
            F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'    ,	font_size=18    ,	)

        key_text.move_to([-5, 2.4, 0]							)

        self.add(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	MarkupText(
            F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'    ,	font_size=18    ,	)

        blue_text.next_to(SCREAMING_SNAKE_CASE_    ,	DOWN * 2.4    ,	aligned_edge=key_text.get_left()							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	MarkupText(
            F'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'    ,	font_size=24    ,	)
        step_a.move_to([2, 2, 0]							)

        __lowerCamelCase:				List[Any]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				Optional[int]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				str     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				List[str]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""Disk"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        disk.move_to([-4.0, -1.25, 0]							)
        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)    ,	Write(SCREAMING_SNAKE_CASE_    ,	run_time=1							)    ,	Create(SCREAMING_SNAKE_CASE_    ,	run_time=1							)							)

        __lowerCamelCase:				int     =	[]
        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	rect.copy()
           target.generate_target()
           target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
           animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_    ,	run_time=1.5							)							)
        self.play(*SCREAMING_SNAKE_CASE_							)

        self.play(FadeOut(SCREAMING_SNAKE_CASE_							)							)

        __lowerCamelCase:				List[Any]     =	MarkupText(F'Then, the checkpoint is removed from memory\nthrough garbage collection.'    ,	font_size=24							)
        step_a.move_to([2, 2, 0]							)

        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)							)

        self.play(
            FadeOut(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)    ,	)

        self.wait()


