



'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, 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 (
						    TFDebertaVaForMaskedLM,
						    TFDebertaVaForQuestionAnswering,
						    TFDebertaVaForSequenceClassification,
						    TFDebertaVaForTokenClassification,
						    TFDebertaVaModel,
						)

class        SCREAMING_SNAKE_CASE:







	"""simple docstring"""






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







	def   A			(		self    :  Any	)    ->	Optional[Any]:
								UpperCAmelCase				: Union[str, Any]											=	ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size	)

								UpperCAmelCase				: List[Any]											=	None
								if self.use_input_mask:
															UpperCAmelCase				: List[Any]											=	random_attention_mask([self.batch_size, self.seq_length]	)

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

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

								UpperCAmelCase				: Dict											=	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	,    relative_attention=self.relative_attention	,    position_biased_input=self.position_biased_input	,    initializer_range=self.initializer_range	,    return_dict=__snake_case	,    )

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







	def   A			(		self    :  List[str]	,    __snake_case    :  int	,    __snake_case    :  Tuple	,    __snake_case    :  Any	,    __snake_case    :  Any	,    __snake_case    :  Tuple	,    __snake_case    :  Tuple	,    __snake_case    :  Optional[Any]	)    ->	Optional[int]:
								UpperCAmelCase				: List[Any]											=	TFDebertaVaModel(config=__snake_case	)
								UpperCAmelCase				: Dict											=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

								UpperCAmelCase				: Optional[Any]											=	[input_ids, input_mask]
								UpperCAmelCase				: Dict											=	model(__snake_case	)

								UpperCAmelCase				: Dict											=	model(__snake_case	)

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







	def   A			(		self    :  int	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Any	,    __snake_case    :  Tuple	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Tuple	)    ->	str:
								UpperCAmelCase				: int											=	TFDebertaVaForMaskedLM(config=__snake_case	)
								UpperCAmelCase				: List[Any]											=	{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								    '''token_type_ids''': token_type_ids,
								}
								UpperCAmelCase				: Union[str, Any]											=	model(__snake_case	)
								self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.vocab_size)	)







	def   A			(		self    :  Dict	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Tuple	,    __snake_case    :  Any	,    __snake_case    :  Optional[int]	,    __snake_case    :  List[Any]	,    __snake_case    :  List[str]	,    __snake_case    :  Optional[Any]	)    ->	Any:
								UpperCAmelCase				: List[Any]											=	self.num_labels
								UpperCAmelCase				: Optional[int]											=	TFDebertaVaForSequenceClassification(config=__snake_case	)
								UpperCAmelCase				: int											=	{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								    '''token_type_ids''': token_type_ids,
								}

								UpperCAmelCase				: int											=	model(__snake_case	)
								self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)	)







	def   A			(		self    :  Dict	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  List[Any]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  int	,    __snake_case    :  str	,    __snake_case    :  Union[str, Any]	)    ->	str:
								UpperCAmelCase				: Tuple											=	self.num_labels
								UpperCAmelCase				: Tuple											=	TFDebertaVaForTokenClassification(config=__snake_case	)
								UpperCAmelCase				: Tuple											=	{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								    '''token_type_ids''': token_type_ids,
								}
								UpperCAmelCase				: Any											=	model(__snake_case	)
								self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.num_labels)	)







	def   A			(		self    :  int	,    __snake_case    :  Optional[int]	,    __snake_case    :  Optional[int]	,    __snake_case    :  List[Any]	,    __snake_case    :  Optional[Any]	,    __snake_case    :  int	,    __snake_case    :  int	,    __snake_case    :  int	)    ->	Any:
								UpperCAmelCase				: Any											=	TFDebertaVaForQuestionAnswering(config=__snake_case	)
								UpperCAmelCase				: Dict											=	{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								    '''token_type_ids''': token_type_ids,
								}

								UpperCAmelCase				: Optional[int]											=	model(__snake_case	)
								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    :  Tuple	)    ->	List[str]:
								UpperCAmelCase				: Optional[Any]											=	self.prepare_config_and_inputs()
								(
								    UpperCAmelCase	
								)				: str											=	config_and_inputs
								UpperCAmelCase				: List[Any]											=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



