






import dataclasses
import re
import string
from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple

import numpy as np

from . import residue_constants


lowerCamelCase : List[str]						   =       Mapping[str, np.ndarray]
lowerCamelCase : Any						   =       Mapping[str, Any]  # Is a nested dict.
lowerCamelCase : List[Any]						   =       0.01

@dataclasses.dataclass(frozen=lowercase__			)
class 		lowerCAmelCase     :

					'''simple docstring'''

					_A						:			int									=      42  # [num_res, num_atom_type, 3]

					# Amino-acid type for each residue represented as an integer between 0 and
					# 20, where 20 is 'X'.
					_A						:			Dict									=      42  # [num_res]

					# Binary float mask to indicate presence of a particular atom. 1.0 if an atom
					# is present and 0.0 if not. This should be used for loss masking.
					_A						:			int									=      42  # [num_res, num_atom_type]

					# Residue index as used in PDB. It is not necessarily continuous or 0-indexed.
					_A						:			str									=      42  # [num_res]

					# B-factors, or temperature factors, of each residue (in sq. angstroms units),
					# representing the displacement of the residue from its ground truth mean
					# value.
					_A						:			Optional[Any]									=      42  # [num_res, num_atom_type]

					# Chain indices for multi-chain predictions
					_A						:			List[Any]									=      None

					# Optional remark about the protein. Included as a comment in output PDB
					# files
					_A						:			List[str]									=      None

					# Templates used to generate this protein (prediction-only)
					_A						:			Optional[int]									=      None

					# Chain corresponding to each parent
					_A						:			Dict									=      None







