

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     snake_case_			:


							"""simple docstring"""





							def __init__( self					,						_A					,						_A=1_3					,						_A=7					,						_A=True					,						_A=True					,						_A=True					,						_A=True					,						_A=9_9					,						_A=1_6					,						_A=3_6					,						_A=6					,						_A=6					,						_A=6					,						_A=3_7					,						_A="gelu"					,						_A=0.1					,						_A=0.1					,						_A=5_1_2					,						_A=1_6					,						_A=2					,						_A=0.02					,						_A=3					,						_A=4					,						_A=None					,						):
												__lowerCAmelCase					=							parent
												__lowerCAmelCase					=							batch_size
												__lowerCAmelCase					=							seq_length
												__lowerCAmelCase					=							is_training
												__lowerCAmelCase					=							use_input_mask
												__lowerCAmelCase					=							use_token_type_ids
												__lowerCAmelCase					=							use_labels
												__lowerCAmelCase					=							vocab_size
												__lowerCAmelCase					=							embedding_size
												__lowerCAmelCase					=							hidden_size
												__lowerCAmelCase					=							num_hidden_layers
												__lowerCAmelCase					=							num_hidden_groups
												__lowerCAmelCase					=							num_attention_heads
												__lowerCAmelCase					=							intermediate_size
												__lowerCAmelCase					=							hidden_act
												__lowerCAmelCase					=							hidden_dropout_prob
												__lowerCAmelCase					=							attention_probs_dropout_prob
												__lowerCAmelCase					=							max_position_embeddings
												__lowerCAmelCase					=							type_vocab_size
												__lowerCAmelCase					=							type_sequence_label_size
												__lowerCAmelCase					=							initializer_range
												__lowerCAmelCase					=							num_labels
												__lowerCAmelCase					=							num_choices
												__lowerCAmelCase					=							scope




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

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

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

												__lowerCAmelCase					=							None
												__lowerCAmelCase					=							None
												__lowerCAmelCase					=							None
												if self.use_labels:
																	__lowerCAmelCase					=							ids_tensor([self.batch_size]					,						self.type_sequence_label_size							)
																	__lowerCAmelCase					=							ids_tensor([self.batch_size, self.seq_length]					,						self.num_labels							)
																	__lowerCAmelCase					=							ids_tensor([self.batch_size]					,						self.num_choices							)

												__lowerCAmelCase					=							self.get_config()

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




							def        A__  ( 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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							AlbertModel(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(_A					,						attention_mask=_A					,						token_type_ids=_A							)
												__lowerCAmelCase					=							model(_A					,						token_type_ids=_A							)
												__lowerCAmelCase					=							model(_A							)
												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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							AlbertForPreTraining(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(
												    _A					,						attention_mask=_A					,						token_type_ids=_A					,						labels=_A					,						sentence_order_label=_A					,						)
												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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							AlbertForMaskedLM(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(_A					,						attention_mask=_A					,						token_type_ids=_A					,						labels=_A							)
												self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size)							)




							def        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							AlbertForQuestionAnswering(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(
												    _A					,						attention_mask=_A					,						token_type_ids=_A					,						start_positions=_A					,						end_positions=_A					,						)
												self.parent.assertEqual(result.start_logits.shape					,						(self.batch_size, self.seq_length)							)
												self.parent.assertEqual(result.end_logits.shape					,						(self.batch_size, self.seq_length)							)




							def        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							self.num_labels
												__lowerCAmelCase					=							AlbertForSequenceClassification(_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(_A					,						attention_mask=_A					,						token_type_ids=_A					,						labels=_A							)
												self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels)							)




							def        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							self.num_labels
												__lowerCAmelCase					=							AlbertForTokenClassification(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							model(_A					,						attention_mask=_A					,						token_type_ids=_A					,						labels=_A							)
												self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels)							)




							def        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							self.num_choices
												__lowerCAmelCase					=							AlbertForMultipleChoice(config=_A							)
												model.to(_A							)
												model.eval()
												__lowerCAmelCase					=							input_ids.unsqueeze(1							).expand(-1					,						self.num_choices					,						-1							).contiguous()
												__lowerCAmelCase					=							token_type_ids.unsqueeze(1							).expand(-1					,						self.num_choices					,						-1							).contiguous()
												__lowerCAmelCase					=							input_mask.unsqueeze(1							).expand(-1					,						self.num_choices					,						-1							).contiguous()
												__lowerCAmelCase					=							model(
												    _A					,						attention_mask=_A					,						token_type_ids=_A					,						labels=_A					,						)
												self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices)							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.prepare_config_and_inputs()
												(
												    (
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	(
												    __lowerCAmelCase
												),	
												)					=							config_and_inputs
												__lowerCAmelCase					=							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
												return config, inputs_dict





@require_torch
class     snake_case_			(				_a			, _a			, unittest.TestCase							):


							"""simple docstring"""





							__UpperCAmelCase       =(
							    (
							        AlbertModel,
							        AlbertForPreTraining,
							        AlbertForMaskedLM,
							        AlbertForMultipleChoice,
							        AlbertForSequenceClassification,
							        AlbertForTokenClassification,
							        AlbertForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							__UpperCAmelCase       =(
							    {
							        """feature-extraction""": AlbertModel,
							        """fill-mask""": AlbertForMaskedLM,
							        """question-answering""": AlbertForQuestionAnswering,
							        """text-classification""": AlbertForSequenceClassification,
							        """token-classification""": AlbertForTokenClassification,
							        """zero-shot""": AlbertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							__UpperCAmelCase       =True




							def        A__  ( self					,						_A					,						_A					,						_A=False							):
												__lowerCAmelCase					=							super()._prepare_for_class(_A					,						_A					,						return_labels=_A							)

												if return_labels:
																	if model_class in get_values(_A							):
																						__lowerCAmelCase					=							torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)					,						dtype=torch.long					,						device=_A							)
																						__lowerCAmelCase					=							torch.zeros(
																						    self.model_tester.batch_size					,						dtype=torch.long					,						device=_A							)
												return inputs_dict




							def        A__  ( self							):
												__lowerCAmelCase					=							AlbertModelTester(self							)
												__lowerCAmelCase					=							ConfigTester(self					,						config_class=_A					,						hidden_size=3_7							)




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




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_pretraining(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*_A							)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																	__lowerCAmelCase					=							type
																	self.model_tester.create_and_check_model(*_A							)




							@slow
							def        A__  ( self							):
												for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__lowerCAmelCase					=							AlbertModel.from_pretrained(_A							)
																	self.assertIsNotNone(_A							)





@require_torch
class     snake_case_			(				unittest.TestCase							):


							"""simple docstring"""





							@slow
							def        A__  ( self							):
												__lowerCAmelCase					=							AlbertModel.from_pretrained('albert-base-v2'							)
												__lowerCAmelCase					=							torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]							)
												__lowerCAmelCase					=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
												with torch.no_grad():
																	__lowerCAmelCase					=							model(_A					,						attention_mask=_A							)[0]
												__lowerCAmelCase					=							torch.Size((1, 1_1, 7_6_8)							)
												self.assertEqual(output.shape					,						_A							)
												__lowerCAmelCase					=							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]					,						_A					,						atol=1e-4							)							)









# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool





class     snake_case_			(				_a							):


							"""simple docstring"""





							__UpperCAmelCase       ="""facebook/bart-large-mnli"""
							__UpperCAmelCase       =(
							    """This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which """
							    """should be the text to classify, and `labels`, which should be the list of labels to use for classification. """
							    """It returns the most likely label in the list of provided `labels` for the input text."""
							)
							__UpperCAmelCase       ="""text_classifier"""
							__UpperCAmelCase       =AutoTokenizer
							__UpperCAmelCase       =AutoModelForSequenceClassification

							__UpperCAmelCase       =["""text""", ["""text"""]]
							__UpperCAmelCase       =["""text"""]




							def        A__  ( self							):
												super().setup()
												__lowerCAmelCase					=							self.model.config
												__lowerCAmelCase					=							-1
												for idx, label in config.idalabel.items():
																	if label.lower().startswith('entail'							):
																						__lowerCAmelCase					=							int(_A							)
												if self.entailment_id == -1:
																	raise ValueError('Could not determine the entailment ID from the model config, please pass it at init.'							)




							def        A__  ( self					,						_A					,						_A							):
												__lowerCAmelCase					=							labels
												return self.pre_processor(
												    [text] * len(_A							)					,						[F"""This example is {label}""" for label in labels]					,						return_tensors='pt'					,						padding='max_length'					,						)




							def        A__  ( self					,						_A							):
												__lowerCAmelCase					=							outputs.logits
												__lowerCAmelCase					=							torch.argmax(logits[:, 2]							).item()
												return self._labels[label_id]