@require_tf
class        SCREAMING_SNAKE_CASE(							A__							,	A__							,	unittest.TestCase    ):







	"""simple docstring"""






	lowerCamelCase__    				= (
	    (
	        TFDebertaVaModel,
	        TFDebertaVaForMaskedLM,
	        TFDebertaVaForQuestionAnswering,
	        TFDebertaVaForSequenceClassification,
	        TFDebertaVaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	lowerCamelCase__    				= (
	    {
	        """feature-extraction""": TFDebertaVaModel,
	        """fill-mask""": TFDebertaVaForMaskedLM,
	        """question-answering""": TFDebertaVaForQuestionAnswering,
	        """text-classification""": TFDebertaVaForSequenceClassification,
	        """token-classification""": TFDebertaVaForTokenClassification,
	        """zero-shot""": TFDebertaVaForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)

	lowerCamelCase__    				= False
	lowerCamelCase__    				= False







	def   A			(		self    :  Union[str, Any]	)    ->	Optional[Any]:
								UpperCAmelCase				: List[str]											=	TFDebertaVaModelTester(self	)
								UpperCAmelCase				: str											=	ConfigTester(self	,    config_class=__snake_case	,    hidden_size=37	)







	def   A			(		self    :  Optional[int]	)    ->	Any:
								self.config_tester.run_common_tests()







	def   A			(		self    :  List[Any]	)    ->	List[str]:
								UpperCAmelCase				: Union[str, Any]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__snake_case	)







	def   A			(		self    :  Optional[int]	)    ->	Tuple:
								UpperCAmelCase				: str											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*__snake_case	)







	def   A			(		self    :  Dict	)    ->	str:
								UpperCAmelCase				: Dict											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__snake_case	)







	def   A			(		self    :  Any	)    ->	List[Any]:
								UpperCAmelCase				: str											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*__snake_case	)







	def   A			(		self    :  List[str]	)    ->	List[str]:
								UpperCAmelCase				: Any											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__snake_case	)







	@slow
	def   A			(		self    :  Any	)    ->	List[str]:
								UpperCAmelCase				: Tuple											=	TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''	)
								self.assertIsNotNone(__snake_case	)



@require_tf
class        SCREAMING_SNAKE_CASE(							unittest.TestCase    ):







	"""simple docstring"""






	@unittest.skip(reason='''Model not available yet'''	)
	def   A			(		self    :  List[str]	)    ->	int:
								pass







	@slow
	def   A			(		self    :  List[Any]	)    ->	Optional[Any]:
								UpperCAmelCase				: Optional[Any]											=	TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''	)
								UpperCAmelCase				: Optional[Any]											=	tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]	)
								UpperCAmelCase				: Tuple											=	tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
								UpperCAmelCase				: List[Any]											=	model(__snake_case	,    attention_mask=__snake_case	)[0]

								UpperCAmelCase				: Optional[int]											=	tf.constant(
								    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]	)
								tf.debugging.assert_near(output[:, 1:4, 1:4]	,    __snake_case	,    atol=1E-4	)










'''simple docstring'''






import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy


def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		Tuple						)   ->     str:
							assert isinstance(_lowerCAmelCase     ,   _lowerCAmelCase						)
							assert dataset.num_rows == 4
							assert dataset.num_columns == 3
							assert dataset.column_names == ["col_1", "col_2", "col_3"]
							for feature, expected_dtype in expected_features.items():
														assert dataset.features[feature].dtype == expected_dtype


@require_sqlalchemy
@pytest.mark.parametrize('''keep_in_memory'''     ,   [False, True]						)
def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		List[str]     ,   _lowerCAmelCase						:		Any     ,   _lowerCAmelCase						:		Optional[Any]						)   ->     List[str]:
							UpperCAmelCase				: int											=	tmp_path / '''cache'''
							UpperCAmelCase				: Dict											=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
							with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
														UpperCAmelCase				: Any											=	SqlDatasetReader(
														    '''dataset'''     ,   '''sqlite:///''' + sqlite_path     ,   cache_dir=_lowerCAmelCase     ,   keep_in_memory=_lowerCAmelCase						).read()
							_check_sql_dataset(_lowerCAmelCase     ,   _lowerCAmelCase						)


@require_sqlalchemy
@pytest.mark.parametrize(
    '''features'''     ,   [
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]     ,   )
def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		Any     ,   _lowerCAmelCase						:		Optional[int]     ,   _lowerCAmelCase						:		str						)   ->     Optional[int]:
							UpperCAmelCase				: Union[str, Any]											=	tmp_path / '''cache'''
							UpperCAmelCase				: Union[str, Any]											=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
							UpperCAmelCase				: str											=	features.copy() if features else default_expected_features
							UpperCAmelCase				: Optional[Any]											=	(
							    Features({feature: Value(_lowerCAmelCase						) for feature, dtype in features.items()}						) if features is not None else None
							)
							UpperCAmelCase				: Tuple											=	SqlDatasetReader('''dataset'''     ,   '''sqlite:///''' + sqlite_path     ,   features=_lowerCAmelCase     ,   cache_dir=_lowerCAmelCase						).read()
							_check_sql_dataset(_lowerCAmelCase     ,   _lowerCAmelCase						)


def 				snake_case_  (      _lowerCAmelCase						:		List[Any]						)   ->     Dict:
							with contextlib.closing(sqlitea.connect(_lowerCAmelCase						)						) as con:
														UpperCAmelCase				: Any											=	con.cursor()
														cur.execute('''SELECT * FROM dataset'''						)
														for row in cur:
																					yield row


@require_sqlalchemy
def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		str     ,   _lowerCAmelCase						:		Any						)   ->     Optional[int]:
							UpperCAmelCase				: Optional[int]											=	tmp_path / '''cache'''
							UpperCAmelCase				: int											=	os.path.join(_lowerCAmelCase     ,   '''tmp.sql'''						)
							UpperCAmelCase				: Any											=	SqlDatasetReader('''dataset'''     ,   '''sqlite:///''' + sqlite_path     ,   cache_dir=_lowerCAmelCase						).read()
							SqlDatasetWriter(_lowerCAmelCase     ,   '''dataset'''     ,   '''sqlite:///''' + output_sqlite_path     ,   num_proc=1						).write()

							UpperCAmelCase				: List[Any]											=	iter_sql_file(_lowerCAmelCase						)
							UpperCAmelCase				: List[Any]											=	iter_sql_file(_lowerCAmelCase						)

							for rowa, rowa in zip(_lowerCAmelCase     ,   _lowerCAmelCase						):
														assert rowa == rowa


@require_sqlalchemy
def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		Optional[Any]     ,   _lowerCAmelCase						:		Union[str, Any]						)   ->     int:
							UpperCAmelCase				: Optional[Any]											=	tmp_path / '''cache'''
							UpperCAmelCase				: Any											=	os.path.join(_lowerCAmelCase     ,   '''tmp.sql'''						)
							UpperCAmelCase				: List[str]											=	SqlDatasetReader('''dataset'''     ,   '''sqlite:///''' + sqlite_path     ,   cache_dir=_lowerCAmelCase						).read()
							SqlDatasetWriter(_lowerCAmelCase     ,   '''dataset'''     ,   '''sqlite:///''' + output_sqlite_path     ,   num_proc=2						).write()

							UpperCAmelCase				: List[str]											=	iter_sql_file(_lowerCAmelCase						)
							UpperCAmelCase				: Any											=	iter_sql_file(_lowerCAmelCase						)

							for rowa, rowa in zip(_lowerCAmelCase     ,   _lowerCAmelCase						):
														assert rowa == rowa




@require_sqlalchemy
def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		Any     ,   _lowerCAmelCase						:		Optional[Any]						)   ->     List[Any]:
							UpperCAmelCase				: Union[str, Any]											=	tmp_path / '''cache'''
							UpperCAmelCase				: Tuple											=	os.path.join(_lowerCAmelCase     ,   '''tmp.sql'''						)
							UpperCAmelCase				: Optional[int]											=	SqlDatasetReader('''dataset'''     ,   '''sqlite:///''' + sqlite_path     ,   cache_dir=_lowerCAmelCase						).read()
							with pytest.raises(_lowerCAmelCase						):
														SqlDatasetWriter(_lowerCAmelCase     ,   '''dataset'''     ,   '''sqlite:///''' + output_sqlite_path     ,   num_proc=0						).write()





