






import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s''',
    datefmt='''%m/%d/%Y %H:%M:%S''',
    level=logging.INFO,
)
A_       :List[Any]       =						logging.getLogger(__name__)






def 							A     (       a_					)     ->						Any:
					__UpperCamelCase      :					int       		=git.Repo(search_parent_directories=_A					)
					__UpperCamelCase      :					str       		={
					    'repo_id': str(_A					),
					    'repo_sha': str(repo.head.object.hexsha					),
					    'repo_branch': str(repo.active_branch					),
					}

					with open(os.path.join(_A							,'git_log.json'					)							,'w'					) as f:
										json.dump(_A							,_A							,indent=4					)






def 							A     (       a_					)     ->						Optional[int]:







					if params.n_gpu <= 0:
										__UpperCamelCase      :					str       		=0
										__UpperCamelCase      :					Optional[Any]       		=-1
										__UpperCamelCase      :					List[str]       		=True
										__UpperCamelCase      :					Optional[Any]       		=False
										return

					assert torch.cuda.is_available()

					logger.info('Initializing GPUs'					)
					if params.n_gpu > 1:
										assert params.local_rank != -1

										__UpperCamelCase      :					Tuple       		=int(os.environ['WORLD_SIZE']					)
										__UpperCamelCase      :					Any       		=int(os.environ['N_GPU_NODE']					)
										__UpperCamelCase      :					int       		=int(os.environ['RANK']					)

										# number of nodes / node ID
										__UpperCamelCase      :					Any       		=params.world_size // params.n_gpu_per_node
										__UpperCamelCase      :					Optional[Any]       		=params.global_rank // params.n_gpu_per_node
										__UpperCamelCase      :					Dict       		=True

										assert params.n_nodes == int(os.environ['N_NODES']					)
										assert params.node_id == int(os.environ['NODE_RANK']					)

					# local job (single GPU)
					else:
										assert params.local_rank == -1

										__UpperCamelCase      :					Tuple       		=1
										__UpperCamelCase      :					int       		=0
										__UpperCamelCase      :					str       		=0
										__UpperCamelCase      :					int       		=0
										__UpperCamelCase      :					Tuple       		=1
										__UpperCamelCase      :					List[Any]       		=1
										__UpperCamelCase      :					int       		=False

					# sanity checks
					assert params.n_nodes >= 1
					assert 0 <= params.node_id < params.n_nodes
					assert 0 <= params.local_rank <= params.global_rank < params.world_size
					assert params.world_size == params.n_nodes * params.n_gpu_per_node

					# define whether this is the master process / if we are in multi-node distributed mode
					__UpperCamelCase      :					Dict       		=params.node_id == 0 and params.local_rank == 0
					__UpperCamelCase      :					Tuple       		=params.n_nodes > 1

					# summary
					__UpperCamelCase      :					Dict       		=F'--- Global rank: {params.global_rank} - '
					logger.info(PREFIX + 'Number of nodes: %i' % params.n_nodes					)
					logger.info(PREFIX + 'Node ID        : %i' % params.node_id					)
					logger.info(PREFIX + 'Local rank     : %i' % params.local_rank					)
					logger.info(PREFIX + 'World size     : %i' % params.world_size					)
					logger.info(PREFIX + 'GPUs per node  : %i' % params.n_gpu_per_node					)
					logger.info(PREFIX + 'Master         : %s' % str(params.is_master					)					)
					logger.info(PREFIX + 'Multi-node     : %s' % str(params.multi_node					)					)
					logger.info(PREFIX + 'Multi-GPU      : %s' % str(params.multi_gpu					)					)
					logger.info(PREFIX + 'Hostname       : %s' % socket.gethostname()					)

					# set GPU device
					torch.cuda.set_device(params.local_rank					)

					# initialize multi-GPU
					if params.multi_gpu:
										logger.info('Initializing PyTorch distributed'					)
										torch.distributed.init_process_group(
										    init_method='env://'							,backend='nccl'							,)






def 							A     (       a_					)     ->						Optional[int]:
					np.random.seed(args.seed					)
					torch.manual_seed(args.seed					)
					if args.n_gpu > 0:
										torch.cuda.manual_seed_all(args.seed					)




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

   '''simple docstring'''




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






     """simple docstring"""






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



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






     """simple docstring"""






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

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

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

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

     a__  =self.get_config()

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



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






     """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=lowercase_,    initializer_range=self.initializer_range,    use_stable_embedding=lowercase_,    )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    List[str]:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    Any:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[str]:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[Any]:






     """simple docstring"""






     a__  =True
     a__  =True
     a__  =OpenLlamaForCausalLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()

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

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

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

     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    output_hidden_states=lowercase_,    )['''hidden_states'''][0]
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    past_key_values=lowercase_,    output_hidden_states=lowercase_,    )['''hidden_states'''][0]

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



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






     """simple docstring"""






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







@require_torch
class 						__magic_name__			(							lowerCamelCase__		,       lowerCamelCase__		,       lowerCamelCase__		,       unittest.TestCase							):

   '''simple docstring'''




   lowerCamelCase__ :	Optional[Any]    =   (
       (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
   )
   lowerCamelCase__ :	Optional[Any]    =   (OpenLlamaForCausalLM,) if is_torch_available() else ()
   lowerCamelCase__ :	List[str]    =   (
       {
           'feature-extraction': OpenLlamaModel,
           'text-classification': OpenLlamaForSequenceClassification,
           'text-generation': OpenLlamaForCausalLM,
           'zero-shot': OpenLlamaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   lowerCamelCase__ :	int    =   False
   lowerCamelCase__ :	Any    =   False



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






     """simple docstring"""






     a__  =OpenLlamaModelTester(self       )
     a__  =ConfigTester(self,    config_class=lowercase_,    hidden_size=37       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     self.config_tester.run_common_tests()



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






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






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



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






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =3
     a__  ='''multi_label_classification'''
     a__  =input_dict['''input_ids''']
     a__  =input_ids.ne(1       ).to(lowercase_       )
     a__  =ids_tensor(
         [self.model_tester.batch_size, config.num_labels],    self.model_tester.type_sequence_label_size       ).to(torch.float       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     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  _UpperCAmelCase (				self       )							->    List[str]:






     """simple docstring"""






     pass



   @parameterized.expand([('''linear''',), ('''dynamic''',)]       )
   def  _UpperCAmelCase (				self,    lowercase_       )							->    Optional[Any]:






     """simple docstring"""






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

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

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