
'''simple docstring'''



import os
from pathlib import Path

import numpy as np
import pytest
from pack_dataset import pack_data_dir
from parameterized import parameterized
from save_len_file import save_len_file
from torch.utils.data import DataLoader

from transformers import AutoTokenizer
from transformers.models.mbart.modeling_mbart import shift_tokens_right
from transformers.testing_utils import TestCasePlus, slow
from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset


_lowercase          =						"""bert-base-cased"""
_lowercase          =						"""google/pegasus-xsum"""
_lowercase          =						[""" Sam ate lunch today.""", """Sams lunch ingredients."""]
_lowercase          =						["""A very interesting story about what I ate for lunch.""", """Avocado, celery, turkey, coffee"""]
_lowercase          =						"""patrickvonplaten/t5-tiny-random"""
_lowercase          =						"""sshleifer/bart-tiny-random"""
_lowercase          =						"""sshleifer/tiny-mbart"""
_lowercase          =						"""sshleifer/tiny-marian-en-de"""


def A (__lowerCamelCase :Path					,      __lowerCamelCase :list						):
						_lowerCAmelCase    		=					"""\n""".join(__lowerCamelCase						)
						Path(__lowerCamelCase						).open("""w"""						).writelines(__lowerCamelCase						)


def A (__lowerCamelCase :Union[str, Any]						):
						for split in ["train", "val", "test"]:
												_dump_articles(os.path.join(__lowerCamelCase					,      f'{split}.source'						)					,      __lowerCamelCase						)
												_dump_articles(os.path.join(__lowerCamelCase					,      f'{split}.target'						)					,      __lowerCamelCase						)
						return tmp_dir





