




'''simple docstring'''


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    __lowercase						(  __magic_name__ ):







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







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a			=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size      )
     __a			=					random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

     __a			=					self.get_config()

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







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    Union[str, Any]:
     __a			=					FlaubertModel(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							lengths=UpperCamelCase      ,							langs=UpperCamelCase      )
     __a			=					model(UpperCamelCase      ,							langs=UpperCamelCase      )
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    Union[str, Any]:
     __a			=					FlaubertWithLMHeadModel(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					model(UpperCamelCase      ,							token_type_ids=UpperCamelCase      ,							labels=UpperCamelCase      )
     self.parent.assertEqual(result.loss.shape      ,							()      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    Optional[int]:
     __a			=					FlaubertForQuestionAnsweringSimple(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					model(UpperCamelCase      )

     __a			=					model(UpperCamelCase      ,							start_positions=UpperCamelCase      ,							end_positions=UpperCamelCase      )
     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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    Tuple:
     __a			=					FlaubertForQuestionAnswering(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					model(UpperCamelCase      )

     __a			=					model(
         UpperCamelCase      ,							start_positions=UpperCamelCase      ,							end_positions=UpperCamelCase      ,							cls_index=UpperCamelCase      ,							is_impossible=UpperCamelCase      ,							p_mask=UpperCamelCase      ,							)

     __a			=					model(
         UpperCamelCase      ,							start_positions=UpperCamelCase      ,							end_positions=UpperCamelCase      ,							cls_index=UpperCamelCase      ,							is_impossible=UpperCamelCase      ,							)

     ((__a)	,       )			=					result_with_labels.to_tuple()

     __a			=					model(UpperCamelCase      ,							start_positions=UpperCamelCase      ,							end_positions=UpperCamelCase      )

     ((__a)	,       )			=					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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    Tuple:
     __a			=					FlaubertForSequenceClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					model(UpperCamelCase      )
     __a			=					model(UpperCamelCase      ,							labels=UpperCamelCase      )

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







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    str:
     __a			=					self.num_labels
     __a			=					FlaubertForTokenClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							labels=UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.num_labels)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    int:
     __a			=					self.num_choices
     __a			=					FlaubertForMultipleChoice(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					input_ids.unsqueeze(1      ).expand(-1      ,							self.num_choices      ,							-1      ).contiguous()
     __a			=					token_type_ids.unsqueeze(1      ).expand(-1      ,							self.num_choices      ,							-1      ).contiguous()
     __a			=					input_mask.unsqueeze(1      ).expand(-1      ,							self.num_choices      ,							-1      ).contiguous()
     __a			=					model(
         UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							token_type_ids=UpperCamelCase      ,							labels=UpperCamelCase      ,							)
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_choices)      )







   def 		UpperCamelCase__			(	self      )				->    str:
     __a			=					self.prepare_config_and_inputs()
     (
         (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       
     )			=					config_and_inputs
     __a			=					{
         'input_ids': input_ids,
         'token_type_ids': token_type_ids,
         'lengths': input_lengths,
         'attention_mask': input_mask,
     }
     return config, inputs_dict





@require_torch
class    __lowercase						(  __magic_name__       ,							__magic_name__       ,							unittest.TestCase ):
   _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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Union[str, Any]:
     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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase=False      )				->    Optional[Any]:
     __a			=					super()._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      ,							return_labels=UpperCamelCase      )

     if return_labels:
       if model_class.__name__ == "FlaubertForQuestionAnswering":
         __a			=					torch.zeros(
             self.model_tester.batch_size      ,							dtype=torch.long      ,							device=UpperCamelCase      )
         __a			=					torch.zeros(
             self.model_tester.batch_size      ,							dtype=torch.long      ,							device=UpperCamelCase      )

     return inputs_dict







   def 		UpperCamelCase__			(	self      )				->    int:
     __a			=					FlaubertModelTester(self      )
     __a			=					ConfigTester(self      ,							config_class=UpperCamelCase      ,							emb_dim=37      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     self.config_tester.run_common_tests()







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    int:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCamelCase      )







   @slow
   def 		UpperCamelCase__			(	self      )				->    str:
     for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __a			=					FlaubertModel.from_pretrained(UpperCamelCase      )
       self.assertIsNotNone(UpperCamelCase      )







   @slow
   @require_torch_gpu
   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a	,       __a			=					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

       __a			=					True
       __a			=					model_class(config=UpperCamelCase      )

       __a			=					self._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      )
       __a			=					torch.jit.trace(
           UpperCamelCase      ,							(inputs_dict['input_ids'].to('cpu'      ), inputs_dict['attention_mask'].to('cpu'      ))      )

       with tempfile.TemporaryDirectory() as tmp:
         torch.jit.save(UpperCamelCase      ,							os.path.join(UpperCamelCase      ,							'traced_model.pt'      )      )
         __a			=					torch.jit.load(os.path.join(UpperCamelCase      ,							'traced_model.pt'      )      ,							map_location=UpperCamelCase      )
         loaded(inputs_dict['input_ids'].to(UpperCamelCase      )      ,							inputs_dict['attention_mask'].to(UpperCamelCase      )      )





