





"""simple docstring"""
from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import (
           TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
           TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
           TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
           TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
           TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
           LayoutLMvaConfig,
           TFLayoutLMvaForQuestionAnswering,
           TFLayoutLMvaForSequenceClassification,
           TFLayoutLMvaForTokenClassification,
           TFLayoutLMvaModel,
       )

if is_vision_available():
       from PIL import Image

       from transformers import LayoutLMvaImageProcessor







class       a							:



 def __init__(     self				,       _snake_case				,       _snake_case=2				,       _snake_case=3				,       _snake_case=4				,       _snake_case=2				,       _snake_case=7				,       _snake_case=True				,       _snake_case=True				,       _snake_case=True				,       _snake_case=True				,       _snake_case=99				,       _snake_case=36				,       _snake_case=2				,       _snake_case=4				,       _snake_case=37				,       _snake_case="gelu"				,       _snake_case=0.1				,       _snake_case=0.1				,       _snake_case=5_12				,       _snake_case=16				,       _snake_case=2				,       _snake_case=0.02				,       _snake_case=6				,       _snake_case=6				,       _snake_case=3				,       _snake_case=4				,       _snake_case=None				,       _snake_case=10_00				,       ):







  """simple docstring"""






  lowerCAmelCase    				= parent
  lowerCAmelCase    				= batch_size
  lowerCAmelCase    				= num_channels
  lowerCAmelCase    				= image_size
  lowerCAmelCase    				= patch_size
  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    				= coordinate_size
  lowerCAmelCase    				= shape_size
  lowerCAmelCase    				= num_labels
  lowerCAmelCase    				= num_choices
  lowerCAmelCase    				= scope
  lowerCAmelCase    				= range_bbox

  # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
  lowerCAmelCase    				= text_seq_length
  lowerCAmelCase    				= (image_size // patch_size) ** 2 + 1
  lowerCAmelCase    				= self.text_seq_length + self.image_seq_length



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= ids_tensor([self.batch_size, self.text_seq_length]				,       self.vocab_size     )

  lowerCAmelCase    				= ids_tensor([self.batch_size, self.text_seq_length, 4]				,       self.range_bbox     )
  lowerCAmelCase    				= bbox.numpy()
  # Ensure that bbox is legal
  for i in range(bbox.shape[0]     ):
   for j in range(bbox.shape[1]     ):
    if bbox[i, j, 3] < bbox[i, j, 1]:
     lowerCAmelCase    				= bbox[i, j, 3]
     lowerCAmelCase    				= bbox[i, j, 1]
     lowerCAmelCase    				= tmp_coordinate
    if bbox[i, j, 2] < bbox[i, j, 0]:
     lowerCAmelCase    				= bbox[i, j, 2]
     lowerCAmelCase    				= bbox[i, j, 0]
     lowerCAmelCase    				= tmp_coordinate
  lowerCAmelCase    				= tf.constant(_snake_case     )

  lowerCAmelCase    				= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

  lowerCAmelCase    				= None
  if self.use_input_mask:
   lowerCAmelCase    				= random_attention_mask([self.batch_size, self.text_seq_length]     )

  lowerCAmelCase    				= None
  if self.use_token_type_ids:
   lowerCAmelCase    				= ids_tensor([self.batch_size, self.text_seq_length]				,       self.type_vocab_size     )

  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.text_seq_length]				,       self.num_labels     )

  lowerCAmelCase    				= LayoutLMvaConfig(
      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				,       initializer_range=self.initializer_range				,       coordinate_size=self.coordinate_size				,       shape_size=self.shape_size				,       input_size=self.image_size				,       patch_size=self.patch_size				,       )

  return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= TFLayoutLMvaModel(config=_snake_case     )

  # text + image
  lowerCAmelCase    				= model(_snake_case				,       pixel_values=_snake_case				,       training=_snake_case     )
  lowerCAmelCase    				= model(
      _snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       attention_mask=_snake_case				,       token_type_ids=_snake_case				,       training=_snake_case				,       )
  lowerCAmelCase    				= model(_snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       training=_snake_case     )

  self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)     )

  # text only
  lowerCAmelCase    				= model(_snake_case				,       training=_snake_case     )
  self.parent.assertEqual(
      result.last_hidden_state.shape				,       (self.batch_size, self.text_seq_length, self.hidden_size)     )

  # image only
  lowerCAmelCase    				= model({'pixel_values': pixel_values}				,       training=_snake_case     )
  self.parent.assertEqual(
      result.last_hidden_state.shape				,       (self.batch_size, self.image_seq_length, self.hidden_size)     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= self.num_labels
  lowerCAmelCase    				= TFLayoutLMvaForSequenceClassification(config=_snake_case     )
  lowerCAmelCase    				= model(
      _snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       attention_mask=_snake_case				,       token_type_ids=_snake_case				,       labels=_snake_case				,       training=_snake_case				,       )
  self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= self.num_labels
  lowerCAmelCase    				= TFLayoutLMvaForTokenClassification(config=_snake_case     )
  lowerCAmelCase    				= model(
      _snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       attention_mask=_snake_case				,       token_type_ids=_snake_case				,       labels=_snake_case				,       training=_snake_case				,       )
  self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.text_seq_length, self.num_labels)     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= 2
  lowerCAmelCase    				= TFLayoutLMvaForQuestionAnswering(config=_snake_case     )
  lowerCAmelCase    				= model(
      _snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       attention_mask=_snake_case				,       token_type_ids=_snake_case				,       start_positions=_snake_case				,       end_positions=_snake_case				,       training=_snake_case				,       )
  self.parent.assertEqual(result.start_logits.shape				,       (self.batch_size, self.seq_length)     )
  self.parent.assertEqual(result.end_logits.shape				,       (self.batch_size, self.seq_length)     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= self.prepare_config_and_inputs()
  ((lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase)				,(lowerCAmelCase))    				= config_and_inputs
  lowerCAmelCase    				= {
      'input_ids': input_ids,
      'bbox': bbox,
      'pixel_values': pixel_values,
      'token_type_ids': token_type_ids,
      'attention_mask': input_mask,
  }
  return config, inputs_dict







@require_tf
class       a							(   a__   , a__   , unittest.TestCase  ):
 snake_case__             =       (
     (
         TFLayoutLMvaModel,
         TFLayoutLMvaForQuestionAnswering,
         TFLayoutLMvaForSequenceClassification,
         TFLayoutLMvaForTokenClassification,
     )
     if is_tf_available()
     else ()
 )
 snake_case__             =       (
     {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
     if is_tf_available()
     else {}
 )

 snake_case__             =       False
 snake_case__             =       False
 snake_case__             =       False



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  return True



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case=False     ):







  """simple docstring"""






  lowerCAmelCase    				= copy.deepcopy(_snake_case     )

  if model_class in get_values(_snake_case     ):
   lowerCAmelCase    				= {
       k: tf.tile(tf.expand_dims(_snake_case				,       1     )				,       (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)     )
       if isinstance(_snake_case				,       tf.Tensor     ) and v.ndim > 0
       else v
       for k, v in inputs_dict.items()
   }

  if return_labels:
   if model_class in get_values(_snake_case     ):
    lowerCAmelCase    				= tf.ones(self.model_tester.batch_size				,       dtype=tf.intaa     )
   elif model_class in get_values(_snake_case     ):
    lowerCAmelCase    				= tf.zeros(self.model_tester.batch_size				,       dtype=tf.intaa     )
    lowerCAmelCase    				= tf.zeros(self.model_tester.batch_size				,       dtype=tf.intaa     )
   elif model_class in get_values(_snake_case     ):
    lowerCAmelCase    				= tf.zeros(self.model_tester.batch_size				,       dtype=tf.intaa     )
   elif model_class in get_values(_snake_case     ):
    lowerCAmelCase    				= tf.zeros(
        (self.model_tester.batch_size, self.model_tester.text_seq_length)				,       dtype=tf.intaa     )

  return inputs_dict



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= TFLayoutLMvaModelTester(self     )
  lowerCAmelCase    				= ConfigTester(self				,       config_class=_snake_case				,       hidden_size=37     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  self.config_tester.run_common_tests()



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase				,lowerCAmelCase    				= self.model_tester.prepare_config_and_inputs_for_common()
  for model_class in self.all_model_classes:
   lowerCAmelCase    				= model_class(_snake_case     )
   if getattr(_snake_case				,       'hf_compute_loss'				,       _snake_case     ):
    # The number of elements in the loss should be the same as the number of elements in the label
    lowerCAmelCase    				= self._prepare_for_class(inputs_dict.copy()				,       _snake_case				,       return_labels=_snake_case     )
    lowerCAmelCase    				= prepared_for_class[
        sorted(prepared_for_class.keys() - inputs_dict.keys()				,       reverse=_snake_case     )[0]
    ]
    lowerCAmelCase    				= added_label.shape.as_list()[:1]

    # Test that model correctly compute the loss with kwargs
    lowerCAmelCase    				= self._prepare_for_class(inputs_dict.copy()				,       _snake_case				,       return_labels=_snake_case     )
    lowerCAmelCase    				= prepared_for_class.pop('input_ids'     )

    lowerCAmelCase    				= model(_snake_case				,       **_snake_case     )[0]
    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )

    # Test that model correctly compute the loss when we mask some positions
    lowerCAmelCase    				= self._prepare_for_class(inputs_dict.copy()				,       _snake_case				,       return_labels=_snake_case     )
    lowerCAmelCase    				= prepared_for_class.pop('input_ids'     )
    if "labels" in prepared_for_class:
     lowerCAmelCase    				= prepared_for_class['labels'].numpy()
     if len(labels.shape     ) > 1 and labels.shape[1] != 1:
      lowerCAmelCase    				= -1_00
      lowerCAmelCase    				= tf.convert_to_tensor(_snake_case     )
      lowerCAmelCase    				= model(_snake_case				,       **_snake_case     )[0]
      self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )
      self.assertTrue(not np.any(np.isnan(loss.numpy()     )     )     )

                # Test that model correctly compute the loss with a dict
    lowerCAmelCase    				= self._prepare_for_class(inputs_dict.copy()				,       _snake_case				,       return_labels=_snake_case     )
    lowerCAmelCase    				= model(_snake_case     )[0]
    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )

    # Test that model correctly compute the loss with a tuple
    lowerCAmelCase    				= self._prepare_for_class(inputs_dict.copy()				,       _snake_case				,       return_labels=_snake_case     )

    # Get keys that were added with the _prepare_for_class function
    lowerCAmelCase    				= prepared_for_class.keys() - inputs_dict.keys()
    lowerCAmelCase    				= inspect.signature(model.call     ).parameters
    lowerCAmelCase    				= list(signature.keys()     )

    # Create a dictionary holding the location of the tensors in the tuple
    lowerCAmelCase    				= {0: 'input_ids'}
    for label_key in label_keys:
     lowerCAmelCase    				= signature_names.index(_snake_case     )
     lowerCAmelCase    				= label_key
    lowerCAmelCase    				= sorted(tuple_index_mapping.items()     )
    # Initialize a list with their default values, update the values and convert to a tuple
    lowerCAmelCase    				= []

    for name in signature_names:
     if name != "kwargs":
      list_input.append(signature[name].default     )

    for index, value in sorted_tuple_index_mapping:
     lowerCAmelCase    				= prepared_for_class[value]

    lowerCAmelCase    				= tuple(_snake_case     )

    # Send to model
    lowerCAmelCase    				= model(tuple_input[:-1]     )[0]

    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  (
      (
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,
  )    				= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(_snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  (
      (
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      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(_snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  (
      (
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,
  )    				= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_sequence_classification(
      _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  (
      (
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,
  )    				= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_token_classification(
      _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  (
      (
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,(
      lowerCAmelCase
  )				,
  )    				= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_question_answering(
      _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     )



 @slow
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   lowerCAmelCase    				= TFLayoutLMvaModel.from_pretrained(_snake_case     )
   self.assertIsNotNone(_snake_case     )


def 		_SCREAMING_SNAKE_CASE			():
 lowerCAmelCase    				= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
 return image







@require_tf
class       a							(   unittest.TestCase  ):



 @cached_property
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  return LayoutLMvaImageProcessor(apply_ocr=_snake_case     ) if is_vision_available() else None



 @slow
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'     )

  lowerCAmelCase    				= self.default_image_processor
  lowerCAmelCase    				= prepare_img()
  lowerCAmelCase    				= image_processor(images=_snake_case				,       return_tensors='tf'     ).pixel_values

  lowerCAmelCase    				= tf.constant([[1, 2]]     )
  lowerCAmelCase    				= tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]     )				,       axis=0     )

  # forward pass
  lowerCAmelCase    				= model(input_ids=_snake_case				,       bbox=_snake_case				,       pixel_values=_snake_case				,       training=_snake_case     )

  # verify the logits
  lowerCAmelCase    				= (1, 1_99, 7_68)
  self.assertEqual(outputs.last_hidden_state.shape				,       _snake_case     )

  lowerCAmelCase    				= tf.constant(
      [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]     )

  self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3]				,       _snake_case				,       atol=1E-4     )     )












"""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       a							:



 def __init__(     self				,       _snake_case				,       _snake_case=13				,       _snake_case=7				,       _snake_case=True				,       _snake_case=True				,       _snake_case=False				,       _snake_case=True				,       _snake_case=99				,       _snake_case=32				,       _snake_case=5				,       _snake_case=4				,       _snake_case=37				,       _snake_case="gelu"				,       _snake_case=0.1				,       _snake_case=0.1				,       _snake_case=5_12				,       _snake_case=16				,       _snake_case=2				,       _snake_case=0.02				,       _snake_case=3				,       _snake_case=4				,       _snake_case=None				,       ):







  """simple docstring"""






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







  """simple docstring"""






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







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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       ):







  """simple docstring"""






  lowerCAmelCase    				= True
  lowerCAmelCase    				= True
  lowerCAmelCase    				= OpenLlamaForCausalLM(config=_snake_case     )
  model.to(_snake_case     )
  model.eval()

  # first forward pass
  lowerCAmelCase    				= model(
      _snake_case				,       attention_mask=_snake_case				,       encoder_hidden_states=_snake_case				,       encoder_attention_mask=_snake_case				,       use_cache=_snake_case				,       )
  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(
      _snake_case				,       attention_mask=_snake_case				,       encoder_hidden_states=_snake_case				,       encoder_attention_mask=_snake_case				,       output_hidden_states=_snake_case				,       )['hidden_states'][0]
  lowerCAmelCase    				= model(
      _snake_case				,       attention_mask=_snake_case				,       encoder_hidden_states=_snake_case				,       encoder_attention_mask=_snake_case				,       past_key_values=_snake_case				,       output_hidden_states=_snake_case				,       )['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(_snake_case				,       _snake_case				,       atol=1E-3     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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       a							(   a__   , a__   , a__   , 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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= OpenLlamaModelTester(self     )
  lowerCAmelCase    				= ConfigTester(self				,       config_class=_snake_case				,       hidden_size=37     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  self.config_tester.run_common_tests()



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*_snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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(*_snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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(_snake_case     )
  lowerCAmelCase    				= ids_tensor([self.model_tester.batch_size]				,       self.model_tester.type_sequence_label_size     )
  lowerCAmelCase    				= OpenLlamaForSequenceClassification(_snake_case     )
  model.to(_snake_case     )
  model.eval()
  lowerCAmelCase    				= model(_snake_case				,       attention_mask=_snake_case				,       labels=_snake_case     )
  self.assertEqual(result.logits.shape				,       (self.model_tester.batch_size, self.model_tester.num_labels)     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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(_snake_case     )
  lowerCAmelCase    				= ids_tensor(
      [self.model_tester.batch_size, config.num_labels]				,       self.model_tester.type_sequence_label_size     ).to(torch.float     )
  lowerCAmelCase    				= OpenLlamaForSequenceClassification(_snake_case     )
  model.to(_snake_case     )
  model.eval()
  lowerCAmelCase    				= model(_snake_case				,       attention_mask=_snake_case				,       labels=_snake_case     )
  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     ):







  """simple docstring"""






  pass



 @parameterized.expand([('linear',), ('dynamic',)]     )
 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  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    				= OpenLlamaModel(_snake_case     )
  original_model.to(_snake_case     )
  original_model.eval()
  lowerCAmelCase    				= original_model(_snake_case     ).last_hidden_state
  lowerCAmelCase    				= original_model(_snake_case     ).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    				= OpenLlamaModel(_snake_case     )
  scaled_model.to(_snake_case     )
  scaled_model.eval()
  lowerCAmelCase    				= scaled_model(_snake_case     ).last_hidden_state
  lowerCAmelCase    				= scaled_model(_snake_case     ).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(_snake_case				,       _snake_case				,       atol=1E-5     )     )
  else:
   self.assertFalse(torch.allclose(_snake_case				,       _snake_case				,       atol=1E-5     )     )

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





