



'''simple docstring'''




import unittest

from transformers import DebertaConfig, 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 (
						    DebertaForMaskedLM,
						    DebertaForQuestionAnswering,
						    DebertaForSequenceClassification,
						    DebertaForTokenClassification,
						    DebertaModel,
						)
						from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def __init__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				str=1_3							,  lowerCAmelCase__  :				Union[str, Any]=7							,  lowerCAmelCase__  :				Dict=True							,  lowerCAmelCase__  :				int=True							,  lowerCAmelCase__  :				Optional[int]=True							,  lowerCAmelCase__  :				Optional[int]=True							,  lowerCAmelCase__  :				Union[str, Any]=9_9							,  lowerCAmelCase__  :				Dict=3_2							,  lowerCAmelCase__  :				Dict=5							,  lowerCAmelCase__  :				int=4							,  lowerCAmelCase__  :				Dict=3_7							,  lowerCAmelCase__  :				Union[str, Any]="gelu"							,  lowerCAmelCase__  :				Optional[Any]=0.1							,  lowerCAmelCase__  :				Dict=0.1							,  lowerCAmelCase__  :				Dict=5_1_2							,  lowerCAmelCase__  :				Tuple=1_6							,  lowerCAmelCase__  :				Dict=2							,  lowerCAmelCase__  :				List[Any]=0.02							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Dict=True							,  lowerCAmelCase__  :				Optional[int]="None"							,  lowerCAmelCase__  :				Dict=3							,  lowerCAmelCase__  :				Union[str, Any]=4							,  lowerCAmelCase__  :				Dict=None							,  ):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size		)

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

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

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

					__SCREAMING_SNAKE_CASE :	int					 =	self.get_config()

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

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					return 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							,  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  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.get_config()
					__SCREAMING_SNAKE_CASE :	Any					 =	3_0_0
					return config

			def UpperCamelCase__      (							self  :				str							,  lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				Any							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Dict							,  lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	DebertaModel(config=lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	Dict					 =	model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__		)[0]
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	model(lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__		)[0]
					__SCREAMING_SNAKE_CASE :	Any					 =	model(lowerCAmelCase__		)[0]

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

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Dict							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	DebertaForMaskedLM(config=lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	Tuple					 =	model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  labels=lowerCAmelCase__		)

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

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






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

			def UpperCamelCase__      (							self  :				str							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	self.num_labels
					__SCREAMING_SNAKE_CASE :	Tuple					 =	DebertaForTokenClassification(config=lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	str					 =	model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  labels=lowerCAmelCase__		)
					self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.num_labels)		)

			def UpperCamelCase__      (							self  :				Optional[int]							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				List[Any]							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Tuple					 =	DebertaForQuestionAnswering(config=lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	int					 =	model(
					    lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  start_positions=lowerCAmelCase__							,  end_positions=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 UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.prepare_config_and_inputs()
					(
					    (
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		(
					    __SCREAMING_SNAKE_CASE
					) ,		
					) :	int					 =	config_and_inputs
					__SCREAMING_SNAKE_CASE :	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					):


			'''simple docstring'''

			_A   :	Tuple    					=	(
			    (
			        DebertaModel,
			        DebertaForMaskedLM,
			        DebertaForSequenceClassification,
			        DebertaForTokenClassification,
			        DebertaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			_A   :	Dict    					=	(
			    {
			        '''feature-extraction''': DebertaModel,
			        '''fill-mask''': DebertaForMaskedLM,
			        '''question-answering''': DebertaForQuestionAnswering,
			        '''text-classification''': DebertaForSequenceClassification,
			        '''token-classification''': DebertaForTokenClassification,
			        '''zero-shot''': DebertaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			_A   :	Optional[int]    					=	True
			_A   :	List[Any]    					=	False
			_A   :	List[Any]    					=	False
			_A   :	Optional[int]    					=	False
			_A   :	Union[str, Any]    					=	False

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	DebertaModelTester(self		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	ConfigTester(self							,  config_class=lowerCAmelCase__							,  hidden_size=3_7		)

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					self.config_tester.run_common_tests()

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_model(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Union[str, Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase__		)



			@slow
			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE :	List[str]					 =	DebertaModel.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)






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


			'''simple docstring'''

			@unittest.skip(reason="""Model not available yet"""		)
			def UpperCamelCase__      (							self  :				Union[str, Any]		):





					"""simple docstring"""






					pass



			@slow
			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	DebertaModel.from_pretrained("""microsoft/deberta-base"""		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
					with torch.no_grad():
							__SCREAMING_SNAKE_CASE :	int					 =	model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__		)[0]
					# compare the actual values for a slice.
					__SCREAMING_SNAKE_CASE :	int					 =	torch.tensor(
					    [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]		)
					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							,  lowerCAmelCase__							,  atol=1E-4		)							,  F"{output[:, 1:4, 1:4]}"		)




'''simple docstring'''




# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


UpperCamelCase__ :			Tuple												=				'''3'''

print('''Python version:''', sys.version)
print('''transformers version:''', transformers.__version__)

try:
						import torch

						print('''Torch version:''', torch.__version__)
						print('''Cuda available:''', torch.cuda.is_available())
						print('''Cuda version:''', torch.version.cuda)
						print('''CuDNN version:''', torch.backends.cudnn.version())
						print('''Number of GPUs available:''', torch.cuda.device_count())
						print('''NCCL version:''', torch.cuda.nccl.version())
except ImportError:
						print('''Torch version:''', None)

try:
						import deepspeed

						print('''DeepSpeed version:''', deepspeed.__version__)
except ImportError:
						print('''DeepSpeed version:''', None)

try:
						import tensorflow as tf

						print('''TensorFlow version:''', tf.__version__)
						print('''TF GPUs available:''', bool(tf.config.list_physical_devices('''GPU''')))
						print('''Number of TF GPUs available:''', len(tf.config.list_physical_devices('''GPU''')))
except ImportError:
						print('''TensorFlow version:''', None)