

import argparse
import logging
import os
import sys
from collections import namedtuple

import torch
from modeling_bertabs import BertAbs, build_predictor
from torch.utils.data import DataLoader, SequentialSampler
from tqdm import tqdm

from transformers import BertTokenizer

from .utils_summarization import (
    CNNDMDataset,
    build_mask,
    compute_token_type_ids,
    encode_for_summarization,
    truncate_or_pad,
)


A__							=				logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)


A__							=				namedtuple("Batch", ["document_names", "batch_size", "src", "segs", "mask_src", "tgt_str"])




def        _lowercase  (					a_					:       Optional[int]					)					->						List[Any]:




					'''simple docstring'''







					__magic_name__				=     BertTokenizer.from_pretrained('bert-base-uncased'       ,do_lower_case=a__					)
					__magic_name__				=     BertAbs.from_pretrained('remi/bertabs-finetuned-extractive-abstractive-summarization'					)
					model.to(args.device					)
					model.eval()

					__magic_name__				=     {
					    '''BOS''': tokenizer.vocab['''[unused0]'''],
					    '''EOS''': tokenizer.vocab['''[unused1]'''],
					    '''PAD''': tokenizer.vocab['''[PAD]'''],
					}

					if args.compute_rouge:
										__magic_name__				=     []
										__magic_name__				=     []

										import nltk
										import rouge

										nltk.download('punkt'					)
										__magic_name__				=     rouge.Rouge(
										    metrics=['rouge-n', 'rouge-l']       ,max_n=2       ,limit_length=a__       ,length_limit=args.beam_size       ,length_limit_type='words'       ,apply_avg=a__       ,apply_best=a__       ,alpha=0.5       ,weight_factor=1.2       ,stemming=a__       ,)

					# these (unused) arguments are defined to keep the compatibility
					# with the legacy code and will be deleted in a next iteration.
					__magic_name__				=     ''''''
					__magic_name__				=     ''''''

					__magic_name__				=     build_data_iterator(a__       ,a__					)
					__magic_name__				=     build_predictor(a__       ,a__       ,a__       ,a__					)

					logger.info('***** Running evaluation *****'					)
					logger.info('  Number examples = %d'       ,len(data_iterator.dataset					)					)
					logger.info('  Batch size = %d'       ,args.batch_size					)
					logger.info(''					)
					logger.info('***** Beam Search parameters *****'					)
					logger.info('  Beam size = %d'       ,args.beam_size					)
					logger.info('  Minimum length = %d'       ,args.min_length					)
					logger.info('  Maximum length = %d'       ,args.max_length					)
					logger.info('  Alpha (length penalty) = %.2f'       ,args.alpha					)
					logger.info('  Trigrams %s be blocked'       ,('will' if args.block_trigram else 'will NOT')					)

					for batch in tqdm(a__					):
										__magic_name__				=     predictor.translate_batch(a__					)
										__magic_name__				=     predictor.from_batch(a__					)
										__magic_name__				=     [format_summary(a__					) for t in translations]
										save_summaries(a__       ,args.summaries_output_dir       ,batch.document_names					)

										if args.compute_rouge:
															reference_summaries += batch.tgt_str
															generated_summaries += summaries

					if args.compute_rouge:
										__magic_name__				=     rouge_evaluator.get_scores(a__       ,a__					)
										__magic_name__				=     format_rouge_scores(a__					)
										save_rouge_scores(a__					)
										print(a__					)




def        _lowercase  (					a_					:       str       ,a_					:       Optional[int]       ,a_					:       Tuple					)					->						str:




					'''simple docstring'''







					for summary, document_name in zip(a__       ,a__					):
										# Prepare the summary file's name
										if "." in document_name:
															__magic_name__				=     '''.'''.join(document_name.split('.'					)[:-1]					)
															__magic_name__				=     document_name.split('.'					)[-1]
															__magic_name__				=     bare_document_name + '''_summary.''' + extension
										else:
															__magic_name__				=     document_name + '''_summary'''

										__magic_name__				=     os.path.join(a__       ,a__					)
										with open(a__       ,'w'					) as output:
															output.write(a__					)