@require_torch
class    __lowercase						(  unittest.TestCase ):







   @slow
   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'      )
     __a			=					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
     with torch.no_grad():
       __a			=					model(UpperCamelCase      )[0]
     __a			=					torch.Size((1, 11, 768)      )
     self.assertEqual(output.shape      ,							UpperCamelCase      )
     __a			=					torch.tensor(
         [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]]      )

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









'''simple docstring'''


from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


UpperCAmelCase_ 		=      logging.get_logger(__name__)


UpperCAmelCase_ 		=      TypeVar("DatasetType", Dataset, IterableDataset)


def 						SCREAMING_SNAKE_CASE       (      a_	:							List[DatasetType]				,					a_	:							Optional[List[float]] = None				,					a_	:							Optional[int] = None				,					a_	:							Optional[DatasetInfo] = None				,					a_	:							Optional[NamedSplit] = None				,					a_	:							Literal["first_exhausted", "all_exhausted"] = "first_exhausted"				,					):
  from .arrow_dataset import Dataset
  from .iterable_dataset import IterableDataset

  if not datasets:
    raise ValueError('Unable to interleave an empty list of datasets.'			)
  for i, dataset in enumerate(a_			):
    if not isinstance(a_				,					(Dataset, IterableDataset)			):
      if isinstance(a_				,					(DatasetDict, IterableDatasetDict)			):
        if not dataset:
          raise ValueError(
              f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
              'is an empty dataset dictionary.'			)
        raise ValueError(
            f"Dataset at position {i} has at least one split: {list(a_			)}\n"
            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(a_			)			)}']"			)
      raise ValueError(
          f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(a_			).__name__}."			)
    if i == 0:
      __a	,       __a			=					(
          (Dataset, IterableDataset) if isinstance(a_				,					a_			) else (IterableDataset, Dataset)
      )
    elif not isinstance(a_				,					a_			):
      raise ValueError(
          f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."			)
  if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
    raise ValueError(f"{stopping_strategy} is not supported. Please enter a valid stopping_strategy."			)
  if dataset_type is Dataset:
    return _interleave_map_style_datasets(
        a_				,					a_				,					a_				,					info=a_				,					split=a_				,					stopping_strategy=a_			)
  else:
    return _interleave_iterable_datasets(
        a_				,					a_				,					a_				,					info=a_				,					split=a_				,					stopping_strategy=a_			)


def 						SCREAMING_SNAKE_CASE       (      a_	:							List[DatasetType]				,					a_	:							Optional[DatasetInfo] = None				,					a_	:							Optional[NamedSplit] = None				,					a_	:							int = 0				,					):

  if not dsets:
    raise ValueError('Unable to concatenate an empty list of datasets.'			)
  for i, dataset in enumerate(a_			):
    if not isinstance(a_				,					(Dataset, IterableDataset)			):
      if isinstance(a_				,					(DatasetDict, IterableDatasetDict)			):
        if not dataset:
          raise ValueError(
              f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
              'is an empty dataset dictionary.'			)
        raise ValueError(
            f"Dataset at position {i} has at least one split: {list(a_			)}\n"
            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(a_			)			)}']"			)
      raise ValueError(
          f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(a_			).__name__}."			)
    if i == 0:
      __a	,       __a			=					(
          (Dataset, IterableDataset) if isinstance(a_				,					a_			) else (IterableDataset, Dataset)
      )
    elif not isinstance(a_				,					a_			):
      raise ValueError(
          f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."			)
  if dataset_type is Dataset:
    return _concatenate_map_style_datasets(a_				,					info=a_				,					split=a_				,					axis=a_			)
  else:
    return _concatenate_iterable_datasets(a_				,					info=a_				,					split=a_				,					axis=a_			)



