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 					_lowercase    :






    '''simple docstring'''




    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.0_2       ,						lowerCamelCase__=3       ,						lowerCamelCase__=4       ,						lowerCamelCase__=None       ,						):
       lowerCAmelCase_:							List[Any]       =	parent
       lowerCAmelCase_:							List[str]       =	batch_size
       lowerCAmelCase_:							List[str]       =	seq_length
       lowerCAmelCase_:							Any       =	is_training
       lowerCAmelCase_:							str       =	use_input_mask
       lowerCAmelCase_:							Tuple       =	use_token_type_ids
       lowerCAmelCase_:							int       =	use_labels
       lowerCAmelCase_:							Union[str, Any]       =	vocab_size
       lowerCAmelCase_:							Any       =	hidden_size
       lowerCAmelCase_:							Dict       =	num_hidden_layers
       lowerCAmelCase_:							Optional[Any]       =	num_attention_heads
       lowerCAmelCase_:							int       =	intermediate_size
       lowerCAmelCase_:							int       =	hidden_act
       lowerCAmelCase_:							int       =	hidden_dropout_prob
       lowerCAmelCase_:							Optional[int]       =	attention_probs_dropout_prob
       lowerCAmelCase_:							int       =	max_position_embeddings
       lowerCAmelCase_:							Optional[int]       =	type_vocab_size
       lowerCAmelCase_:							str       =	type_sequence_label_size
       lowerCAmelCase_:							Tuple       =	initializer_range
       lowerCAmelCase_:							str       =	num_labels
       lowerCAmelCase_:							List[str]       =	num_choices
       lowerCAmelCase_:							Dict       =	scope




    def      _a    (    self ):
       lowerCAmelCase_:							Optional[int]       =	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

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

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

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

       lowerCAmelCase_:							Optional[Any]       =	self.get_config()

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




    def      _a    (    self ):
       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=lowerCamelCase__       ,						initializer_range=self.initializer_range       ,						use_stable_embedding=lowerCamelCase__       ,						)




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							Optional[Any]       =	OpenLlamaModel(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							Any       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__ )
       lowerCAmelCase_:							List[Any]       =	model(lowerCamelCase__ )
       self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size) )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						):
       lowerCAmelCase_:							Dict       =	True
       lowerCAmelCase_:							Optional[int]       =	OpenLlamaModel(lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							int       =	model(
           lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						encoder_hidden_states=lowerCamelCase__       ,						encoder_attention_mask=lowerCamelCase__       ,						)
       lowerCAmelCase_:							Union[str, Any]       =	model(
           lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						encoder_hidden_states=lowerCamelCase__       ,						)
       lowerCAmelCase_:							Optional[Any]       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__ )
       self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size) )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						):
       lowerCAmelCase_:							List[Any]       =	OpenLlamaForCausalLM(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							Optional[int]       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size) )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						):
       lowerCAmelCase_:							Optional[int]       =	True
       lowerCAmelCase_:							Optional[Any]       =	True
       lowerCAmelCase_:							Union[str, Any]       =	OpenLlamaForCausalLM(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()

       # first forward pass
       lowerCAmelCase_:							Tuple       =	model(
           lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						encoder_hidden_states=lowerCamelCase__       ,						encoder_attention_mask=lowerCamelCase__       ,						use_cache=lowerCamelCase__       ,						)
       lowerCAmelCase_:							Optional[int]       =	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_:							Dict       =	ids_tensor((self.batch_size, 3)       ,						vocab_size=2 )

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

       lowerCAmelCase_:							Optional[Any]       =	model(
           lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						encoder_hidden_states=lowerCamelCase__       ,						encoder_attention_mask=lowerCamelCase__       ,						output_hidden_states=lowerCamelCase__       ,						)["hidden_states"][0]
       lowerCAmelCase_:							Union[str, Any]       =	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_:							Tuple       =	ids_tensor((1,)       ,						output_from_past.shape[-1] ).item()
       lowerCAmelCase_:							Union[str, Any]       =	output_from_no_past[:, -3:, random_slice_idx].detach()
       lowerCAmelCase_:							Dict       =	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      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	self.prepare_config_and_inputs()
       (
           (
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	(
           lowerCAmelCase_
       )  ,	
       ):							List[str]       =	config_and_inputs
       lowerCAmelCase_:							Union[str, Any]       =	{"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict





@require_torch
class 					_lowercase    (     UpperCAmelCase__ ,  UpperCAmelCase__ ,  UpperCAmelCase__ ,  unittest.TestCase			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Any          =    (
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    SCREAMING_SNAKE_CASE:				List[str]          =    (OpenLlamaForCausalLM,) if is_torch_available() else ()
    SCREAMING_SNAKE_CASE:				Dict          =    (
        {
            'feature-extraction': OpenLlamaModel,
            'text-classification': OpenLlamaForSequenceClassification,
            'text-generation': OpenLlamaForCausalLM,
            'zero-shot': OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    SCREAMING_SNAKE_CASE:				str          =    False
    SCREAMING_SNAKE_CASE:				Tuple          =    False




    def      _a    (    self ):
       lowerCAmelCase_:							Union[str, Any]       =	OpenLlamaModelTester(self )
       lowerCAmelCase_:							List[Any]       =	ConfigTester(self       ,						config_class=lowerCamelCase__       ,						hidden_size=37 )




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




    def      _a    (    self ):
       lowerCAmelCase_:							Any       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							str       =	self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
          lowerCAmelCase_:							Union[str, Any]       =	type
          self.model_tester.create_and_check_model(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_  ,	lowerCAmelCase_:							int       =	self.model_tester.prepare_config_and_inputs_for_common()
       lowerCAmelCase_:							Dict       =	3
       lowerCAmelCase_:							Any       =	input_dict["input_ids"]
       lowerCAmelCase_:							Tuple       =	input_ids.ne(1 ).to(lowerCamelCase__ )
       lowerCAmelCase_:							Optional[int]       =	ids_tensor([self.model_tester.batch_size]       ,						self.model_tester.type_sequence_label_size )
       lowerCAmelCase_:							str       =	OpenLlamaForSequenceClassification(lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							List[Any]       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       self.assertEqual(result.logits.shape       ,						(self.model_tester.batch_size, self.model_tester.num_labels) )




    def      _a    (    self ):
       lowerCAmelCase_  ,	lowerCAmelCase_:							Optional[Any]       =	self.model_tester.prepare_config_and_inputs_for_common()
       lowerCAmelCase_:							Dict       =	3
       lowerCAmelCase_:							Tuple       =	"single_label_classification"
       lowerCAmelCase_:							Tuple       =	input_dict["input_ids"]
       lowerCAmelCase_:							str       =	input_ids.ne(1 ).to(lowerCamelCase__ )
       lowerCAmelCase_:							List[Any]       =	ids_tensor([self.model_tester.batch_size]       ,						self.model_tester.type_sequence_label_size )
       lowerCAmelCase_:							Optional[Any]       =	OpenLlamaForSequenceClassification(lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							List[Any]       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       self.assertEqual(result.logits.shape       ,						(self.model_tester.batch_size, self.model_tester.num_labels) )




    def      _a    (    self ):
       lowerCAmelCase_  ,	lowerCAmelCase_:							Tuple       =	self.model_tester.prepare_config_and_inputs_for_common()
       lowerCAmelCase_:							Optional[Any]       =	3
       lowerCAmelCase_:							List[str]       =	"multi_label_classification"
       lowerCAmelCase_:							str       =	input_dict["input_ids"]
       lowerCAmelCase_:							Dict       =	input_ids.ne(1 ).to(lowerCamelCase__ )
       lowerCAmelCase_:							List[str]       =	ids_tensor(
           [self.model_tester.batch_size, config.num_labels]       ,						self.model_tester.type_sequence_label_size ).to(torch.float )
       lowerCAmelCase_:							Tuple       =	OpenLlamaForSequenceClassification(lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							str       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       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 ):
       pass




    @parameterized.expand([("linear",), ("dynamic",)] )
    def      _a    (    self       ,						lowerCamelCase__ ):
       lowerCAmelCase_  ,	lowerCAmelCase_:							int       =	self.model_tester.prepare_config_and_inputs_for_common()
       lowerCAmelCase_:							Optional[int]       =	ids_tensor([1, 10]       ,						config.vocab_size )
       lowerCAmelCase_:							Any       =	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_:							int       =	OpenLlamaModel(lowerCamelCase__ )
       original_model.to(lowerCamelCase__ )
       original_model.eval()
       lowerCAmelCase_:							List[Any]       =	original_model(lowerCamelCase__ ).last_hidden_state
       lowerCAmelCase_:							Dict       =	original_model(lowerCamelCase__ ).last_hidden_state

       set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
       lowerCAmelCase_:							str       =	{"type": scaling_type, "factor": 1_0.0}
       lowerCAmelCase_:							Union[str, Any]       =	OpenLlamaModel(lowerCamelCase__ )
       scaled_model.to(lowerCamelCase__ )
       scaled_model.eval()
       lowerCAmelCase_:							List[str]       =	scaled_model(lowerCamelCase__ ).last_hidden_state
       lowerCAmelCase_:							List[str]       =	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 ) )
import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand
def 		snake_case__	(		lowercase					):
   return EnvironmentCommand()







class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''




    @staticmethod
    def      _a    (    lowerCamelCase__ ):
       lowerCAmelCase_:							Tuple       =	parser.add_parser("env" )
       download_parser.set_defaults(func=lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							int       =	huggingface_hub.__version__

       lowerCAmelCase_:							List[str]       =	"not installed"
       lowerCAmelCase_:							Dict       =	"NA"
       if is_torch_available():
          import torch

          lowerCAmelCase_:							List[str]       =	torch.__version__
          lowerCAmelCase_:							int       =	torch.cuda.is_available()

       lowerCAmelCase_:							Tuple       =	"not installed"
       if is_transformers_available():
          import transformers

          lowerCAmelCase_:							Tuple       =	transformers.__version__

       lowerCAmelCase_:							Optional[int]       =	"not installed"
       if is_accelerate_available():
          import accelerate

          lowerCAmelCase_:							List[Any]       =	accelerate.__version__

       lowerCAmelCase_:							Dict       =	"not installed"
       if is_xformers_available():
          import xformers

          lowerCAmelCase_:							Dict       =	xformers.__version__

       lowerCAmelCase_:							Union[str, Any]       =	{
           "`diffusers` version": version,
           "Platform": platform.platform(),
           "Python version": platform.python_version(),
           "PyTorch version (GPU?)": F'''{pt_version} ({pt_cuda_available})''',
           "Huggingface_hub version": hub_version,
           "Transformers version": transformers_version,
           "Accelerate version": accelerate_version,
           "xFormers version": xformers_version,
           "Using GPU in script?": "<fill in>",
           "Using distributed or parallel set-up in script?": "<fill in>",
       }

       print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n" )
       print(self.format_dict(lowerCamelCase__ ) )

       return info




    @staticmethod
    def      _a    (    lowerCamelCase__ ):
       return "\n".join([F'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"