import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel



class     _A   :






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=13   ,				SCREAMING_SNAKE_CASE_=7   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=False   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=99   ,				SCREAMING_SNAKE_CASE_=32   ,				SCREAMING_SNAKE_CASE_=5   ,				SCREAMING_SNAKE_CASE_=4   ,				SCREAMING_SNAKE_CASE_=37   ,				SCREAMING_SNAKE_CASE_="gelu"   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=512   ,				SCREAMING_SNAKE_CASE_=16   ,				SCREAMING_SNAKE_CASE_=2   ,				SCREAMING_SNAKE_CASE_=0.02   ,				SCREAMING_SNAKE_CASE_=3   ,				SCREAMING_SNAKE_CASE_=4   ,				SCREAMING_SNAKE_CASE_=None   ,				)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      parent
       UpperCamelCase__										=      batch_size
       UpperCamelCase__										=      seq_length
       UpperCamelCase__										=      is_training
       UpperCamelCase__										=      use_input_mask
       UpperCamelCase__										=      use_token_type_ids
       UpperCamelCase__										=      use_labels
       UpperCamelCase__										=      vocab_size
       UpperCamelCase__										=      hidden_size
       UpperCamelCase__										=      num_hidden_layers
       UpperCamelCase__										=      num_attention_heads
       UpperCamelCase__										=      intermediate_size
       UpperCamelCase__										=      hidden_act
       UpperCamelCase__										=      hidden_dropout_prob
       UpperCamelCase__										=      attention_probs_dropout_prob
       UpperCamelCase__										=      max_position_embeddings
       UpperCamelCase__										=      type_vocab_size
       UpperCamelCase__										=      type_sequence_label_size
       UpperCamelCase__										=      initializer_range
       UpperCamelCase__										=      num_labels
       UpperCamelCase__										=      num_choices
       UpperCamelCase__										=      scope






  def 		_a					(self	)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size	)

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

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

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

       UpperCamelCase__										=      self.get_config()

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






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       return OpenLlamaConfig(
           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=UpperCamelCase_   ,				initializer_range=self.initializer_range   ,				use_stable_embedding=UpperCamelCase_   ,				)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      OpenLlamaModel(config=UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_	)
       UpperCamelCase__										=      model(UpperCamelCase_	)
       self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				)		->      Optional[int]:





       '''simple docstring'''





       UpperCamelCase__										=      True
       UpperCamelCase__										=      OpenLlamaModel(UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(
           UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				encoder_hidden_states=UpperCamelCase_   ,				encoder_attention_mask=UpperCamelCase_   ,				)
       UpperCamelCase__										=      model(
           UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				encoder_hidden_states=UpperCamelCase_   ,				)
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_	)
       self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				)		->      Any:





       '''simple docstring'''





       UpperCamelCase__										=      OpenLlamaForCausalLM(config=UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				labels=UpperCamelCase_	)
       self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				)		->      Optional[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      True
       UpperCamelCase__										=      True
       UpperCamelCase__										=      OpenLlamaForCausalLM(config=UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()

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

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

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

       UpperCamelCase__										=      model(
           UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				encoder_hidden_states=UpperCamelCase_   ,				encoder_attention_mask=UpperCamelCase_   ,				output_hidden_states=UpperCamelCase_   ,				)['hidden_states'][0]
       UpperCamelCase__										=      model(
           UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				encoder_hidden_states=UpperCamelCase_   ,				encoder_attention_mask=UpperCamelCase_   ,				past_key_values=UpperCamelCase_   ,				output_hidden_states=UpperCamelCase_   ,				)['hidden_states'][0]

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






  def 		_a					(self	)		->      int:





       '''simple docstring'''





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



@require_torch
class     _A   (    __UpperCamelCase	, __UpperCamelCase	, __UpperCamelCase	, unittest.TestCase							):
  SCREAMING_SNAKE_CASE_	:	int						=(
      (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
  )
  SCREAMING_SNAKE_CASE_	:	List[str]						=(OpenLlamaForCausalLM,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE_	:	Optional[Any]						=(
      {
          "feature-extraction": OpenLlamaModel,
          "text-classification": OpenLlamaForSequenceClassification,
          "text-generation": OpenLlamaForCausalLM,
          "zero-shot": OpenLlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE_	:	Dict						=False
  SCREAMING_SNAKE_CASE_	:	List[str]						=False






  def 		_a					(self	)		->      Union[str, Any]:





       '''simple docstring'''





       UpperCamelCase__										=      OpenLlamaModelTester(self	)
       UpperCamelCase__										=      ConfigTester(self   ,				config_class=UpperCamelCase_   ,				hidden_size=37	)






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       self.config_tester.run_common_tests()






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase_	)






  def 		_a					(self	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__										=      type
            self.model_tester.create_and_check_model(*UpperCamelCase_	)






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__										=      3
       UpperCamelCase__										=      input_dict['input_ids']
       UpperCamelCase__										=      input_ids.ne(1	).to(UpperCamelCase_	)
       UpperCamelCase__										=      ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size	)
       UpperCamelCase__										=      OpenLlamaForSequenceClassification(UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				labels=UpperCamelCase_	)
       self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)	)






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__										=      3
       UpperCamelCase__										=      'single_label_classification'
       UpperCamelCase__										=      input_dict['input_ids']
       UpperCamelCase__										=      input_ids.ne(1	).to(UpperCamelCase_	)
       UpperCamelCase__										=      ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size	)
       UpperCamelCase__										=      OpenLlamaForSequenceClassification(UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				labels=UpperCamelCase_	)
       self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)	)






  def 		_a					(self	)		->      List[str]:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__										=      3
       UpperCamelCase__										=      'multi_label_classification'
       UpperCamelCase__										=      input_dict['input_ids']
       UpperCamelCase__										=      input_ids.ne(1	).to(UpperCamelCase_	)
       UpperCamelCase__										=      ids_tensor(
           [self.model_tester.batch_size, config.num_labels]   ,				self.model_tester.type_sequence_label_size	).to(torch.float	)
       UpperCamelCase__										=      OpenLlamaForSequenceClassification(UpperCamelCase_	)
       model.to(UpperCamelCase_	)
       model.eval()
       UpperCamelCase__										=      model(UpperCamelCase_   ,				attention_mask=UpperCamelCase_   ,				labels=UpperCamelCase_	)
       self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)	)






  @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test'''	)
  def 		_a					(self	)		->      List[Any]:





       '''simple docstring'''





       pass






  @parameterized.expand([('''linear''',), ('''dynamic''',)]	)
  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase__										=      ids_tensor([1, 10]   ,				config.vocab_size	)
       UpperCamelCase__										=      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
       UpperCamelCase__										=      OpenLlamaModel(UpperCamelCase_	)
       original_model.to(UpperCamelCase_	)
       original_model.eval()
       UpperCamelCase__										=      original_model(UpperCamelCase_	).last_hidden_state
       UpperCamelCase__										=      original_model(UpperCamelCase_	).last_hidden_state

       set_seed(42	)  # Fixed seed at init time so the two models get the same random weights
       UpperCamelCase__										=      {'type': scaling_type, 'factor': 10.0}
       UpperCamelCase__										=      OpenLlamaModel(UpperCamelCase_	)
       scaled_model.to(UpperCamelCase_	)
       scaled_model.eval()
       UpperCamelCase__										=      scaled_model(UpperCamelCase_	).last_hidden_state
       UpperCamelCase__										=      scaled_model(UpperCamelCase_	).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(UpperCamelCase_   ,				UpperCamelCase_   ,				atol=1E-5	)	)
       else:
            self.assertFalse(torch.allclose(UpperCamelCase_   ,				UpperCamelCase_   ,				atol=1E-5	)	)

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




# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

__magic_name__								='''2.13.1'''

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse('''3.7'''):
     raise ImportWarning(
         '''To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.'''
     )

if version.parse(pyarrow.__version__).major < 8:
     raise ImportWarning(
         '''To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n'''
         '''If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.'''
     )

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

__magic_name__								=concatenate_datasets
__magic_name__								=DownloadConfig
__magic_name__								=DownloadManager
__magic_name__								=DownloadMode
__magic_name__								=DownloadConfig
__magic_name__								=DownloadMode
__magic_name__								=DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager



