






from __future__ import annotations

import unittest

from transformers import MPNetConfig, 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.models.mpnet.modeling_tf_mpnet import (
				    TFMPNetForMaskedLM,
				    TFMPNetForMultipleChoice,
				    TFMPNetForQuestionAnswering,
				    TFMPNetForSequenceClassification,
				    TFMPNetForTokenClassification,
				    TFMPNetModel,
				)







class 				lowerCamelCase_     :
							def __init__(						self		:					Dict							,__lowerCamelCase		:					Any							,__lowerCamelCase		:					int=13							,__lowerCamelCase		:					Any=7							,__lowerCamelCase		:					Optional[int]=True							,__lowerCamelCase		:					str=True							,__lowerCamelCase		:					str=False							,__lowerCamelCase		:					Optional[Any]=True							,__lowerCamelCase		:					int=99							,__lowerCamelCase		:					Optional[int]=64							,__lowerCamelCase		:					Dict=2							,__lowerCamelCase		:					List[Any]=4							,__lowerCamelCase		:					Union[str, Any]=64							,__lowerCamelCase		:					List[Any]="gelu"							,__lowerCamelCase		:					str=0.1							,__lowerCamelCase		:					List[str]=0.1							,__lowerCamelCase		:					Any=5_12							,__lowerCamelCase		:					int=16							,__lowerCamelCase		:					List[Any]=2							,__lowerCamelCase		:					int=0.02							,__lowerCamelCase		:					int=3							,__lowerCamelCase		:					Optional[Any]=4							,__lowerCamelCase		:					Tuple=None							,):





								'''simple docstring'''



								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		=			scope
							def  SCREAMING_SNAKE_CASE_ (						self		:					int					):





								'''simple docstring'''



								a		=			ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size					)

								a		=			None
								if self.use_input_mask:
									a		=			random_attention_mask([self.batch_size, self.seq_length]					)

								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		=			MPNetConfig(
								    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							,initializer_range=self.initializer_range							,)
								return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
							def  SCREAMING_SNAKE_CASE_ (						self		:					Union[str, Any]							,__lowerCamelCase		:					Tuple							,__lowerCamelCase		:					Tuple							,__lowerCamelCase		:					Dict							,__lowerCamelCase		:					Tuple							,__lowerCamelCase		:					Optional[int]							,__lowerCamelCase		:					Dict					):





								'''simple docstring'''



								a		=			TFMPNetModel(config=__lowerCamelCase					)
								a		=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								a		=			model(__lowerCamelCase					)
								a		=			[input_ids, input_mask]
								a		=			model(__lowerCamelCase					)
								self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Dict							,__lowerCamelCase		:					str							,__lowerCamelCase		:					Optional[Any]							,__lowerCamelCase		:					Optional[int]							,__lowerCamelCase		:					Optional[Any]							,__lowerCamelCase		:					int							,__lowerCamelCase		:					int					):





								'''simple docstring'''



								a		=			TFMPNetForMaskedLM(config=__lowerCamelCase					)
								a		=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								a		=			model(__lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					str							,__lowerCamelCase		:					Any							,__lowerCamelCase		:					List[Any]							,__lowerCamelCase		:					Optional[Any]							,__lowerCamelCase		:					List[str]							,__lowerCamelCase		:					Optional[Any]							,__lowerCamelCase		:					Optional[int]					):





								'''simple docstring'''



								a		=			TFMPNetForQuestionAnswering(config=__lowerCamelCase					)
								a		=			{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								}
								a		=			model(__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  SCREAMING_SNAKE_CASE_ (						self		:					Optional[int]							,__lowerCamelCase		:					List[Any]							,__lowerCamelCase		:					str							,__lowerCamelCase		:					Dict							,__lowerCamelCase		:					int							,__lowerCamelCase		:					List[str]							,__lowerCamelCase		:					str					):





								'''simple docstring'''



								a		=			self.num_labels
								a		=			TFMPNetForSequenceClassification(__lowerCamelCase					)
								a		=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								a		=			model(__lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					str							,__lowerCamelCase		:					List[str]							,__lowerCamelCase		:					List[str]							,__lowerCamelCase		:					int							,__lowerCamelCase		:					Optional[Any]							,__lowerCamelCase		:					Tuple							,__lowerCamelCase		:					Optional[int]					):





								'''simple docstring'''



								a		=			self.num_choices
								a		=			TFMPNetForMultipleChoice(__lowerCamelCase					)
								a		=			tf.tile(tf.expand_dims(__lowerCamelCase							,1					)							,(1, self.num_choices, 1)					)
								a		=			tf.tile(tf.expand_dims(__lowerCamelCase							,1					)							,(1, self.num_choices, 1)					)
								a		=			{
								    '''input_ids''': multiple_choice_inputs_ids,
								    '''attention_mask''': multiple_choice_input_mask,
								}
								a		=			model(__lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Tuple							,__lowerCamelCase		:					Union[str, Any]							,__lowerCamelCase		:					Optional[int]							,__lowerCamelCase		:					Dict							,__lowerCamelCase		:					Optional[int]							,__lowerCamelCase		:					Tuple							,__lowerCamelCase		:					List[Any]					):





								'''simple docstring'''



								a		=			self.num_labels
								a		=			TFMPNetForTokenClassification(__lowerCamelCase					)
								a		=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								a		=			model(__lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)					)




							def  SCREAMING_SNAKE_CASE_ (						self		:					str					):





								'''simple docstring'''



								a		=			self.prepare_config_and_inputs()
								((a)	, (a)	, (a)	, (a)	, (a)	, (a))		=			config_and_inputs
								a		=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict







