




'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFDebertaForMaskedLM,
      TFDebertaForQuestionAnswering,
      TFDebertaForSequenceClassification,
      TFDebertaForTokenClassification,
      TFDebertaModel,
  )




class      UpperCamelCase       :



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







      '''simple docstring'''


      _a				:							List[str]							       =      parent
      _a				:							str							       =      13
      _a				:							str							       =      7
      _a				:							Any							       =      True
      _a				:							Union[str, Any]							       =      True
      _a				:							List[Any]							       =      True
      _a				:							List[Any]							       =      True
      _a				:							Union[str, Any]							       =      99
      _a				:							str							       =      32
      _a				:							Tuple							       =      2
      _a				:							List[Any]							       =      4
      _a				:							Any							       =      37
      _a				:							Any							       =      """gelu"""
      _a				:							str							       =      0.1
      _a				:							Tuple							       =      0.1
      _a				:							Tuple							       =      512
      _a				:							Optional[int]							       =      16
      _a				:							Optional[int]							       =      False
      _a				:							Tuple							       =      -1
      _a				:							Optional[int]							       =      True
      _a				:							Tuple							       =      2
      _a				:							int							       =      0.02
      _a				:							List[str]							       =      3
      _a				:							Any							       =      4
      _a				:							List[Any]							       =      None



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[Any]							       =      ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size )

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

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

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

      _a				:							int							       =      DebertaConfig(
          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		,     relative_attention=self.relative_attention		,     max_relative_positions=self.max_relative_positions		,     position_biased_input=self.position_biased_input		,     initializer_range=self.initializer_range		,     return_dict=__lowerCAmelCase		,     )

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



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







      '''simple docstring'''


      _a				:							str							       =      TFDebertaModel(config=__lowerCAmelCase )
      _a				:							Optional[int]							       =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

      _a				:							Optional[int]							       =      [input_ids, input_mask]
      _a				:							Optional[Any]							       =      model(__lowerCAmelCase )

      _a				:							Any							       =      model(__lowerCAmelCase )

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



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







      '''simple docstring'''


      _a				:							str							       =      TFDebertaForMaskedLM(config=__lowerCAmelCase )
      _a				:							Dict							       =      {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }
      _a				:							Optional[int]							       =      model(__lowerCAmelCase )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size) )



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







      '''simple docstring'''


      _a				:							Optional[int]							       =      self.num_labels
      _a				:							str							       =      TFDebertaForSequenceClassification(config=__lowerCAmelCase )
      _a				:							Dict							       =      {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }

      _a				:							List[Any]							       =      model(__lowerCAmelCase )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels) )



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







      '''simple docstring'''


      _a				:							Any							       =      self.num_labels
      _a				:							Optional[int]							       =      TFDebertaForTokenClassification(config=__lowerCAmelCase )
      _a				:							int							       =      {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }
      _a				:							Union[str, Any]							       =      model(__lowerCAmelCase )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels) )



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







      '''simple docstring'''


      _a				:							Any							       =      TFDebertaForQuestionAnswering(config=__lowerCAmelCase )
      _a				:							Union[str, Any]							       =      {
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
          """token_type_ids""": token_type_ids,
      }

      _a				:							Optional[Any]							       =      model(__lowerCAmelCase )
      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     a_							(			self ):







      '''simple docstring'''


      _a				:							int							       =      self.prepare_config_and_inputs()
      (
          (
          _a
      )       , (
          _a
      )       , (
          _a
      )       , (
          _a
      )       , (
          _a
      )       , (
          _a
      )       , (
          _a
      )       , 
      )				:							Optional[Any]							       =      config_and_inputs
      _a				:							str							       =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict






@require_tf
class      UpperCamelCase       (	snake_case__      ,		snake_case__      ,		unittest.TestCase       ):
  a     :int          =					(
      (
          TFDebertaModel,
          TFDebertaForMaskedLM,
          TFDebertaForQuestionAnswering,
          TFDebertaForSequenceClassification,
          TFDebertaForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  a     :List[str]          =					(
      {
          'feature-extraction': TFDebertaModel,
          'fill-mask': TFDebertaForMaskedLM,
          'question-answering': TFDebertaForQuestionAnswering,
          'text-classification': TFDebertaForSequenceClassification,
          'token-classification': TFDebertaForTokenClassification,
          'zero-shot': TFDebertaForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )

  a     :str          =					False
  a     :Dict          =					False



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[int]							       =      TFDebertaModelTester(self )
      _a				:							List[Any]							       =      ConfigTester(self		,     config_class=__lowerCAmelCase		,     hidden_size=37 )



  def     a_							(			self ):







      '''simple docstring'''


      self.config_tester.run_common_tests()



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Any							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							str							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							int							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[int]							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Union[str, Any]							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase )



  @slow
  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Union[str, Any]							       =      TFDebertaModel.from_pretrained("""kamalkraj/deberta-base""" )
      self.assertIsNotNone(__lowerCAmelCase )






@require_tf
class      UpperCamelCase       (	unittest.TestCase       ):



  @unittest.skip(reason="""Model not available yet""" )
  def     a_							(			self ):







      '''simple docstring'''


      pass



  @slow
  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Tuple							       =      TFDebertaModel.from_pretrained("""kamalkraj/deberta-base""" )
      _a				:							List[Any]							       =      tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] )
      _a				:							Optional[Any]							       =      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
      _a				:							List[str]							       =      model(__lowerCAmelCase		,     attention_mask=__lowerCAmelCase )[0]

      _a				:							Tuple							       =      tf.constant(
          [
              [
                  [-0.59855896, -0.80552566, -0.8462135],
                  [1.4484025, -0.93483794, -0.80593085],
                  [0.3122741, 0.00316059, -1.4131377],
              ]
          ] )
      tf.debugging.assert_near(output[:, 1:4, 1:4]		,     __lowerCAmelCase		,     atol=1e-4 )








