


'''simple docstring'''






import collections
import json
import os
import re
from typing import TYPE_CHECKING, List, Optional, Tuple

import numpy as np

from ...tokenization_utils_fast import PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
							from transformers.pipelines.conversational import Conversation


__A						:						Optional[int]  =  logging.get_logger(__name__)

__A						:						int  =  {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''}

__A						:						Tuple  =  {
    '''vocab_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''',
    },
    '''emoji_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''',
    },
}

__A						:						str  =  {
    '''abeja/gpt-neox-japanese-2.7b''': 2_048,
}





def   UpperCAmelCase   (		lowerCamelCase_	:Dict					,   lowerCamelCase_	:Dict			):







		'''simple docstring'''
		with open(__snake_case					,   """r"""					,   encoding="""utf-8"""			) as f:
				snake_case_		:				Optional[int]      							=    json.loads(f.read()			)

		snake_case_		:				Any      							=    collections.OrderedDict()
		snake_case_		:				Optional[Any]      							=    collections.OrderedDict()
		snake_case_		:				Union[str, Any]      							=    collections.OrderedDict()
		with open(__snake_case					,   """r"""					,   encoding="""utf-8"""			) as f:
				snake_case_		:				Dict      							=    f.readlines()
		snake_case_		:				Tuple      							=    [[t.rstrip("""\n"""			)] if (t == ',' or ',' not in t) else t.rstrip("""\n"""			).split(""","""			) for t in token]
		for idx, b in enumerate(__snake_case			):
				snake_case_		:				int      							=    b
				snake_case_		:				int      							=    idx
				for wd in b:
						snake_case_		:				Any      							=    idx

		return vocab, raw_vocab, ids_to_tokens, emoji