class     UpperCAmelCase_	(						_SCREAMING_SNAKE_CASE					):






						'''simple docstring'''






						@parameterized.expand(
						    [
						        MBART_TINY,
						        MARIAN_TINY,
						        T5_TINY,
						        BART_TINY,
						        PEGASUS_XSUM,
						    ]       ,  )
						@slow
						def    _lowercase      (   self       ,  _lowercase ):

												"""simple docstring"""



												_lowerCAmelCase    		=					AutoTokenizer.from_pretrained(_lowercase )
												_lowerCAmelCase    		=					make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() )
												_lowerCAmelCase    		=					max(len(tokenizer.encode(_lowercase ) ) for a in ARTICLES )
												_lowerCAmelCase    		=					max(len(tokenizer.encode(_lowercase ) ) for a in SUMMARIES )
												_lowerCAmelCase    		=					4
												_lowerCAmelCase    		=					8
												assert max_len_target > max_src_len  # Will be truncated
												assert max_len_source > max_src_len  # Will be truncated
												_lowerCAmelCase		,       _lowerCAmelCase    		=					"""ro_RO""", """de_DE"""  # ignored for all but mbart, but never causes error.
												_lowerCAmelCase    		=					SeqaSeqDataset(
												    _lowercase       ,  data_dir=_lowercase       ,  type_path="""train"""       ,  max_source_length=_lowercase       ,  max_target_length=_lowercase       ,  src_lang=_lowercase       ,  tgt_lang=_lowercase       ,  )
												_lowerCAmelCase    		=					DataLoader(_lowercase       ,  batch_size=2       ,  collate_fn=train_dataset.collate_fn )
												for batch in dataloader:
																		assert isinstance(_lowercase       ,  _lowercase )
																		assert batch["attention_mask"].shape == batch["input_ids"].shape
																		# show that articles were trimmed.
																		assert batch["input_ids"].shape[1] == max_src_len
																		# show that targets are the same len
																		assert batch["labels"].shape[1] == max_tgt_len
																		if tok_name != MBART_TINY:
																								continue
																		# check language codes in correct place
																		_lowerCAmelCase    		=					shift_tokens_right(batch["""labels"""]       ,  tokenizer.pad_token_id )
																		assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang]
																		assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id
																		assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id
																		assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang]

																		break  # No need to test every batch




						@parameterized.expand([BART_TINY, BERT_BASE_CASED] )
						def    _lowercase      (   self       ,  _lowercase ):

												"""simple docstring"""



												_lowerCAmelCase    		=					AutoTokenizer.from_pretrained(_lowercase )
												_lowerCAmelCase    		=					make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() )
												_lowerCAmelCase    		=					max(len(tokenizer.encode(_lowercase ) ) for a in ARTICLES )
												_lowerCAmelCase    		=					max(len(tokenizer.encode(_lowercase ) ) for a in SUMMARIES )
												_lowerCAmelCase    		=					4
												_lowerCAmelCase    		=					LegacySeqaSeqDataset(
												    _lowercase       ,  data_dir=_lowercase       ,  type_path="""train"""       ,  max_source_length=20       ,  max_target_length=_lowercase       ,  )
												_lowerCAmelCase    		=					DataLoader(_lowercase       ,  batch_size=2       ,  collate_fn=train_dataset.collate_fn )
												for batch in dataloader:
																		assert batch["attention_mask"].shape == batch["input_ids"].shape
																		# show that articles were trimmed.
																		assert batch["input_ids"].shape[1] == max_len_source
																		assert 20 >= batch["input_ids"].shape[1]  # trimmed significantly
																		# show that targets were truncated
																		assert batch["labels"].shape[1] == trunc_target  # Truncated
																		assert max_len_target > trunc_target  # Truncated
																		break  # No need to test every batch




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					AutoTokenizer.from_pretrained("""facebook/mbart-large-cc25""" )

												_lowerCAmelCase    		=					Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) )
												_lowerCAmelCase    		=					tmp_dir.joinpath("""train.source""" ).open().readlines()
												_lowerCAmelCase    		=					Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) )
												pack_data_dir(_lowercase       ,  _lowercase       ,  128       ,  _lowercase )
												_lowerCAmelCase    		=					{x.name for x in tmp_dir.iterdir()}
												_lowerCAmelCase    		=					{x.name for x in save_dir.iterdir()}
												_lowerCAmelCase    		=					save_dir.joinpath("""train.source""" ).open().readlines()
												# orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.']
												# desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.']
												assert len(_lowercase ) < len(_lowercase )
												assert len(_lowercase ) == 1
												assert len(packed_examples[0] ) == sum(len(_lowercase ) for x in orig_examples )
												assert orig_paths == new_paths




						@pytest.mark.skipif(not FAIRSEQ_AVAILABLE       ,  reason="""This test requires fairseq""" )
						def    _lowercase      (   self ):

												"""simple docstring"""



												if not FAIRSEQ_AVAILABLE:
																		return
												_lowerCAmelCase		,       _lowerCAmelCase		,       _lowerCAmelCase    		=					self._get_dataset(max_len=64 )
												_lowerCAmelCase    		=					64
												_lowerCAmelCase    		=					ds.make_dynamic_sampler(_lowercase       ,  required_batch_size_multiple=_lowercase )
												_lowerCAmelCase    		=					[len(_lowercase ) for x in batch_sampler]
												assert len(set(_lowercase ) ) > 1  # it's not dynamic batch size if every batch is the same length
												assert sum(_lowercase ) == len(_lowercase )  # no dropped or added examples
												_lowerCAmelCase    		=					DataLoader(_lowercase       ,  batch_sampler=_lowercase       ,  collate_fn=ds.collate_fn       ,  num_workers=2 )
												_lowerCAmelCase    		=					[]
												_lowerCAmelCase    		=					[]
												for batch in data_loader:
																		_lowerCAmelCase    		=					batch["""input_ids"""].shape
																		_lowerCAmelCase    		=					src_shape[0]
																		assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple
																		_lowerCAmelCase    		=					np.product(batch["""input_ids"""].shape )
																		num_src_per_batch.append(_lowercase )
																		if num_src_tokens > (max_tokens * 1.1):
																								failures.append(_lowercase )
												assert num_src_per_batch[0] == max(_lowercase )
												if failures:
																		raise AssertionError(F'too many tokens in {len(_lowercase )} batches' )




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase		,       _lowerCAmelCase		,       _lowerCAmelCase    		=					self._get_dataset(max_len=512 )
												_lowerCAmelCase    		=					2
												_lowerCAmelCase    		=					ds.make_sortish_sampler(_lowercase       ,  shuffle=_lowercase )

												_lowerCAmelCase    		=					DataLoader(_lowercase       ,  batch_size=_lowercase       ,  collate_fn=ds.collate_fn       ,  num_workers=2 )
												_lowerCAmelCase    		=					DataLoader(_lowercase       ,  batch_size=_lowercase       ,  collate_fn=ds.collate_fn       ,  num_workers=2       ,  sampler=_lowercase )

												_lowerCAmelCase    		=					tokenizer.pad_token_id

												def count_pad_tokens(_lowercase       ,  _lowercase="input_ids" ):
																		return [batch[k].eq(_lowercase ).sum().item() for batch in data_loader]

												assert sum(count_pad_tokens(_lowercase       ,  k="""labels""" ) ) < sum(count_pad_tokens(_lowercase       ,  k="""labels""" ) )
												assert sum(count_pad_tokens(_lowercase ) ) < sum(count_pad_tokens(_lowercase ) )
												assert len(_lowercase ) == len(_lowercase )




						def    _lowercase      (   self       ,  _lowercase=1_000       ,  _lowercase=128 ):

												"""simple docstring"""



												if os.getenv("""USE_REAL_DATA"""       ,  _lowercase ):
																		_lowerCAmelCase    		=					"""examples/seq2seq/wmt_en_ro"""
																		_lowerCAmelCase    		=					max_len * 2 * 64
																		if not Path(_lowercase ).joinpath("""train.len""" ).exists():
																								save_len_file(_lowercase       ,  _lowercase )
												else:
																		_lowerCAmelCase    		=					"""examples/seq2seq/test_data/wmt_en_ro"""
																		_lowerCAmelCase    		=					max_len * 4
																		save_len_file(_lowercase       ,  _lowercase )

												_lowerCAmelCase    		=					AutoTokenizer.from_pretrained(_lowercase )
												_lowerCAmelCase    		=					SeqaSeqDataset(
												    _lowercase       ,  data_dir=_lowercase       ,  type_path="""train"""       ,  max_source_length=_lowercase       ,  max_target_length=_lowercase       ,  n_obs=_lowercase       ,  )
												return ds, max_tokens, tokenizer




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase		,       _lowerCAmelCase		,       _lowerCAmelCase    		=					self._get_dataset()
												_lowerCAmelCase    		=					set(DistributedSortishSampler(_lowercase       ,  256       ,  num_replicas=2       ,  rank=0       ,  add_extra_examples=_lowercase ) )
												_lowerCAmelCase    		=					set(DistributedSortishSampler(_lowercase       ,  256       ,  num_replicas=2       ,  rank=1       ,  add_extra_examples=_lowercase ) )
												assert idsa.intersection(_lowercase ) == set()




						@parameterized.expand(
						    [
						        MBART_TINY,
						        MARIAN_TINY,
						        T5_TINY,
						        BART_TINY,
						        PEGASUS_XSUM,
						    ]       ,  )
						def    _lowercase      (   self       ,  _lowercase ):

												"""simple docstring"""



												_lowerCAmelCase    		=					AutoTokenizer.from_pretrained(_lowercase       ,  use_fast=_lowercase )
												if tok_name == MBART_TINY:
																		_lowerCAmelCase    		=					SeqaSeqDataset(
																		    _lowercase       ,  data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() )       ,  type_path="""train"""       ,  max_source_length=4       ,  max_target_length=8       ,  src_lang="""EN"""       ,  tgt_lang="""FR"""       ,  )
																		_lowerCAmelCase    		=					train_dataset.dataset_kwargs
																		assert "src_lang" in kwargs and "tgt_lang" in kwargs
												else:
																		_lowerCAmelCase    		=					SeqaSeqDataset(
																		    _lowercase       ,  data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() )       ,  type_path="""train"""       ,  max_source_length=4       ,  max_target_length=8       ,  )
																		_lowerCAmelCase    		=					train_dataset.dataset_kwargs
																		assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs
																		assert len(_lowercase ) == 1 if tok_name == BART_TINY else len(_lowercase ) == 0








