


'''simple docstring'''



from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


__lowercase     :						List[str]	=     logging.get_logger(__name__)






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       Union[tf.Tensor, np.ndarray]       ):
					if isinstance(_SCREAMING_SNAKE_CASE						,				np.ndarray       ):
										return list(tensor.shape       )

					__a      :	Tuple    =  tf.shape(_SCREAMING_SNAKE_CASE       )

					if tensor.shape == tf.TensorShape(_SCREAMING_SNAKE_CASE       ):
										return dynamic

					__a      :	List[Any]    =  tensor.shape.as_list()

					return [dynamic[i] if s is None else s for i, s in enumerate(_SCREAMING_SNAKE_CASE       )]






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       tf.Tensor						,				_SCREAMING_SNAKE_CASE				:       Optional[int] = None						,				_SCREAMING_SNAKE_CASE				:       Optional[str] = None       ):
					return tf.nn.softmax(logits=logits + 1e-9						,				axis=_SCREAMING_SNAKE_CASE						,				name=_SCREAMING_SNAKE_CASE       )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       int						,				_SCREAMING_SNAKE_CASE				:       List[str]						,				_SCREAMING_SNAKE_CASE				:       Tuple						,				_SCREAMING_SNAKE_CASE				:       Tuple=1e-5						,				_SCREAMING_SNAKE_CASE				:       Dict=-1       ):
					# This is a very simplified functional layernorm, designed to duplicate
					# the functionality of PyTorch nn.functional.layer_norm when this is needed to port
					# models in Transformers.

					if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       ):
										raise NotImplementedError('Only 1D weight and bias tensors are supported for now, with only a single axis.'       )

					# Get mean and variance on the axis to be normalized
					__a   ,    __a      :	Any    =  tf.nn.moments(_SCREAMING_SNAKE_CASE						,				axes=[axis]						,				keepdims=_SCREAMING_SNAKE_CASE       )

					if axis != -1:
										# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
										# on every dimension except axis
										__a      :	str    =  [1] * inputs.shape.rank
										__a      :	int    =  shape_list(_SCREAMING_SNAKE_CASE       )[axis]
										__a      :	int    =  tf.reshape(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       )
										__a      :	List[Any]    =  tf.reshape(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       )

					# Compute layer normalization using the batch_normalization
					# function.
					__a      :	str    =  tf.nn.batch_normalization(
					    _SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				offset=_SCREAMING_SNAKE_CASE						,				scale=_SCREAMING_SNAKE_CASE						,				variance_epsilon=_SCREAMING_SNAKE_CASE						,				)
					return outputs






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       List[str]						,				_SCREAMING_SNAKE_CASE				:       Any=0						,				_SCREAMING_SNAKE_CASE				:       Union[str, Any]=-1       ):
					# Replicates the behavior of torch.flatten in TF

					# If end_dim or start_dim is negative, count them from the end
					if end_dim < 0:
										end_dim += input.shape.rank
					if start_dim < 0:
										start_dim += input.shape.rank

					if start_dim == end_dim:
										return input

					__a      :	Any    =  tf.shape(_SCREAMING_SNAKE_CASE       )
					__a      :	Union[str, Any]    =  tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]       )
					__a      :	List[Any]    =  tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]						,				axis=0       )
					return tf.reshape(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       tf.Tensor       ):
					if not isinstance(_SCREAMING_SNAKE_CASE						,				tf.Tensor       ):
										__a      :	List[str]    =  tf.convert_to_tensor(_SCREAMING_SNAKE_CASE       )  # Catches stray NumPy inputs
					if encoder_attention_mask.shape.rank == 3:
										__a      :	Dict    =  encoder_attention_mask[:, None, :, :]
					if encoder_attention_mask.shape.rank == 2:
										__a      :	str    =  encoder_attention_mask[:, None, None, :]
					# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
					# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
					# /transformer/transformer_layers.py#L270
					# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
					# encoder_extended_attention_mask.transpose(-1, -2))
					__a      :	Optional[int]    =  (
					    tf.cast(1						,				encoder_attention_mask.dtype       ) - encoder_extended_attention_mask
					) * encoder_extended_attention_mask.dtype.min

					return encoder_extended_attention_mask






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       tf.Tensor						,				_SCREAMING_SNAKE_CASE				:       int						,				_SCREAMING_SNAKE_CASE				:       str = "input_ids"       ):
					tf.debugging.assert_less(
					    _SCREAMING_SNAKE_CASE						,				tf.cast(_SCREAMING_SNAKE_CASE						,				dtype=tensor.dtype       )						,				message=(
					        F"""The maximum value of {tensor_name} ({tf.math.reduce_max(_SCREAMING_SNAKE_CASE       )}) must be smaller than the embedding """
					        F"""layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time."""
					    )						,				)






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       Dict						,				_SCREAMING_SNAKE_CASE				:       Tuple						,				_SCREAMING_SNAKE_CASE				:       Tuple       ):
					__a      :	Tuple    =  64_512
					# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
					# because in that case even chunking the array would not make the saving
					# possible.
					__a      :	str    =  [x for x in data if len(_SCREAMING_SNAKE_CASE       ) > HDF5_OBJECT_HEADER_LIMIT]

					# Expecting this to never be true.
					if bad_attributes:
										raise RuntimeError(
										    'The following attributes cannot be saved to HDF5 file because '
										    F"""they are larger than {HDF5_OBJECT_HEADER_LIMIT} """
										    F"""bytes: {bad_attributes}"""       )

					__a      :	Any    =  np.asarray(_SCREAMING_SNAKE_CASE       )

					__a      :	str    =  1
					__a      :	List[str]    =  np.array_split(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       )

					# This will never loop forever thanks to the test above.
					while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data       ):
										num_chunks += 1
										__a      :	List[Any]    =  np.array_split(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE       )

					if num_chunks > 1:
										for chunk_id, chunk_data in enumerate(_SCREAMING_SNAKE_CASE       ):
															__a      :	int    =  chunk_data
					else:
										__a      :	Tuple    =  data






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       Tuple						,				_SCREAMING_SNAKE_CASE				:       Dict       ):
					if name in group.attrs:
										__a      :	List[str]    =  [n.decode('utf8'       ) if hasattr(_SCREAMING_SNAKE_CASE						,				'decode'       ) else n for n in group.attrs[name]]
					else:
										__a      :	str    =  []
										__a      :	Optional[int]    =  0
										while "%s%d" % (name, chunk_id) in group.attrs:
															data.extend(
															    [n.decode('utf8'       ) if hasattr(_SCREAMING_SNAKE_CASE						,				'decode'       ) else n for n in group.attrs['%s%d' % (name, chunk_id)]]       )
															chunk_id += 1
					return data






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE				:       Tuple       ):

					def _expand_single_ad_tensor(_SCREAMING_SNAKE_CASE				:       Union[str, Any]       ):
										if isinstance(_SCREAMING_SNAKE_CASE						,				tf.Tensor       ) and t.shape.rank == 1:
															return tf.expand_dims(_SCREAMING_SNAKE_CASE						,				axis=-1       )
										return t

					return tf.nest.map_structure(_expand_single_ad_tensor						,				_SCREAMING_SNAKE_CASE       )







