



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







							def __init__(							self :	str     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	int=13     ,_UpperCAmelCase :	Optional[Any]=7     ,_UpperCAmelCase :	Optional[int]=True     ,_UpperCAmelCase :	List[Any]=True     ,_UpperCAmelCase :	Optional[Any]=True     ,_UpperCAmelCase :	str=True     ,_UpperCAmelCase :	List[str]=99     ,_UpperCAmelCase :	str=32     ,_UpperCAmelCase :	List[Any]=5     ,_UpperCAmelCase :	List[Any]=4     ,_UpperCAmelCase :	Dict=37     ,_UpperCAmelCase :	Union[str, Any]="gelu"     ,_UpperCAmelCase :	Tuple=0.1     ,_UpperCAmelCase :	Any=0.1     ,_UpperCAmelCase :	Optional[int]=512     ,_UpperCAmelCase :	Union[str, Any]=16     ,_UpperCAmelCase :	Any=2     ,_UpperCAmelCase :	Dict=0.02     ,_UpperCAmelCase :	Any=False     ,_UpperCAmelCase :	Union[str, Any]=True     ,_UpperCAmelCase :	List[Any]="None"     ,_UpperCAmelCase :	Any=3     ,_UpperCAmelCase :	Optional[int]=4     ,_UpperCAmelCase :	List[Any]=None     ,):
									_a					:				Any				=							parent
									_a					:				Optional[Any]				=							batch_size
									_a					:				List[Any]				=							seq_length
									_a					:				str				=							is_training
									_a					:				List[str]				=							use_input_mask
									_a					:				Union[str, Any]				=							use_token_type_ids
									_a					:				List[Any]				=							use_labels
									_a					:				str				=							vocab_size
									_a					:				int				=							hidden_size
									_a					:				List[str]				=							num_hidden_layers
									_a					:				int				=							num_attention_heads
									_a					:				Optional[int]				=							intermediate_size
									_a					:				List[str]				=							hidden_act
									_a					:				List[Any]				=							hidden_dropout_prob
									_a					:				Tuple				=							attention_probs_dropout_prob
									_a					:				List[Any]				=							max_position_embeddings
									_a					:				Optional[Any]				=							type_vocab_size
									_a					:				Any				=							type_sequence_label_size
									_a					:				List[str]				=							initializer_range
									_a					:				List[str]				=							num_labels
									_a					:				Tuple				=							num_choices
									_a					:				Optional[int]				=							relative_attention
									_a					:				List[Any]				=							position_biased_input
									_a					:				List[str]				=							pos_att_type
									_a					:				str				=							scope







							def 							__lowercase      (							self :	Optional[int]		):
									_a					:				int				=							ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size		)

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

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

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

									_a					:				List[str]				=							self.get_config()

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







							def 							__lowercase      (							self :	Dict		):
									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 							__lowercase      (							self :	Union[str, Any]		):
									_a					:				Optional[int]				=							self.get_config()
									_a					:				str				=							300
									return config







							def 							__lowercase      (							self :	int     ,_UpperCAmelCase :	int		):
									self.parent.assertListEqual(list(result.loss.size()		)     ,[]		)







							def 							__lowercase      (							self :	List[str]     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	List[str]     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Tuple		):
									_a					:				Optional[Any]				=							DebertaModel(config=_lowerCamelCase		)
									model.to(_lowerCamelCase		)
									model.eval()
									_a					:				Dict				=							model(_lowerCamelCase     ,attention_mask=_lowerCamelCase     ,token_type_ids=_lowerCamelCase		)[0]
									_a					:				Optional[int]				=							model(_lowerCamelCase     ,token_type_ids=_lowerCamelCase		)[0]
									_a					:				str				=							model(_lowerCamelCase		)[0]

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







							def 							__lowercase      (							self :	str     ,_UpperCAmelCase :	List[str]     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	int		):
									_a					:				Union[str, Any]				=							DebertaForMaskedLM(config=_lowerCamelCase		)
									model.to(_lowerCamelCase		)
									model.eval()
									_a					:				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.vocab_size)		)







							def 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	int		):
									_a					:				int				=							self.num_labels
									_a					:				Optional[Any]				=							DebertaForSequenceClassification(_lowerCamelCase		)
									model.to(_lowerCamelCase		)
									model.eval()
									_a					:				List[Any]				=							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 							__lowercase      (							self :	Dict     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Optional[int]		):
									_a					:				List[str]				=							self.num_labels
									_a					:				Tuple				=							DebertaForTokenClassification(config=_lowerCamelCase		)
									model.to(_lowerCamelCase		)
									model.eval()
									_a					:				Union[str, Any]				=							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 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	str		):
									_a					:				Union[str, Any]				=							DebertaForQuestionAnswering(config=_lowerCamelCase		)
									model.to(_lowerCamelCase		)
									model.eval()
									_a					:				Optional[Any]				=							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 							__lowercase      (							self :	Tuple		):
									_a					:				Union[str, Any]				=							self.prepare_config_and_inputs()
									(
									    (
									    _a 
									)	, (
									    _a 
									)	, (
									    _a 
									)	, (
									    _a 
									)	, (
									    _a 
									)	, (
									    _a 
									)	, (
									    _a 
									)	,  
									)					:				Optional[Any]				=							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      __magic_name__						(   A_      ,						A_      ,						unittest.TestCase       ):
							lowerCAmelCase	:   List[str]           = (
							    (
							        DebertaModel,
							        DebertaForMaskedLM,
							        DebertaForSequenceClassification,
							        DebertaForTokenClassification,
							        DebertaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCAmelCase	:   Any           = (
							    {
							        'feature-extraction': DebertaModel,
							        'fill-mask': DebertaForMaskedLM,
							        'question-answering': DebertaForQuestionAnswering,
							        'text-classification': DebertaForSequenceClassification,
							        'token-classification': DebertaForTokenClassification,
							        'zero-shot': DebertaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							lowerCAmelCase	:   Union[str, Any]           = True
							lowerCAmelCase	:   str           = False
							lowerCAmelCase	:   str           = False
							lowerCAmelCase	:   List[str]           = False
							lowerCAmelCase	:   List[str]           = False







							def 							__lowercase      (							self :	Tuple		):
									_a					:				Union[str, Any]				=							DebertaModelTester(self		)
									_a					:				Tuple				=							ConfigTester(self     ,config_class=_lowerCamelCase     ,hidden_size=37		)







							def 							__lowercase      (							self :	Optional[int]		):
									self.config_tester.run_common_tests()







							def 							__lowercase      (							self :	Union[str, Any]		):
									_a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_model(*_lowerCamelCase		)







							def 							__lowercase      (							self :	Tuple		):
									_a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCamelCase		)







							def 							__lowercase      (							self :	Dict		):
									_a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCamelCase		)







							def 							__lowercase      (							self :	Optional[Any]		):
									_a					:				Dict				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCamelCase		)







							def 							__lowercase      (							self :	str		):
									_a					:				int				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCamelCase		)







							@slow
							def 							__lowercase      (							self :	Union[str, Any]		):
									for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_a					:				Any				=							DebertaModel.from_pretrained(_lowerCamelCase		)
											self.assertIsNotNone(_lowerCamelCase		)

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







							@unittest.skip(reason='Model not available yet'		)
							def 							__lowercase      (							self :	List[Any]		):
									pass







							@slow
							def 							__lowercase      (							self :	int		):
									_a					:				Any				=							DebertaModel.from_pretrained('microsoft/deberta-base'		)

									_a					:				Optional[int]				=							torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		)
									_a					:				Dict				=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
									with torch.no_grad():
											_a					:				str				=							model(_lowerCamelCase     ,attention_mask=_lowerCamelCase		)[0]
									# compare the actual values for a slice.
									_a					:				Optional[Any]				=							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'''

# This script creates a super tiny model that is useful inside tests, when we just want to test that
# the machinery works, without needing to the check the quality of the outcomes.
#
# This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny -
# all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and
# emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files.
# The latter is done by `fsmt-make-super-tiny-model.py`.
#
# It will be used then as "stas/tiny-wmt19-en-ru"

from pathlib import Path
import json
import tempfile

from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration
from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES

__lowerCAmelCase						      =       '''tiny-wmt19-en-ru'''

# Build

# borrowed from a test
__lowerCAmelCase						      =       [
    '''l''',
    '''o''',
    '''w''',
    '''e''',
    '''r''',
    '''s''',
    '''t''',
    '''i''',
    '''d''',
    '''n''',
    '''w</w>''',
    '''r</w>''',
    '''t</w>''',
    '''lo''',
    '''low''',
    '''er</w>''',
    '''low</w>''',
    '''lowest</w>''',
    '''newer</w>''',
    '''wider</w>''',
    '''<unk>''',
]
__lowerCAmelCase						      =       dict(zip(vocab, range(len(vocab))))
__lowerCAmelCase						      =       ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

with tempfile.TemporaryDirectory() as tmpdirname:
							__lowerCAmelCase						      =       Path(tmpdirname)
							__lowerCAmelCase						      =       build_dir / VOCAB_FILES_NAMES['''src_vocab_file''']
							__lowerCAmelCase						      =       build_dir / VOCAB_FILES_NAMES['''tgt_vocab_file''']
							__lowerCAmelCase						      =       build_dir / VOCAB_FILES_NAMES['''merges_file''']
							with open(src_vocab_file, '''w''') as fp:
														fp.write(json.dumps(vocab_tokens))
							with open(tgt_vocab_file, '''w''') as fp:
														fp.write(json.dumps(vocab_tokens))
							with open(merges_file, '''w''') as fp:
														fp.write('''\n'''.join(merges))

							__lowerCAmelCase						      =       FSMTTokenizer(
							    langs=['''en''', '''ru'''],
							    src_vocab_size=len(vocab),
							    tgt_vocab_size=len(vocab),
							    src_vocab_file=src_vocab_file,
							    tgt_vocab_file=tgt_vocab_file,
							    merges_file=merges_file,
							)

__lowerCAmelCase						      =       FSMTConfig(
    langs=['''ru''', '''en'''],
    src_vocab_size=1_000,
    tgt_vocab_size=1_000,
    d_model=4,
    encoder_layers=1,
    decoder_layers=1,
    encoder_ffn_dim=4,
    decoder_ffn_dim=4,
    encoder_attention_heads=1,
    decoder_attention_heads=1,
)

__lowerCAmelCase						      =       FSMTForConditionalGeneration(config)
print(f"""num of params {tiny_model.num_parameters()}""")

# Test
__lowerCAmelCase						      =       tokenizer(['''Making tiny model'''], return_tensors='''pt''')
__lowerCAmelCase						      =       tiny_model(**batch)

print('''test output:''', len(outputs.logits[0]))

# Save
tiny_model.half()  # makes it smaller
tiny_model.save_pretrained(mname_tiny)
tokenizer.save_pretrained(mname_tiny)

print(f"""Generated {mname_tiny}""")

# Upload
# transformers-cli upload tiny-wmt19-en-ru




