
import itertools
import json
import linecache
import os
import pickle
import re
import socket
import string
from collections import Counter
from logging import getLogger
from pathlib import Path
from typing import Callable, Dict, Iterable, List

import git
import torch
from torch.utils.data import Dataset

from transformers import BartTokenizer, RagTokenizer, TaTokenizer
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase=True	,  _UpperCamelCase="pt"		):
	__lowerCAmelCase      :						List[str]						      =				{'add_prefix_space': True} if isinstance(_UpperCamelCase	,  _UpperCamelCase		) and not line.startswith(' '		) else {}
	__lowerCAmelCase      :						Union[str, Any]						      =				padding_side
	return tokenizer(
	    [line]	,  max_length=_UpperCamelCase	,  padding='max_length' if pad_to_max_length else None	,  truncation=_UpperCamelCase	,  return_tensors=_UpperCamelCase	,  add_special_tokens=_UpperCamelCase	,  **_UpperCamelCase	,  )
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase=None	,  ):
	__lowerCAmelCase      :						Optional[int]						      =				input_ids.ne(_UpperCamelCase		).any(dim=0		)
	if attention_mask is None:
		return input_ids[:, keep_column_mask]
	else:
		return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask])




class 			A__	(     _lowerCamelCase):

		def __init__(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE="train"	,		_SCREAMING_SNAKE_CASE=None	,		_SCREAMING_SNAKE_CASE=None	,		_SCREAMING_SNAKE_CASE=None	,		_SCREAMING_SNAKE_CASE=""	,		):
			super().__init__()
			__lowerCAmelCase      :						List[str]						      =				Path(_SCREAMING_SNAKE_CASE			).joinpath(type_path + '.source'			)
			__lowerCAmelCase      :						Any						      =				Path(_SCREAMING_SNAKE_CASE			).joinpath(type_path + '.target'			)
			__lowerCAmelCase      :						Any						      =				self.get_char_lens(self.src_file			)
			__lowerCAmelCase      :						Optional[int]						      =				max_source_length
			__lowerCAmelCase      :						Any						      =				max_target_length
			assert min(self.src_lens			) > 0, f"found empty line in {self.src_file}"
			__lowerCAmelCase      :						Optional[int]						      =				tokenizer
			__lowerCAmelCase      :						Tuple						      =				prefix
			if n_obs is not None:
				__lowerCAmelCase      :						int						      =				self.src_lens[:n_obs]
			__lowerCAmelCase      :						Union[str, Any]						      =				src_lang
			__lowerCAmelCase      :						List[Any]						      =				tgt_lang

		def __len__(				self			):
			return len(self.src_lens			)

		def __getitem__(				self	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						str						      =				index + 1  # linecache starts at 1
			__lowerCAmelCase      :						int						      =				self.prefix + linecache.getline(str(self.src_file			)	,		_SCREAMING_SNAKE_CASE			).rstrip('\n'			)
			__lowerCAmelCase      :						Tuple						      =				linecache.getline(str(self.tgt_file			)	,		_SCREAMING_SNAKE_CASE			).rstrip('\n'			)
			assert source_line, f"empty source line for index {index}"
			assert tgt_line, f"empty tgt line for index {index}"

			# Need to add eos token manually for T5
			if isinstance(self.tokenizer	,		_SCREAMING_SNAKE_CASE			):
				source_line += self.tokenizer.eos_token
				tgt_line += self.tokenizer.eos_token

			# Pad source and target to the right
			__lowerCAmelCase      :						List[Any]						      =				(
			    self.tokenizer.question_encoder if isinstance(self.tokenizer	,		_SCREAMING_SNAKE_CASE			) else self.tokenizer
			)
			__lowerCAmelCase      :						Optional[Any]						      =				self.tokenizer.generator if isinstance(self.tokenizer	,		_SCREAMING_SNAKE_CASE			) else self.tokenizer

			__lowerCAmelCase      :						Any						      =				encode_line(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		self.max_source_length	,		'right'			)
			__lowerCAmelCase      :						int						      =				encode_line(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		self.max_target_length	,		'right'			)

			__lowerCAmelCase      :						Union[str, Any]						      =				source_inputs['input_ids'].squeeze()
			__lowerCAmelCase      :						str						      =				target_inputs['input_ids'].squeeze()
			__lowerCAmelCase      :						Optional[Any]						      =				source_inputs['attention_mask'].squeeze()
			return {
			    "input_ids": source_ids,
			    "attention_mask": src_mask,
			    "decoder_input_ids": target_ids,
			}

		@staticmethod
		def 							__lowerCamelCase							(				_SCREAMING_SNAKE_CASE			):
			return [len(_SCREAMING_SNAKE_CASE			) for x in Path(_SCREAMING_SNAKE_CASE			).open().readlines()]







		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Any						      =				torch.stack([x['input_ids'] for x in batch]			)
			__lowerCAmelCase      :						Any						      =				torch.stack([x['attention_mask'] for x in batch]			)
			__lowerCAmelCase      :						Tuple						      =				torch.stack([x['decoder_input_ids'] for x in batch]			)
			__lowerCAmelCase      :						Any						      =				(
			    self.tokenizer.generator.pad_token_id
			    if isinstance(self.tokenizer	,		_SCREAMING_SNAKE_CASE			)
			    else self.tokenizer.pad_token_id
			)
			__lowerCAmelCase      :						Union[str, Any]						      =				(
			    self.tokenizer.question_encoder.pad_token_id
			    if isinstance(self.tokenizer	,		_SCREAMING_SNAKE_CASE			)
			    else self.tokenizer.pad_token_id
			)
			__lowerCAmelCase      :						Tuple						      =				trim_batch(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						int						      =				trim_batch(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Optional[Any]						      =				{
			    'input_ids': source_ids,
			    'attention_mask': source_mask,
			    'decoder_input_ids': y,
			}
			return batch



lowerCamelCase__        =		getLogger(__name__)
def    __lowerCAmelCase    (_UpperCamelCase		):
	return list(itertools.chain.from_iterable(_UpperCamelCase		)		)
def    __lowerCAmelCase    (_UpperCamelCase		):
	__lowerCAmelCase      :						Optional[Any]						      =				get_git_info()
	save_json(_UpperCamelCase	,  os.path.join(_UpperCamelCase	,  'git_log.json'		)		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase=4	,  **_UpperCamelCase		):
	with open(_UpperCamelCase	,  'w'		) as f:
		json.dump(_UpperCamelCase	,  _UpperCamelCase	,  indent=_UpperCamelCase	,  **_UpperCamelCase		)
def    __lowerCAmelCase    (_UpperCamelCase		):
	with open(_UpperCamelCase		) as f:
		return json.load(_UpperCamelCase		)
def    __lowerCAmelCase    ():
	__lowerCAmelCase      :						Any						      =				git.Repo(search_parent_directories=_UpperCamelCase		)
	__lowerCAmelCase      :						int						      =				{
	    'repo_id': str(_UpperCamelCase		),
	    'repo_sha': str(repo.head.object.hexsha		),
	    'repo_branch': str(repo.active_branch		),
	    'hostname': str(socket.gethostname()		),
	}
	return repo_infos
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	return list(map(_UpperCamelCase	,  _UpperCamelCase		)		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	with open(_UpperCamelCase	,  'wb'		) as f:
		return pickle.dump(_UpperCamelCase	,  _UpperCamelCase		)
def    __lowerCAmelCase    (_UpperCamelCase		):

	def remove_articles(_UpperCamelCase		):
		return re.sub(r'\b(a|an|the)\b'	,  ' '	,  _UpperCamelCase		)

	def white_space_fix(_UpperCamelCase		):
		return " ".join(text.split()		)

	def remove_punc(_UpperCamelCase		):
		__lowerCAmelCase      :						Optional[Any]						      =				set(string.punctuation		)
		return "".join(ch for ch in text if ch not in exclude		)

	def lower(_UpperCamelCase		):
		return text.lower()

	return white_space_fix(remove_articles(remove_punc(lower(_UpperCamelCase		)		)		)		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	__lowerCAmelCase      :						Tuple						      =				normalize_answer(_UpperCamelCase		).split()
	__lowerCAmelCase      :						Tuple						      =				normalize_answer(_UpperCamelCase		).split()
	__lowerCAmelCase      :						Optional[int]						      =				Counter(_UpperCamelCase		) & Counter(_UpperCamelCase		)
	__lowerCAmelCase      :						List[Any]						      =				sum(common.values()		)
	if num_same == 0:
		return 0
	__lowerCAmelCase      :						Optional[Any]						      =				1.0 * num_same / len(_UpperCamelCase		)
	__lowerCAmelCase      :						Union[str, Any]						      =				1.0 * num_same / len(_UpperCamelCase		)
	__lowerCAmelCase      :						Any						      =				(2 * precision * recall) / (precision + recall)
	return fa
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	return normalize_answer(_UpperCamelCase		) == normalize_answer(_UpperCamelCase		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	assert len(_UpperCamelCase		) == len(_UpperCamelCase		)
	__lowerCAmelCase      :						str						      =				0
	for hypo, pred in zip(_UpperCamelCase	,  _UpperCamelCase		):
		em += exact_match_score(_UpperCamelCase	,  _UpperCamelCase		)
	if len(_UpperCamelCase		) > 0:
		em /= len(_UpperCamelCase		)
	return {"em": em}
def    __lowerCAmelCase    (_UpperCamelCase		):
	return model_prefix.startswith('rag'		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase		):
	__lowerCAmelCase      :						Tuple						      =				{p: p for p in extra_params}
	# T5 models don't have `dropout` param, they have `dropout_rate` instead
	__lowerCAmelCase      :						int						      =				'dropout_rate'
	for p in extra_params:
		if getattr(_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase		):
			if not hasattr(_UpperCamelCase	,  _UpperCamelCase		) and not hasattr(_UpperCamelCase	,  equivalent_param[p]		):
				logger.info('config doesn\'t have a `{}` attribute'.format(_UpperCamelCase		)		)
				delattr(_UpperCamelCase	,  _UpperCamelCase		)
				continue
			__lowerCAmelCase      :						List[str]						      =				p if hasattr(_UpperCamelCase	,  _UpperCamelCase		) else equivalent_param[p]
			setattr(_UpperCamelCase	,  _UpperCamelCase	,  getattr(_UpperCamelCase	,  _UpperCamelCase		)		)
			delattr(_UpperCamelCase	,  _UpperCamelCase		)
	return hparams, config

"""simple docstring"""







import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    AlbertForMaskedLM,
							    AlbertForMultipleChoice,
							    AlbertForPreTraining,
							    AlbertForQuestionAnswering,
							    AlbertForSequenceClassification,
							    AlbertForTokenClassification,
							    AlbertModel,
							)
							from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST




class 			A__	:

		def __init__(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE=13	,		_SCREAMING_SNAKE_CASE=7	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=99	,		_SCREAMING_SNAKE_CASE=16	,		_SCREAMING_SNAKE_CASE=36	,		_SCREAMING_SNAKE_CASE=6	,		_SCREAMING_SNAKE_CASE=6	,		_SCREAMING_SNAKE_CASE=6	,		_SCREAMING_SNAKE_CASE=37	,		_SCREAMING_SNAKE_CASE="gelu"	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=5_12	,		_SCREAMING_SNAKE_CASE=16	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=0.02	,		_SCREAMING_SNAKE_CASE=3	,		_SCREAMING_SNAKE_CASE=4	,		_SCREAMING_SNAKE_CASE=None	,		):
			__lowerCAmelCase      :						List[str]						      =				parent
			__lowerCAmelCase      :						Optional[int]						      =				batch_size
			__lowerCAmelCase      :						List[Any]						      =				seq_length
			__lowerCAmelCase      :						List[Any]						      =				is_training
			__lowerCAmelCase      :						Tuple						      =				use_input_mask
			__lowerCAmelCase      :						Tuple						      =				use_token_type_ids
			__lowerCAmelCase      :						Optional[int]						      =				use_labels
			__lowerCAmelCase      :						Tuple						      =				vocab_size
			__lowerCAmelCase      :						List[Any]						      =				embedding_size
			__lowerCAmelCase      :						List[Any]						      =				hidden_size
			__lowerCAmelCase      :						Union[str, Any]						      =				num_hidden_layers
			__lowerCAmelCase      :						int						      =				num_hidden_groups
			__lowerCAmelCase      :						List[Any]						      =				num_attention_heads
			__lowerCAmelCase      :						Dict						      =				intermediate_size
			__lowerCAmelCase      :						List[str]						      =				hidden_act
			__lowerCAmelCase      :						Optional[Any]						      =				hidden_dropout_prob
			__lowerCAmelCase      :						List[Any]						      =				attention_probs_dropout_prob
			__lowerCAmelCase      :						Any						      =				max_position_embeddings
			__lowerCAmelCase      :						str						      =				type_vocab_size
			__lowerCAmelCase      :						List[Any]						      =				type_sequence_label_size
			__lowerCAmelCase      :						List[Any]						      =				initializer_range
			__lowerCAmelCase      :						Tuple						      =				num_labels
			__lowerCAmelCase      :						int						      =				num_choices
			__lowerCAmelCase      :						Dict						      =				scope

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				ids_tensor([self.batch_size, self.seq_length]	,		self.vocab_size			)

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

			__lowerCAmelCase      :						Any						      =				None
			if self.use_token_type_ids:
				__lowerCAmelCase      :						List[str]						      =				ids_tensor([self.batch_size, self.seq_length]	,		self.type_vocab_size			)

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

			__lowerCAmelCase      :						Any						      =				self.get_config()

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

		def 							__lowerCamelCase							(				self			):
			return AlbertConfig(
			    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	,		num_hidden_groups=self.num_hidden_groups	,		)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						List[str]						      =				AlbertModel(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Dict						      =				model(_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Optional[Any]						      =				model(_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						List[Any]						      =				model(_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.seq_length, self.hidden_size)			)
			self.parent.assertEqual(result.pooler_output.shape	,		(self.batch_size, self.hidden_size)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						str						      =				AlbertForPreTraining(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Any						      =				model(
			    _SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE	,		sentence_order_label=_SCREAMING_SNAKE_CASE	,		)
			self.parent.assertEqual(result.prediction_logits.shape	,		(self.batch_size, self.seq_length, self.vocab_size)			)
			self.parent.assertEqual(result.sop_logits.shape	,		(self.batch_size, config.num_labels)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[int]						      =				AlbertForMaskedLM(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Any						      =				model(_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.vocab_size)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[int]						      =				AlbertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						List[str]						      =				model(
			    _SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		start_positions=_SCREAMING_SNAKE_CASE	,		end_positions=_SCREAMING_SNAKE_CASE	,		)
			self.parent.assertEqual(result.start_logits.shape	,		(self.batch_size, self.seq_length)			)
			self.parent.assertEqual(result.end_logits.shape	,		(self.batch_size, self.seq_length)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[int]						      =				self.num_labels
			__lowerCAmelCase      :						str						      =				AlbertForSequenceClassification(_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						str						      =				model(_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_labels)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[Any]						      =				self.num_labels
			__lowerCAmelCase      :						List[str]						      =				AlbertForTokenClassification(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Dict						      =				model(_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.num_labels)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						str						      =				self.num_choices
			__lowerCAmelCase      :						Dict						      =				AlbertForMultipleChoice(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Union[str, Any]						      =				input_ids.unsqueeze(1			).expand(-1	,		self.num_choices	,		-1			).contiguous()
			__lowerCAmelCase      :						Dict						      =				token_type_ids.unsqueeze(1			).expand(-1	,		self.num_choices	,		-1			).contiguous()
			__lowerCAmelCase      :						List[str]						      =				input_mask.unsqueeze(1			).expand(-1	,		self.num_choices	,		-1			).contiguous()
			__lowerCAmelCase      :						Dict						      =				model(
			    _SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE	,		token_type_ids=_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE	,		)
			self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_choices)			)







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[Any]						      =				self.prepare_config_and_inputs()
			(
			    (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       (
			    __lowerCAmelCase
			)      ,       
			)      :						int						      =				config_and_inputs
			__lowerCAmelCase      :						Tuple						      =				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
			return config, inputs_dict




@require_torch
class 			A__	(     _lowerCamelCase	,					_lowerCamelCase	,					unittest.TestCase):
		A_						:	List[str]           =     (
		    (
		        AlbertModel,
		        AlbertForPreTraining,
		        AlbertForMaskedLM,
		        AlbertForMultipleChoice,
		        AlbertForSequenceClassification,
		        AlbertForTokenClassification,
		        AlbertForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		A_						:	str           =     (
		    {
		        'feature-extraction': AlbertModel,
		        'fill-mask': AlbertForMaskedLM,
		        'question-answering': AlbertForQuestionAnswering,
		        'text-classification': AlbertForSequenceClassification,
		        'token-classification': AlbertForTokenClassification,
		        'zero-shot': AlbertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		A_						:	Union[str, Any]           =     True

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE=False			):
			__lowerCAmelCase      :						Optional[Any]						      =				super()._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		return_labels=_SCREAMING_SNAKE_CASE			)

			if return_labels:
				if model_class in get_values(_SCREAMING_SNAKE_CASE			):
					__lowerCAmelCase      :						List[Any]						      =				torch.zeros(
					    (self.model_tester.batch_size, self.model_tester.seq_length)	,		dtype=torch.long	,		device=_SCREAMING_SNAKE_CASE			)
					__lowerCAmelCase      :						List[Any]						      =				torch.zeros(
					    self.model_tester.batch_size	,		dtype=torch.long	,		device=_SCREAMING_SNAKE_CASE			)
			return inputs_dict

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[str]						      =				AlbertModelTester(self			)
			__lowerCAmelCase      :						List[str]						      =				ConfigTester(self	,		config_class=_SCREAMING_SNAKE_CASE	,		hidden_size=37			)

		def 							__lowerCamelCase							(				self			):
			self.config_tester.run_common_tests()

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Any						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Tuple						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Tuple						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[Any]						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[Any]						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[Any]						      =				self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
				__lowerCAmelCase      :						Optional[Any]						      =				type
				self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE			)







		@slow
		def 							__lowerCamelCase							(				self			):
			for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__lowerCAmelCase      :						Optional[int]						      =				AlbertModel.from_pretrained(_SCREAMING_SNAKE_CASE			)
				self.assertIsNotNone(_SCREAMING_SNAKE_CASE			)




@require_torch
class 			A__	(     unittest.TestCase):

		@slow
		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						str						      =				AlbertModel.from_pretrained('albert-base-v2'			)
			__lowerCAmelCase      :						List[str]						      =				torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]			)
			__lowerCAmelCase      :						Optional[Any]						      =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
			with torch.no_grad():
				__lowerCAmelCase      :						Dict						      =				model(_SCREAMING_SNAKE_CASE	,		attention_mask=_SCREAMING_SNAKE_CASE			)[0]
			__lowerCAmelCase      :						Dict						      =				torch.Size((1, 11, 7_68)			)
			self.assertEqual(output.shape	,		_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						List[Any]						      =				torch.tensor(
			    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]			)

			self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,		_SCREAMING_SNAKE_CASE	,		atol=1E-4			)			)