'''simple docstring'''

import unittest

from parameterized import parameterized

from transformers import LlamaConfig, is_torch_available, set_seed
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 LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer


class     __UpperCAmelCase       :





  def __init__(	self    ,     _lowerCamelCase    ,     _lowerCamelCase=13    ,     _lowerCamelCase=7    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=False    ,     _lowerCamelCase=True    ,     _lowerCamelCase=99    ,     _lowerCamelCase=32    ,     _lowerCamelCase=5    ,     _lowerCamelCase=4    ,     _lowerCamelCase=37    ,     _lowerCamelCase="gelu"    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=512    ,     _lowerCamelCase=16    ,     _lowerCamelCase=2    ,     _lowerCamelCase=0.02    ,     _lowerCamelCase=3    ,     _lowerCamelCase=4    ,     _lowerCamelCase=None    ,     ):
     lowerCAmelCase_			=      parent
     lowerCAmelCase_			=      batch_size
     lowerCAmelCase_			=      seq_length
     lowerCAmelCase_			=      is_training
     lowerCAmelCase_			=      use_input_mask
     lowerCAmelCase_			=      use_token_type_ids
     lowerCAmelCase_			=      use_labels
     lowerCAmelCase_			=      vocab_size
     lowerCAmelCase_			=      hidden_size
     lowerCAmelCase_			=      num_hidden_layers
     lowerCAmelCase_			=      num_attention_heads
     lowerCAmelCase_			=      intermediate_size
     lowerCAmelCase_			=      hidden_act
     lowerCAmelCase_			=      hidden_dropout_prob
     lowerCAmelCase_			=      attention_probs_dropout_prob
     lowerCAmelCase_			=      max_position_embeddings
     lowerCAmelCase_			=      type_vocab_size
     lowerCAmelCase_			=      type_sequence_label_size
     lowerCAmelCase_			=      initializer_range
     lowerCAmelCase_			=      num_labels
     lowerCAmelCase_			=      num_choices
     lowerCAmelCase_			=      scope





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size )

     lowerCAmelCase_			=      None
     if self.use_input_mask:
        lowerCAmelCase_			=      random_attention_mask([self.batch_size, self.seq_length] )

     lowerCAmelCase_			=      None
     if self.use_token_type_ids:
        lowerCAmelCase_			=      ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size )

     lowerCAmelCase_			=      None
     lowerCAmelCase_			=      None
     lowerCAmelCase_			=      None
     if self.use_labels:
        lowerCAmelCase_			=      ids_tensor([self.batch_size]    ,     self.type_sequence_label_size )
        lowerCAmelCase_			=      ids_tensor([self.batch_size, self.seq_length]    ,     self.num_labels )
        lowerCAmelCase_			=      ids_tensor([self.batch_size]    ,     self.num_choices )

     lowerCAmelCase_			=      self.get_config()

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





  def 						UpperCAmelCase_	(	self ):
     return LlamaConfig(
         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    ,     )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      LlamaModel(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase )
     lowerCAmelCase_			=      model(_lowerCamelCase )
     self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.hidden_size) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     ):
     lowerCAmelCase_			=      True
     lowerCAmelCase_			=      LlamaModel(_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     encoder_hidden_states=_lowerCamelCase    ,     encoder_attention_mask=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     encoder_hidden_states=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase )
     self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.hidden_size) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     ):
     lowerCAmelCase_			=      LlamaForCausalLM(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.vocab_size) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     ):
     lowerCAmelCase_			=      True
     lowerCAmelCase_			=      True
     lowerCAmelCase_			=      LlamaForCausalLM(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()

     # first forward pass
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     encoder_hidden_states=_lowerCamelCase    ,     encoder_attention_mask=_lowerCamelCase    ,     use_cache=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      outputs.past_key_values

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

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

     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     encoder_hidden_states=_lowerCamelCase    ,     encoder_attention_mask=_lowerCamelCase    ,     output_hidden_states=_lowerCamelCase    ,     )['''hidden_states'''][0]
     lowerCAmelCase_			=      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
     lowerCAmelCase_			=      ids_tensor((1,)    ,     output_from_past.shape[-1] ).item()
     lowerCAmelCase_			=      output_from_no_past[:, -3:, random_slice_idx].detach()
     lowerCAmelCase_			=      output_from_past[:, :, random_slice_idx].detach()

     self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] )

     # test that outputs are equal for slice
     self.parent.assertTrue(torch.allclose(_lowerCamelCase    ,     _lowerCamelCase    ,     atol=1E-3 ) )







  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.prepare_config_and_inputs()
     (
         (
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,
     )			=      config_and_inputs
     lowerCAmelCase_			=      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






@require_torch
class     __UpperCAmelCase       ( __a						,       __a						,       __a						,       unittest.TestCase		):
  __A :				str											= (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else ()
  __A :				Tuple											= (LlamaForCausalLM,) if is_torch_available() else ()
  __A :				str											= (
      {
          'feature-extraction': LlamaModel,
          'text-classification': LlamaForSequenceClassification,
          'text-generation': LlamaForCausalLM,
          'zero-shot': LlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  __A :				str											= False
  __A :				Optional[int]											= False





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      LlamaModelTester(self )
     lowerCAmelCase_			=      ConfigTester(self    ,     config_class=_lowerCamelCase    ,     hidden_size=37 )





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





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
        lowerCAmelCase_			=      type
        self.model_tester.create_and_check_model(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()
     lowerCAmelCase_			=      3
     lowerCAmelCase_			=      input_dict['''input_ids''']
     lowerCAmelCase_			=      input_ids.ne(1 ).to(_lowerCamelCase )
     lowerCAmelCase_			=      ids_tensor([self.model_tester.batch_size]    ,     self.model_tester.type_sequence_label_size )
     lowerCAmelCase_			=      LlamaForSequenceClassification(_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.assertEqual(result.logits.shape    ,     (self.model_tester.batch_size, self.model_tester.num_labels) )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()
     lowerCAmelCase_			=      3
     lowerCAmelCase_			=      '''single_label_classification'''
     lowerCAmelCase_			=      input_dict['''input_ids''']
     lowerCAmelCase_			=      input_ids.ne(1 ).to(_lowerCamelCase )
     lowerCAmelCase_			=      ids_tensor([self.model_tester.batch_size]    ,     self.model_tester.type_sequence_label_size )
     lowerCAmelCase_			=      LlamaForSequenceClassification(_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.assertEqual(result.logits.shape    ,     (self.model_tester.batch_size, self.model_tester.num_labels) )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()
     lowerCAmelCase_			=      3
     lowerCAmelCase_			=      '''multi_label_classification'''
     lowerCAmelCase_			=      input_dict['''input_ids''']
     lowerCAmelCase_			=      input_ids.ne(1 ).to(_lowerCamelCase )
     lowerCAmelCase_			=      ids_tensor(
         [self.model_tester.batch_size, config.num_labels]    ,     self.model_tester.type_sequence_label_size ).to(torch.float )
     lowerCAmelCase_			=      LlamaForSequenceClassification(_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.assertEqual(result.logits.shape    ,     (self.model_tester.batch_size, self.model_tester.num_labels) )





  @unittest.skip('''LLaMA buffers include complex numbers, which breaks this test''' )
  def 						UpperCAmelCase_	(	self ):
     pass







  @parameterized.expand([('''linear''',), ('''dynamic''',)] )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     lowerCAmelCase_	,lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs_for_common()
     lowerCAmelCase_			=      ids_tensor([1, 10]    ,     config.vocab_size )
     lowerCAmelCase_			=      ids_tensor([1, int(config.max_position_embeddings * 1.5 )]    ,     config.vocab_size )

     set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
     lowerCAmelCase_			=      LlamaModel(_lowerCamelCase )
     original_model.to(_lowerCamelCase )
     original_model.eval()
     lowerCAmelCase_			=      original_model(_lowerCamelCase ).last_hidden_state
     lowerCAmelCase_			=      original_model(_lowerCamelCase ).last_hidden_state

     set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
     lowerCAmelCase_			=      {'''type''': scaling_type, '''factor''': 10.0}
     lowerCAmelCase_			=      LlamaModel(_lowerCamelCase )
     scaled_model.to(_lowerCamelCase )
     scaled_model.eval()
     lowerCAmelCase_			=      scaled_model(_lowerCamelCase ).last_hidden_state
     lowerCAmelCase_			=      scaled_model(_lowerCamelCase ).last_hidden_state

     # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
     # maximum sequence length, so the outputs for the short input should match.
     if scaling_type == "dynamic":
        self.assertTrue(torch.allclose(_lowerCamelCase    ,     _lowerCamelCase    ,     atol=1E-5 ) )
     else:
        self.assertFalse(torch.allclose(_lowerCamelCase    ,     _lowerCamelCase    ,     atol=1E-5 ) )

     # The output should be different for long inputs
     self.assertFalse(torch.allclose(_lowerCamelCase    ,     _lowerCamelCase    ,     atol=1E-5 ) )






@require_torch
class     __UpperCAmelCase       ( unittest.TestCase		):





  @unittest.skip('''Logits are not exactly the same, once we fix the instabalities somehow, will update!''' )
  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      [1, 306, 4658, 278, 6593, 310, 2834, 338]
     lowerCAmelCase_			=      LlamaForCausalLM.from_pretrained('''meta-llama/Llama-2-7b-hf'''    ,     device_map='''auto''' )
     lowerCAmelCase_			=      model(torch.tensor([input_ids] ) )
     # Expected mean on dim = -1
     lowerCAmelCase_			=      torch.tensor([[-6.65_50, -4.12_27, -4.98_59, -3.24_06, 0.82_62, -3.00_33, 1.29_64, -3.36_99]] )
     torch.testing.assert_close(out.mean(-1 )    ,     _lowerCamelCase    ,     atol=1E-2    ,     rtol=1E-2 )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     lowerCAmelCase_			=      torch.tensor([-12.82_81, -7.44_53, -0.46_39, -8.06_25, -7.25_00, -8.00_00, -6.48_83, -7.76_95, -7.84_38, -7.03_12, -6.21_88, -7.13_28, -1.84_96, 1.99_61, -8.62_50, -6.72_27, -12.82_81, -6.94_92, -7.07_42, -7.78_52, -7.58_20, -7.90_62, -6.93_75, -7.98_05, -8.34_38, -8.15_62, -8.04_69, -7.62_50, -7.74_22, -7.33_98,] )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]    ,     _lowerCamelCase    ,     atol=1E-5    ,     rtol=1E-5 )





  @unittest.skip('''Logits are not exactly the same, once we fix the instabalities somehow, will update!''' )
  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      [1, 306, 4658, 278, 6593, 310, 2834, 338]
     lowerCAmelCase_			=      LlamaForCausalLM.from_pretrained('''meta-llama/Llama-2-13b-hf'''    ,     device_map='''auto''' )
     lowerCAmelCase_			=      model(torch.tensor(_lowerCamelCase ) )
     # Expected mean on dim = -1
     lowerCAmelCase_			=      torch.tensor([[-2.06_22, -1.27_94, -1.16_38, -0.97_88, -1.46_03, -1.02_38, -1.78_93, -1.44_11]] )
     torch.testing.assert_close(out.mean(-1 )    ,     _lowerCamelCase    ,     atol=1E-2    ,     rtol=1E-2 )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     lowerCAmelCase_			=      torch.tensor([-8.14_06, -8.05_47, 2.74_61, -1.23_44, -0.14_48, -1.82_62, -1.00_20, -1.81_54, -1.68_95, -1.85_16, -2.35_74, -0.92_77, 3.75_98, 6.57_42, -1.29_98, -0.11_77, -8.14_06, -2.96_88, -2.91_99, -3.16_99, -3.52_54, -2.35_55, -2.79_88, -3.41_41, -2.82_62, -4.51_95, -3.33_79, -3.31_64, -2.78_32, -3.02_73] )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]    ,     _lowerCamelCase    ,     atol=1E-5    ,     rtol=1E-5 )





  @unittest.skip('''Logits are not exactly the same, once we fix the instabalities somehow, will update!''' )
  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      [1, 306, 4658, 278, 6593, 310, 2834, 338]
     lowerCAmelCase_			=      LlamaForCausalLM.from_pretrained('''meta-llama/Llama-2-13b-chat-hf'''    ,     device_map='''auto''' )
     lowerCAmelCase_			=      model(torch.tensor(_lowerCamelCase ) )
     # Expected mean on dim = -1
     lowerCAmelCase_			=      torch.tensor([[-0.85_62, -1.85_20, -0.75_51, -0.41_62, -1.51_61, -1.20_38, -2.48_23, -2.32_54]] )
     torch.testing.assert_close(out.mean(-1 )    ,     _lowerCamelCase    ,     atol=1E-2    ,     rtol=1E-2 )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     lowerCAmelCase_			=      torch.tensor([-2.22_27, 4.88_28, 0.90_23, -0.45_78, -0.78_71, -0.10_33, -0.62_21, -0.57_86, -0.78_03, -1.06_74, -1.29_20, -0.15_70, 0.80_08, 2.07_23, -0.94_97, 0.27_71, -2.22_27, -0.76_12, -1.43_46, -1.20_61, -1.64_26, -0.30_00, -0.71_39, -1.19_34, -1.86_91, -1.69_73, -1.59_47, -1.27_05, -0.35_23, -0.55_13] )
     # fmt: on
     torch.testing.assert_close(out.mean(-1 )    ,     _lowerCamelCase    ,     atol=1E-2    ,     rtol=1E-2 )





  @unittest.skip(
      '''Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test''' )
  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      [1, 306, 4658, 278, 6593, 310, 2834, 338]
     lowerCAmelCase_			=      LlamaForCausalLM.from_pretrained('''meta-llama/Llama-2-70b-hf'''    ,     device_map='''auto''' )
     lowerCAmelCase_			=      model(torch.tensor(_lowerCamelCase ) )

     lowerCAmelCase_			=      torch.tensor(
         [[-4.23_27, -3.33_60, -4.66_65, -4.76_31, -1.81_80, -3.41_70, -1.42_11, -3.18_10]]    ,     dtype=torch.floataa )
     torch.testing.assert_close(out.mean(-1 )    ,     _lowerCamelCase    ,     atol=1E-2    ,     rtol=1E-2 )
     # fmt: off
     lowerCAmelCase_			=      torch.tensor([-9.49_22, -3.95_51, 1.79_98, -5.67_58, -5.10_55, -5.89_84, -4.83_20, -6.80_86, -6.53_91, -5.61_72, -5.58_20, -5.53_52, 1.78_81, 3.62_89, -6.51_17, -3.47_85, -9.50_00, -6.03_52, -6.81_25, -6.01_95, -6.68_36, -5.47_27, -6.28_12, -6.03_91, -7.33_98, -7.42_97, -7.48_44, -6.58_20, -5.87_89, -5.53_12] )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]    ,     _lowerCamelCase    ,     atol=1E-5    ,     rtol=1E-5 )







  @unittest.skip('''Model is curently gated''' )
  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      '''Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi'''
     lowerCAmelCase_			=      '''Simply put, the theory of relativity states that '''
     lowerCAmelCase_			=      LlamaTokenizer.from_pretrained('''meta-llama/Llama-2-13b-chat-hf''' )
     lowerCAmelCase_			=      tokenizer.encode(_lowerCamelCase    ,     return_tensors='''pt''' )
     lowerCAmelCase_			=      LlamaForCausalLM.from_pretrained(
         '''meta-llama/Llama-2-13b-chat-hf'''    ,     device_map='''sequential'''    ,     use_safetensors=_lowerCamelCase )

     # greedy generation outputs
     lowerCAmelCase_			=      model.generate(_lowerCamelCase    ,     max_new_tokens=64    ,     top_p=_lowerCamelCase    ,     temperature=1    ,     do_sample=_lowerCamelCase )
     lowerCAmelCase_			=      tokenizer.decode(generated_ids[0]    ,     skip_special_tokens=_lowerCamelCase )
     self.assertEqual(_lowerCamelCase    ,     _lowerCamelCase )






'''simple docstring'''

import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
 from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
 from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
 from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


A_     :						Optional[Any]    							=DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A_     :						Tuple    							='''main'''
# Default branch name
A_     :						Dict    							='''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'''
# One particular commit (not the top of `main`)
A_     :						int    							='''aaaaaaa'''
# This commit does not exist, so we should 404.

A_     :						List[str]    							='''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684'''
# Sha-1 of config.json on the top of `main`, for checking purposes
A_     :						List[str]    							='''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'''



@contextlib.contextmanager
def snake_case_      (					)			->					Tuple:
   print('''Welcome!''')
   yield
   print('''Bye!''')



@contextlib.contextmanager
def snake_case_      (					)			->					str:
   print('''Bonjour!''')
   yield
   print('''Au revoir!''')


class     __UpperCAmelCase       ( unittest.TestCase		):





  def 						UpperCAmelCase_	(	self ):
     # If the spec is missing, importlib would not be able to import the module dynamically.
     assert transformers.__spec__ is not None
     assert importlib.util.find_spec('''transformers''' ) is not None






class     __UpperCAmelCase       ( unittest.TestCase		):





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([] ):
        print('''Transformers are awesome!''' )
     # The print statement adds a new line at the end of the output
     self.assertEqual(mock_stdout.getvalue()    ,     '''Transformers are awesome!\n''' )





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([context_en()] ):
        print('''Transformers are awesome!''' )
     # The output should be wrapped with an English welcome and goodbye
     self.assertEqual(mock_stdout.getvalue()    ,     '''Welcome!\nTransformers are awesome!\nBye!\n''' )





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([context_fr(), context_en()] ):
        print('''Transformers are awesome!''' )
     # The output should be wrapped with an English and French welcome and goodbye
     self.assertEqual(mock_stdout.getvalue()    ,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n''' )





  @require_torch
  def 						UpperCAmelCase_	(	self ):
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels''', '''next_sentence_label'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''start_positions''', '''end_positions'''] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )





  @require_tf
  def 						UpperCAmelCase_	(	self ):
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels''', '''next_sentence_label'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''start_positions''', '''end_positions'''] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )







  @require_flax
  def 						UpperCAmelCase_	(	self ):
     # Flax models don't have labels
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )





