'''simple docstring'''




import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
    import torch

if is_soundfile_availble():
    import soundfile as sf

if is_vision_available():
    from PIL import Image







def _lowerCAmelCase     (      _lowerCAmelCase=""    )->     str:
 __UpperCAmelCase					=   tempfile.mkdtemp()
 return os.path.join(_lowerCAmelCase       ,    str(uuid.uuida()    ) + suffix    )






@require_soundfile
@require_torch
class UpperCAmelCase							(		unittest.TestCase							):



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   torch.rand(12     ,			dtype=torch.floataa					) - 0.5
     __UpperCAmelCase					=   AgentAudio(__A					)
     __UpperCAmelCase					=   str(agent_type.to_string()					)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(__A     ,			agent_type.to_raw()     ,			atol=1E-4					)					)

     del agent_type

     # Ensure the path remains even after the object deletion
     self.assertTrue(os.path.exists(__A					)					)

     # Ensure that the file contains the same value as the original tensor
     __UpperCAmelCase					,    __UpperCAmelCase					=   sf.read(__A					)
     self.assertTrue(torch.allclose(__A     ,			torch.tensor(__A					)     ,			atol=1E-4					)					)




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   torch.rand(12     ,			dtype=torch.floataa					) - 0.5
     __UpperCAmelCase					=   get_new_path(suffix='.wav'					)
     sf.write(__A     ,			__A     ,			16_000					)

     __UpperCAmelCase					=   AgentAudio(__A					)

     self.assertTrue(torch.allclose(__A     ,			agent_type.to_raw()     ,			atol=1E-4					)					)
     self.assertEqual(agent_type.to_string()     ,			__A					)






@require_vision
@require_torch
class UpperCAmelCase							(		unittest.TestCase							):



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   torch.randint(0     ,			256     ,			(64, 64, 3)					)
     __UpperCAmelCase					=   AgentImage(__A					)
     __UpperCAmelCase					=   str(agent_type.to_string()					)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(__A     ,			agent_type._tensor     ,			atol=1E-4					)					)

     self.assertIsInstance(agent_type.to_raw()     ,			Image.Image					)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(__A					)					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
     __UpperCAmelCase					=   Image.open(__A					)
     __UpperCAmelCase					=   AgentImage(__A					)

     self.assertTrue(path.samefile(agent_type.to_string()					)					)
     self.assertTrue(image == agent_type.to_raw()					)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(__A					)					)




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
     __UpperCAmelCase					=   Image.open(__A					)
     __UpperCAmelCase					=   AgentImage(__A					)

     self.assertFalse(path.samefile(agent_type.to_string()					)					)
     self.assertTrue(image == agent_type.to_raw()					)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(__A					)					)






class UpperCAmelCase							(		unittest.TestCase							):



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   'Hey!'
     __UpperCAmelCase					=   AgentText(__A					)

     self.assertEqual(__A     ,			agent_type.to_string()					)
     self.assertEqual(__A     ,			agent_type.to_raw()					)
     self.assertEqual(__A     ,			__A					)












'''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							(		UpperCAmelCase_							):



    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.0_2     ,			__A=False     ,			__A=True     ,			__A="None"     ,			__A=3     ,			__A=4     ,			__A=None     ,			):
     __UpperCAmelCase					=   parent
     __UpperCAmelCase					=   batch_size
     __UpperCAmelCase					=   seq_length
     __UpperCAmelCase					=   is_training
     __UpperCAmelCase					=   use_input_mask
     __UpperCAmelCase					=   use_token_type_ids
     __UpperCAmelCase					=   use_labels
     __UpperCAmelCase					=   vocab_size
     __UpperCAmelCase					=   hidden_size
     __UpperCAmelCase					=   num_hidden_layers
     __UpperCAmelCase					=   num_attention_heads
     __UpperCAmelCase					=   intermediate_size
     __UpperCAmelCase					=   hidden_act
     __UpperCAmelCase					=   hidden_dropout_prob
     __UpperCAmelCase					=   attention_probs_dropout_prob
     __UpperCAmelCase					=   max_position_embeddings
     __UpperCAmelCase					=   type_vocab_size
     __UpperCAmelCase					=   type_sequence_label_size
     __UpperCAmelCase					=   initializer_range
     __UpperCAmelCase					=   num_labels
     __UpperCAmelCase					=   num_choices
     __UpperCAmelCase					=   relative_attention
     __UpperCAmelCase					=   position_biased_input
     __UpperCAmelCase					=   pos_att_type
     __UpperCAmelCase					=   scope



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size					)

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

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

     __UpperCAmelCase					=   None
     __UpperCAmelCase					=   None
     __UpperCAmelCase					=   None
     if self.use_labels:
      __UpperCAmelCase					=   ids_tensor([self.batch_size]     ,			self.type_sequence_label_size					)
      __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.num_labels					)
      __UpperCAmelCase					=   ids_tensor([self.batch_size]     ,			self.num_choices					)

     __UpperCAmelCase					=   self.get_config()

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



    def 					__lowerCamelCase						(  self					):
     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 					__lowerCamelCase						(  self     ,			__A					):
     self.parent.assertListEqual(list(result.loss.size()					)     ,			[]					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaModel(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A					)[0]
     __UpperCAmelCase					=   model(__A     ,			token_type_ids=__A					)[0]
     __UpperCAmelCase					=   model(__A					)[0]

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



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForMaskedLM(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   DebertaVaForSequenceClassification(__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   DebertaVaForTokenClassification(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForQuestionAnswering(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   DebertaVaForMultipleChoice(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   input_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   token_type_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   input_mask.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)					)




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.prepare_config_and_inputs()
     (
         (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    (
         __UpperCAmelCase
     )					,    
     )					=   config_and_inputs
     __UpperCAmelCase					=   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
     return config, inputs_dict






@require_torch
class UpperCAmelCase							(		UpperCAmelCase_					,			UpperCAmelCase_					,			unittest.TestCase							):
    _A :			Optional[Any]						   =       (
        (
            DebertaVaModel,
            DebertaVaForMaskedLM,
            DebertaVaForSequenceClassification,
            DebertaVaForTokenClassification,
            DebertaVaForQuestionAnswering,
            DebertaVaForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    _A :			int						   =       (
        {
            """feature-extraction""": DebertaVaModel,
            """fill-mask""": DebertaVaForMaskedLM,
            """question-answering""": DebertaVaForQuestionAnswering,
            """text-classification""": DebertaVaForSequenceClassification,
            """token-classification""": DebertaVaForTokenClassification,
            """zero-shot""": DebertaVaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   DebertaVaModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			hidden_size=37					)



    def 					__lowerCamelCase						(  self					):
     self.config_tester.run_common_tests()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_multiple_choice(*__A					)




    @slow
    def 					__lowerCamelCase						(  self					):
     for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __UpperCAmelCase					=   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 					__lowerCamelCase						(  self					):
     pass




    @slow
    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'					)

     __UpperCAmelCase					=   torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]					)
     __UpperCAmelCase					=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
     with torch.no_grad():
      __UpperCAmelCase					=   model(__A     ,			attention_mask=__A					)[0]
     # compare the actual values for a slice.
     __UpperCAmelCase					=   torch.tensor(
         [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]					)
     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			__A     ,			atol=1E-4					)     ,			f'{output[:, 1:4, 1:4]}'					)