def        _lowercase  (					a_					:       str					)					->						Any:




					'''simple docstring'''







					__magic_name__				=     translation
					__magic_name__				=     (
					    raw_summary.replace('[unused0]'       ,''					)
					    .replace('[unused3]'       ,''					)
					    .replace('[PAD]'       ,''					)
					    .replace('[unused1]'       ,''					)
					    .replace(R' +'       ,' '					)
					    .replace(' [unused2] '       ,'. '					)
					    .replace('[unused2]'       ,''					)
					    .strip()
					)

					return summary




def        _lowercase  (					a_					:       List[str]					)					->						int:




					'''simple docstring'''







					return """\n
****** ROUGE SCORES ******

** ROUGE 1
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE 2
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE L
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}""".format(
					    scores['rouge-1']['f']       ,scores['rouge-1']['p']       ,scores['rouge-1']['r']       ,scores['rouge-2']['f']       ,scores['rouge-2']['p']       ,scores['rouge-2']['r']       ,scores['rouge-l']['f']       ,scores['rouge-l']['p']       ,scores['rouge-l']['r']       ,)




def        _lowercase  (					a_					:       Optional[int]					)					->						Optional[Any]:




					'''simple docstring'''







					with open('rouge_scores.txt'       ,'w'					) as output:
										output.write(a__					)




def        _lowercase  (					a_					:       str       ,a_					:       Optional[int]					)					->						Optional[Any]:




					'''simple docstring'''







					__magic_name__				=     load_and_cache_examples(a__       ,a__					)
					__magic_name__				=     SequentialSampler(a__					)

					def collate_fn(a_					:       Optional[Any]					):
										return collate(a__       ,a__       ,block_size=5_1_2       ,device=args.device					)

					__magic_name__				=     DataLoader(
					    a__       ,sampler=a__       ,batch_size=args.batch_size       ,collate_fn=a__       ,)

					return iterator




def        _lowercase  (					a_					:       Dict       ,a_					:       List[Any]					)					->						Optional[int]:




					'''simple docstring'''







					__magic_name__				=     CNNDMDataset(args.documents_dir					)
					return dataset




def        _lowercase  (					a_					:       Optional[int]       ,a_					:       Optional[int]       ,a_					:       Optional[Any]       ,a_					:       Optional[int]					)					->						Optional[int]:




					'''simple docstring'''







					__magic_name__				=     [x for x in data if not len(x[1]					) == 0]  # remove empty_files
					__magic_name__				=     [name for name, _, _ in data]
					__magic_name__				=     [''' '''.join(a__					) for _, _, summary_list in data]

					__magic_name__				=     [encode_for_summarization(a__       ,a__       ,a__					) for _, story, summary in data]
					__magic_name__				=     torch.tensor(
					    [truncate_or_pad(a__       ,a__       ,tokenizer.pad_token_id					) for story, _ in encoded_text]					)
					__magic_name__				=     compute_token_type_ids(a__       ,tokenizer.cls_token_id					)
					__magic_name__				=     build_mask(a__       ,tokenizer.pad_token_id					)

					__magic_name__				=     Batch(
					    document_names=a__       ,batch_size=len(a__					)       ,src=encoded_stories.to(a__					)       ,segs=encoder_token_type_ids.to(a__					)       ,mask_src=encoder_mask.to(a__					)       ,tgt_str=a__       ,)

					return batch




def        _lowercase  (					a_					:       Tuple       ,a_					:       List[Any]					)					->						str:




					'''simple docstring'''







					__magic_name__				=     summary_tokens.to('cpu'					).numpy()
					__magic_name__				=     tokenizer.decode(a__					)
					__magic_name__				=     summary.split('.'					)
					__magic_name__				=     [s + '''.''' for s in sentences]
					return sentences