def       snake_case_		(     lowerCAmelCase_       :						str      ):
							__lowercase      :							List[str]				=     r"(\[[A-Z]+\]\n)"
							__lowercase      :							List[str]				=     [tag.strip() for tag in re.split(_lowerCamelCase       ,       _lowerCamelCase      ) if len(_lowerCamelCase      ) > 0]
							__lowercase      :							Iterator[Tuple[str, List[str]]]				=     zip(tags[0::2]       ,       [l.split("""\n"""      ) for l in tags[1::2]]      )

							__lowercase      :							List[str]				=     ["N", "CA", "C"]
							__lowercase      :							Optional[int]				=     None
							__lowercase      :							Union[str, Any]				=     None
							__lowercase      :							List[str]				=     None
							for g in groups:
														if "[PRIMARY]" == g[0]:
																					__lowercase      :							List[str]				=     g[1][0].strip()
																					for i in range(len(_lowerCamelCase      )      ):
																												if seq[i] not in residue_constants.restypes:
																																			__lowercase      :							Dict				=     "X"  # FIXME: strings are immutable
																					__lowercase      :							Tuple				=     np.array(
																					    [residue_constants.restype_order.get(_lowerCamelCase       ,       residue_constants.restype_num      ) for res_symbol in seq]      )
														elif "[TERTIARY]" == g[0]:
																					__lowercase      :							List[List[float]]				=     []
																					for axis in range(3      ):
																												tertiary.append(list(map(_lowerCamelCase       ,       g[1][axis].split()      )      )      )
																					__lowercase      :							List[Any]				=     np.array(_lowerCamelCase      )
																					__lowercase      :							str				=     np.zeros((len(tertiary[0]      ) // 3, residue_constants.atom_type_num, 3)      ).astype(np.floataa      )
																					for i, atom in enumerate(_lowerCamelCase      ):
																												__lowercase      :							Tuple				=     np.transpose(tertiary_np[:, i::3]      )
																					atom_positions *= PICO_TO_ANGSTROM
														elif "[MASK]" == g[0]:
																					__lowercase      :							str				=     np.array(list(map({"""-""": 0, """+""": 1}.get       ,       g[1][0].strip()      )      )      )
																					__lowercase      :							Optional[int]				=     np.zeros(
																					    (
																					        len(_lowerCamelCase      ),
																					        residue_constants.atom_type_num,
																					    )      ).astype(np.floataa      )
																					for i, atom in enumerate(_lowerCamelCase      ):
																												__lowercase      :							int				=     1
																					atom_mask *= mask[..., None]

							assert aatype is not None

							return Protein(
							    atom_positions=_lowerCamelCase       ,       atom_mask=_lowerCamelCase       ,       aatype=_lowerCamelCase       ,       residue_index=np.arange(len(_lowerCamelCase      )      )       ,       b_factors=_lowerCamelCase       ,       )
def       snake_case_		(     lowerCAmelCase_       :						Protein       ,       lowerCAmelCase_       :						int = 0      ):
							__lowercase      :							List[str]				=     []

							__lowercase      :							int				=     prot.remark
							if remark is not None:
														pdb_headers.append(F"REMARK {remark}"      )

							__lowercase      :							str				=     prot.parents
							__lowercase      :							int				=     prot.parents_chain_index
							if parents is not None and parents_chain_index is not None:
														__lowercase      :							Union[str, Any]				=     [p for i, p in zip(_lowerCamelCase       ,       _lowerCamelCase      ) if i == chain_id]

							if parents is None or len(_lowerCamelCase      ) == 0:
														__lowercase      :							List[str]				=     ["N/A"]

							pdb_headers.append(F"PARENT {' '.join(_lowerCamelCase      )}"      )

							return pdb_headers
def       snake_case_		(     lowerCAmelCase_       :						Protein       ,       lowerCAmelCase_       :						str      ):
							__lowercase      :							List[str]				=     []
							__lowercase      :							List[str]				=     pdb_str.split("""\n"""      )

							__lowercase      :							Optional[Any]				=     prot.remark
							if remark is not None:
														out_pdb_lines.append(F"REMARK {remark}"      )

							__lowercase      :							List[List[str]]
							if prot.parents is not None and len(prot.parents      ) > 0:
														__lowercase      :							Any				=     []
														if prot.parents_chain_index is not None:
																					__lowercase      :							Dict[str, List[str]]				=     {}
																					for p, i in zip(prot.parents       ,       prot.parents_chain_index      ):
																												parent_dict.setdefault(str(_lowerCamelCase      )       ,       []      )
																												parent_dict[str(_lowerCamelCase      )].append(_lowerCamelCase      )

																					__lowercase      :							List[str]				=     max([int(_lowerCamelCase      ) for chain_idx in parent_dict]      )
																					for i in range(max_idx + 1      ):
																												__lowercase      :							Tuple				=     parent_dict.get(str(_lowerCamelCase      )       ,       ["""N/A"""]      )
																												parents_per_chain.append(_lowerCamelCase      )
														else:
																					parents_per_chain.append(list(prot.parents      )      )
							else:
														__lowercase      :							List[Any]				=     [["N/A"]]

							def make_parent_line(lowerCAmelCase_       :						Sequence[str]      ) -> str:
														return F"PARENT {' '.join(_lowerCamelCase      )}"

							out_pdb_lines.append(make_parent_line(parents_per_chain[0]      )      )

							__lowercase      :							List[Any]				=     0
							for i, l in enumerate(_lowerCamelCase      ):
														if "PARENT" not in l and "REMARK" not in l:
																					out_pdb_lines.append(_lowerCamelCase      )
														if "TER" in l and "END" not in lines[i + 1]:
																					chain_counter += 1
																					if not chain_counter >= len(_lowerCamelCase      ):
																												__lowercase      :							Union[str, Any]				=     parents_per_chain[chain_counter]
																					else:
																												__lowercase      :							Dict				=     ["N/A"]

																					out_pdb_lines.append(make_parent_line(_lowerCamelCase      )      )

							return "\n".join(_lowerCamelCase      )
def       snake_case_		(     lowerCAmelCase_       :						Protein      ):
							__lowercase      :							Union[str, Any]				=     residue_constants.restypes + ["X"]

							def res_atoa(lowerCAmelCase_       :						int      ) -> str:
														return residue_constants.restype_atoa.get(restypes[r]       ,       """UNK"""      )

							__lowercase      :							str				=     residue_constants.atom_types

							__lowercase      :							List[str]				=     []

							__lowercase      :							Dict				=     prot.atom_mask
							__lowercase      :							Tuple				=     prot.aatype
							__lowercase      :							str				=     prot.atom_positions
							__lowercase      :							str				=     prot.residue_index.astype(np.intaa      )
							__lowercase      :							Tuple				=     prot.b_factors
							__lowercase      :							Dict				=     prot.chain_index

							if np.any(aatype > residue_constants.restype_num      ):
														raise ValueError("""Invalid aatypes."""      )

							__lowercase      :							int				=     get_pdb_headers(_lowerCamelCase      )
							if len(_lowerCamelCase      ) > 0:
														pdb_lines.extend(_lowerCamelCase      )

							__lowercase      :							str				=     aatype.shape[0]
							__lowercase      :							Union[str, Any]				=     1
							__lowercase      :							Union[str, Any]				=     0
							__lowercase      :							Union[str, Any]				=     string.ascii_uppercase
							__lowercase      :							Dict				=     None
							# Add all atom sites.
							for i in range(_lowerCamelCase      ):
														__lowercase      :							List[str]				=     res_atoa(aatype[i]      )
														for atom_name, pos, mask, b_factor in zip(_lowerCamelCase       ,       atom_positions[i]       ,       atom_mask[i]       ,       b_factors[i]      ):
																					if mask < 0.5:
																												continue

																					__lowercase      :							Optional[Any]				=     "ATOM"
																					__lowercase      :							List[Any]				=     atom_name if len(_lowerCamelCase      ) == 4 else F" {atom_name}"
																					__lowercase      :							Tuple				=     ""
																					__lowercase      :							Tuple				=     ""
																					__lowercase      :							Optional[Any]				=     1.00
																					__lowercase      :							Optional[Any]				=     atom_name[0]  # Protein supports only C, N, O, S, this works.
																					__lowercase      :							List[str]				=     ""

																					__lowercase      :							Any				=     "A"
																					if chain_index is not None:
																												__lowercase      :							int				=     chain_tags[chain_index[i]]

																					# PDB is a columnar format, every space matters here!
																					__lowercase      :							Optional[int]				=     (
																					    F"{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}"
																					    F"{res_name_a:>3} {chain_tag:>1}"
																					    F"{residue_index[i]:>4}{insertion_code:>1}   "
																					    F"{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}"
																					    F"{occupancy:>6.2f}{b_factor:>6.2f}          "
																					    F"{element:>2}{charge:>2}"
																					)
																					pdb_lines.append(_lowerCamelCase      )
																					atom_index += 1

														__lowercase      :							Optional[int]				=     i == n - 1
														if chain_index is not None:
																					if i != n - 1 and chain_index[i + 1] != prev_chain_index:
																												__lowercase      :							Optional[Any]				=     True
																												__lowercase      :							Any				=     chain_index[i + 1]

														if should_terminate:
																					# Close the chain.
																					__lowercase      :							Tuple				=     "TER"
																					__lowercase      :							List[Any]				=     (
																					    F"{chain_end:<6}{atom_index:>5}      {res_atoa(aatype[i]      ):>3} {chain_tag:>1}{residue_index[i]:>4}"
																					)
																					pdb_lines.append(_lowerCamelCase      )
																					atom_index += 1

																					if i != n - 1:
																												# "prev" is a misnomer here. This happens at the beginning of
																												# each new chain.
																												pdb_lines.extend(get_pdb_headers(_lowerCamelCase       ,       _lowerCamelCase      )      )

							pdb_lines.append("""END"""      )
							pdb_lines.append(""""""      )
							return "\n".join(_lowerCamelCase      )
def       snake_case_		(     lowerCAmelCase_       :						Protein      ):
							return residue_constants.STANDARD_ATOM_MASK[prot.aatype]



def       snake_case_		(     lowerCAmelCase_       :						FeatureDict       ,       lowerCAmelCase_       :						ModelOutput       ,       lowerCAmelCase_       :						Optional[np.ndarray] = None       ,       lowerCAmelCase_       :						Optional[np.ndarray] = None       ,       lowerCAmelCase_       :						Optional[str] = None       ,       lowerCAmelCase_       :						Optional[Sequence[str]] = None       ,       lowerCAmelCase_       :						Optional[Sequence[int]] = None       ,       ):
							return Protein(
							    aatype=features["""aatype"""]       ,       atom_positions=result["""final_atom_positions"""]       ,       atom_mask=result["""final_atom_mask"""]       ,       residue_index=features["""residue_index"""] + 1       ,       b_factors=b_factors if b_factors is not None else np.zeros_like(result["""final_atom_mask"""]      )       ,       chain_index=_lowerCamelCase       ,       remark=_lowerCamelCase       ,       parents=_lowerCamelCase       ,       parents_chain_index=_lowerCamelCase       ,       )







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							)