


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 							a						(_lowerCAmelCase ):




							"""simple docstring"""


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

							def        __snake_case   (				self			:    Any							)      ->       List[Any]:
														__snake_case						:     str									=  ids_tensor([self.batch_size, self.seq_length]       ,      self.vocab_size							)
														__snake_case						:     Optional[Any]									=  random_attention_mask([self.batch_size, self.seq_length]							)

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

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

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

														__snake_case						:     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        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														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        __snake_case   (				self			:    Tuple       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Optional[Any]       ,      )      ->       Dict:
														__snake_case						:     List[str]									=  FlaubertModel(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     str									=  model(lowerCamelCase       ,      lengths=lowerCamelCase       ,      langs=lowerCamelCase							)
														__snake_case						:     List[Any]									=  model(lowerCamelCase       ,      langs=lowerCamelCase							)
														__snake_case						:     Tuple									=  model(lowerCamelCase							)
														self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, self.seq_length, self.hidden_size)							)

							def        __snake_case   (				self			:    List[str]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    Tuple       ,      )      ->       Optional[Any]:
														__snake_case						:     Optional[Any]									=  FlaubertWithLMHeadModel(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     Dict									=  model(lowerCamelCase       ,      token_type_ids=lowerCamelCase       ,      labels=lowerCamelCase							)
														self.parent.assertEqual(result.loss.shape       ,      ()							)
														self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.vocab_size)							)

							def        __snake_case   (				self			:    Dict       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    List[Any]       ,      )      ->       List[str]:
														__snake_case						:     Union[str, Any]									=  FlaubertForQuestionAnsweringSimple(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     Optional[int]									=  model(lowerCamelCase							)

														__snake_case						:     Optional[int]									=  model(lowerCamelCase       ,      start_positions=lowerCamelCase       ,      end_positions=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        __snake_case   (				self			:    Tuple       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Dict       ,      )      ->       Union[str, Any]:
														__snake_case						:     str									=  FlaubertForQuestionAnswering(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     int									=  model(lowerCamelCase							)

														__snake_case						:     Dict									=  model(
														    lowerCamelCase       ,      start_positions=lowerCamelCase       ,      end_positions=lowerCamelCase       ,      cls_index=lowerCamelCase       ,      is_impossible=lowerCamelCase       ,      p_mask=lowerCamelCase       ,      )

														__snake_case						:     List[Any]									=  model(
														    lowerCamelCase       ,      start_positions=lowerCamelCase       ,      end_positions=lowerCamelCase       ,      cls_index=lowerCamelCase       ,      is_impossible=lowerCamelCase       ,      )

														((__snake_case)					,     )						:     Union[str, Any]									=  result_with_labels.to_tuple()

														__snake_case						:     List[Any]									=  model(lowerCamelCase       ,      start_positions=lowerCamelCase       ,      end_positions=lowerCamelCase							)

														((__snake_case)					,     )						:     List[str]									=  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        __snake_case   (				self			:    Union[str, Any]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    List[Any]       ,      )      ->       str:
														__snake_case						:     Union[str, Any]									=  FlaubertForSequenceClassification(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     Dict									=  model(lowerCamelCase							)
														__snake_case						:     List[Any]									=  model(lowerCamelCase       ,      labels=lowerCamelCase							)

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

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

														__snake_case						:     int									=  model(lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      labels=lowerCamelCase							)
														self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.num_labels)							)

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






							def        __snake_case   (				self			:    Tuple							)      ->       Tuple:
														__snake_case						:     int									=  self.prepare_config_and_inputs()
														(
														    (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     (
														    __snake_case
														)					,     
														)						:     int									=  config_and_inputs
														__snake_case						:     str									=  {
														    "input_ids": input_ids,
														    "token_type_ids": token_type_ids,
														    "lengths": input_lengths,
														    "attention_mask": input_mask,
														}
														return config, inputs_dict

@require_torch
class 							a						(_lowerCAmelCase	,							_lowerCAmelCase	,							unittest.TestCase ):




							"""simple docstring"""


							__UpperCAmelCase       :				Dict	    =    (
							    (
							        FlaubertModel,
							        FlaubertWithLMHeadModel,
							        FlaubertForQuestionAnswering,
							        FlaubertForQuestionAnsweringSimple,
							        FlaubertForSequenceClassification,
							        FlaubertForTokenClassification,
							        FlaubertForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							__UpperCAmelCase       :				Optional[Any]	    =    (
							    {
							        "feature-extraction": FlaubertModel,
							        "fill-mask": FlaubertWithLMHeadModel,
							        "question-answering": FlaubertForQuestionAnsweringSimple,
							        "text-classification": FlaubertForSequenceClassification,
							        "token-classification": FlaubertForTokenClassification,
							        "zero-shot": FlaubertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

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

														if return_labels:
																					if model_class.__name__ == "FlaubertForQuestionAnswering":
																												__snake_case						:     Optional[int]									=  torch.zeros(
																												    self.model_tester.batch_size       ,      dtype=torch.long       ,      device=lowerCamelCase							)
																												__snake_case						:     Optional[Any]									=  torch.zeros(
																												    self.model_tester.batch_size       ,      dtype=torch.long       ,      device=lowerCamelCase							)

														return inputs_dict

							def        __snake_case   (				self			:    Tuple							)      ->       int:
														__snake_case						:     int									=  FlaubertModelTester(self							)
														__snake_case						:     Union[str, Any]									=  ConfigTester(self       ,      config_class=lowerCamelCase       ,      emb_dim=37							)

							def        __snake_case   (				self			:    str							)      ->       Dict:
														self.config_tester.run_common_tests()

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Optional[int]:
														__snake_case						:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_model(*lowerCamelCase							)

							def        __snake_case   (				self			:    Any							)      ->       str:
														__snake_case						:     Any									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase							)

							def        __snake_case   (				self			:    Any							)      ->       Union[str, Any]:
														__snake_case						:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_simple_qa(*lowerCamelCase							)

							def        __snake_case   (				self			:    List[str]							)      ->       Optional[int]:
														__snake_case						:     Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase							)

							def        __snake_case   (				self			:    int							)      ->       Tuple:
														__snake_case						:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase							)

							def        __snake_case   (				self			:    int							)      ->       Tuple:
														__snake_case						:     int									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_token_classif(*lowerCamelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       int:
														__snake_case						:     Dict									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_multiple_choice(*lowerCamelCase							)

							@slow
							def        __snake_case   (				self			:    Any							)      ->       str:
														for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case						:     Dict									=  FlaubertModel.from_pretrained(lowerCamelCase							)
																					self.assertIsNotNone(lowerCamelCase							)






							@slow
							@require_torch_gpu
							def        __snake_case   (				self			:    str							)      ->       int:
														__snake_case					,     __snake_case						:     List[str]									=  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

																					__snake_case						:     str									=  True
																					__snake_case						:     List[Any]									=  model_class(config=lowerCamelCase							)

																					__snake_case						:     Union[str, Any]									=  self._prepare_for_class(lowerCamelCase       ,      lowerCamelCase							)
																					__snake_case						:     str									=  torch.jit.trace(
																					    lowerCamelCase       ,      (inputs_dict["input_ids"].to("cpu"							), inputs_dict["attention_mask"].to("cpu"							))							)

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

@require_torch
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							@slow
							def        __snake_case   (				self			:    str							)      ->       List[Any]:
														__snake_case						:     int									=  FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"							)
														__snake_case						:     Dict									=  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
														with torch.no_grad():
																					__snake_case						:     List[str]									=  model(lowerCamelCase							)[0]
														__snake_case						:     Any									=  torch.Size((1, 11, 768)							)
														self.assertEqual(output.shape       ,      lowerCamelCase							)
														__snake_case						:     Dict									=  torch.tensor(
														    [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]							)

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




import math


def 					_UpperCAmelCase				(						a__ = 1_0_0):

			'''simple docstring'''



			a_						:						List[str] 							= sum(i * i for i in range(1  ,	n + 1))
			a_						:						Optional[Any] 							= int(math.pow(sum(range(1  ,	n + 1))  ,	2))
			return square_of_sum - sum_of_squares


if __name__ == "__main__":
	print(F"""{solution() = }""")