'''simple docstring'''



import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    DebertaVaForMaskedLM,
	    DebertaVaForMultipleChoice,
	    DebertaVaForQuestionAnswering,
	    DebertaVaForSequenceClassification,
	    DebertaVaForTokenClassification,
	    DebertaVaModel,
	)
	from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST





class 							__UpperCamelCase    (		lowerCAmelCase_ ):




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

						'''simple docstring'''





						__a      :	int    =  parent
						__a      :	Union[str, Any]    =  batch_size
						__a      :	Optional[int]    =  seq_length
						__a      :	List[str]    =  is_training
						__a      :	Any    =  use_input_mask
						__a      :	Optional[int]    =  use_token_type_ids
						__a      :	Any    =  use_labels
						__a      :	List[str]    =  vocab_size
						__a      :	str    =  hidden_size
						__a      :	List[str]    =  num_hidden_layers
						__a      :	str    =  num_attention_heads
						__a      :	Optional[int]    =  intermediate_size
						__a      :	Tuple    =  hidden_act
						__a      :	Union[str, Any]    =  hidden_dropout_prob
						__a      :	Dict    =  attention_probs_dropout_prob
						__a      :	Optional[int]    =  max_position_embeddings
						__a      :	Dict    =  type_vocab_size
						__a      :	Any    =  type_sequence_label_size
						__a      :	Dict    =  initializer_range
						__a      :	Optional[Any]    =  num_labels
						__a      :	Optional[Any]    =  num_choices
						__a      :	Union[str, Any]    =  relative_attention
						__a      :	List[str]    =  position_biased_input
						__a      :	List[Any]    =  pos_att_type
						__a      :	Tuple    =  scope




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size							)

						__a      :	List[Any]    =  None
						if self.use_input_mask:
											__a      :	Any    =  ids_tensor([self.batch_size, self.seq_length]						,    vocab_size=2							)

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

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

						__a      :	Optional[int]    =  self.get_config()

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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						return DebertaVaConfig(
						    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						,    relative_attention=self.relative_attention						,    position_biased_input=self.position_biased_input						,    pos_att_type=self.pos_att_type						,    )




	def 				__UpperCAmelCase							(       self						,    __a							):

						'''simple docstring'''





						self.parent.assertListEqual(list(result.loss.size()							)						,    []							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Dict    =  DebertaVaModel(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	Optional[int]    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a							)[0]
						__a      :	str    =  model(__a						,    token_type_ids=__a							)[0]
						__a      :	Optional[int]    =  model(__a							)[0]

						self.parent.assertListEqual(list(sequence_output.size()							)						,    [self.batch_size, self.seq_length, self.hidden_size]							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	int    =  DebertaVaForMaskedLM(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	List[Any]    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)

						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.num_labels
						__a      :	List[Any]    =  DebertaVaForSequenceClassification(__a							)
						model.to(__a							)
						model.eval()
						__a      :	Any    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)
						self.parent.assertListEqual(list(result.logits.size()							)						,    [self.batch_size, self.num_labels]							)
						self.check_loss_output(__a							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Any    =  self.num_labels
						__a      :	Dict    =  DebertaVaForTokenClassification(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	str    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.num_labels)							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	List[str]    =  DebertaVaForQuestionAnswering(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	str    =  model(
						    __a						,    attention_mask=__a						,    token_type_ids=__a						,    start_positions=__a						,    end_positions=__a						,    )
						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						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Optional[int]    =  DebertaVaForMultipleChoice(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	Any    =  input_ids.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	Optional[Any]    =  token_type_ids.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	Optional[int]    =  input_mask.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	int    =  model(
						    __a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a						,    )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_choices)							)





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.prepare_config_and_inputs()
						(
						    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    
						)      :	Dict    =  config_and_inputs
						__a      :	Optional[int]    =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict





@require_torch
class 							__UpperCamelCase    (		lowerCAmelCase_		,				lowerCAmelCase_		,				unittest.TestCase ):
	A_           =   (
	    (
	        DebertaVaModel,
	        DebertaVaForMaskedLM,
	        DebertaVaForSequenceClassification,
	        DebertaVaForTokenClassification,
	        DebertaVaForQuestionAnswering,
	        DebertaVaForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	A_           =   (
	    {
	        "feature-extraction": DebertaVaModel,
	        "fill-mask": DebertaVaForMaskedLM,
	        "question-answering": DebertaVaForQuestionAnswering,
	        "text-classification": DebertaVaForSequenceClassification,
	        "token-classification": DebertaVaForTokenClassification,
	        "zero-shot": DebertaVaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	A_           =   True
	A_           =   False
	A_           =   False
	A_           =   False
	A_           =   False




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  DebertaVaModelTester(self							)
						__a      :	List[str]    =  ConfigTester(self						,    config_class=__a						,    hidden_size=37							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[Any]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*__a							)





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a      :	str    =  DebertaVaModel.from_pretrained(__a							)
											self.assertIsNotNone(__a							)





@require_torch
@require_sentencepiece
@require_tokenizers
class 							__UpperCamelCase    (		unittest.TestCase ):




	@unittest.skip(reason='Model not available yet'							)
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						pass





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'							)

						__a      :	Optional[Any]    =  torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]							)
						__a      :	str    =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
						with torch.no_grad():
											__a      :	int    =  model(__a						,    attention_mask=__a							)[0]
						# compare the actual values for a slice.
						__a      :	str    =  torch.tensor(
						    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]							)
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,    __a						,    atol=1E-4							)						,    f"""{output[:, 1:4, 1:4]}"""							)



