


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 	lowercase       (snake_case__				):
					def __init__(				self       :str      ,__snake_case       :Dict      ,__snake_case       :List[Any]=13      ,__snake_case       :List[Any]=7      ,__snake_case       :Tuple=True      ,__snake_case       :Union[str, Any]=True      ,__snake_case       :Any=True      ,__snake_case       :Any=True      ,__snake_case       :List[str]=99      ,__snake_case       :List[Any]=32      ,__snake_case       :Optional[Any]=5      ,__snake_case       :List[Any]=4      ,__snake_case       :Optional[int]=37      ,__snake_case       :Dict="gelu"      ,__snake_case       :Dict=0.1      ,__snake_case       :List[Any]=0.1      ,__snake_case       :Union[str, Any]=5_12      ,__snake_case       :str=16      ,__snake_case       :Optional[int]=2      ,__snake_case       :str=0.02      ,__snake_case       :Any=False      ,__snake_case       :Tuple=True      ,__snake_case       :Optional[int]="None"      ,__snake_case       :Optional[int]=3      ,__snake_case       :Tuple=4      ,__snake_case       :List[Any]=None      ,) ->   str:
												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__					=					relative_attention
												a__					=					position_biased_input
												a__					=					pos_att_type
												a__					=					scope
					def 			lowerCamelCase__(				self       :List[str]				) ->   Dict:
												a__					=					ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size				)

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

												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 			lowerCamelCase__(				self       :List[str]				) ->   Union[str, Any]:
												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 			lowerCamelCase__(				self       :Dict				) ->   Union[str, Any]:
												a__					=					self.get_config()
												a__					=					3_00
												return config
					def 			lowerCamelCase__(				self       :Tuple      ,__snake_case       :Union[str, Any]				) ->   int:
												self.parent.assertListEqual(list(result.loss.size()				)      ,[]				)
					def 			lowerCamelCase__(				self       :Optional[int]      ,__snake_case       :Optional[Any]      ,__snake_case       :Optional[int]      ,__snake_case       :Union[str, Any]      ,__snake_case       :List[str]      ,__snake_case       :Tuple      ,__snake_case       :List[Any]      ,__snake_case       :Tuple				) ->   List[str]:
												a__					=					DebertaModel(config=_A				)
												model.to(_A				)
												model.eval()
												a__					=					model(_A      ,attention_mask=_A      ,token_type_ids=_A				)[0]
												a__					=					model(_A      ,token_type_ids=_A				)[0]
												a__					=					model(_A				)[0]

												self.parent.assertListEqual(list(sequence_output.size()				)      ,[self.batch_size, self.seq_length, self.hidden_size]				)
					def 			lowerCamelCase__(				self       :Union[str, Any]      ,__snake_case       :int      ,__snake_case       :str      ,__snake_case       :List[str]      ,__snake_case       :int      ,__snake_case       :Any      ,__snake_case       :Any      ,__snake_case       :int				) ->   int:
												a__					=					DebertaForMaskedLM(config=_A				)
												model.to(_A				)
												model.eval()
												a__					=					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 			lowerCamelCase__(				self       :Any      ,__snake_case       :Any      ,__snake_case       :Dict      ,__snake_case       :int      ,__snake_case       :Tuple      ,__snake_case       :Optional[Any]      ,__snake_case       :int      ,__snake_case       :List[str]				) ->   Optional[Any]:
												a__					=					self.num_labels
												a__					=					DebertaForSequenceClassification(_A				)
												model.to(_A				)
												model.eval()
												a__					=					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 			lowerCamelCase__(				self       :Tuple      ,__snake_case       :Union[str, Any]      ,__snake_case       :Dict      ,__snake_case       :Union[str, Any]      ,__snake_case       :Any      ,__snake_case       :Any      ,__snake_case       :Tuple      ,__snake_case       :Optional[Any]				) ->   Optional[int]:
												a__					=					self.num_labels
												a__					=					DebertaForTokenClassification(config=_A				)
												model.to(_A				)
												model.eval()
												a__					=					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 			lowerCamelCase__(				self       :str      ,__snake_case       :Tuple      ,__snake_case       :int      ,__snake_case       :Union[str, Any]      ,__snake_case       :Any      ,__snake_case       :Tuple      ,__snake_case       :Dict      ,__snake_case       :List[Any]				) ->   int:
												a__					=					DebertaForQuestionAnswering(config=_A				)
												model.to(_A				)
												model.eval()
												a__					=					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 			lowerCamelCase__(				self       :str				) ->   Tuple:
												a__					=					self.prepare_config_and_inputs()
												(
												    (
												    a__							
												)				, (
												    a__							
												)				, (
												    a__							
												)				, (
												    a__							
												)				, (
												    a__							
												)				, (
												    a__							
												)				, (
												    a__							
												)				, 							
												)					=					config_and_inputs
												a__					=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
												return config, inputs_dict