'''simple docstring'''







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 						SCREAMING_SNAKE_CASE       :






			def __init__(			self   :      Union[str, Any]	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      str=13	,    __SCREAMING_SNAKE_CASE   :      Optional[int]=7	,    __SCREAMING_SNAKE_CASE   :      List[str]=True	,    __SCREAMING_SNAKE_CASE   :      Optional[int]=True	,    __SCREAMING_SNAKE_CASE   :      Any=True	,    __SCREAMING_SNAKE_CASE   :      int=True	,    __SCREAMING_SNAKE_CASE   :      List[str]=99	,    __SCREAMING_SNAKE_CASE   :      Optional[int]=32	,    __SCREAMING_SNAKE_CASE   :      Tuple=2	,    __SCREAMING_SNAKE_CASE   :      str=4	,    __SCREAMING_SNAKE_CASE   :      int=37	,    __SCREAMING_SNAKE_CASE   :      Any="gelu"	,    __SCREAMING_SNAKE_CASE   :      List[Any]=0.1	,    __SCREAMING_SNAKE_CASE   :      List[Any]=0.1	,    __SCREAMING_SNAKE_CASE   :      Tuple=512	,    __SCREAMING_SNAKE_CASE   :      Optional[Any]=16	,    __SCREAMING_SNAKE_CASE   :      Dict=2	,    __SCREAMING_SNAKE_CASE   :      List[Any]=0.02	,    __SCREAMING_SNAKE_CASE   :      List[Any]=3	,    __SCREAMING_SNAKE_CASE   :      List[Any]=4	,    __SCREAMING_SNAKE_CASE   :      Dict=None	,    )      ->      Tuple:
							a_		:	Any									=   parent
							a_		:	List[str]									=   13
							a_		:	Dict									=   7
							a_		:	List[str]									=   True
							a_		:	str									=   True
							a_		:	List[str]									=   True
							a_		:	Any									=   True
							a_		:	List[Any]									=   99
							a_		:	Optional[Any]									=   384
							a_		:	List[str]									=   2
							a_		:	Optional[int]									=   4
							a_		:	Optional[int]									=   37
							a_		:	Union[str, Any]									=   '''gelu'''
							a_		:	List[str]									=   0.1
							a_		:	str									=   0.1
							a_		:	Optional[Any]									=   512
							a_		:	Union[str, Any]									=   16
							a_		:	Optional[Any]									=   2
							a_		:	Optional[int]									=   0.02
							a_		:	Union[str, Any]									=   3
							a_		:	Union[str, Any]									=   4
							a_		:	List[Any]									=   128
							a_		:	Optional[Any]									=   2
							a_		:	Any									=   9
							a_		:	Optional[int]									=   1
							a_		:	int									=   None






			def       SCREAMING_SNAKE_CASE   (			self   :      Dict       )      ->      Union[str, Any]:
							a_		:	Dict									=   ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size       )

							a_		:	int									=   None
							if self.use_input_mask:
											a_		:	Dict									=   random_attention_mask([self.batch_size, self.seq_length]       )

							a_		:	Optional[int]									=   None
							if self.use_token_type_ids:
											a_		:	Any									=   ids_tensor([self.batch_size, self.seq_length]	,    self.type_vocab_size       )

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

							a_		:	Union[str, Any]									=   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=__SCREAMING_SNAKE_CASE	,    )

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






			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[Any]	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      int	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      Optional[Any]       )      ->      Union[str, Any]:
							a_		:	Tuple									=   TFConvBertModel(config=__SCREAMING_SNAKE_CASE       )
							a_		:	str									=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

							a_		:	Optional[Any]									=   [input_ids, input_mask]
							a_		:	Tuple									=   model(__SCREAMING_SNAKE_CASE       )

							a_		:	int									=   model(__SCREAMING_SNAKE_CASE       )

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






			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[Any]	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      Any	,    __SCREAMING_SNAKE_CASE   :      List[str]	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Dict       )      ->      str:
							a_		:	List[Any]									=   TFConvBertForMaskedLM(config=__SCREAMING_SNAKE_CASE       )
							a_		:	Dict									=   {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							    '''token_type_ids''': token_type_ids,
							}
							a_		:	str									=   model(__SCREAMING_SNAKE_CASE       )
							self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.vocab_size)       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[int]	,    __SCREAMING_SNAKE_CASE   :      str	,    __SCREAMING_SNAKE_CASE   :      int	,    __SCREAMING_SNAKE_CASE   :      str	,    __SCREAMING_SNAKE_CASE   :      List[str]	,    __SCREAMING_SNAKE_CASE   :      Dict	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      int       )      ->      Any:
							a_		:	Union[str, Any]									=   self.num_labels
							a_		:	List[str]									=   TFConvBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE       )
							a_		:	Optional[Any]									=   {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							    '''token_type_ids''': token_type_ids,
							}

							a_		:	Tuple									=   model(__SCREAMING_SNAKE_CASE       )
							self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)       )






			def       SCREAMING_SNAKE_CASE   (			self   :      List[Any]	,    __SCREAMING_SNAKE_CASE   :      List[str]	,    __SCREAMING_SNAKE_CASE   :      Optional[Any]	,    __SCREAMING_SNAKE_CASE   :      str	,    __SCREAMING_SNAKE_CASE   :      str	,    __SCREAMING_SNAKE_CASE   :      Dict	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Tuple       )      ->      Optional[int]:
							a_		:	Optional[Any]									=   self.num_choices
							a_		:	int									=   TFConvBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE       )
							a_		:	List[str]									=   tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,    1       )	,    (1, self.num_choices, 1)       )
							a_		:	Tuple									=   tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,    1       )	,    (1, self.num_choices, 1)       )
							a_		:	List[Any]									=   tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,    1       )	,    (1, self.num_choices, 1)       )
							a_		:	Union[str, Any]									=   {
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							    '''token_type_ids''': multiple_choice_token_type_ids,
							}
							a_		:	Optional[int]									=   model(__SCREAMING_SNAKE_CASE       )
							self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_choices)       )






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






			def       SCREAMING_SNAKE_CASE   (			self   :      List[str]	,    __SCREAMING_SNAKE_CASE   :      Optional[Any]	,    __SCREAMING_SNAKE_CASE   :      List[Any]	,    __SCREAMING_SNAKE_CASE   :      List[Any]	,    __SCREAMING_SNAKE_CASE   :      str	,    __SCREAMING_SNAKE_CASE   :      Optional[int]	,    __SCREAMING_SNAKE_CASE   :      Tuple	,    __SCREAMING_SNAKE_CASE   :      Union[str, Any]       )      ->      Tuple:
							a_		:	List[Any]									=   TFConvBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE       )
							a_		:	Optional[Any]									=   {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							    '''token_type_ids''': token_type_ids,
							}

							a_		:	Dict									=   model(__SCREAMING_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       SCREAMING_SNAKE_CASE   (			self   :      List[str]       )      ->      Any:
							a_		:	List[Any]									=   self.prepare_config_and_inputs()
							(
							    (
							    a_
							)					,      (
							    a_
							)					,      (
							    a_
							)					,      (
							    a_
							)					,      (
							    a_
							)					,      (
							    a_
							)					,      (
							    a_
							)					,      
							)		:	List[Any]									=   config_and_inputs
							a_		:	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       (       SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, unittest.TestCase							):
			snake_case__	=					(
			    (
			        TFConvBertModel,
			        TFConvBertForMaskedLM,
			        TFConvBertForQuestionAnswering,
			        TFConvBertForSequenceClassification,
			        TFConvBertForTokenClassification,
			        TFConvBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case__	=					(
			    {
			        "feature-extraction": TFConvBertModel,
			        "fill-mask": TFConvBertForMaskedLM,
			        "question-answering": TFConvBertForQuestionAnswering,
			        "text-classification": TFConvBertForSequenceClassification,
			        "token-classification": TFConvBertForTokenClassification,
			        "zero-shot": TFConvBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			snake_case__	=					False
			snake_case__	=					False
			snake_case__	=					False






			def       SCREAMING_SNAKE_CASE   (			self   :      List[str]       )      ->      List[Any]:
							a_		:	Optional[int]									=   TFConvBertModelTester(self       )
							a_		:	Any									=   ConfigTester(self	,    config_class=__SCREAMING_SNAKE_CASE	,    hidden_size=37       )






			def       SCREAMING_SNAKE_CASE   (			self   :      str       )      ->      Optional[Any]:
							self.config_tester.run_common_tests()






			def       SCREAMING_SNAKE_CASE   (			self   :      Tuple       )      ->      str:
							a_		:	Any									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[Any]       )      ->      List[str]:
							a_		:	str									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Any       )      ->      str:
							a_		:	Dict									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE       )






			def       SCREAMING_SNAKE_CASE   (			self   :      str       )      ->      Optional[Any]:
							a_		:	Optional[Any]									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Any       )      ->      str:
							a_		:	List[str]									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE       )






			def       SCREAMING_SNAKE_CASE   (			self   :      str       )      ->      Optional[int]:
							a_		:	Any									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE       )






			@slow
			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[int]       )      ->      Union[str, Any]:
							a_					,      a_		:	Optional[Any]									=   self.model_tester.prepare_config_and_inputs_for_common()
							a_		:	Any									=   True
							a_		:	Optional[int]									=   True

							if hasattr(__SCREAMING_SNAKE_CASE	,    '''use_cache'''       ):
											a_		:	Dict									=   True

							a_		:	Any									=   getattr(self.model_tester	,    '''encoder_seq_length'''	,    self.model_tester.seq_length       )
							a_		:	List[Any]									=   getattr(self.model_tester	,    '''key_length'''	,    __SCREAMING_SNAKE_CASE       )

							for model_class in self.all_model_classes:
											a_		:	str									=   self._prepare_for_class(__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )
											a_		:	Optional[int]									=   model_class(__SCREAMING_SNAKE_CASE       )
											a_		:	Any									=   len(model(__SCREAMING_SNAKE_CASE       )       )

											with tempfile.TemporaryDirectory() as tmpdirname:
															model.save_pretrained(__SCREAMING_SNAKE_CASE	,    saved_model=__SCREAMING_SNAKE_CASE       )
															a_		:	Union[str, Any]									=   os.path.join(__SCREAMING_SNAKE_CASE	,    '''saved_model'''	,    '''1'''       )
															a_		:	Optional[int]									=   tf.keras.models.load_model(__SCREAMING_SNAKE_CASE       )
															a_		:	Any									=   model(__SCREAMING_SNAKE_CASE       )

															if self.is_encoder_decoder:
																			a_		:	Optional[int]									=   outputs['''encoder_hidden_states''']
																			a_		:	Dict									=   outputs['''encoder_attentions''']
															else:
																			a_		:	Union[str, Any]									=   outputs['''hidden_states''']
																			a_		:	Tuple									=   outputs['''attentions''']

															self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,    __SCREAMING_SNAKE_CASE       )

															a_		:	Union[str, Any]									=   getattr(
															    self.model_tester	,    '''expected_num_hidden_layers'''	,    self.model_tester.num_hidden_layers + 1       )

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

															self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,    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       SCREAMING_SNAKE_CASE   (			self   :      str       )      ->      Optional[int]:
							a_		:	Tuple									=   TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
							self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )







			def       SCREAMING_SNAKE_CASE   (			self   :      Any       )      ->      List[Any]:
							a_					,      a_		:	Union[str, Any]									=   self.model_tester.prepare_config_and_inputs_for_common()
							a_		:	Optional[Any]									=   True
							a_		:	Dict									=   getattr(self.model_tester	,    '''decoder_seq_length'''	,    self.model_tester.seq_length       )
							a_		:	List[str]									=   getattr(self.model_tester	,    '''encoder_seq_length'''	,    self.model_tester.seq_length       )
							a_		:	Dict									=   getattr(self.model_tester	,    '''key_length'''	,    __SCREAMING_SNAKE_CASE       )
							a_		:	Any									=   getattr(self.model_tester	,    '''key_length'''	,    __SCREAMING_SNAKE_CASE       )

							def check_decoder_attentions_output(__SCREAMING_SNAKE_CASE   :      List[str]       ):
											a_		:	int									=   len(__SCREAMING_SNAKE_CASE       )
											self.assertEqual(out_len % 2	,    0       )
											a_		:	Dict									=   outputs.decoder_attentions
											self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,    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(__SCREAMING_SNAKE_CASE   :      List[str]       ):
											a_		:	Optional[int]									=   [
											    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
											]
											self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,    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:
											a_		:	Tuple									=   True
											a_		:	Tuple									=   False
											a_		:	Optional[Any]									=   model_class(__SCREAMING_SNAKE_CASE       )
											a_		:	List[Any]									=   model(self._prepare_for_class(__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )       )
											a_		:	Union[str, Any]									=   len(__SCREAMING_SNAKE_CASE       )
											self.assertEqual(config.output_hidden_states	,    __SCREAMING_SNAKE_CASE       )
											check_encoder_attentions_output(__SCREAMING_SNAKE_CASE       )

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

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

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

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





@require_tf
class 						SCREAMING_SNAKE_CASE       (       unittest.TestCase							):






			@slow
			def       SCREAMING_SNAKE_CASE   (			self   :      int       )      ->      Any:
							a_		:	int									=   TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
							a_		:	List[Any]									=   tf.constant([[0, 1, 2, 3, 4, 5]]       )
							a_		:	Any									=   model(__SCREAMING_SNAKE_CASE       )[0]

							a_		:	str									=   [1, 6, 768]
							self.assertEqual(output.shape	,    __SCREAMING_SNAKE_CASE       )

							a_		:	Dict									=   tf.constant(
							    [
							        [
							            [-0.0347_5493, -0.468_6034, -0.3063_8832],
							            [0.2263_7248, -0.2698_8646, -0.742_3424],
							            [0.1032_4868, -0.4501_3508, -0.5828_0784],
							        ]
							    ]       )
							tf.debugging.assert_near(output[:, :3, :3]	,    __SCREAMING_SNAKE_CASE	,    atol=1e-4       )