def        _lowercase  (					)					->						List[str]:




					'''simple docstring'''







					__magic_name__				=     argparse.ArgumentParser()
					parser.add_argument(
					    '--documents_dir'       ,default=a__       ,type=a__       ,required=a__       ,help='The folder where the documents to summarize are located.'       ,)
					parser.add_argument(
					    '--summaries_output_dir'       ,default=a__       ,type=a__       ,required=a__       ,help='The folder in wich the summaries should be written. Defaults to the folder where the documents are'       ,)
					parser.add_argument(
					    '--compute_rouge'       ,default=a__       ,type=a__       ,required=a__       ,help='Compute the ROUGE metrics during evaluation. Only available for the CNN/DailyMail dataset.'       ,)
					# EVALUATION options
					parser.add_argument(
					    '--no_cuda'       ,default=a__       ,type=a__       ,help='Whether to force the execution on CPU.'       ,)
					parser.add_argument(
					    '--batch_size'       ,default=4       ,type=a__       ,help='Batch size per GPU/CPU for training.'       ,)
					# BEAM SEARCH arguments
					parser.add_argument(
					    '--min_length'       ,default=5_0       ,type=a__       ,help='Minimum number of tokens for the summaries.'       ,)
					parser.add_argument(
					    '--max_length'       ,default=2_0_0       ,type=a__       ,help='Maixmum number of tokens for the summaries.'       ,)
					parser.add_argument(
					    '--beam_size'       ,default=5       ,type=a__       ,help='The number of beams to start with for each example.'       ,)
					parser.add_argument(
					    '--alpha'       ,default=0.95       ,type=a__       ,help='The value of alpha for the length penalty in the beam search.'       ,)
					parser.add_argument(
					    '--block_trigram'       ,default=a__       ,type=a__       ,help='Whether to block the existence of repeating trigrams in the text generated by beam search.'       ,)
					__magic_name__				=     parser.parse_args()

					# Select device (distibuted not available)
					__magic_name__				=     torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu'					)

					# Check the existence of directories
					if not args.summaries_output_dir:
										__magic_name__				=     args.documents_dir

					if not documents_dir_is_valid(args.documents_dir					):
										raise FileNotFoundError(
										    'We could not find the directory you specified for the documents to summarize, or it was empty. Please'
										    ' specify a valid path.'					)
					os.makedirs(args.summaries_output_dir       ,exist_ok=a__					)

					evaluate(a__					)




def        _lowercase  (					a_					:       Any					)					->						Optional[Any]:




					'''simple docstring'''







					if not os.path.exists(a__					):
										return False

					__magic_name__				=     os.listdir(a__					)
					if len(a__					) == 0:
										return False

					return True


if __name__ == "__main__":
						main()






from __future__ import annotations

import unittest

from transformers import AlbertConfig, is_tf_available
from transformers.models.auto import get_values
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_MODEL_FOR_PRETRAINING_MAPPING
      from transformers.models.albert.modeling_tf_albert import (
          TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFAlbertForMaskedLM,
          TFAlbertForMultipleChoice,
          TFAlbertForPreTraining,
          TFAlbertForQuestionAnswering,
          TFAlbertForSequenceClassification,
          TFAlbertForTokenClassification,
          TFAlbertModel,
      )