@require_torch
class 	lowercase       (snake_case__     ,						snake_case__     ,						unittest.TestCase				):
					UpperCAmelCase__ :   Tuple   				=      (
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__ :   int   				=      (
					    {
					        """feature-extraction""": DebertaModel,
					        """fill-mask""": DebertaForMaskedLM,
					        """question-answering""": DebertaForQuestionAnswering,
					        """text-classification""": DebertaForSequenceClassification,
					        """token-classification""": DebertaForTokenClassification,
					        """zero-shot""": DebertaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					UpperCAmelCase__ :   Union[str, Any]   				=      True
					UpperCAmelCase__ :   Optional[Any]   				=      False
					UpperCAmelCase__ :   int   				=      False
					UpperCAmelCase__ :   List[str]   				=      False
					UpperCAmelCase__ :   Dict   				=      False
					def 			lowerCamelCase__(				self       :Dict				) ->   Any:
												a__					=					DebertaModelTester(self				)
												a__					=					ConfigTester(self      ,config_class=_A      ,hidden_size=37				)
					def 			lowerCamelCase__(				self       :Tuple				) ->   Dict:
												self.config_tester.run_common_tests()
					def 			lowerCamelCase__(				self       :Dict				) ->   int:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_model(*_A				)
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   List[Any]:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_sequence_classification(*_A				)
					def 			lowerCamelCase__(				self       :Dict				) ->   Any:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_masked_lm(*_A				)
					def 			lowerCamelCase__(				self       :Dict				) ->   Dict:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_question_answering(*_A				)
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_token_classification(*_A				)



					@slow
					def 			lowerCamelCase__(				self       :Tuple				) ->   List[Any]:



												for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a__					=					DebertaModel.from_pretrained(_A				)
																			self.assertIsNotNone(_A				)







@require_torch
@require_sentencepiece
@require_tokenizers
class 	lowercase       (unittest.TestCase				):
					@unittest.skip(reason='Model not available yet'				)
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Dict:
												pass



					@slow
					def 			lowerCamelCase__(				self       :Any				) ->   str:
												a__					=					DebertaModel.from_pretrained('microsoft/deberta-base'				)

												a__					=					torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]				)
												a__					=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
												with torch.no_grad():
																			a__					=					model(_A      ,attention_mask=_A				)[0]
												# compare the actual values for a slice.
												a__					=					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]      ,_A      ,atol=1E-4				)      ,F'{output[:, 1:4, 1:4]}'				)








import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command







class 	snake_case_       (unittest.TestCase				):
					UpperCAmelCase__ :   Optional[Any]   				=      inspect.getfile(accelerate.test_utils				)
					UpperCAmelCase__ :   Tuple   				=      os.path.sep.join(mod_file.split(os.path.sep				)[:-1] + ['''scripts''', '''test_cli.py''']				)

					UpperCAmelCase__ :   Optional[int]   				=      ['''accelerate''', '''launch''']
					UpperCAmelCase__ :   List[str]   				=      Path.home() / '''.cache/huggingface/accelerate'''
					UpperCAmelCase__ :   Union[str, Any]   				=      '''default_config.yaml'''
					UpperCAmelCase__ :   List[Any]   				=      config_folder / config_file
					UpperCAmelCase__ :   Optional[int]   				=      config_folder / '''_default_config.yaml'''

					UpperCAmelCase__ :   int   				=      Path('''tests/test_configs'''				)
					@classmethod
					def 			lowerCamelCase__(				cls       :Any				) ->   int:
												if cls.config_path.is_file():
																			cls.config_path.rename(cls.changed_path				)
					@classmethod
					def 			lowerCamelCase__(				cls       :Dict				) ->   Union[str, Any]:
												if cls.changed_path.is_file():
																			cls.changed_path.rename(cls.config_path				)
					def 			lowerCamelCase__(				self       :Any				) ->   Optional[int]:
												a__					=					self.base_cmd
												if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
																			cmd += ["--multi_gpu"]
												execute_subprocess_async(cmd + [self.test_file_path]      ,env=os.environ.copy()				)
					def 			lowerCamelCase__(				self       :str				) ->   List[Any]:
												for config in sorted(self.test_config_path.glob('**/*.yaml'				)				):
																			with self.subTest(config_file=__snake_case				):
																										execute_subprocess_async(
																										    self.base_cmd + ['--config_file', str(__snake_case				), self.test_file_path]      ,env=os.environ.copy()				)



					def 			lowerCamelCase__(				self       :int				) ->   List[str]:
												execute_subprocess_async(['accelerate', 'test']      ,env=os.environ.copy()				)







class 	snake_case_       (unittest.TestCase				):
					UpperCAmelCase__ :   List[Any]   				=      '''test-tpu'''
					UpperCAmelCase__ :   str   				=      '''us-central1-a'''
					UpperCAmelCase__ :   Optional[Any]   				=      '''ls'''
					UpperCAmelCase__ :   Optional[int]   				=      ['''accelerate''', '''tpu-config''']
					UpperCAmelCase__ :   Any   				=      '''cd /usr/share'''
					UpperCAmelCase__ :   Tuple   				=      '''tests/test_samples/test_command_file.sh'''
					UpperCAmelCase__ :   List[Any]   				=      '''Running gcloud compute tpus tpu-vm ssh'''
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												a__					=					run_command(
												    self.cmd
												    + ['--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :int				) ->   str:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/0_12_0.yaml',
												        '--command',
												        self.command,
												        '--tpu_zone',
												        self.tpu_zone,
												        '--tpu_name',
												        self.tpu_name,
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Any				) ->   Dict:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--debug']      ,return_stdout=__snake_case				)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Tuple				) ->   str:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Dict				) ->   int:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/latest.yaml',
												        '--command',
												        self.command,
												        '--command',
												        'echo "Hello World"',
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo "Hello World" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   List[Any]:
												a__					=					run_command(
												    self.cmd
												    + ['--config_file', 'tests/test_configs/latest.yaml', '--command_file', self.command_file, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :int				) ->   int:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/0_12_0.yaml',
												        '--command_file',
												        self.command_file,
												        '--tpu_zone',
												        self.tpu_zone,
												        '--tpu_name',
												        self.tpu_name,
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   List[Any]:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)



					def 			lowerCamelCase__(				self       :Any				) ->   Optional[int]:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/latest.yaml',
												        '--install_accelerate',
												        '--accelerate_version',
												        '12.0.0',
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)




