

"""simple docstring"""




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      :

  def __init__(	self		:      str       ,A		:      Tuple       ,A		:      List[str]=13       ,A		:      List[str]=7       ,A		:      Union[str, Any]=True       ,A		:      Tuple=True       ,A		:      List[Any]=False       ,A		:      int=True       ,A		:      str=99       ,A		:      int=32       ,A		:      Optional[Any]=5       ,A		:      int=4       ,A		:      int=37       ,A		:      Union[str, Any]="gelu"       ,A		:      Dict=0.1       ,A		:      Optional[int]=0.1       ,A		:      List[str]=512       ,A		:      Union[str, Any]=16       ,A		:      int=2       ,A		:      Optional[Any]=0.0_2       ,A		:      Optional[int]=3       ,A		:      Dict=4       ,A		:      Tuple=None       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       parent
   UpperCAmelCase__       :	Any        =       batch_size
   UpperCAmelCase__       :	int        =       seq_length
   UpperCAmelCase__       :	Any        =       is_training
   UpperCAmelCase__       :	Any        =       use_input_mask
   UpperCAmelCase__       :	List[Any]        =       use_token_type_ids
   UpperCAmelCase__       :	List[str]        =       use_labels
   UpperCAmelCase__       :	Optional[int]        =       vocab_size
   UpperCAmelCase__       :	str        =       hidden_size
   UpperCAmelCase__       :	List[str]        =       num_hidden_layers
   UpperCAmelCase__       :	Dict        =       num_attention_heads
   UpperCAmelCase__       :	Optional[Any]        =       intermediate_size
   UpperCAmelCase__       :	str        =       hidden_act
   UpperCAmelCase__       :	int        =       hidden_dropout_prob
   UpperCAmelCase__       :	Any        =       attention_probs_dropout_prob
   UpperCAmelCase__       :	Tuple        =       max_position_embeddings
   UpperCAmelCase__       :	Tuple        =       type_vocab_size
   UpperCAmelCase__       :	List[str]        =       type_sequence_label_size
   UpperCAmelCase__       :	List[Any]        =       initializer_range
   UpperCAmelCase__       :	Optional[Any]        =       num_labels
   UpperCAmelCase__       :	List[str]        =       num_choices
   UpperCAmelCase__       :	List[str]        =       scope

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

   UpperCAmelCase__       :	Optional[int]        =       None
   if self.use_input_mask:
    UpperCAmelCase__       :	Any        =       random_attention_mask([self.batch_size, self.seq_length]     )

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

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

   UpperCAmelCase__       :	Tuple        =       self.get_config()

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

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

  def  __lowercase							(	self		:      List[str]       ,A		:      Any       ,A		:      List[Any]       ,A		:      str       ,A		:      List[str]       ,A		:      int       ,A		:      str       ,A		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       OpenLlamaModel(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	int        =       model(__a       ,attention_mask=__a     )
   UpperCAmelCase__       :	List[Any]        =       model(__a     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)     )

  def  __lowercase							(	self		:      Optional[Any]       ,A		:      Tuple       ,A		:      Tuple       ,A		:      Tuple       ,A		:      Union[str, Any]       ,A		:      List[str]       ,A		:      int       ,A		:      Union[str, Any]       ,A		:      Optional[Any]       ,A		:      int       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       True
   UpperCAmelCase__       :	Optional[int]        =       OpenLlamaModel(__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	Tuple        =       model(
       __a       ,attention_mask=__a       ,encoder_hidden_states=__a       ,encoder_attention_mask=__a       ,)
   UpperCAmelCase__       :	Dict        =       model(
       __a       ,attention_mask=__a       ,encoder_hidden_states=__a       ,)
   UpperCAmelCase__       :	Optional[int]        =       model(__a       ,attention_mask=__a     )
   self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)     )

  def  __lowercase							(	self		:      Optional[int]       ,A		:      Dict       ,A		:      Tuple       ,A		:      Optional[Any]       ,A		:      Dict       ,A		:      str       ,A		:      Optional[Any]       ,A		:      Optional[Any]       ,A		:      Any       ,A		:      Tuple       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       OpenLlamaForCausalLM(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	Union[str, Any]        =       model(__a       ,attention_mask=__a       ,labels=__a     )
   self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)     )

  def  __lowercase							(	self		:      Tuple       ,A		:      Optional[int]       ,A		:      Optional[int]       ,A		:      List[Any]       ,A		:      Any       ,A		:      Any       ,A		:      Union[str, Any]       ,A		:      Optional[int]       ,A		:      List[Any]       ,A		:      str       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       True
   UpperCAmelCase__       :	Optional[int]        =       True
   UpperCAmelCase__       :	Dict        =       OpenLlamaForCausalLM(config=__a     )
   model.to(__a     )
   model.eval()

   # first forward pass
   UpperCAmelCase__       :	List[Any]        =       model(
       __a       ,attention_mask=__a       ,encoder_hidden_states=__a       ,encoder_attention_mask=__a       ,use_cache=__a       ,)
   UpperCAmelCase__       :	int        =       outputs.past_key_values

   # create hypothetical multiple next token and extent to next_input_ids
   UpperCAmelCase__       :	Tuple        =       ids_tensor((self.batch_size, 3)       ,config.vocab_size     )
   UpperCAmelCase__       :	List[str]        =       ids_tensor((self.batch_size, 3)       ,vocab_size=2     )

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

   UpperCAmelCase__       :	Tuple        =       model(
       __a       ,attention_mask=__a       ,encoder_hidden_states=__a       ,encoder_attention_mask=__a       ,output_hidden_states=__a       ,)["""hidden_states"""][0]
   UpperCAmelCase__       :	List[str]        =       model(
       __a       ,attention_mask=__a       ,encoder_hidden_states=__a       ,encoder_attention_mask=__a       ,past_key_values=__a       ,output_hidden_states=__a       ,)["""hidden_states"""][0]

   # select random slice
   UpperCAmelCase__       :	str        =       ids_tensor((1,)       ,output_from_past.shape[-1]     ).item()
   UpperCAmelCase__       :	int        =       output_from_no_past[:, -3:, random_slice_idx].detach()
   UpperCAmelCase__       :	Tuple        =       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(__a       ,__a       ,atol=1e-3     )     )




  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,         
   )       :	Union[str, Any]        =       config_and_inputs
   UpperCAmelCase__       :	Any        =       {"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_torch
class   __lowercase      (     __lowerCamelCase						,      __lowerCamelCase						,      __lowerCamelCase						,      unittest.TestCase       ):
  snake_case_												=							(
      (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
  )
  snake_case_												=							(OpenLlamaForCausalLM,) if is_torch_available() else ()
  snake_case_												=							(
      {
          """feature-extraction""": OpenLlamaModel,
          """text-classification""": OpenLlamaForSequenceClassification,
          """text-generation""": OpenLlamaForCausalLM,
          """zero-shot""": OpenLlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  snake_case_												=							False
  snake_case_												=							False

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       OpenLlamaModelTester(self     )
   UpperCAmelCase__       :	int        =       ConfigTester(self       ,config_class=__a       ,hidden_size=37     )

  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*__a     )

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
    UpperCAmelCase__       :	Optional[Any]        =       type
    self.model_tester.create_and_check_model(*__a     )

  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	List[str]        =       self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase__       :	str        =       3
   UpperCAmelCase__       :	Optional[int]        =       input_dict["""input_ids"""]
   UpperCAmelCase__       :	int        =       input_ids.ne(1     ).to(__a     )
   UpperCAmelCase__       :	Optional[Any]        =       ids_tensor([self.model_tester.batch_size]       ,self.model_tester.type_sequence_label_size     )
   UpperCAmelCase__       :	List[Any]        =       OpenLlamaForSequenceClassification(__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	Optional[int]        =       model(__a       ,attention_mask=__a       ,labels=__a     )
   self.assertEqual(result.logits.shape       ,(self.model_tester.batch_size, self.model_tester.num_labels)     )

  def  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	Dict        =       self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase__       :	str        =       3
   UpperCAmelCase__       :	List[str]        =       """single_label_classification"""
   UpperCAmelCase__       :	Any        =       input_dict["""input_ids"""]
   UpperCAmelCase__       :	str        =       input_ids.ne(1     ).to(__a     )
   UpperCAmelCase__       :	Any        =       ids_tensor([self.model_tester.batch_size]       ,self.model_tester.type_sequence_label_size     )
   UpperCAmelCase__       :	Optional[Any]        =       OpenLlamaForSequenceClassification(__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	int        =       model(__a       ,attention_mask=__a       ,labels=__a     )
   self.assertEqual(result.logits.shape       ,(self.model_tester.batch_size, self.model_tester.num_labels)     )

  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	Optional[Any]        =       self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase__       :	Tuple        =       3
   UpperCAmelCase__       :	Optional[int]        =       """multi_label_classification"""
   UpperCAmelCase__       :	Optional[int]        =       input_dict["""input_ids"""]
   UpperCAmelCase__       :	List[str]        =       input_ids.ne(1     ).to(__a     )
   UpperCAmelCase__       :	Dict        =       ids_tensor(
       [self.model_tester.batch_size, config.num_labels]       ,self.model_tester.type_sequence_label_size     ).to(torch.float     )
   UpperCAmelCase__       :	Optional[Any]        =       OpenLlamaForSequenceClassification(__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	List[Any]        =       model(__a       ,attention_mask=__a       ,labels=__a     )
   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  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   pass




  @parameterized.expand([("""linear""",), ("""dynamic""",)]     )
  def  __lowercase							(	self		:      int       ,A		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase__       :	Optional[int]        =       ids_tensor([1, 10]       ,config.vocab_size     )
   UpperCAmelCase__       :	Optional[int]        =       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__       :	Optional[int]        =       OpenLlamaModel(__a     )
   original_model.to(__a     )
   original_model.eval()
   UpperCAmelCase__       :	str        =       original_model(__a     ).last_hidden_state
   UpperCAmelCase__       :	int        =       original_model(__a     ).last_hidden_state

   set_seed(42     )  # Fixed seed at init time so the two models get the same random weights
   UpperCAmelCase__       :	Any        =       {"""type""": scaling_type, """factor""": 10.0}
   UpperCAmelCase__       :	Union[str, Any]        =       OpenLlamaModel(__a     )
   scaled_model.to(__a     )
   scaled_model.eval()
   UpperCAmelCase__       :	Union[str, Any]        =       scaled_model(__a     ).last_hidden_state
   UpperCAmelCase__       :	Any        =       scaled_model(__a     ).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(__a       ,__a       ,atol=1e-5     )     )
   else:
    self.assertFalse(torch.allclose(__a       ,__a       ,atol=1e-5     )     )

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






'''simple docstring'''


from __future__ import annotations

__lowerCAmelCase						:							List[str]    							=						8.988e9  # units = N * m^s * C^-2





def    lowerCAmelCase       (		UpperCamelCase__					: float     ,       UpperCamelCase__					: float     ,       UpperCamelCase__					: float     ,       UpperCamelCase__					: float							):


	"""simple docstring"""
	__UpperCAmelCase      					=      abs(chargea * chargea							)

	if (force, chargea, chargea, distance).count(0							) != 1:
		raise ValueError('''One and only one argument must be 0'''							)
	if distance < 0:
		raise ValueError('''Distance cannot be negative'''							)
	if force == 0:
		__UpperCAmelCase      					=      COULOMBS_CONSTANT * charge_product / (distance**2)
		return {"force": force}
	elif chargea == 0:
		__UpperCAmelCase      					=      abs(UpperCamelCase__							) * (distance**2) / (COULOMBS_CONSTANT * chargea)
		return {"charge1": chargea}
	elif chargea == 0:
		__UpperCAmelCase      					=      abs(UpperCamelCase__							) * (distance**2) / (COULOMBS_CONSTANT * chargea)
		return {"charge2": chargea}
	elif distance == 0:
		__UpperCAmelCase      					=      (COULOMBS_CONSTANT * charge_product / abs(UpperCamelCase__							)) ** 0.5
		return {"distance": distance}
	raise ValueError('''Exactly one argument must be 0'''							)


if __name__ == "__main__":
					import doctest

					doctest.testmod()
