






import asyncio
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from distutils.util import strtobool
from functools import partial
from pathlib import Path
from typing import List, Union
from unittest import mock

import torch

from ..state import AcceleratorState, PartialState
from ..utils import (
    gather,
    is_bnb_available,
    is_comet_ml_available,
    is_datasets_available,
    is_deepspeed_available,
    is_mps_available,
    is_safetensors_available,
    is_tensorboard_available,
    is_torch_version,
    is_tpu_available,
    is_transformers_available,
    is_wandb_available,
    is_xpu_available,
)
def       snake_case_		(     lowerCAmelCase_       :						Optional[int]       ,       lowerCAmelCase_       :						Optional[int]=False      ):






							try:
														__lowercase      :							Optional[Any]				=     os.environ[key]
							except KeyError:
														# KEY isn't set, default to `default`.
														__lowercase      :							List[str]				=     default
							else:
														# KEY is set, convert it to True or False.
														try:
																					__lowercase      :							Dict				=     strtobool(__SCREAMING_SNAKE_CASE      )
														except ValueError:
																					# More values are supported, but let's keep the message simple.
																					raise ValueError(F"If set, {key} must be yes or no."      )
							return _value


lowerCamelCase : List[Any]						   =       parse_flag_from_env('''RUN_SLOW''', default=False)
def       snake_case_		(     lowerCAmelCase_       :						Tuple      ):
							return unittest.skip("""Test was skipped"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[str]      ):
							return unittest.skipUnless(_run_slow_tests       ,       """test is slow"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[Any]      ):
							return unittest.skipUnless(not torch.cuda.is_available()       ,       """test requires only a CPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						int      ):
							return unittest.skipUnless(torch.cuda.is_available()       ,       """test requires a GPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Any      ):
							return unittest.skipUnless(is_xpu_available()       ,       """test requires a XPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						str      ):
							return unittest.skipUnless(is_mps_available()       ,       """test requires a `mps` backend support in `torch`"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[Any]      ):
							return unittest.skipUnless(
							    is_transformers_available() and is_datasets_available()       ,       """test requires the Hugging Face suite"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[str]      ):
							return unittest.skipUnless(is_bnb_available()       ,       """test requires the bitsandbytes library"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Any      ):
							return unittest.skipUnless(is_tpu_available()       ,       """test requires TPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Union[str, Any]      ):
							return unittest.skipUnless(torch.cuda.device_count() == 1       ,       """test requires a GPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Union[str, Any]      ):
							return unittest.skipUnless(torch.xpu.device_count() == 1       ,       """test requires a XPU"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Optional[int]      ):
							return unittest.skipUnless(torch.cuda.device_count() > 1       ,       """test requires multiple GPUs"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Optional[int]      ):
							return unittest.skipUnless(torch.xpu.device_count() > 1       ,       """test requires multiple XPUs"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[Any]      ):
							return unittest.skipUnless(is_safetensors_available()       ,       """test requires safetensors"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Dict      ):
							return unittest.skipUnless(is_deepspeed_available()       ,       """test requires DeepSpeed"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Union[str, Any]      ):
							return unittest.skipUnless(is_torch_version(""">="""       ,       """1.12.0"""      )       ,       """test requires torch version >= 1.12.0"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[str]=None       ,       lowerCAmelCase_       :						Union[str, Any]=None      ):






							if test_case is None:
														return partial(__SCREAMING_SNAKE_CASE       ,       version=__SCREAMING_SNAKE_CASE      )
							return unittest.skipUnless(is_torch_version(""">="""       ,       __SCREAMING_SNAKE_CASE      )       ,       F"test requires torch version >= {version}"      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						List[str]      ):
							return unittest.skipUnless(is_tensorboard_available()       ,       """test requires Tensorboard"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						int      ):
							return unittest.skipUnless(is_wandb_available()       ,       """test requires wandb"""      )(__SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Any      ):
							return unittest.skipUnless(is_comet_ml_available()       ,       """test requires comet_ml"""      )(__SCREAMING_SNAKE_CASE      )


lowerCamelCase : List[Any]						   =       (
    any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available()
)
def       snake_case_		(     lowerCAmelCase_       :						str      ):
							return unittest.skipUnless(
							    _atleast_one_tracker_available       ,       """test requires at least one tracker to be available and for `comet_ml` to not be installed"""       ,       )(__SCREAMING_SNAKE_CASE      )

class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					_A						:			Optional[int]									=      True



					@classmethod
					def       lowerCAmelCase				(  cls				:	Union[str, Any]							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     tempfile.mkdtemp()



					@classmethod
					def       lowerCAmelCase				(  cls				:	Optional[int]							)			->  List[Any]:


												"""simple docstring"""

												if os.path.exists(cls.tmpdir							):
																			shutil.rmtree(cls.tmpdir							)



					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Tuple:


												"""simple docstring"""

												if self.clear_on_setup:
																			for path in Path(self.tmpdir							).glob("""**/*"""							):
																										if path.is_file():
																																	path.unlink()
																										elif path.is_dir():
																																	shutil.rmtree(lowercase_							)







class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					def       lowerCAmelCase				(  self				:	Dict							)			->  List[str]:


												"""simple docstring"""

												super().tearDown()
												# Reset the state of the AcceleratorState singleton.
												AcceleratorState._reset_state()
												PartialState._reset_state()







class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					def       lowerCAmelCase				(  self				:	int		,     __a				:	Optional[int]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							Dict				=     mocks if isinstance(lowercase_		,     (tuple, list)							) else [mocks]
												for m in self.mocks:
																			m.start()
																			self.addCleanup(m.stop							)







def       snake_case_		(     lowerCAmelCase_       :						Optional[Any]      ):
							__lowercase      :							Any				=     AcceleratorState()
							__lowercase      :							Optional[int]				=     tensor[None].clone().to(state.device      )
							__lowercase      :							Any				=     gather(__SCREAMING_SNAKE_CASE      ).cpu()
							__lowercase      :							List[str]				=     tensor[0].cpu()
							for i in range(tensors.shape[0]      ):
														if not torch.equal(tensors[i]       ,       __SCREAMING_SNAKE_CASE      ):
																					return False
							return True

class 		lowerCAmelCase     :

					'''simple docstring'''

					def __init__(  self				:	Optional[int]		,     __a				:	str		,     __a				:	List[Any]		,     __a				:	Union[str, Any]							)			->  List[str]:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     returncode
												__lowercase      :							List[Any]				=     stdout
												__lowercase      :							Tuple				=     stderr







async def       snake_case_		(     lowerCAmelCase_       :						Union[str, Any]       ,       lowerCAmelCase_       :						List[Any]      ):






							while True:
														__lowercase      :							List[str]				=     await stream.readline()
														if line:
																					callback(__SCREAMING_SNAKE_CASE      )
														else:
																					break
async def       snake_case_		(     lowerCAmelCase_       :						int       ,       lowerCAmelCase_       :						List[Any]=None       ,       lowerCAmelCase_       :						Tuple=None       ,       lowerCAmelCase_       :						Dict=None       ,       lowerCAmelCase_       :						Union[str, Any]=False       ,       lowerCAmelCase_       :						int=False      ):






							if echo:
														print("""\nRunning: """       ,       """ """.join(__SCREAMING_SNAKE_CASE      )      )

							__lowercase      :							List[Any]				=     await asyncio.create_subprocess_exec(
							    cmd[0]       ,       *cmd[1:]       ,       stdin=__SCREAMING_SNAKE_CASE       ,       stdout=asyncio.subprocess.PIPE       ,       stderr=asyncio.subprocess.PIPE       ,       env=__SCREAMING_SNAKE_CASE       ,       )

							# note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe
							# https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait
							#
							# If it starts hanging, will need to switch to the following code. The problem is that no data
							# will be seen until it's done and if it hangs for example there will be no debug info.
							# out, err = await p.communicate()
							# return _RunOutput(p.returncode, out, err)

							__lowercase      :							Any				=     []
							__lowercase      :							Any				=     []

							def tee(lowerCAmelCase_       :						Union[str, Any]       ,       lowerCAmelCase_       :						List[str]       ,       lowerCAmelCase_       :						Dict       ,       lowerCAmelCase_       :						Any=""      ):
														__lowercase      :							int				=     line.decode("""utf-8"""      ).rstrip()
														sink.append(__SCREAMING_SNAKE_CASE      )
														if not quiet:
																					print(__SCREAMING_SNAKE_CASE       ,       __SCREAMING_SNAKE_CASE       ,       file=__SCREAMING_SNAKE_CASE      )

    # XXX: the timeout doesn't seem to make any difference here
							await asyncio.wait(
							    [
							        asyncio.create_task(_read_stream(p.stdout       ,       lambda lowerCAmelCase_      : tee(__SCREAMING_SNAKE_CASE       ,       __SCREAMING_SNAKE_CASE       ,       sys.stdout       ,       label="""stdout:"""      )      )      ),
							        asyncio.create_task(_read_stream(p.stderr       ,       lambda lowerCAmelCase_      : tee(__SCREAMING_SNAKE_CASE       ,       __SCREAMING_SNAKE_CASE       ,       sys.stderr       ,       label="""stderr:"""      )      )      ),
							    ]       ,       timeout=__SCREAMING_SNAKE_CASE       ,       )
							return _RunOutput(await p.wait()       ,       __SCREAMING_SNAKE_CASE       ,       __SCREAMING_SNAKE_CASE      )
def       snake_case_		(     lowerCAmelCase_       :						Any       ,       lowerCAmelCase_       :						List[Any]=None       ,       lowerCAmelCase_       :						int=None       ,       lowerCAmelCase_       :						str=180       ,       lowerCAmelCase_       :						List[str]=False       ,       lowerCAmelCase_       :						List[str]=True      ):
							__lowercase      :							Optional[int]				=     asyncio.get_event_loop()
							__lowercase      :							Union[str, Any]				=     loop.run_until_complete(
							    _stream_subprocess(__SCREAMING_SNAKE_CASE       ,       env=__SCREAMING_SNAKE_CASE       ,       stdin=__SCREAMING_SNAKE_CASE       ,       timeout=__SCREAMING_SNAKE_CASE       ,       quiet=__SCREAMING_SNAKE_CASE       ,       echo=__SCREAMING_SNAKE_CASE      )      )

							__lowercase      :							Optional[Any]				=     " ".join(__SCREAMING_SNAKE_CASE      )
							if result.returncode > 0:
														__lowercase      :							List[Any]				=     "\n".join(result.stderr      )
														raise RuntimeError(
														    F"\'{cmd_str}\' failed with returncode {result.returncode}\n\n"
														    F"The combined stderr from workers follows:\n{stderr}"      )

							return result

class 		lowerCAmelCase     ( snake_case__			):

					'''simple docstring'''

					pass







def       snake_case_		(     lowerCAmelCase_       :						str       ,       lowerCAmelCase_       :						int=False      ):






							try:
														__lowercase      :							Dict				=     subprocess.check_output(__SCREAMING_SNAKE_CASE       ,       stderr=subprocess.STDOUT      )
														if return_stdout:
																					if hasattr(__SCREAMING_SNAKE_CASE       ,       """decode"""      ):
																												__lowercase      :							int				=     output.decode("""utf-8"""      )
																					return output
							except subprocess.CalledProcessError as e:
														raise SubprocessCallException(
														    F"Command `{' '.join(__SCREAMING_SNAKE_CASE      )}` failed with the following error:\n\n{e.output.decode()}"      ) from e







from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 (
	    TFConvBertForMaskedLM,
	    TFConvBertForMultipleChoice,
	    TFConvBertForQuestionAnswering,
	    TFConvBertForSequenceClassification,
	    TFConvBertForTokenClassification,
	    TFConvBertModel,
	)

class 		lowerCAmelCase     :

					'''simple docstring'''

					def __init__(  self				:	Union[str, Any]		,     __a				:	Dict		,     __a				:	Union[str, Any]=13		,     __a				:	Dict=7		,     __a				:	Dict=True		,     __a				:	Dict=True		,     __a				:	Any=True		,     __a				:	List[str]=True		,     __a				:	int=99		,     __a				:	Optional[int]=32		,     __a				:	str=2		,     __a				:	int=4		,     __a				:	List[str]=37		,     __a				:	Union[str, Any]="gelu"		,     __a				:	Union[str, Any]=0.1		,     __a				:	Union[str, Any]=0.1		,     __a				:	List[Any]=512		,     __a				:	int=16		,     __a				:	Union[str, Any]=2		,     __a				:	Union[str, Any]=0.02		,     __a				:	List[str]=3		,     __a				:	Dict=4		,     __a				:	Optional[Any]=None		,     )			->  Optional[Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     parent
												__lowercase      :							Tuple				=     13
												__lowercase      :							Dict				=     7
												__lowercase      :							List[Any]				=     True
												__lowercase      :							Tuple				=     True
												__lowercase      :							List[str]				=     True
												__lowercase      :							Any				=     True
												__lowercase      :							Optional[int]				=     99
												__lowercase      :							str				=     384
												__lowercase      :							Optional[Any]				=     2
												__lowercase      :							Dict				=     4
												__lowercase      :							str				=     37
												__lowercase      :							Optional[int]				=     """gelu"""
												__lowercase      :							int				=     0.1
												__lowercase      :							Union[str, Any]				=     0.1
												__lowercase      :							Tuple				=     512
												__lowercase      :							Tuple				=     16
												__lowercase      :							Optional[int]				=     2
												__lowercase      :							Optional[Any]				=     0.02
												__lowercase      :							Dict				=     3
												__lowercase      :							Union[str, Any]				=     4
												__lowercase      :							Tuple				=     128
												__lowercase      :							Optional[Any]				=     2
												__lowercase      :							int				=     9
												__lowercase      :							List[Any]				=     1
												__lowercase      :							Union[str, Any]				=     None



					def       lowerCAmelCase				(  self				:	str							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size							)

												__lowercase      :							Optional[Any]				=     None
												if self.use_input_mask:
																			__lowercase      :							Dict				=     random_attention_mask([self.batch_size, self.seq_length]							)

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

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

												__lowercase      :							Optional[int]				=     ConvBertConfig(
												    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		,     return_dict=__a		,     )

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



					def       lowerCAmelCase				(  self				:	Dict		,     __a				:	List[Any]		,     __a				:	List[str]		,     __a				:	Union[str, Any]		,     __a				:	str		,     __a				:	Union[str, Any]		,     __a				:	Tuple		,     __a				:	Tuple							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Dict				=     TFConvBertModel(config=__a							)
												__lowercase      :							Tuple				=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

												__lowercase      :							Any				=     [input_ids, input_mask]
												__lowercase      :							Dict				=     model(__a							)

												__lowercase      :							str				=     model(__a							)

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



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Union[str, Any]		,     __a				:	Any		,     __a				:	Tuple		,     __a				:	Union[str, Any]		,     __a				:	str		,     __a				:	Dict		,     __a				:	str							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     TFConvBertForMaskedLM(config=__a							)
												__lowercase      :							List[Any]				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}
												__lowercase      :							Any				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)							)



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	int		,     __a				:	Any		,     __a				:	Optional[int]		,     __a				:	int		,     __a				:	int		,     __a				:	List[Any]		,     __a				:	Optional[int]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							str				=     self.num_labels
												__lowercase      :							List[Any]				=     TFConvBertForSequenceClassification(config=__a							)
												__lowercase      :							int				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}

												__lowercase      :							List[str]				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	Any		,     __a				:	Optional[Any]		,     __a				:	int		,     __a				:	Optional[int]		,     __a				:	Tuple		,     __a				:	int		,     __a				:	int							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.num_choices
												__lowercase      :							Dict				=     TFConvBertForMultipleChoice(config=__a							)
												__lowercase      :							List[str]				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							int				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							str				=     tf.tile(tf.expand_dims(__a		,     1							)		,     (1, self.num_choices, 1)							)
												__lowercase      :							str				=     {
												    """input_ids""": multiple_choice_inputs_ids,
												    """attention_mask""": multiple_choice_input_mask,
												    """token_type_ids""": multiple_choice_token_type_ids,
												}
												__lowercase      :							Dict				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)							)



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	str		,     __a				:	List[str]		,     __a				:	List[str]		,     __a				:	List[str]		,     __a				:	Any		,     __a				:	Tuple		,     __a				:	Union[str, Any]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.num_labels
												__lowercase      :							Tuple				=     TFConvBertForTokenClassification(config=__a							)
												__lowercase      :							Dict				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}
												__lowercase      :							str				=     model(__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	List[Any]		,     __a				:	Optional[int]		,     __a				:	List[str]		,     __a				:	Optional[Any]		,     __a				:	int		,     __a				:	Tuple		,     __a				:	Any		,     __a				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     TFConvBertForQuestionAnswering(config=__a							)
												__lowercase      :							str				=     {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												}

												__lowercase      :							List[Any]				=     model(__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				:	Tuple							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.prepare_config_and_inputs()
												(
												    (
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			(
												    __lowercase
												)			,			
												)      :							int				=     config_and_inputs
												__lowercase      :							Union[str, Any]				=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
												return config, inputs_dict







@require_tf
class 		lowerCAmelCase     ( __a      ,       __a      ,       unittest.TestCase			):

					'''simple docstring'''

					_A						:			Dict									=      (
					    (
					        TFConvBertModel,
					        TFConvBertForMaskedLM,
					        TFConvBertForQuestionAnswering,
					        TFConvBertForSequenceClassification,
					        TFConvBertForTokenClassification,
					        TFConvBertForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					_A						:			str									=      (
					    {
					        '''feature-extraction''': TFConvBertModel,
					        '''fill-mask''': TFConvBertForMaskedLM,
					        '''question-answering''': TFConvBertForQuestionAnswering,
					        '''text-classification''': TFConvBertForSequenceClassification,
					        '''token-classification''': TFConvBertForTokenClassification,
					        '''zero-shot''': TFConvBertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					_A						:			Union[str, Any]									=      False
					_A						:			List[str]									=      False
					_A						:			Dict									=      False



					def       lowerCAmelCase				(  self				:	List[str]							)			->  int:


												"""simple docstring"""

												__lowercase      :							int				=     TFConvBertModelTester(self							)
												__lowercase      :							Tuple				=     ConfigTester(self		,     config_class=__a		,     hidden_size=37							)



					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Optional[Any]:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Union[str, Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*__a							)



					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase      :							List[str]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  str:


												"""simple docstring"""

												__lowercase      :							List[Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*__a							)



					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*__a							)



					@slow
					def       lowerCAmelCase				(  self				:	str							)			->  Any:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							Optional[int]				=     self.model_tester.prepare_config_and_inputs_for_common()
												__lowercase      :							Union[str, Any]				=     True
												__lowercase      :							List[Any]				=     True

												if hasattr(__a		,     """use_cache"""							):
																			__lowercase      :							Optional[Any]				=     True

												__lowercase      :							List[str]				=     getattr(self.model_tester		,     """encoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							int				=     getattr(self.model_tester		,     """key_length"""		,     __a							)

												for model_class in self.all_model_classes:
																			__lowercase      :							Optional[Any]				=     self._prepare_for_class(__a		,     __a							)
																			__lowercase      :							Tuple				=     model_class(__a							)
																			__lowercase      :							Tuple				=     len(model(__a							)							)

																			with tempfile.TemporaryDirectory() as tmpdirname:
																										model.save_pretrained(__a		,     saved_model=__a							)
																										__lowercase      :							List[Any]				=     os.path.join(__a		,     """saved_model"""		,     """1"""							)
																										__lowercase      :							str				=     tf.keras.models.load_model(__a							)
																										__lowercase      :							Optional[int]				=     model(__a							)

																										if self.is_encoder_decoder:
																																	__lowercase      :							Union[str, Any]				=     outputs["""encoder_hidden_states"""]
																																	__lowercase      :							Union[str, Any]				=     outputs["""encoder_attentions"""]
																										else:
																																	__lowercase      :							Union[str, Any]				=     outputs["""hidden_states"""]
																																	__lowercase      :							List[str]				=     outputs["""attentions"""]

																										self.assertEqual(len(__a							)		,     __a							)

																										__lowercase      :							List[Any]				=     getattr(
																										    self.model_tester		,     """expected_num_hidden_layers"""		,     self.model_tester.num_hidden_layers + 1							)

																										self.assertEqual(len(__a							)		,     __a							)
																										self.assertListEqual(
																										    list(output_hidden_states[0].shape[-2:]							)		,     [self.model_tester.seq_length, self.model_tester.hidden_size]		,     )

																										self.assertEqual(len(__a							)		,     self.model_tester.num_hidden_layers							)
																										self.assertListEqual(
																										    list(output_attentions[0].shape[-3:]							)		,     [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]		,     )



					@slow
					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							str				=     TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
												self.assertIsNotNone(__a							)



					def       lowerCAmelCase				(  self				:	Any							)			->  List[Any]:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs_for_common()
												__lowercase      :							List[str]				=     True
												__lowercase      :							List[Any]				=     getattr(self.model_tester		,     """decoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							Optional[int]				=     getattr(self.model_tester		,     """encoder_seq_length"""		,     self.model_tester.seq_length							)
												__lowercase      :							List[str]				=     getattr(self.model_tester		,     """key_length"""		,     __a							)
												__lowercase      :							List[Any]				=     getattr(self.model_tester		,     """key_length"""		,     __a							)

												def check_decoder_attentions_output(__a				:	List[str]							):
																			__lowercase      :							Union[str, Any]				=     len(__a							)
																			self.assertEqual(out_len % 2		,     0							)
																			__lowercase      :							Any				=     outputs.decoder_attentions
																			self.assertEqual(len(__a							)		,     self.model_tester.num_hidden_layers							)
																			self.assertListEqual(
																			    list(decoder_attentions[0].shape[-3:]							)		,     [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]		,     )

												def check_encoder_attentions_output(__a				:	str							):
																			__lowercase      :							str				=     [
																			    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
																			]
																			self.assertEqual(len(__a							)		,     self.model_tester.num_hidden_layers							)
																			self.assertListEqual(
																			    list(attentions[0].shape[-3:]							)		,     [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]		,     )

												for model_class in self.all_model_classes:
																			__lowercase      :							int				=     True
																			__lowercase      :							Any				=     False
																			__lowercase      :							List[Any]				=     model_class(__a							)
																			__lowercase      :							Tuple				=     model(self._prepare_for_class(__a		,     __a							)							)
																			__lowercase      :							Dict				=     len(__a							)
																			self.assertEqual(config.output_hidden_states		,     __a							)
																			check_encoder_attentions_output(__a							)

																			if self.is_encoder_decoder:
																										__lowercase      :							Any				=     model_class(__a							)
																										__lowercase      :							List[str]				=     model(self._prepare_for_class(__a		,     __a							)							)
																										self.assertEqual(config.output_hidden_states		,     __a							)
																										check_decoder_attentions_output(__a							)

																			# Check that output attentions can also be changed via the config
																			del inputs_dict["output_attentions"]
																			__lowercase      :							Dict				=     True
																			__lowercase      :							Optional[Any]				=     model_class(__a							)
																			__lowercase      :							Optional[int]				=     model(self._prepare_for_class(__a		,     __a							)							)
																			self.assertEqual(config.output_hidden_states		,     __a							)
																			check_encoder_attentions_output(__a							)

																			# Check attention is always last and order is fine
																			__lowercase      :							List[str]				=     True
																			__lowercase      :							List[Any]				=     True
																			__lowercase      :							Any				=     model_class(__a							)
																			__lowercase      :							Optional[int]				=     model(self._prepare_for_class(__a		,     __a							)							)

																			self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)		,     len(__a							)							)
																			self.assertEqual(model.config.output_hidden_states		,     __a							)
																			check_encoder_attentions_output(__a							)







@require_tf
class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					@slow
					def       lowerCAmelCase				(  self				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							List[str]				=     TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
												__lowercase      :							str				=     tf.constant([[0, 1, 2, 3, 4, 5]]							)
												__lowercase      :							Tuple				=     model(__a							)[0]

												__lowercase      :							Any				=     [1, 6, 768]
												self.assertEqual(output.shape		,     __a							)

												__lowercase      :							Optional[Any]				=     tf.constant(
												    [
												        [
												            [-0.03475493, -0.4686034, -0.30638832],
												            [0.22637248, -0.26988646, -0.7423424],
												            [0.10324868, -0.45013508, -0.58280784],
												        ]
												    ]							)
												tf.debugging.assert_near(output[:, :3, :3]		,     __a		,     atol=1E-4							)