class     a_       :



       """simple docstring"""






       def __init__(		self				,       _lowerCamelCase				,       _lowerCamelCase=13				,       _lowerCamelCase=7				,       _lowerCamelCase=True				,       _lowerCamelCase=True				,       _lowerCamelCase=True				,       _lowerCamelCase=True				,       _lowerCamelCase=99				,       _lowerCamelCase=16				,       _lowerCamelCase=32				,       _lowerCamelCase=2				,       _lowerCamelCase=4				,       _lowerCamelCase=37				,       _lowerCamelCase="gelu"				,       _lowerCamelCase=0.1				,       _lowerCamelCase=0.1				,       _lowerCamelCase=512				,       _lowerCamelCase=16				,       _lowerCamelCase=2				,       _lowerCamelCase=0.0_2				,       _lowerCamelCase=3				,       _lowerCamelCase=4				,       _lowerCamelCase=None				,       )					->List[Any]:
           SCREAMING_SNAKE_CASE				:				int				      = parent
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = 13
           SCREAMING_SNAKE_CASE				:				int				      = 7
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = True
           SCREAMING_SNAKE_CASE				:				List[str]				      = True
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = True
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = True
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = 99
           SCREAMING_SNAKE_CASE				:				str				      = 16
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = 32
           SCREAMING_SNAKE_CASE				:				str				      = 2
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = 4
           SCREAMING_SNAKE_CASE				:				int				      = 37
           SCREAMING_SNAKE_CASE				:				Any				      = '''gelu'''
           SCREAMING_SNAKE_CASE				:				str				      = 0.1
           SCREAMING_SNAKE_CASE				:				Dict				      = 0.1
           SCREAMING_SNAKE_CASE				:				Tuple				      = 512
           SCREAMING_SNAKE_CASE				:				Any				      = 16
           SCREAMING_SNAKE_CASE				:				str				      = 2
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = 0.0_2
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = 3
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = 4
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = None






       def 	__lowerCAmelCase     (		self   )					->List[str]:
           SCREAMING_SNAKE_CASE				:				Tuple				      = ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size   )

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

           SCREAMING_SNAKE_CASE				:				Dict				      = None
           if self.use_token_type_ids:
               SCREAMING_SNAKE_CASE				:				Dict				      = ids_tensor([self.batch_size, self.seq_length]				,       self.type_vocab_size   )

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

           SCREAMING_SNAKE_CASE				:				List[Any]				      = AlbertConfig(
               vocab_size=self.vocab_size				,       hidden_size=self.hidden_size				,       embedding_size=self.embedding_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 config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->List[str]:
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = TFAlbertModel(config=_lowerCamelCase   )
           # inputs = {'input_ids': input_ids,
           #           'attention_mask': input_mask,
           #           'token_type_ids': token_type_ids}
           # sequence_output, pooled_output = model(**inputs)
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = model(_lowerCamelCase   )

           SCREAMING_SNAKE_CASE				:				List[str]				      = [input_ids, input_mask]
           SCREAMING_SNAKE_CASE				:				List[str]				      = model(_lowerCamelCase   )

           SCREAMING_SNAKE_CASE				:				Any				      = model(_lowerCamelCase   )

           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				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->List[Any]:
           SCREAMING_SNAKE_CASE				:				str				      = self.num_labels
           SCREAMING_SNAKE_CASE				:				List[str]				      = TFAlbertForPreTraining(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				Any				      = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           SCREAMING_SNAKE_CASE				:				int				      = model(_lowerCamelCase   )
           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, self.num_labels)   )






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->str:
           SCREAMING_SNAKE_CASE				:				str				      = TFAlbertForMaskedLM(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				int				      = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = model(_lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)   )






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->int:
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = self.num_labels
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = TFAlbertForSequenceClassification(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           SCREAMING_SNAKE_CASE				:				Tuple				      = model(_lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)   )






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->Dict:
           SCREAMING_SNAKE_CASE				:				List[str]				      = TFAlbertForQuestionAnswering(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           SCREAMING_SNAKE_CASE				:				int				      = model(_lowerCamelCase   )
           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				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->Any:
           SCREAMING_SNAKE_CASE				:				List[Any]				      = self.num_choices
           SCREAMING_SNAKE_CASE				:				Any				      = TFAlbertForMultipleChoice(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				Tuple				      = tf.tile(tf.expand_dims(_lowerCamelCase				,       1   )				,       (1, self.num_choices, 1)   )
           SCREAMING_SNAKE_CASE				:				Tuple				      = tf.tile(tf.expand_dims(_lowerCamelCase				,       1   )				,       (1, self.num_choices, 1)   )
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = tf.tile(tf.expand_dims(_lowerCamelCase				,       1   )				,       (1, self.num_choices, 1)   )
           SCREAMING_SNAKE_CASE				:				Tuple				      = {
               '''input_ids''': multiple_choice_inputs_ids,
               '''attention_mask''': multiple_choice_input_mask,
               '''token_type_ids''': multiple_choice_token_type_ids,
           }
           SCREAMING_SNAKE_CASE				:				str				      = model(_lowerCamelCase   )
           self.parent.assertListEqual(list(result['''logits'''].shape   )				,       [self.batch_size, self.num_choices]   )






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase   )					->Optional[Any]:
           SCREAMING_SNAKE_CASE				:				str				      = self.num_labels
           SCREAMING_SNAKE_CASE				:				List[Any]				      = TFAlbertForTokenClassification(config=_lowerCamelCase   )
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           SCREAMING_SNAKE_CASE				:				str				      = model(_lowerCamelCase   )
           self.parent.assertListEqual(list(result['''logits'''].shape   )				,       [self.batch_size, self.seq_length, self.num_labels]   )






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






@require_tf
class     a_       (			a__				,    a__				,    unittest.TestCase							):



       """simple docstring"""
       __SCREAMING_SNAKE_CASE       :					int						=  (
           (
               TFAlbertModel,
               TFAlbertForPreTraining,
               TFAlbertForMaskedLM,
               TFAlbertForSequenceClassification,
               TFAlbertForQuestionAnswering,
               TFAlbertForTokenClassification,
               TFAlbertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       __SCREAMING_SNAKE_CASE       :					Optional[int]						=  (
           {
               'feature-extraction': TFAlbertModel,
               'fill-mask': TFAlbertForMaskedLM,
               'question-answering': TFAlbertForQuestionAnswering,
               'text-classification': TFAlbertForSequenceClassification,
               'token-classification': TFAlbertForTokenClassification,
               'zero-shot': TFAlbertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __SCREAMING_SNAKE_CASE       :					Tuple						=  False
       __SCREAMING_SNAKE_CASE       :					Any						=  False






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase				,       _lowerCamelCase				,       _lowerCamelCase=False   )					->int:
           SCREAMING_SNAKE_CASE				:				Tuple				      = super()._prepare_for_class(_lowerCamelCase				,       _lowerCamelCase				,       return_labels=_lowerCamelCase   )

           if return_labels:
               if model_class in get_values(_lowerCamelCase   ):
                   SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = tf.zeros(self.model_tester.batch_size				,       dtype=tf.intaa   )

           return inputs_dict






       def 	__lowerCAmelCase     (		self   )					->Tuple:
           SCREAMING_SNAKE_CASE				:				Dict				      = TFAlbertModelTester(self   )
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = ConfigTester(self				,       config_class=_lowerCamelCase				,       hidden_size=37   )






       def 	__lowerCAmelCase     (		self   )					->Union[str, Any]:
           self.config_tester.run_common_tests()






       def 	__lowerCAmelCase     (		self   )					->str:
           SCREAMING_SNAKE_CASE				:				Tuple				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_model(*_lowerCamelCase   )






       def 	__lowerCAmelCase     (		self   )					->str:
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_for_pretraining(*_lowerCamelCase   )






       def 	__lowerCAmelCase     (		self   )					->Any:
           SCREAMING_SNAKE_CASE				:				Dict				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_for_masked_lm(*_lowerCamelCase   )






       def 	__lowerCAmelCase     (		self   )					->Optional[Any]:
           SCREAMING_SNAKE_CASE				:				List[str]				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_for_multiple_choice(*_lowerCamelCase   )






       def 	__lowerCAmelCase     (		self   )					->Dict:
           SCREAMING_SNAKE_CASE				:				List[Any]				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_for_sequence_classification(*_lowerCamelCase   )






       def 	__lowerCAmelCase     (		self   )					->str:
           SCREAMING_SNAKE_CASE				:				Optional[int]				      = self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_albert_for_question_answering(*_lowerCamelCase   )






       @slow
       def 	__lowerCAmelCase     (		self   )					->int:
           for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               SCREAMING_SNAKE_CASE				:				Tuple				      = TFAlbertModel.from_pretrained(_lowerCamelCase   )
               self.assertIsNotNone(_lowerCamelCase   )






@require_tf
class     a_       (			unittest.TestCase							):



       """simple docstring"""






       @slow
       def 	__lowerCAmelCase     (		self   )					->List[Any]:
           SCREAMING_SNAKE_CASE				:				Dict				      = TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''   )
           SCREAMING_SNAKE_CASE				:				str				      = tf.constant([[0, 1, 2, 3, 4, 5]]   )
           SCREAMING_SNAKE_CASE				:				Union[str, Any]				      = model(_lowerCamelCase   )[0]

           SCREAMING_SNAKE_CASE				:				List[str]				      = [1, 6, 3_0000]
           self.assertEqual(output.shape				,       _lowerCamelCase   )

           SCREAMING_SNAKE_CASE				:				Dict				      = tf.constant(
               [
                   [
                       [4.5_9_5_6_6_8, 0.7_4_4_6_2_7_5_4, -1.8_1_8_1_4_7],
                       [4.5_9_5_4_3_4_7, 0.7_4_5_4_1_8_4, -1.8_1_8_8_2_5_8],
                       [4.5_9_5_4_9_0_5, 0.7_4_4_8_2_3_5, -1.8_1_8_2_3_1_6],
                   ]
               ]   )
           tf.debugging.assert_near(output[:, :3, :3]				,       _lowerCamelCase				,       atol=1e-4   )