@require_tf
class 				lowerCamelCase_     (	a_ ,						a_ ,						unittest.TestCase     ):
							SCREAMING_SNAKE_CASE_						     =			(
							    (
							        TFMPNetForMaskedLM,
							        TFMPNetForMultipleChoice,
							        TFMPNetForQuestionAnswering,
							        TFMPNetForSequenceClassification,
							        TFMPNetForTokenClassification,
							        TFMPNetModel,
							    )
							    if is_tf_available()
							    else ()
							)
							SCREAMING_SNAKE_CASE_						     =			(
							    {
							        'feature-extraction': TFMPNetModel,
							        'fill-mask': TFMPNetForMaskedLM,
							        'question-answering': TFMPNetForQuestionAnswering,
							        'text-classification': TFMPNetForSequenceClassification,
							        'token-classification': TFMPNetForTokenClassification,
							        'zero-shot': TFMPNetForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							SCREAMING_SNAKE_CASE_						     =			False
							SCREAMING_SNAKE_CASE_						     =			False
							def  SCREAMING_SNAKE_CASE_ (						self		:					List[str]					):





								'''simple docstring'''



								a		=			TFMPNetModelTester(self					)
								a		=			ConfigTester(self							,config_class=__lowerCamelCase							,hidden_size=37					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					List[Any]					):





								'''simple docstring'''



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





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_model(*__lowerCamelCase					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Optional[Any]					):





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_masked_lm(*__lowerCamelCase					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Union[str, Any]					):





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCamelCase					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Union[str, Any]					):





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCamelCase					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					int					):





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCamelCase					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Dict					):





								'''simple docstring'''



								a		=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCamelCase					)




							@slow
							def  SCREAMING_SNAKE_CASE_ (						self		:					Optional[Any]					):





								'''simple docstring'''



								for model_name in ["microsoft/mpnet-base"]:
									a		=			TFMPNetModel.from_pretrained(__lowerCamelCase					)
									self.assertIsNotNone(__lowerCamelCase					)







@require_tf
class 				lowerCamelCase_     (	unittest.TestCase     ):
							@slow
							def  SCREAMING_SNAKE_CASE_ (						self		:					Tuple					):





								'''simple docstring'''



								a		=			TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''					)
								a		=			tf.constant([[0, 1, 2, 3, 4, 5]]					)
								a		=			model(__lowerCamelCase					)[0]

								a		=			[1, 6, 7_68]
								self.assertEqual(output.shape							,__lowerCamelCase					)

								a		=			tf.constant(
								    [
								        [
								            [-0.1_067_172, 0.08_216_473, 0.0_024_543],
								            [-0.03_465_879, 0.8_354_118, -0.03_252_288],
								            [-0.06_569_476, -0.12_424_111, -0.0_494_436],
								        ]
								    ]					)
								tf.debugging.assert_near(output[:, :3, :3]							,__lowerCamelCase							,atol=1e-4					)













