






import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


UpperCAmelCase__			 =	1000




class        snake_case_					:

						"""simple docstring"""



						def __init__(self:				Any   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				int=1.2   ,				__UpperCAmelCase:				int="g"   ,				__UpperCAmelCase:				Union[str, Any]=0.5   ,				__UpperCAmelCase:				Any="-"   ,				__UpperCAmelCase:				List[Any]="test_out.jpg"   ,				__UpperCAmelCase:				Any=True   ,				__UpperCAmelCase:				Tuple=False   ,				__UpperCAmelCase:				Tuple=None   ,				__UpperCAmelCase:				Dict=None   ,				__UpperCAmelCase:				Tuple=0.7   ,				)						->    Dict:




											'''simple docstring'''



											if isinstance(__UpperCAmelCase   ,				torch.Tensor      ):
																__a				:	Any			=				img.numpy().astype("np.uint8"      )
											if isinstance(__UpperCAmelCase   ,				__UpperCAmelCase      ):
																__a				:	int			=				img_tensorize(__UpperCAmelCase      )
											assert isinstance(__UpperCAmelCase   ,				np.ndarray      )

											__a			, __a				:	Optional[int]			=				img.shape[1], img.shape[0]
											__a				:	Optional[int]			=				mplfigure.Figure(frameon=__UpperCAmelCase      )
											__a				:	Optional[int]			=				fig.get_dpi()
											__a				:	Any			=				(width * scale + 1E-2) / dpi
											__a				:	List[str]			=				(height * scale + 1E-2) / dpi
											fig.set_size_inches(__UpperCAmelCase   ,				__UpperCAmelCase      )
											__a				:	Any			=				fig.add_axes([0.0, 0.0, 1.0, 1.0]      )
											ax.axis("off"      )
											ax.set_xlim(0.0   ,				__UpperCAmelCase      )
											ax.set_ylim(__UpperCAmelCase      )

											__a				:	str			=				saveas
											__a				:	Union[str, Any]			=				rgb
											__a				:	List[Any]			=				pynb
											__a				:	List[str]			=				img
											__a				:	List[Any]			=				edgecolor
											__a				:	Tuple			=				0.5
											__a				:	Optional[Any]			=				linestyle
											__a				:	Dict			=				int(np.sqrt(min(__UpperCAmelCase   ,				__UpperCAmelCase      )      ) * scale // 3      )
											__a				:	int			=				width
											__a				:	Optional[Any]			=				height
											__a				:	str			=				scale
											__a				:	Any			=				fig
											__a				:	Union[str, Any]			=				ax
											__a				:	Any			=				pad
											__a				:	Optional[int]			=				idaobj
											__a				:	List[Any]			=				idaattr
											__a				:	str			=				FigureCanvasAgg(__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				int   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Any=None      )						->    Optional[int]:




											'''simple docstring'''



											if color is None:
																__a				:	int			=				self.edgecolor
											((__a)			, (__a)			, (__a)			, (__a))				:	Optional[Any]			=				box
											__a				:	Optional[Any]			=				xa - xa
											__a				:	Optional[int]			=				ya - ya
											self.ax.add_patch(
											    mpl.patches.Rectangle(
											        (xa, ya)   ,				__UpperCAmelCase   ,				__UpperCAmelCase   ,				fill=__UpperCAmelCase   ,				edgecolor=__UpperCAmelCase   ,				linewidth=self.font_size // 3   ,				alpha=self.alpha   ,				linestyle=self.linestyle   ,				)      )



						def 					UpperCAmelCase__				(self:				List[Any]   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				str=None   ,				__UpperCAmelCase:				str=None   ,				__UpperCAmelCase:				List[Any]=None   ,				__UpperCAmelCase:				List[str]=None      )						->    Union[str, Any]:




											'''simple docstring'''



											if len(boxes.shape      ) > 2:
																__a				:	List[str]			=				boxes[0]
											if len(obj_ids.shape      ) > 1:
																__a				:	int			=				obj_ids[0]
											if len(obj_scores.shape      ) > 1:
																__a				:	List[str]			=				obj_scores[0]
											if len(attr_ids.shape      ) > 1:
																__a				:	List[str]			=				attr_ids[0]
											if len(attr_scores.shape      ) > 1:
																__a				:	int			=				attr_scores[0]
											if isinstance(__UpperCAmelCase   ,				torch.Tensor      ):
																__a				:	str			=				boxes.numpy()
											if isinstance(__UpperCAmelCase   ,				__UpperCAmelCase      ):
																__a				:	Any			=				np.array(__UpperCAmelCase      )
											assert isinstance(__UpperCAmelCase   ,				np.ndarray      )
											__a				:	Tuple			=				np.prod(boxes[:, 2:] - boxes[:, :2]   ,				axis=1      )
											__a				:	Tuple			=				np.argsort(-areas      ).tolist()
											__a				:	Dict			=				boxes[sorted_idxs] if boxes is not None else None
											__a				:	str			=				obj_ids[sorted_idxs] if obj_ids is not None else None
											__a				:	List[Any]			=				obj_scores[sorted_idxs] if obj_scores is not None else None
											__a				:	int			=				attr_ids[sorted_idxs] if attr_ids is not None else None
											__a				:	Any			=				attr_scores[sorted_idxs] if attr_scores is not None else None

											__a				:	Any			=				[self._random_color(maximum=1      ) for _ in range(len(__UpperCAmelCase      )      )]
											__a				:	str			=				[assigned_colors[idx] for idx in sorted_idxs]
											if obj_ids is not None:
																__a				:	Tuple			=				self._create_text_labels_attr(__UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase      )
																for i in range(len(__UpperCAmelCase      )      ):
																					__a				:	int			=				assigned_colors[i]
																					self.add_box(boxes[i]   ,				__UpperCAmelCase      )
																					self.draw_labels(labels[i]   ,				boxes[i]   ,				__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Any   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Any      )						->    str:




											'''simple docstring'''



											__a			, __a			, __a			, __a				:	Tuple			=				box
											__a				:	List[str]			=				(xa, ya)
											__a				:	str			=				(ya - ya) * (xa - xa)
											__a				:	Optional[Any]			=				_SMALL_OBJ * self.scale
											if instance_area < small or ya - ya < 40 * self.scale:
																if ya >= self.height - 5:
																					__a				:	Any			=				(xa, ya)
																else:
																					__a				:	Union[str, Any]			=				(xa, ya)

											__a				:	Optional[Any]			=				(ya - ya) / np.sqrt(self.height * self.width      )
											__a				:	Optional[Any]			=				self._change_color_brightness(__UpperCAmelCase   ,				brightness_factor=0.7      )
											__a				:	Optional[int]			=				np.clip((height_ratio - 0.02) / 0.08 + 1   ,				1.2   ,				2      )
											font_size *= 0.75 * self.font_size

											self.draw_text(
											    text=__UpperCAmelCase   ,				position=__UpperCAmelCase   ,				color=__UpperCAmelCase   ,				)



						def 					UpperCAmelCase__				(self:				List[str]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				List[Any]="g"   ,				__UpperCAmelCase:				Optional[Any]="left"   ,				)						->    Tuple:




											'''simple docstring'''



											__a				:	Dict			=				0
											__a				:	Any			=				self.font_size
											__a				:	Tuple			=				np.maximum(list(mplc.to_rgb(__UpperCAmelCase      )      )   ,				0.2      )
											__a				:	List[str]			=				max(0.8   ,				np.max(__UpperCAmelCase      )      )
											__a				:	List[str]			=				{
											    "facecolor": "black",
											    "alpha": self.alpha,
											    "pad": self.pad,
											    "edgecolor": "none",
											}
											__a			, __a				:	List[str]			=				position
											self.ax.text(
											    __UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase   ,				size=font_size * self.scale   ,				family="sans-serif"   ,				bbox=__UpperCAmelCase   ,				verticalalignment="top"   ,				horizontalalignment=__UpperCAmelCase   ,				color=__UpperCAmelCase   ,				zorder=10   ,				rotation=__UpperCAmelCase   ,				)



						def 					UpperCAmelCase__				(self:				Tuple   ,				__UpperCAmelCase:				Tuple=None      )						->    Optional[int]:




											'''simple docstring'''



											if saveas is None:
																__a				:	List[Any]			=				self.saveas
											if saveas.lower().endswith(".jpg"      ) or saveas.lower().endswith(".png"      ):
																cva.imwrite(
																    __UpperCAmelCase   ,				self._get_buffer()[:, :, ::-1]   ,				)
											else:
																self.fig.savefig(__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				List[Any]   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				str      )						->    Tuple:




											'''simple docstring'''



											__a				:	Optional[Any]			=				[self.idaobj[i] for i in classes]
											__a				:	Any			=				[self.idaattr[i] for i in attr_classes]
											__a				:	Optional[int]			=				[
											    f'{label} {score:.2f} {attr} {attr_score:.2f}'
											    for label, score, attr, attr_score in zip(__UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase      )
											]
											return labels



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




											'''simple docstring'''



											__a				:	Tuple			=				[self.idaobj[i] for i in classes]
											if scores is not None:
																if labels is None:
																					__a				:	Any			=				["{:.0f}%".format(s * 100      ) for s in scores]
																else:
																					__a				:	str			=				["{} {:.0f}%".format(__UpperCAmelCase   ,				s * 100      ) for li, s in zip(__UpperCAmelCase   ,				__UpperCAmelCase      )]
											return labels



						def 					UpperCAmelCase__				(self:				List[Any]   ,				__UpperCAmelCase:				List[str]=255      )						->    Tuple:




											'''simple docstring'''



											__a				:	Optional[int]			=				np.random.randint(0   ,				len(_COLORS      )      )
											__a				:	Any			=				_COLORS[idx] * maximum
											if not self.rgb:
																__a				:	List[Any]			=				ret[::-1]
											return ret



						def 					UpperCAmelCase__				(self:				List[str]      )						->    Tuple:




											'''simple docstring'''



											if not self.pynb:
																__a			, __a				:	List[Any]			=				self.canvas.print_to_buffer()
																if (width, height) != (self.width, self.height):
																					__a				:	Union[str, Any]			=				cva.resize(self.img   ,				(width, height)      )
																else:
																					__a				:	Dict			=				self.img
											else:
																__a				:	Optional[Any]			=				io.BytesIO()  # works for cairo backend
																self.canvas.print_rgba(__UpperCAmelCase      )
																__a			, __a				:	Tuple			=				self.width, self.height
																__a				:	Tuple			=				buf.getvalue()
																__a				:	List[Any]			=				self.img

											__a				:	Any			=				np.frombuffer(__UpperCAmelCase   ,				dtype="uint8"      )
											__a				:	List[str]			=				buffer.reshape(__UpperCAmelCase   ,				__UpperCAmelCase   ,				4      )
											__a			, __a				:	int			=				np.split(__UpperCAmelCase   ,				[3]   ,				axis=2      )

											try:
																import numexpr as ne  # fuse them with numexpr

																__a				:	int			=				ne.evaluate("img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"      )
											except ImportError:
																__a				:	Any			=				alpha.astype("float32"      ) / 2_55.0
																__a				:	Tuple			=				img * (1 - alpha) + rgb * alpha

											return visualized_image.astype("uint8"      )



						def 					UpperCAmelCase__				(self:				int   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Optional[int]      )						->    Dict:




											'''simple docstring'''



											assert brightness_factor >= -1.0 and brightness_factor <= 1.0
											__a				:	Any			=				mplc.to_rgb(__UpperCAmelCase      )
											__a				:	Union[str, Any]			=				colorsys.rgb_to_hls(*mplc.to_rgb(__UpperCAmelCase      )      )
											__a				:	Union[str, Any]			=				polygon_color[1] + (brightness_factor * polygon_color[1])
											__a				:	Dict			=				0.0 if modified_lightness < 0.0 else modified_lightness
											__a				:	Dict			=				1.0 if modified_lightness > 1.0 else modified_lightness
											__a				:	List[str]			=				colorsys.hls_to_rgb(polygon_color[0]   ,				__UpperCAmelCase   ,				polygon_color[2]      )
											return modified_color


UpperCAmelCase__			 =	(
    np.array(
        [
            0.000,
            0.447,
            0.741,
            0.850,
            0.325,
            0.098,
            0.929,
            0.694,
            0.125,
            0.494,
            0.184,
            0.556,
            0.466,
            0.674,
            0.188,
            0.301,
            0.745,
            0.933,
            0.635,
            0.078,
            0.184,
            0.300,
            0.300,
            0.300,
            0.600,
            0.600,
            0.600,
            1.000,
            0.000,
            0.000,
            1.000,
            0.500,
            0.000,
            0.749,
            0.749,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.333,
            0.333,
            0.000,
            0.333,
            0.667,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            0.333,
            0.000,
            0.667,
            0.667,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            1.000,
            0.000,
            0.000,
            0.333,
            0.500,
            0.000,
            0.667,
            0.500,
            0.000,
            1.000,
            0.500,
            0.333,
            0.000,
            0.500,
            0.333,
            0.333,
            0.500,
            0.333,
            0.667,
            0.500,
            0.333,
            1.000,
            0.500,
            0.667,
            0.000,
            0.500,
            0.667,
            0.333,
            0.500,
            0.667,
            0.667,
            0.500,
            0.667,
            1.000,
            0.500,
            1.000,
            0.000,
            0.500,
            1.000,
            0.333,
            0.500,
            1.000,
            0.667,
            0.500,
            1.000,
            1.000,
            0.500,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.333,
            0.333,
            1.000,
            0.333,
            0.667,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.667,
            0.333,
            1.000,
            0.667,
            0.667,
            1.000,
            0.667,
            1.000,
            1.000,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            1.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.143,
            0.143,
            0.143,
            0.857,
            0.857,
            0.857,
            1.000,
            1.000,
            1.000,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)













from __future__ import annotations

import unittest

from transformers import 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 (
			    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TFXLMForMultipleChoice,
			    TFXLMForQuestionAnsweringSimple,
			    TFXLMForSequenceClassification,
			    TFXLMForTokenClassification,
			    TFXLMModel,
			    TFXLMWithLMHeadModel,
			    XLMConfig,
			)




class        snake_case_					:

						"""simple docstring"""



						def __init__(self:				List[Any]   ,				__UpperCAmelCase:				str   ,				)						->    int:




											'''simple docstring'''



											__a				:	str			=				parent
											__a				:	Dict			=				13
											__a				:	Tuple			=				7
											__a				:	Union[str, Any]			=				True
											__a				:	Dict			=				True
											__a				:	Optional[Any]			=				True
											__a				:	Tuple			=				True
											__a				:	int			=				True
											__a				:	Tuple			=				False
											__a				:	Optional[int]			=				False
											__a				:	str			=				False
											__a				:	Optional[Any]			=				2
											__a				:	Optional[Any]			=				99
											__a				:	List[str]			=				0
											__a				:	int			=				32
											__a				:	List[Any]			=				2
											__a				:	Union[str, Any]			=				4
											__a				:	List[str]			=				0.1
											__a				:	Tuple			=				0.1
											__a				:	str			=				512
											__a				:	Any			=				16
											__a				:	Optional[Any]			=				2
											__a				:	str			=				0.02
											__a				:	Union[str, Any]			=				3
											__a				:	Union[str, Any]			=				4
											__a				:	Optional[int]			=				"last"
											__a				:	List[str]			=				True
											__a				:	List[Any]			=				None
											__a				:	Any			=				0



						def 					UpperCAmelCase__				(self:				int      )						->    str:




											'''simple docstring'''



											__a				:	Optional[Any]			=				ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size      )
											__a				:	Union[str, Any]			=				random_attention_mask([self.batch_size, self.seq_length]   ,				dtype=tf.floataa      )

											__a				:	Dict			=				None
											if self.use_input_lengths:
																__a				:	List[Any]			=				(
																    ids_tensor([self.batch_size]   ,				vocab_size=2      ) + self.seq_length - 2
																)  # small variation of seq_length

											__a				:	int			=				None
											if self.use_token_type_ids:
																__a				:	Tuple			=				ids_tensor([self.batch_size, self.seq_length]   ,				self.n_langs      )

											__a				:	Union[str, Any]			=				None
											__a				:	str			=				None
											__a				:	str			=				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				:	int			=				ids_tensor([self.batch_size]   ,				2   ,				dtype=tf.floataa      )
																__a				:	Tuple			=				ids_tensor([self.batch_size]   ,				self.num_choices      )

											__a				:	str			=				XLMConfig(
											    vocab_size=self.vocab_size   ,				n_special=self.n_special   ,				emb_dim=self.hidden_size   ,				n_layers=self.num_hidden_layers   ,				n_heads=self.num_attention_heads   ,				dropout=self.hidden_dropout_prob   ,				attention_dropout=self.attention_probs_dropout_prob   ,				gelu_activation=self.gelu_activation   ,				sinusoidal_embeddings=self.sinusoidal_embeddings   ,				asm=self.asm   ,				causal=self.causal   ,				n_langs=self.n_langs   ,				max_position_embeddings=self.max_position_embeddings   ,				initializer_range=self.initializer_range   ,				summary_type=self.summary_type   ,				use_proj=self.use_proj   ,				bos_token_id=self.bos_token_id   ,				)

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_lengths,
											    sequence_labels,
											    token_labels,
											    is_impossible_labels,
											    choice_labels,
											    input_mask,
											)



						def 					UpperCAmelCase__				(self:				int   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				)						->    List[Any]:




											'''simple docstring'''



											__a				:	Union[str, Any]			=				TFXLMModel(config=__UpperCAmelCase      )
											__a				:	Tuple			=				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
											__a				:	List[str]			=				model(__UpperCAmelCase      )

											__a				:	List[Any]			=				[input_ids, input_mask]
											__a				:	str			=				model(__UpperCAmelCase      )
											self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)      )



						def 					UpperCAmelCase__				(self:				Tuple   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				List[str]   ,				)						->    Union[str, Any]:




											'''simple docstring'''



											__a				:	int			=				TFXLMWithLMHeadModel(__UpperCAmelCase      )

											__a				:	List[Any]			=				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
											__a				:	int			=				model(__UpperCAmelCase      )

											__a				:	int			=				outputs

											self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)      )



						def 					UpperCAmelCase__				(self:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				List[Any]   ,				)						->    List[str]:




											'''simple docstring'''



											__a				:	Tuple			=				TFXLMForQuestionAnsweringSimple(__UpperCAmelCase      )

											__a				:	Union[str, Any]			=				{"input_ids": input_ids, "lengths": input_lengths}

											__a				:	int			=				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 					UpperCAmelCase__				(self:				List[Any]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Dict   ,				)						->    str:




											'''simple docstring'''



											__a				:	Dict			=				TFXLMForSequenceClassification(__UpperCAmelCase      )

											__a				:	Union[str, Any]			=				{"input_ids": input_ids, "lengths": input_lengths}

											__a				:	Any			=				model(__UpperCAmelCase      )

											self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)      )



						def 					UpperCAmelCase__				(self:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				int   ,				)						->    int:




											'''simple docstring'''



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



						def 					UpperCAmelCase__				(self:				Optional[int]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				)						->    List[str]:




											'''simple docstring'''



											__a				:	List[str]			=				self.num_choices
											__a				:	str			=				TFXLMForMultipleChoice(config=__UpperCAmelCase      )
											__a				:	List[str]			=				tf.tile(tf.expand_dims(__UpperCAmelCase   ,				1      )   ,				(1, self.num_choices, 1)      )
											__a				:	Tuple			=				tf.tile(tf.expand_dims(__UpperCAmelCase   ,				1      )   ,				(1, self.num_choices, 1)      )
											__a				:	int			=				tf.tile(tf.expand_dims(__UpperCAmelCase   ,				1      )   ,				(1, self.num_choices, 1)      )
											__a				:	Dict			=				{
											    "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 					UpperCAmelCase__				(self:				Tuple      )						->    Optional[Any]:




											'''simple docstring'''



											__a				:	Tuple			=				self.prepare_config_and_inputs()
											(
											    (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, 
											)				:	List[Any]			=				config_and_inputs
											__a				:	Optional[Any]			=				{
											    "input_ids": input_ids,
											    "token_type_ids": token_type_ids,
											    "langs": token_type_ids,
											    "lengths": input_lengths,
											}
											return config, inputs_dict




@require_tf
class        snake_case_					(						__UpperCamelCase		,       __UpperCamelCase		,       unittest.TestCase			):

						"""simple docstring"""



						snake_case__   					=			(
						    (
						        TFXLMModel,
						        TFXLMWithLMHeadModel,
						        TFXLMForSequenceClassification,
						        TFXLMForQuestionAnsweringSimple,
						        TFXLMForTokenClassification,
						        TFXLMForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						snake_case__   					=			(
						    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Check other models whether language generation is also applicable
						snake_case__   					=			(
						    {
						        """feature-extraction""": TFXLMModel,
						        """fill-mask""": TFXLMWithLMHeadModel,
						        """question-answering""": TFXLMForQuestionAnsweringSimple,
						        """text-classification""": TFXLMForSequenceClassification,
						        """text-generation""": TFXLMWithLMHeadModel,
						        """token-classification""": TFXLMForTokenClassification,
						        """zero-shot""": TFXLMForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						snake_case__   					=			False
						snake_case__   					=			False



						def 					UpperCAmelCase__				(self:				Union[str, Any]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Tuple      )						->    Tuple:




											'''simple docstring'''



											if (
											    pipeline_test_casse_name == "QAPipelineTests"
											    and tokenizer_name is not None
											    and not tokenizer_name.endswith("Fast"      )
											):
																# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																return True

											return False



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




											'''simple docstring'''



											__a				:	List[Any]			=				TFXLMModelTester(self      )
											__a				:	int			=				ConfigTester(self   ,				config_class=__UpperCAmelCase   ,				emb_dim=37      )



						def 					UpperCAmelCase__				(self:				Tuple      )						->    Optional[Any]:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						def 					UpperCAmelCase__				(self:				Any      )						->    int:




											'''simple docstring'''



											__a				:	Optional[int]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_model(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Dict      )						->    Dict:




											'''simple docstring'''



											__a				:	str			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_lm_head(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    Optional[int]:




											'''simple docstring'''



											__a				:	List[str]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_qa(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    Any:




											'''simple docstring'''



											__a				:	List[Any]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_sequence_classif(*__UpperCAmelCase      )



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




											'''simple docstring'''



											__a				:	Any			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_token_classification(*__UpperCAmelCase      )



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




											'''simple docstring'''



											__a				:	List[str]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_multiple_choice(*__UpperCAmelCase      )



						@slow
						def 					UpperCAmelCase__				(self:				List[str]      )						->    Optional[int]:




											'''simple docstring'''



											for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a				:	Optional[int]			=				TFXLMModel.from_pretrained(__UpperCAmelCase      )
																self.assertIsNotNone(__UpperCAmelCase      )




@require_tf
class        snake_case_					(						unittest.TestCase			):

						"""simple docstring"""



						@slow
						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    str:




											'''simple docstring'''



											__a				:	Any			=				TFXLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"      )
											__a				:	List[Any]			=				tf.convert_to_tensor([[14, 447]]   ,				dtype=tf.intaa      )  # the president
											__a				:	Any			=				[
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											]  # the president the president the president the president the president the president the president the president the president the president
											# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
											__a				:	Tuple			=				model.generate(__UpperCAmelCase   ,				do_sample=__UpperCAmelCase      )
											self.assertListEqual(output_ids[0].numpy().tolist()   ,				__UpperCAmelCase      )