from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			if not inplace:
						UpperCamelCase_     				= ma + ma
			else:
						ma += ma

			return ma




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= -1 * len(__snake_case					)
			UpperCamelCase_     				= list(range(len(tensor.shape[:zero_index]					)					)					)
			return tensor.permute(first_inds + [zero_index + i for i in inds]					)




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			return t.reshape(t.shape[:-no_dims] + (-1,)					)




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case     ,		__snake_case = 1E-4					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= mask.expand(*value.shape					)
			return torch.sum(mask * value     ,		dim=__snake_case					) / (eps + torch.sum(__snake_case     ,		dim=__snake_case					))




def  a_		(							__snake_case     ,		__snake_case = 2.31_25     ,		__snake_case = 21.68_75     ,		__snake_case = 6_4					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= torch.linspace(__snake_case     ,		__snake_case     ,		no_bins - 1     ,		device=pts.device					)
			UpperCamelCase_     				= torch.sqrt(torch.sum((pts.unsqueeze(-2					) - pts.unsqueeze(-3					)) ** 2     ,		dim=-1					)					)
			return torch.bucketize(__snake_case     ,		__snake_case					)




def  a_		(							__snake_case     ,		__snake_case					)	->			dict:

			'''simple docstring'''


			UpperCamelCase_     				= dicts[0]
			UpperCamelCase_     				= {}
			for k, v in first.items():
						UpperCamelCase_     				= [d[k] for d in dicts]
						if isinstance(__snake_case     ,		__snake_case					):
									UpperCamelCase_     				= dict_multimap(__snake_case     ,		__snake_case					)
						else:
									UpperCamelCase_     				= fn(__snake_case					)

			return new_dict




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= v_bins.view(((1,) * len(x.shape					)) + (len(__snake_case					),)					)
			UpperCamelCase_     				= x[..., None] - reshaped_bins
			UpperCamelCase_     				= torch.argmin(torch.abs(__snake_case					)     ,		dim=-1					)
			return nn.functional.one_hot(__snake_case     ,		num_classes=len(__snake_case					)					).float()




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case = 0     ,		__snake_case = 0					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= []
			for i, s in enumerate(data.shape[:no_batch_dims]					):
						UpperCamelCase_     				= torch.arange(__snake_case					)
						UpperCamelCase_     				= r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape					) - i - 1)))					)
						ranges.append(__snake_case					)

			UpperCamelCase_     				= [slice(__snake_case					) for _ in range(len(data.shape					) - no_batch_dims					)]
			UpperCamelCase_     				= inds
			ranges.extend(__snake_case					)
			# Matt note: Editing this to get around the behaviour of using a list as an array index changing
			# in recent Numpy versions
			return data[tuple(__snake_case					)]


__a			:	List[Any]							     =		TypeVar("""T""")




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Dict[Any, Union[dict, list, tuple, Any]]:

			'''simple docstring'''


			UpperCamelCase_     				= {}
			for k, v in dic.items():
						if isinstance(__snake_case     ,		__snake_case					):
									UpperCamelCase_     				= dict_map(__snake_case     ,		__snake_case     ,		__snake_case					)
						else:
									UpperCamelCase_     				= tree_map(__snake_case     ,		__snake_case     ,		__snake_case					)

			return new_dict




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Any:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			dict:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			list:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			tuple:

			'''simple docstring'''


			...




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Any:

			'''simple docstring'''


			if isinstance(__snake_case     ,		__snake_case					):
						return dict_map(__snake_case     ,		__snake_case     ,		__snake_case					)
			elif isinstance(__snake_case     ,		__snake_case					):
						return [tree_map(__snake_case     ,		__snake_case     ,		__snake_case					) for x in tree]
			elif isinstance(__snake_case     ,		__snake_case					):
						return tuple(tree_map(__snake_case     ,		__snake_case     ,		__snake_case					) for x in tree					)
			elif isinstance(__snake_case     ,		__snake_case					):
						return fn(__snake_case					)
			else:
						print(type(__snake_case					)					)
						raise ValueError('Not supported'					)





__a			:	Tuple							     =		partial(tree_map, leaf_type=torch.Tensor)
