
"""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 			UpperCamelCase      :
     def __init__(     self  ,     snake_case__  ,     snake_case__=3  ,     snake_case__=7  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=False  ,     snake_case__=True  ,     snake_case__=99  ,     snake_case__=32  ,     snake_case__=5  ,     snake_case__=4  ,     snake_case__=37  ,     snake_case__="gelu"  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=512  ,     snake_case__=16  ,     snake_case__=2  ,     snake_case__=0.02  ,     snake_case__=3  ,     snake_case__=4  ,     snake_case__=None  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = parent
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = batch_size
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = seq_length
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = is_training
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = use_input_mask
       _SCREAMING_SNAKE_CASE   :				Tuple							    = use_token_type_ids
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = use_labels
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				Dict							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				List[str]							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				Any							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				int							    = hidden_act
       _SCREAMING_SNAKE_CASE   :				Dict							    = hidden_dropout_prob
       _SCREAMING_SNAKE_CASE   :				Tuple							    = attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = max_position_embeddings
       _SCREAMING_SNAKE_CASE   :				int							    = type_vocab_size
       _SCREAMING_SNAKE_CASE   :				List[str]							    = type_sequence_label_size
       _SCREAMING_SNAKE_CASE   :				List[str]							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				Dict							    = num_labels
       _SCREAMING_SNAKE_CASE   :				str							    = num_choices
       _SCREAMING_SNAKE_CASE   :				int							    = scope
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size				)

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

       _SCREAMING_SNAKE_CASE   :				Tuple							    = None

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

       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """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=snake_case__  ,     initializer_range=self.initializer_range  ,     pad_token_id=1  ,     new_decoder_architecture=snake_case__  ,     )
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = FalconModel(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = model(snake_case__  ,     attention_mask=snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(snake_case__				)
       self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = True
       _SCREAMING_SNAKE_CASE   :				int							    = FalconModel(snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     encoder_hidden_states=snake_case__  ,     encoder_attention_mask=snake_case__  ,     )
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     encoder_hidden_states=snake_case__  ,     )
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(snake_case__  ,     attention_mask=snake_case__				)
       self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = FalconForCausalLM(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Tuple							    = model(snake_case__  ,     attention_mask=snake_case__  ,     labels=snake_case__				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = True
       _SCREAMING_SNAKE_CASE   :				Dict							    = True
       _SCREAMING_SNAKE_CASE   :				str							    = FalconForCausalLM(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()

       # first forward pass
       _SCREAMING_SNAKE_CASE   :				Any							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     encoder_hidden_states=snake_case__  ,     encoder_attention_mask=snake_case__  ,     use_cache=snake_case__  ,     )
       _SCREAMING_SNAKE_CASE   :				int							    = outputs.past_key_values

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

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

       _SCREAMING_SNAKE_CASE   :				str							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     encoder_hidden_states=snake_case__  ,     encoder_attention_mask=snake_case__  ,     output_hidden_states=snake_case__  ,     )["hidden_states"][0]
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     encoder_hidden_states=snake_case__  ,     encoder_attention_mask=snake_case__  ,     past_key_values=snake_case__  ,     output_hidden_states=snake_case__  ,     )["hidden_states"][0]

       # select random slice
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = 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[str]							    = 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(snake_case__  ,     snake_case__  ,     atol=1E-3				)				)




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				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
       )    ,		
       )   :				List[Any]							    = config_and_inputs
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = {"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict
@require_torch
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       unittest.TestCase  ):
     A__      =			(
         (
             FalconModel,
             FalconForCausalLM,
             FalconForSequenceClassification,
             FalconForTokenClassification,
             FalconForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     A__      =			(FalconForCausalLM,) if is_torch_available() else ()
     A__      =			(
         {
             """feature-extraction""": FalconModel,
             """text-classification""": FalconForSequenceClassification,
             """text-generation""": FalconForCausalLM,
             """question-answering""": FalconForQuestionAnswering,
             """token-classification""": FalconForTokenClassification,
             """zero-shot""": FalconForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     A__      =			False
     A__      =			False
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = FalconModelTester(self				)
       _SCREAMING_SNAKE_CASE   :				str							    = ConfigTester(self  ,     config_class=snake_case__  ,     hidden_size=37				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       self.config_tester.run_common_tests()
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE    ,		*_SCREAMING_SNAKE_CASE   :				Any							    = self.model_tester.prepare_config_and_inputs()
       for alibi in [True, False]:
         _SCREAMING_SNAKE_CASE   :				Any							    = alibi
         self.model_tester.create_and_check_model(snake_case__  ,     *snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


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




       """simple docstring"""


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




       """simple docstring"""


       _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE   :				Tuple							    = self.model_tester.prepare_config_and_inputs_for_common()
       _SCREAMING_SNAKE_CASE   :				int							    = input_dict["input_ids"]
       _SCREAMING_SNAKE_CASE   :				Dict							    = FalconForCausalLM(snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = model(snake_case__  ,     use_cache=snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = input_ids.shape[0]
       _SCREAMING_SNAKE_CASE   :				List[str]							    = model._convert_to_rw_cache(result.past_key_values				)
       _SCREAMING_SNAKE_CASE   :				int							    = model._convert_cache_to_standard_format(snake_case__  ,     snake_case__				)
       for layer in range(len(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				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE   :				Optional[Any]							    = self.model_tester.prepare_config_and_inputs_for_common()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = 3
       _SCREAMING_SNAKE_CASE   :				str							    = "multi_label_classification"
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = input_dict["input_ids"]
       _SCREAMING_SNAKE_CASE   :				Any							    = input_ids.ne(1				).to(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = ids_tensor(
           [self.model_tester.batch_size, config.num_labels]  ,     self.model_tester.type_sequence_label_size				).to(torch.float				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = FalconForSequenceClassification(snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(snake_case__  ,     attention_mask=snake_case__  ,     labels=snake_case__				)
       self.assertEqual(result.logits.shape  ,     (self.model_tester.batch_size, self.model_tester.num_labels)				)




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       for model_class in self.all_generative_model_classes:
         _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs_for_common()

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

         _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = model_class(snake_case__				).to(snake_case__				)
         if "use_cache" not in inputs:
           _SCREAMING_SNAKE_CASE   :				List[Any]							    = True
         _SCREAMING_SNAKE_CASE   :				Dict							    = model(**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

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

         _SCREAMING_SNAKE_CASE   :				Tuple							    = outputs["past_key_values"]
         self.assertEqual(len(snake_case__				)  ,     snake_case__				)

         _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE   :				str							    = inputs["input_ids"].shape
         for i in range(snake_case__				):
           if config.new_decoder_architecture:
             _SCREAMING_SNAKE_CASE   :				Any							    = config.num_attention_heads
           elif config.multi_query:
             _SCREAMING_SNAKE_CASE   :				str							    = 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 			UpperCamelCase      (							unittest.TestCase  ):
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b"				)
       _SCREAMING_SNAKE_CASE   :				str							    = FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b"				)
       model.eval()
       model.to(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = tokenizer("My favorite food is"  ,     return_tensors="pt"				).to(snake_case__				)

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

       _SCREAMING_SNAKE_CASE   :				Any							    = model.generate(**snake_case__  ,     do_sample=snake_case__  ,     max_new_tokens=19				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = tokenizer.batch_decode(snake_case__				)[0]

       self.assertEqual(snake_case__  ,     snake_case__				)
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
         _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = AutoTokenizer.from_pretrained(snake_case__				)
         _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = FalconForCausalLM.from_pretrained(snake_case__				)
         model.eval()
         model.to(snake_case__				)
         _SCREAMING_SNAKE_CASE   :				str							    = tokenizer("My favorite food is"  ,     return_tensors="pt"				).to(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(**snake_case__  ,     do_sample=snake_case__  ,     max_new_tokens=4				)
         model.generate(**snake_case__  ,     do_sample=snake_case__  ,     max_new_tokens=4				)
         model.generate(**snake_case__  ,     num_beams=2  ,     max_new_tokens=4				)




     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       with torch.no_grad():
         for repo in [
             "Rocketknight1/falcon-rw-1b",
             "Rocketknight1/tiny-random-falcon-7b",
             "Rocketknight1/tiny-random-falcon-40b",
         ]:
           _SCREAMING_SNAKE_CASE   :				str							    = AutoTokenizer.from_pretrained(snake_case__				)
           _SCREAMING_SNAKE_CASE   :				str							    = FalconForCausalLM.from_pretrained(snake_case__				)
           model.eval()
           model.to(device=snake_case__				)
           _SCREAMING_SNAKE_CASE   :				List[Any]							    = tokenizer("My favorite food is"  ,     return_tensors="pt"				).to(snake_case__				)

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





"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import TFCamembertModel
@require_tf
@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase      (							unittest.TestCase  ):
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = TFCamembertModel.from_pretrained("jplu/tf-camembert-base"				)

       _SCREAMING_SNAKE_CASE   :				str							    = tf.convert_to_tensor(
           [[5, 121, 11, 660, 16, 730, 25543, 110, 83, 6]]  ,     dtype=tf.intaa  ,     )  # J'aime le camembert !"

       _SCREAMING_SNAKE_CASE   :				str							    = model(snake_case__				)["last_hidden_state"]
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = tf.TensorShape((1, 10, 768)				)
       self.assertEqual(output.shape  ,     snake_case__				)
       # compare the actual values for a slice.
       _SCREAMING_SNAKE_CASE   :				int							    = tf.convert_to_tensor(
           [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]]  ,     dtype=tf.floataa  ,     )
       # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
       # camembert.eval()
       # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

       self.assertTrue(np.allclose(output[:, :3, :3].numpy()  ,     expected_slice.numpy()  ,     atol=1E-4				)				)



