




'''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   ,				UpperCamelCase   ,				UpperCamelCase=13   ,				UpperCamelCase=7   ,				UpperCamelCase=True   ,				UpperCamelCase=True   ,				UpperCamelCase=False   ,				UpperCamelCase=True   ,				UpperCamelCase=99   ,				UpperCamelCase=32   ,				UpperCamelCase=5   ,				UpperCamelCase=4   ,				UpperCamelCase=37   ,				UpperCamelCase="gelu"   ,				UpperCamelCase=0.1   ,				UpperCamelCase=0.1   ,				UpperCamelCase=512   ,				UpperCamelCase=16   ,				UpperCamelCase=2   ,				UpperCamelCase=0.02   ,				UpperCamelCase=3   ,				UpperCamelCase=4   ,				UpperCamelCase=None   ,				)					->     Any:
     __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   )					->     List[str]:
     __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   )					->     Optional[int]:
     return OpenLlamaConfig(
         vocab_size=self.vocab_size   ,				hidden_size=self.hidden_size   ,				num_hidden_layers=self.num_hidden_layers   ,				num_attention_heads=self.num_attention_heads   ,				intermediate_size=self.intermediate_size   ,				hidden_act=self.hidden_act   ,				hidden_dropout_prob=self.hidden_dropout_prob   ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,				max_position_embeddings=self.max_position_embeddings   ,				type_vocab_size=self.type_vocab_size   ,				is_decoder=UpperCamelCase   ,				initializer_range=self.initializer_range   ,				use_stable_embedding=UpperCamelCase   ,				)







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   )					->     str:
     __a								=       OpenLlamaModel(config=UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   )
     __a								=       model(UpperCamelCase   )
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)   )







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				)					->     List[str]:
     __a								=       True
     __a								=       OpenLlamaModel(UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(
         UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				encoder_hidden_states=UpperCamelCase   ,				encoder_attention_mask=UpperCamelCase   ,				)
     __a								=       model(
         UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				encoder_hidden_states=UpperCamelCase   ,				)
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   )
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)   )







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				)					->     Dict:
     __a								=       OpenLlamaForCausalLM(config=UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				labels=UpperCamelCase   )
     self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)   )







   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   ,				)					->     Any:
     __a								=       True
     __a								=       True
     __a								=       OpenLlamaForCausalLM(config=UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()

     # first forward pass
     __a								=       model(
         UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				encoder_hidden_states=UpperCamelCase   ,				encoder_attention_mask=UpperCamelCase   ,				use_cache=UpperCamelCase   ,				)
     __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(
         UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				encoder_hidden_states=UpperCamelCase   ,				encoder_attention_mask=UpperCamelCase   ,				output_hidden_states=UpperCamelCase   ,				)['hidden_states'][0]
     __a								=       model(
         UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				encoder_hidden_states=UpperCamelCase   ,				encoder_attention_mask=UpperCamelCase   ,				past_key_values=UpperCamelCase   ,				output_hidden_states=UpperCamelCase   ,				)['hidden_states'][0]

     # select random slice
     __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(UpperCamelCase   ,				UpperCamelCase   ,				atol=1e-3   )   )







   def 		UpperCamelCase__      (						self   )					->     Optional[int]:
     __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  __lowercase    (	__magic_name__							,				__magic_name__							,				__magic_name__							,				unittest.TestCase			):
   _a    						=   (
       (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
   )
   _a    						=   (OpenLlamaForCausalLM,) if is_torch_available() else ()
   _a    						=   (
       {
           """feature-extraction""": OpenLlamaModel,
           """text-classification""": OpenLlamaForSequenceClassification,
           """text-generation""": OpenLlamaForCausalLM,
           """zero-shot""": OpenLlamaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _a    						=   False
   _a    						=   False







   def 		UpperCamelCase__      (						self   )					->     Dict:
     __a								=       OpenLlamaModelTester(self   )
     __a								=       ConfigTester(self   ,				config_class=UpperCamelCase   ,				hidden_size=37   )







   def 		UpperCamelCase__      (						self   )					->     List[str]:
     self.config_tester.run_common_tests()







   def 		UpperCamelCase__      (						self   )					->     Tuple:
     __a								=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase   )







   def 		UpperCamelCase__      (						self   )					->     Union[str, Any]:
     __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(*UpperCamelCase   )







   def 		UpperCamelCase__      (						self   )					->     Any:
     __a						,							__a								=       self.model_tester.prepare_config_and_inputs_for_common()
     __a								=       3
     __a								=       input_dict['input_ids']
     __a								=       input_ids.ne(1   ).to(UpperCamelCase   )
     __a								=       ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size   )
     __a								=       OpenLlamaForSequenceClassification(UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				labels=UpperCamelCase   )
     self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )







   def 		UpperCamelCase__      (						self   )					->     Any:
     __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(UpperCamelCase   )
     __a								=       ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size   )
     __a								=       OpenLlamaForSequenceClassification(UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				labels=UpperCamelCase   )
     self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )







   def 		UpperCamelCase__      (						self   )					->     Optional[Any]:
     __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(UpperCamelCase   )
     __a								=       ids_tensor(
         [self.model_tester.batch_size, config.num_labels]   ,				self.model_tester.type_sequence_label_size   ).to(torch.float   )
     __a								=       OpenLlamaForSequenceClassification(UpperCamelCase   )
     model.to(UpperCamelCase   )
     model.eval()
     __a								=       model(UpperCamelCase   ,				attention_mask=UpperCamelCase   ,				labels=UpperCamelCase   )
     self.assertEqual(result.logits.shape   ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )







   @unittest.skip('Open-Llama buffers include complex numbers, which breaks this test'   )
   def 		UpperCamelCase__      (						self   )					->     Optional[int]:
     pass







   @parameterized.expand([('linear',), ('dynamic',)]   )
   def 		UpperCamelCase__      (						self   ,				UpperCamelCase   )					->     Tuple:
     __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(UpperCamelCase   )
     original_model.to(UpperCamelCase   )
     original_model.eval()
     __a								=       original_model(UpperCamelCase   ).last_hidden_state
     __a								=       original_model(UpperCamelCase   ).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(UpperCamelCase   )
     scaled_model.to(UpperCamelCase   )
     scaled_model.eval()
     __a								=       scaled_model(UpperCamelCase   ).last_hidden_state
     __a								=       scaled_model(UpperCamelCase   ).last_hidden_state

     # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
     # maximum sequence length, so the outputs for the short input should match.
     if scaling_type == "dynamic":
       self.assertTrue(torch.allclose(UpperCamelCase   ,				UpperCamelCase   ,				atol=1e-5   )   )
     else:
       self.assertFalse(torch.allclose(UpperCamelCase   ,				UpperCamelCase   ,				atol=1e-5   )   )

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









'''simple docstring'''


import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_					=   logging.get_logger(__name__)

UpperCAmelCase_					=   {
    "nvidia/segformer-b0-finetuned-ade-512-512": (
        "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}





class  __lowercase    (	__magic_name__			):
   _a    						=   """segformer"""







   def __init__(						self   ,				UpperCamelCase=3   ,				UpperCamelCase=4   ,				UpperCamelCase=[2, 2, 2, 2]   ,				UpperCamelCase=[8, 4, 2, 1]   ,				UpperCamelCase=[32, 64, 160, 256]   ,				UpperCamelCase=[7, 3, 3, 3]   ,				UpperCamelCase=[4, 2, 2, 2]   ,				UpperCamelCase=[1, 2, 5, 8]   ,				UpperCamelCase=[4, 4, 4, 4]   ,				UpperCamelCase="gelu"   ,				UpperCamelCase=0.0   ,				UpperCamelCase=0.0   ,				UpperCamelCase=0.1   ,				UpperCamelCase=0.02   ,				UpperCamelCase=0.1   ,				UpperCamelCase=1e-6   ,				UpperCamelCase=256   ,				UpperCamelCase=255   ,				**UpperCamelCase   ,				)					->     int:
     super().__init__(**UpperCamelCase   )

     if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
       warnings.warn(
           'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
           ' removed, as the behaviour will default to that of reshape_last_stage = True.'   ,				UpperCamelCase   ,				)

     __a								=       num_channels
     __a								=       num_encoder_blocks
     __a								=       depths
     __a								=       sr_ratios
     __a								=       hidden_sizes
     __a								=       patch_sizes
     __a								=       strides
     __a								=       mlp_ratios
     __a								=       num_attention_heads
     __a								=       hidden_act
     __a								=       hidden_dropout_prob
     __a								=       attention_probs_dropout_prob
     __a								=       classifier_dropout_prob
     __a								=       initializer_range
     __a								=       drop_path_rate
     __a								=       layer_norm_eps
     __a								=       decoder_hidden_size
     __a								=       kwargs.get('reshape_last_stage'   ,				UpperCamelCase   )
     __a								=       semantic_loss_ignore_index





class  __lowercase    (	__magic_name__			):
   _a    						=   version.parse("""1.11"""			)







   @property
   def 		UpperCamelCase__      (						self   )					->     Mapping[str, Mapping[int, str]]:
     return OrderedDict(
         [
             ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
         ]   )







   @property
   def 		UpperCamelCase__      (						self   )					->     float:
     return 1e-4







   @property
   def 		UpperCamelCase__      (						self   )					->     int:
     return 12