class     __UpperCamelCase						(    a__				):
							lowercase      :		Union[str, Any] 		=	VOCAB_FILES_NAMES
							lowercase      :		List[str] 		=	PRETRAINED_VOCAB_FILES_MAP
							lowercase      :		str 		=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase      :		str 		=	['input_ids', 'attention_mask']





							def __init__(	self		:Union[str, Any]	,_UpperCamelCase		:List[Any]	,_UpperCamelCase		:Any	,_UpperCamelCase		:Optional[Any]="<|endoftext|>"	,_UpperCamelCase		:List[str]="<|endoftext|>"	,_UpperCamelCase		:Dict="<|startoftext|>"	,_UpperCamelCase		:Tuple="<|endoftext|>"	,_UpperCamelCase		:List[str]=False	,**_UpperCamelCase		:Union[str, Any]	,):
									super().__init__(
									    unk_token=_UpperCAmelCase	,pad_token=_UpperCAmelCase	,bos_token=_UpperCAmelCase	,eos_token=_UpperCAmelCase	,do_clean_text=_UpperCAmelCase	,**_UpperCAmelCase	,)
									if not os.path.isfile(_UpperCAmelCase      ):
											raise ValueError(
											    F'''Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained'''
											    """ model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"""      )
									if not os.path.isfile(_UpperCAmelCase      ):
											raise ValueError(
											    F'''Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google'''
											    """ pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"""      )
									snake_case_		:				Tuple      							=    do_clean_text
									snake_case_		:				Dict      							=    load_vocab_and_emoji(_UpperCAmelCase	,_UpperCAmelCase      )
									snake_case_		:				int      							=    SubWordJapaneseTokenizer(
									    vocab=self.vocab	,ids_to_tokens=self.ids_to_tokens	,emoji=self.emoji      )





							@property
							def 		a__ (	self		:Optional[Any]      ):
									return len(self.raw_vocab      )





							def 		a__ (	self		:Optional[int]      ):
									return dict(self.raw_vocab	,**self.added_tokens_encoder      )





							def 		a__ (	self		:Tuple	,_UpperCamelCase		:List[Any]      ):
									return self.subword_tokenizer.tokenize(_UpperCAmelCase	,clean=self.do_clean_text      )





							def 		a__ (	self		:Union[str, Any]	,_UpperCamelCase		:Any      ):
									return self.vocab.get(_UpperCAmelCase	,self.vocab.get(self.unk_token      )      )





							def 		a__ (	self		:Dict	,_UpperCamelCase		:Tuple      ):
									return self.subword_tokenizer.convert_id_to_token(_UpperCAmelCase      )





							def 		a__ (	self		:str	,_UpperCamelCase		:Tuple      ):
									snake_case_		:				int      							=    ''.join(_UpperCAmelCase      ).strip()
									return out_string





							def 		a__ (	self		:Any	,_UpperCamelCase		:Optional[Any]      ):
									snake_case_		:				int      							=    []
									for is_user, text in conversation.iter_texts():
											input_ids.extend(self.encode(_UpperCAmelCase	,add_special_tokens=_UpperCAmelCase      ) + [self.eos_token_id]      )

									if len(_UpperCAmelCase      ) > self.model_max_length:
											snake_case_		:				Optional[int]      							=    input_ids[-self.model_max_length :]
									return input_ids





							def 		a__ (	self		:str	,_UpperCamelCase		:Optional[int]	,_UpperCamelCase		:Optional[int] = None      ):
									snake_case_		:				List[str]      							=    0
									if os.path.isdir(_UpperCAmelCase      ):
											snake_case_		:				Dict      							=    os.path.join(
											    _UpperCAmelCase	,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]      )
											snake_case_		:				int      							=    os.path.join(
											    _UpperCAmelCase	,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""emoji_file"""]      )
									else:
											snake_case_		:				Tuple      							=    (
											    (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file']
											)
											snake_case_		:				int      							=    (
											    (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file']
											)
									with open(_UpperCAmelCase	,"""w"""	,encoding="""utf-8"""      ) as writer:
											for token_index, token in self.ids_to_tokens.items():
													if index != token_index:
															logger.warning(
															    F'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'''
															    """ Please check that the vocabulary is not corrupted!"""      )
															snake_case_		:				Dict      							=    token_index
													writer.write(""",""".join(_UpperCAmelCase      ) + """\n"""      )
													index += 1
									with open(_UpperCAmelCase	,"""w"""	,encoding="""utf-8"""      ) as writer:
											json.dump(self.emoji	,_UpperCAmelCase      )
									return vocab_file, emoji_file





class     __UpperCamelCase						(    a__				):





							def __init__(	self		:Union[str, Any]	,_UpperCamelCase		:str	,_UpperCamelCase		:List[str]	,_UpperCamelCase		:Union[str, Any]      ):
									snake_case_		:				int      							=    vocab  # same as swe
									snake_case_		:				Union[str, Any]      							=    ids_to_tokens  # same as bpe
									snake_case_		:				Optional[int]      							=    emoji
									snake_case_		:				List[str]      							=    np.max([len(_UpperCAmelCase      ) for w in self.vocab.keys()]      )
									snake_case_		:				Optional[int]      							=    re.compile(R"""(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)"""      )
									snake_case_		:				Optional[Any]      							=    re.compile(R"""[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*"""      )
									snake_case_		:				str      							=    re.compile(R"""[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}"""      )
									snake_case_		:				Any      							=    re.compile(
									    R"""([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*"""      )
									snake_case_		:				List[str]      							=    re.compile(
									    R"""(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*"""      )
									snake_case_		:				Any      							=    re.compile(
									    R"""((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*"""      )
									snake_case_		:				str      							=    '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿'
									snake_case_		:				Dict      							=    '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟'
									snake_case_		:				int      							=    str.maketrans({k: """<BLOCK>""" for k in keisen + blocks}      )





							def __len__(	self		:Tuple      ):
									return len(self.ids_to_tokens      )





							def 		a__ (	self		:List[Any]	,_UpperCamelCase		:Optional[int]      ):
									snake_case_		:				Tuple      							=    self.content_repattera.sub("""<URL>"""	,_UpperCAmelCase      )
									snake_case_		:				Any      							=    self.content_repattera.sub("""<EMAIL>"""	,_UpperCAmelCase      )
									snake_case_		:				Union[str, Any]      							=    self.content_repattera.sub("""<TEL>"""	,_UpperCAmelCase      )
									snake_case_		:				List[str]      							=    self.content_repattera.sub("""<DATE>"""	,_UpperCAmelCase      )
									snake_case_		:				Optional[Any]      							=    self.content_repattera.sub("""<DATE>"""	,_UpperCAmelCase      )
									snake_case_		:				Optional[int]      							=    self.content_repattera.sub("""<PRICE>"""	,_UpperCAmelCase      )
									snake_case_		:				List[str]      							=    content.translate(self.content_transa      )
									while "<BLOCK><BLOCK>" in content:
											snake_case_		:				str      							=    content.replace("""<BLOCK><BLOCK>"""	,"""<BLOCK>"""      )
									return content





							def 		a__ (	self		:Union[str, Any]	,_UpperCamelCase		:Optional[Any]	,_UpperCamelCase		:List[str]=False      ):
									snake_case_		:				Union[str, Any]      							=    text.replace(""" """	,"""<SP>"""      )
									snake_case_		:				Tuple      							=    text.replace("""　"""	,"""<SP>"""      )
									snake_case_		:				Optional[Any]      							=    text.replace("""\r\n"""	,"""<BR>"""      )
									snake_case_		:				Tuple      							=    text.replace("""\n"""	,"""<BR>"""      )
									snake_case_		:				List[Any]      							=    text.replace("""\r"""	,"""<BR>"""      )
									snake_case_		:				Optional[Any]      							=    text.replace("""\t"""	,"""<TAB>"""      )
									snake_case_		:				Union[str, Any]      							=    text.replace("""—"""	,"""ー"""      )
									snake_case_		:				int      							=    text.replace("""−"""	,"""ー"""      )
									for k, v in self.emoji["emoji"].items():
											if k in text:
													snake_case_		:				Union[str, Any]      							=    text.replace(_UpperCAmelCase	,_UpperCAmelCase      )
									if clean:
											snake_case_		:				int      							=    self.clean_text(_UpperCAmelCase      )

									def check_simbol(_UpperCamelCase		:Union[str, Any]      ):
											snake_case_		:				str      							=    x.encode()
											if len(_UpperCAmelCase      ) == 1 and len(_UpperCAmelCase      ) == 2:
													snake_case_		:				Dict      							=    (int(e[0]      ) << 8) + int(e[1]      )
													if (
													    (c >= 0xC_2_A_1 and c <= 0xC_2_B_F)
													    or (c >= 0xC_7_8_0 and c <= 0xC_7_8_3)
													    or (c >= 0xC_A_B_9 and c <= 0xC_B_B_F)
													    or (c >= 0xC_C_8_0 and c <= 0xC_D_A_2)
													):
															return True
											return False

									def checkuae(_UpperCamelCase		:List[str]      ):
											snake_case_		:				Optional[int]      							=    x.encode()
											if len(_UpperCAmelCase      ) == 1 and len(_UpperCAmelCase      ) == 3:
													snake_case_		:				Dict      							=    (int(e[0]      ) << 1_6) + (int(e[1]      ) << 8) + int(e[2]      )
													if c >= 0xE_2_8_0_8_0 and c <= 0xE_2_B_0_7_F:
															return True
											return False

									snake_case_		:				Union[str, Any]      							=    0
									snake_case_		:				int      							=    []
									while pos < len(_UpperCAmelCase      ):
											snake_case_		:				Optional[int]      							=    min(len(_UpperCAmelCase      )	,pos + self.maxlen + 1      ) if text[pos] == '<' else pos + 3
											snake_case_		:				Dict      							=    []  # (token_id, token, pos)
											for e in range(_UpperCAmelCase	,_UpperCAmelCase	,-1      ):
													snake_case_		:				List[str]      							=    text[pos:e]
													if wd in self.vocab:
															if wd[0] == "<" and len(_UpperCAmelCase      ) > 2:
																	snake_case_		:				Union[str, Any]      							=    [(self.vocab[wd], wd, e)]
																	break
															else:
																	candidates.append((self.vocab[wd], wd, e)      )
											if len(_UpperCAmelCase      ) > 0:
													# the smallest token_id is adopted
													snake_case_		:				List[Any]      							=    sorted(_UpperCAmelCase	,key=lambda _UpperCamelCase      : x[0]      )[0]
													result.append(_UpperCAmelCase      )
													snake_case_		:				Optional[int]      							=    e
											else:
													snake_case_		:				int      							=    pos + 1
													snake_case_		:				List[Any]      							=    text[pos:end]
													if check_simbol(_UpperCAmelCase      ):
															result.append("""<KIGOU>"""      )
													elif checkuae(_UpperCAmelCase      ):
															result.append("""<U2000U2BFF>"""      )
													else:
															for i in wd.encode("""utf-8"""      ):
																	result.append("""<|byte%d|>""" % i      )
													snake_case_		:				Optional[Any]      							=    end
									return result





							def 		a__ (	self		:Union[str, Any]	,_UpperCamelCase		:Any	,_UpperCamelCase		:Tuple="\n"      ):
									snake_case_		:				Optional[int]      							=    []
									snake_case_		:				Tuple      							=    []
									snake_case_		:				Any      							=    self.ids_to_tokens[index][0]
									if word[:6] == "<|byte" and word[-2:] == "|>":
											byte_tokens.append(int(word[6:-2]      )      )
									else:
											if len(_UpperCAmelCase      ) > 0:
													words.append(bytearray(_UpperCAmelCase      ).decode("""utf-8"""	,errors="""replace"""      )      )
													snake_case_		:				Tuple      							=    []
											if word[:7] == "<|emoji" and word[-2:] == "|>":
													words.append(self.emoji["""emoji_inv"""][word]      )
											elif word == "<SP>":
													words.append(""" """      )
											elif word == "<BR>":
													words.append(_UpperCAmelCase      )
											elif word == "<TAB>":
													words.append("""\t"""      )
											elif word == "<BLOCK>":
													words.append("""▀"""      )
											elif word == "<KIGOU>":
													words.append("""ǀ"""      )
											elif word == "<U2000U2BFF>":
													words.append("""‖"""      )
											else:
													words.append(_UpperCAmelCase      )
									if len(_UpperCAmelCase      ) > 0:
											words.append(bytearray(_UpperCAmelCase      ).decode("""utf-8"""	,errors="""replace"""      )      )
									snake_case_		:				Optional[int]      							=    ''.join(_UpperCAmelCase      )
									return text






'''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		,      _UpperCAmelCase		,      _UpperCAmelCase=13		,      _UpperCAmelCase=7		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=99		,      _UpperCAmelCase=32		,      _UpperCAmelCase=2		,      _UpperCAmelCase=4		,      _UpperCAmelCase=37		,      _UpperCAmelCase="gelu"		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=512		,      _UpperCAmelCase=16		,      _UpperCAmelCase=2		,      _UpperCAmelCase=0.02		,      _UpperCAmelCase=3		,      _UpperCAmelCase=4		,      _UpperCAmelCase=None		,      ):

          '''simple docstring'''






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


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size)

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

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

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

          __A		:					List[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=_UpperCAmelCase		,      )

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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					int						=			TFConvBertModel(config=_UpperCAmelCase)
          __A		:					Optional[Any]						=			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          __A		:					Tuple						=			[input_ids, input_mask]
          __A		:					Any						=			model(_UpperCAmelCase)

          __A		:					Dict						=			model(_UpperCAmelCase)

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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			TFConvBertForMaskedLM(config=_UpperCAmelCase)
          __A		:					str						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          __A		:					str						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Optional[int]						=			self.num_labels
          __A		:					Any						=			TFConvBertForSequenceClassification(config=_UpperCAmelCase)
          __A		:					Optional[Any]						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          __A		:					Dict						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Tuple						=			self.num_choices
          __A		:					List[str]						=			TFConvBertForMultipleChoice(config=_UpperCAmelCase)
          __A		:					int						=			tf.tile(tf.expand_dims(_UpperCAmelCase		,      1)		,      (1, self.num_choices, 1))
          __A		:					Optional[Any]						=			tf.tile(tf.expand_dims(_UpperCAmelCase		,      1)		,      (1, self.num_choices, 1))
          __A		:					List[Any]						=			tf.tile(tf.expand_dims(_UpperCAmelCase		,      1)		,      (1, self.num_choices, 1))
          __A		:					int						=			{
              'input_ids': multiple_choice_inputs_ids,
              'attention_mask': multiple_choice_input_mask,
              'token_type_ids': multiple_choice_token_type_ids,
          }
          __A		:					Optional[Any]						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_choices))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					List[Any]						=			self.num_labels
          __A		:					List[Any]						=			TFConvBertForTokenClassification(config=_UpperCAmelCase)
          __A		:					str						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          __A		:					int						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Optional[Any]						=			TFConvBertForQuestionAnswering(config=_UpperCAmelCase)
          __A		:					Any						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          __A		:					Union[str, Any]						=			model(_UpperCAmelCase)
          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):

          '''simple docstring'''






          __A		:					Optional[int]						=			self.prepare_config_and_inputs()
          (
              (
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,
          )		:					Union[str, Any]						=			config_and_inputs
          __A		:					List[str]						=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict




@require_tf
class 					SCREAMING_SNAKE_CASE       (a__   ,						a__   ,						unittest.TestCase				):
     lowerCAmelCase						     =			(
         (
             TFConvBertModel,
             TFConvBertForMaskedLM,
             TFConvBertForQuestionAnswering,
             TFConvBertForSequenceClassification,
             TFConvBertForTokenClassification,
             TFConvBertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     lowerCAmelCase						     =			(
         {
             '''feature-extraction''': TFConvBertModel,
             '''fill-mask''': TFConvBertForMaskedLM,
             '''question-answering''': TFConvBertForQuestionAnswering,
             '''text-classification''': TFConvBertForSequenceClassification,
             '''token-classification''': TFConvBertForTokenClassification,
             '''zero-shot''': TFConvBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     lowerCAmelCase						     =			False
     lowerCAmelCase						     =			False
     lowerCAmelCase						     =			False


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			TFConvBertModelTester(self)
          __A		:					str						=			ConfigTester(self		,      config_class=_UpperCAmelCase		,      hidden_size=37)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.config_tester.run_common_tests()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase)


     @slow
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

          if hasattr(_UpperCAmelCase		,      'use_cache'):
               __A		:					List[Any]						=			True

          __A		:					str						=			getattr(self.model_tester		,      'encoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					Union[str, Any]						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)

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

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

                    if self.is_encoder_decoder:
                         __A		:					Optional[int]						=			outputs['encoder_hidden_states']
                         __A		:					str						=			outputs['encoder_attentions']
                    else:
                         __A		:					List[Any]						=			outputs['hidden_states']
                         __A		:					Optional[Any]						=			outputs['attentions']

                    self.assertEqual(len(_UpperCAmelCase)		,      _UpperCAmelCase)

                    __A		:					str						=			getattr(
                        self.model_tester		,      'expected_num_hidden_layers'		,      self.model_tester.num_hidden_layers + 1)

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

                    self.assertEqual(len(_UpperCAmelCase)		,      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):

          '''simple docstring'''






          __A		:					Dict						=			TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          self.assertIsNotNone(_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A		:					Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
          __A		:					Any						=			True
          __A		:					str						=			getattr(self.model_tester		,      'decoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					Any						=			getattr(self.model_tester		,      'encoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					int						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)
          __A		:					Tuple						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)

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

               if self.is_encoder_decoder:
                    __A		:					Union[str, Any]						=			model_class(_UpperCAmelCase)
                    __A		:					int						=			model(self._prepare_for_class(_UpperCAmelCase		,      _UpperCAmelCase))
                    self.assertEqual(config.output_hidden_states		,      _UpperCAmelCase)
                    check_decoder_attentions_output(_UpperCAmelCase)

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

               # Check attention is always last and order is fine
               __A		:					Any						=			True
               __A		:					str						=			True
               __A		:					Union[str, Any]						=			model_class(_UpperCAmelCase)
               __A		:					Union[str, Any]						=			model(self._prepare_for_class(_UpperCAmelCase		,      _UpperCAmelCase))

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




@require_tf
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     @slow
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          __A		:					str						=			tf.constant([[0, 1, 2, 3, 4, 5]])
          __A		:					Optional[int]						=			model(_UpperCAmelCase)[0]

          __A		:					List[Any]						=			[1, 6, 768]
          self.assertEqual(output.shape		,      _UpperCAmelCase)

          __A		:					Tuple						=			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]		,      _UpperCAmelCase		,      atol=1e-4)