import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
				import sqlitea

				import sqlalchemy


UpperCamelCase__						:      Tuple								=					datasets.utils.logging.get_logger(__name__)







@dataclass
class 				lowerCamelCase_     (	datasets.BuilderConfig     ):
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			True
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			None
							SCREAMING_SNAKE_CASE_						     =			1_00_00
							SCREAMING_SNAKE_CASE_						     =			None
							def  SCREAMING_SNAKE_CASE_ (						self		:					Optional[Any]					):





								'''simple docstring'''



								if self.sql is None:
									raise ValueError('''sql must be specified'''					)
								if self.con is None:
									raise ValueError('''con must be specified'''					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Any							,__lowerCamelCase		:					dict							,__lowerCamelCase		:					Optional[datasets.Features] = None							,):





								'''simple docstring'''



								a		=			config_kwargs.copy()
								# We need to stringify the Selectable object to make its hash deterministic

								# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
								a		=			config_kwargs['''sql''']
								if not isinstance(__lowerCamelCase							,__lowerCamelCase					):
									if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
										import sqlalchemy

										if isinstance(__lowerCamelCase							,sqlalchemy.sql.Selectable					):
											a		=			sqlalchemy.create_engine(config_kwargs['''con'''].split('''://'''					)[0] + '''://'''					)
											a		=			str(sql.compile(dialect=engine.dialect					)					)
											a		=			sql_str
										else:
											raise TypeError(
											    F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__lowerCamelCase					)}: {sql}"""					)
									else:
										raise TypeError(
										    F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__lowerCamelCase					)}: {sql}"""					)
								a		=			config_kwargs['''con''']
								if not isinstance(__lowerCamelCase							,__lowerCamelCase					):
									a		=			id(__lowerCamelCase					)
									logger.info(
									    F"""SQL connection 'con' of type {type(__lowerCamelCase					)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."""					)

								return super().create_config_id(__lowerCamelCase							,custom_features=__lowerCamelCase					)




							@property
							def  SCREAMING_SNAKE_CASE_ (						self		:					Dict					):





								'''simple docstring'''



								a		=			{
								    '''index_col''': self.index_col,
								    '''columns''': self.columns,
								    '''params''': self.params,
								    '''coerce_float''': self.coerce_float,
								    '''parse_dates''': self.parse_dates,
								}
								return pd_read_sql_kwargs







class 				lowerCamelCase_     (	datasets.ArrowBasedBuilder     ):
							SCREAMING_SNAKE_CASE_						     =			SqlConfig
							def  SCREAMING_SNAKE_CASE_ (						self		:					Optional[Any]					):





								'''simple docstring'''



								return datasets.DatasetInfo(features=self.config.features					)
							def  SCREAMING_SNAKE_CASE_ (						self		:					Union[str, Any]							,__lowerCamelCase		:					int					):





								'''simple docstring'''



								return [datasets.SplitGenerator(name=datasets.Split.TRAIN							,gen_kwargs={}					)]
							def  SCREAMING_SNAKE_CASE_ (						self		:					Tuple							,__lowerCamelCase		:					pa.Table					):





								'''simple docstring'''



								if self.config.features is not None:
									a		=			self.config.features.arrow_schema
									if all(not require_storage_cast(__lowerCamelCase					) for feature in self.config.features.values()					):
										# cheaper cast
										a		=			pa.Table.from_arrays([pa_table[field.name] for field in schema]							,schema=__lowerCamelCase					)
									else:
										# more expensive cast; allows str <-> int/float or str to Audio for example
										a		=			table_cast(__lowerCamelCase							,__lowerCamelCase					)
								return pa_table




							def  SCREAMING_SNAKE_CASE_ (						self		:					str					):





								'''simple docstring'''



								a		=			self.config.chunksize
								a		=			pd.read_sql(
								    self.config.sql							,self.config.con							,chunksize=__lowerCamelCase							,**self.config.pd_read_sql_kwargs					)
								a		=			[sql_reader] if chunksize is None else sql_reader
								for chunk_idx, df in enumerate(__lowerCamelCase					):
									a		=			pa.Table.from_pandas(__lowerCamelCase					)
									yield chunk_idx, self._cast_table(__lowerCamelCase					)





