
import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
		    FlaubertForMultipleChoice,
		    FlaubertForQuestionAnswering,
		    FlaubertForQuestionAnsweringSimple,
		    FlaubertForSequenceClassification,
		    FlaubertForTokenClassification,
		    FlaubertModel,
		    FlaubertWithLMHeadModel,
		)
		from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST




class 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	def __init__(      self	:Dict  ,     a	:List[str]  ,     a	:Optional[int]=1_3  ,     a	:List[str]=7  ,     a	:int=True  ,     a	:Tuple=True  ,     a	:int=True  ,     a	:List[Any]=True  ,     a	:List[Any]=True  ,     a	:str=False  ,     a	:str=False  ,     a	:Any=False  ,     a	:Any=2  ,     a	:Dict=9_9  ,     a	:List[str]=0  ,     a	:Optional[int]=3_2  ,     a	:Optional[int]=5  ,     a	:Tuple=4  ,     a	:Dict=0.1  ,     a	:Any=0.1  ,     a	:List[str]=5_1_2  ,     a	:Optional[Any]=1_2  ,     a	:List[str]=2  ,     a	:Union[str, Any]=0.02  ,     a	:str=3  ,     a	:str=4  ,     a	:Optional[int]="last"  ,     a	:List[str]=None  ,     a	:Any=None  ,     )	->				List[str]:
								__UpperCamelCase			:     Dict         =      parent
								__UpperCamelCase			:     Union[str, Any]         =      batch_size
								__UpperCamelCase			:     Optional[Any]         =      seq_length
								__UpperCamelCase			:     str         =      is_training
								__UpperCamelCase			:     int         =      use_input_lengths
								__UpperCamelCase			:     Optional[Any]         =      use_token_type_ids
								__UpperCamelCase			:     Any         =      use_labels
								__UpperCamelCase			:     Optional[Any]         =      gelu_activation
								__UpperCamelCase			:     Union[str, Any]         =      sinusoidal_embeddings
								__UpperCamelCase			:     str         =      causal
								__UpperCamelCase			:     Optional[int]         =      asm
								__UpperCamelCase			:     List[str]         =      n_langs
								__UpperCamelCase			:     List[str]         =      vocab_size
								__UpperCamelCase			:     Tuple         =      n_special
								__UpperCamelCase			:     Optional[int]         =      hidden_size
								__UpperCamelCase			:     Optional[int]         =      num_hidden_layers
								__UpperCamelCase			:     Union[str, Any]         =      num_attention_heads
								__UpperCamelCase			:     Optional[int]         =      hidden_dropout_prob
								__UpperCamelCase			:     str         =      attention_probs_dropout_prob
								__UpperCamelCase			:     Tuple         =      max_position_embeddings
								__UpperCamelCase			:     Optional[int]         =      type_vocab_size
								__UpperCamelCase			:     List[Any]         =      type_sequence_label_size
								__UpperCamelCase			:     List[Any]         =      initializer_range
								__UpperCamelCase			:     Tuple         =      num_labels
								__UpperCamelCase			:     str         =      num_choices
								__UpperCamelCase			:     Optional[Any]         =      summary_type
								__UpperCamelCase			:     int         =      use_proj
								__UpperCamelCase			:     int         =      scope







	def      _lowerCamelCase       (      self	:Optional[int]		)	->				Union[str, Any]:
								__UpperCamelCase			:     Optional[Any]         =      ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size		)
								__UpperCamelCase			:     str         =      random_attention_mask([self.batch_size, self.seq_length]		)

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

								__UpperCamelCase			:     Dict         =      None
								if self.use_token_type_ids:
															__UpperCamelCase			:     List[Any]         =      ids_tensor([self.batch_size, self.seq_length]  ,     self.n_langs		)

								__UpperCamelCase			:     Dict         =      None
								__UpperCamelCase			:     Tuple         =      None
								__UpperCamelCase			:     List[Any]         =      None
								if self.use_labels:
															__UpperCamelCase			:     List[Any]         =      ids_tensor([self.batch_size]  ,     self.type_sequence_label_size		)
															__UpperCamelCase			:     Tuple         =      ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels		)
															__UpperCamelCase			:     Optional[int]         =      ids_tensor([self.batch_size]  ,     2		).float()
															__UpperCamelCase			:     Union[str, Any]         =      ids_tensor([self.batch_size]  ,     self.num_choices		)

								__UpperCamelCase			:     List[str]         =      self.get_config()

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







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				Optional[int]:
								return FlaubertConfig(
								    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  ,     )







	def      _lowerCamelCase       (      self	:Tuple  ,     a	:List[Any]  ,     a	:Any  ,     a	:Optional[int]  ,     a	:List[Any]  ,     a	:Optional[Any]  ,     a	:int  ,     a	:Tuple  ,     a	:Union[str, Any]  ,     a	:int  ,     )	->				Optional[Any]:
								__UpperCamelCase			:     List[str]         =      FlaubertModel(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     Optional[Any]         =      model(a  ,     lengths=a  ,     langs=a		)
								__UpperCamelCase			:     Any         =      model(a  ,     langs=a		)
								__UpperCamelCase			:     Tuple         =      model(a		)
								self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)		)







	def      _lowerCamelCase       (      self	:List[Any]  ,     a	:Tuple  ,     a	:int  ,     a	:List[Any]  ,     a	:Any  ,     a	:List[Any]  ,     a	:Any  ,     a	:Dict  ,     a	:Any  ,     a	:Optional[Any]  ,     )	->				Optional[int]:
								__UpperCamelCase			:     Optional[Any]         =      FlaubertWithLMHeadModel(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     Any         =      model(a  ,     token_type_ids=a  ,     labels=a		)
								self.parent.assertEqual(result.loss.shape  ,     ()		)
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)		)







	def      _lowerCamelCase       (      self	:Dict  ,     a	:Optional[int]  ,     a	:Optional[int]  ,     a	:int  ,     a	:List[str]  ,     a	:Optional[Any]  ,     a	:List[Any]  ,     a	:Optional[Any]  ,     a	:Union[str, Any]  ,     a	:Union[str, Any]  ,     )	->				Optional[int]:
								__UpperCamelCase			:     List[Any]         =      FlaubertForQuestionAnsweringSimple(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     List[Any]         =      model(a		)

								__UpperCamelCase			:     Tuple         =      model(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      _lowerCamelCase       (      self	:Optional[int]  ,     a	:int  ,     a	:List[str]  ,     a	:List[Any]  ,     a	:int  ,     a	:Optional[Any]  ,     a	:Any  ,     a	:Dict  ,     a	:List[str]  ,     a	:int  ,     )	->				Optional[int]:
								__UpperCamelCase			:     Union[str, Any]         =      FlaubertForQuestionAnswering(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     Any         =      model(a		)

								__UpperCamelCase			:     Optional[Any]         =      model(
								    a  ,     start_positions=a  ,     end_positions=a  ,     cls_index=a  ,     is_impossible=a  ,     p_mask=a  ,     )

								__UpperCamelCase			:     Dict         =      model(
								    a  ,     start_positions=a  ,     end_positions=a  ,     cls_index=a  ,     is_impossible=a  ,     )

								((__UpperCamelCase)     ,		)			:     Optional[int]         =      result_with_labels.to_tuple()

								__UpperCamelCase			:     str         =      model(a  ,     start_positions=a  ,     end_positions=a		)

								((__UpperCamelCase)     ,		)			:     Optional[int]         =      result_with_labels.to_tuple()

								self.parent.assertEqual(result_with_labels.loss.shape  ,     ()		)
								self.parent.assertEqual(result.start_top_log_probs.shape  ,     (self.batch_size, model.config.start_n_top)		)
								self.parent.assertEqual(result.start_top_index.shape  ,     (self.batch_size, model.config.start_n_top)		)
								self.parent.assertEqual(
								    result.end_top_log_probs.shape  ,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
								self.parent.assertEqual(
								    result.end_top_index.shape  ,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
								self.parent.assertEqual(result.cls_logits.shape  ,     (self.batch_size,)		)







	def      _lowerCamelCase       (      self	:str  ,     a	:int  ,     a	:Union[str, Any]  ,     a	:Any  ,     a	:int  ,     a	:int  ,     a	:int  ,     a	:Tuple  ,     a	:Tuple  ,     a	:Union[str, Any]  ,     )	->				int:
								__UpperCamelCase			:     Dict         =      FlaubertForSequenceClassification(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     Optional[Any]         =      model(a		)
								__UpperCamelCase			:     Optional[Any]         =      model(a  ,     labels=a		)

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







	def      _lowerCamelCase       (      self	:Optional[int]  ,     a	:Optional[Any]  ,     a	:Any  ,     a	:List[Any]  ,     a	:Any  ,     a	:Union[str, Any]  ,     a	:Optional[int]  ,     a	:int  ,     a	:Optional[int]  ,     a	:List[str]  ,     )	->				Dict:
								__UpperCamelCase			:     Optional[int]         =      self.num_labels
								__UpperCamelCase			:     Optional[int]         =      FlaubertForTokenClassification(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     Union[str, Any]         =      model(a  ,     attention_mask=a  ,     labels=a		)
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)		)







	def      _lowerCamelCase       (      self	:Any  ,     a	:str  ,     a	:str  ,     a	:Union[str, Any]  ,     a	:List[Any]  ,     a	:Dict  ,     a	:Optional[Any]  ,     a	:Dict  ,     a	:Union[str, Any]  ,     a	:Dict  ,     )	->				List[Any]:
								__UpperCamelCase			:     str         =      self.num_choices
								__UpperCamelCase			:     Dict         =      FlaubertForMultipleChoice(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     int         =      input_ids.unsqueeze(1		).expand(-1  ,     self.num_choices  ,     -1		).contiguous()
								__UpperCamelCase			:     Optional[int]         =      token_type_ids.unsqueeze(1		).expand(-1  ,     self.num_choices  ,     -1		).contiguous()
								__UpperCamelCase			:     List[str]         =      input_mask.unsqueeze(1		).expand(-1  ,     self.num_choices  ,     -1		).contiguous()
								__UpperCamelCase			:     Union[str, Any]         =      model(
								    a  ,     attention_mask=a  ,     token_type_ids=a  ,     labels=a  ,     )
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_choices)		)







	def      _lowerCamelCase       (      self	:List[str]		)	->				Tuple:
								__UpperCamelCase			:     Union[str, Any]         =      self.prepare_config_and_inputs()
								(
								    (
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		(
								    __UpperCamelCase
								)     ,		
								)			:     Tuple         =      config_and_inputs
								__UpperCamelCase			:     Optional[Any]         =      {
								    "input_ids": input_ids,
								    "token_type_ids": token_type_ids,
								    "lengths": input_lengths,
								    "attention_mask": input_mask,
								}
								return config, inputs_dict




@require_torch
class 				lowerCamelCase__							(       __lowercase  ,		__lowercase  ,		unittest.TestCase):




	'''simple docstring'''




	_A			=  (
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	_A			=  (
	    {
	        'feature-extraction': FlaubertModel,
	        'fill-mask': FlaubertWithLMHeadModel,
	        'question-answering': FlaubertForQuestionAnsweringSimple,
	        'text-classification': FlaubertForSequenceClassification,
	        'token-classification': FlaubertForTokenClassification,
	        'zero-shot': FlaubertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)







	def      _lowerCamelCase       (      self	:Union[str, Any]  ,     a	:Tuple  ,     a	:Optional[Any]  ,     a	:Any  ,     a	:List[Any]  ,     a	:Optional[Any]		)	->				int:
								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      _lowerCamelCase       (      self	:Optional[int]  ,     a	:Optional[int]  ,     a	:Optional[Any]  ,     a	:str=False		)	->				Optional[Any]:
								__UpperCamelCase			:     Tuple         =      super()._prepare_for_class(a  ,     a  ,     return_labels=a		)

								if return_labels:
															if model_class.__name__ == "FlaubertForQuestionAnswering":
																						__UpperCamelCase			:     str         =      torch.zeros(
																						    self.model_tester.batch_size  ,     dtype=torch.long  ,     device=a		)
																						__UpperCamelCase			:     Dict         =      torch.zeros(
																						    self.model_tester.batch_size  ,     dtype=torch.long  ,     device=a		)

								return inputs_dict







	def      _lowerCamelCase       (      self	:Dict		)	->				Tuple:
								__UpperCamelCase			:     Optional[int]         =      FlaubertModelTester(self		)
								__UpperCamelCase			:     Optional[int]         =      ConfigTester(self  ,     config_class=a  ,     emb_dim=3_7		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Optional[Any]:
								self.config_tester.run_common_tests()







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				List[Any]:
								__UpperCamelCase			:     Union[str, Any]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*a		)







	def      _lowerCamelCase       (      self	:Any		)	->				Optional[int]:
								__UpperCamelCase			:     List[str]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*a		)







	def      _lowerCamelCase       (      self	:Any		)	->				int:
								__UpperCamelCase			:     Optional[int]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*a		)







	def      _lowerCamelCase       (      self	:Tuple		)	->				Tuple:
								__UpperCamelCase			:     int         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*a		)







	def      _lowerCamelCase       (      self	:Dict		)	->				Union[str, Any]:
								__UpperCamelCase			:     Optional[int]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*a		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Optional[Any]:
								__UpperCamelCase			:     Tuple         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*a		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Optional[Any]:
								__UpperCamelCase			:     int         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*a		)







	@slow
	def      _lowerCamelCase       (      self	:List[str]		)	->				List[Any]:
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase			:     List[Any]         =      FlaubertModel.from_pretrained(a		)
															self.assertIsNotNone(a		)







	@slow
	@require_torch_gpu
	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				str:
								__UpperCamelCase     ,		__UpperCamelCase			:     int         =      self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes:
															# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
															if model_class == FlaubertForMultipleChoice:
																						return

															__UpperCamelCase			:     Optional[Any]         =      True
															__UpperCamelCase			:     Any         =      model_class(config=a		)

															__UpperCamelCase			:     Tuple         =      self._prepare_for_class(a  ,     a		)
															__UpperCamelCase			:     int         =      torch.jit.trace(
															    a  ,     (inputs_dict["input_ids"].to("cpu"		), inputs_dict["attention_mask"].to("cpu"		))		)

															with tempfile.TemporaryDirectory() as tmp:
																						torch.jit.save(a  ,     os.path.join(a  ,     "traced_model.pt"		)		)
																						__UpperCamelCase			:     Union[str, Any]         =      torch.jit.load(os.path.join(a  ,     "traced_model.pt"		)  ,     map_location=a		)
																						loaded(inputs_dict["input_ids"].to(a		)  ,     inputs_dict["attention_mask"].to(a		)		)




@require_torch
class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	@slow
	def      _lowerCamelCase       (      self	:List[Any]		)	->				Dict:
								__UpperCamelCase			:     List[str]         =      FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"		)
								__UpperCamelCase			:     Any         =      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]]		)
								with torch.no_grad():
															__UpperCamelCase			:     Union[str, Any]         =      model(a		)[0]
								__UpperCamelCase			:     int         =      torch.Size((1, 1_1, 7_6_8)		)
								self.assertEqual(output.shape  ,     a		)
								__UpperCamelCase			:     Union[str, Any]         =      torch.tensor(
								    [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]		)

								self.assertTrue(torch.allclose(output[:, :3, :3]  ,     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 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	_A			=  'facebook/bart-large-mnli'
	_A			=  (
	    '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.'
	)
	_A			=  'text_classifier'
	_A			=  AutoTokenizer
	_A			=  AutoModelForSequenceClassification

	_A			=  ['text', ['text']]
	_A			=  ['text']







	def      _lowerCamelCase       (      self	:str		)	->				Tuple:
								super().setup()
								__UpperCamelCase			:     Any         =      self.model.config
								__UpperCamelCase			:     List[Any]         =      -1
								for idx, label in config.idalabel.items():
															if label.lower().startswith("entail"		):
																						__UpperCamelCase			:     Optional[Any]         =      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      _lowerCamelCase       (      self	:Optional[int]  ,     a	:Dict  ,     a	:Optional[Any]		)	->				Optional[int]:
								__UpperCamelCase			:     str         =      labels
								return self.pre_processor(
								    [text] * len(a		)  ,     [f'This example is {label}' for label in labels]  ,     return_tensors="pt"  ,     padding="max_length"  ,     )







	def      _lowerCamelCase       (      self	:Any  ,     a	:Any		)	->				Optional[int]:
								__UpperCamelCase			:     Optional[int]         =      outputs.logits
								__UpperCamelCase			:     Dict         =      torch.argmax(logits[:, 2]		).item()
								return self._labels[label_id]