





'''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 lowerCamelCase						(a__					):







  def __init__(		self     ,     lowercase__     ,     lowercase__=13     ,     lowercase__=7     ,     lowercase__=True     ,     lowercase__=True     ,     lowercase__=True     ,     lowercase__=True     ,     lowercase__=True     ,     lowercase__=False     ,     lowercase__=False     ,     lowercase__=False     ,     lowercase__=2     ,     lowercase__=99     ,     lowercase__=0     ,     lowercase__=32     ,     lowercase__=5     ,     lowercase__=4     ,     lowercase__=0.1     ,     lowercase__=0.1     ,     lowercase__=512     ,     lowercase__=12     ,     lowercase__=2     ,     lowercase__=0.02     ,     lowercase__=3     ,     lowercase__=4     ,     lowercase__="last"     ,     lowercase__=None     ,     lowercase__=None     ,     )		->						Union[str, Any]:






        """simple docstring"""





        _snake_case		:	Union[str, Any]					     =     parent
        _snake_case		:	int					     =     batch_size
        _snake_case		:	Union[str, Any]					     =     seq_length
        _snake_case		:	Tuple					     =     is_training
        _snake_case		:	int					     =     use_input_lengths
        _snake_case		:	List[str]					     =     use_token_type_ids
        _snake_case		:	List[Any]					     =     use_labels
        _snake_case		:	Union[str, Any]					     =     gelu_activation
        _snake_case		:	int					     =     sinusoidal_embeddings
        _snake_case		:	Union[str, Any]					     =     causal
        _snake_case		:	Any					     =     asm
        _snake_case		:	Union[str, Any]					     =     n_langs
        _snake_case		:	int					     =     vocab_size
        _snake_case		:	str					     =     n_special
        _snake_case		:	Optional[int]					     =     hidden_size
        _snake_case		:	Dict					     =     num_hidden_layers
        _snake_case		:	Union[str, Any]					     =     num_attention_heads
        _snake_case		:	Tuple					     =     hidden_dropout_prob
        _snake_case		:	str					     =     attention_probs_dropout_prob
        _snake_case		:	Tuple					     =     max_position_embeddings
        _snake_case		:	List[str]					     =     type_vocab_size
        _snake_case		:	Dict					     =     type_sequence_label_size
        _snake_case		:	Dict					     =     initializer_range
        _snake_case		:	Optional[int]					     =     num_labels
        _snake_case		:	Tuple					     =     num_choices
        _snake_case		:	List[str]					     =     summary_type
        _snake_case		:	Union[str, Any]					     =     use_proj
        _snake_case		:	List[Any]					     =     scope







  def 				UpperCAmelCase_				(		self		)		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	int					     =     ids_tensor([self.batch_size, self.seq_length]     ,     self.vocab_size		)
        _snake_case		:	str					     =     random_attention_mask([self.batch_size, self.seq_length]		)

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

        _snake_case		:	Union[str, Any]					     =     None
        if self.use_token_type_ids:
              _snake_case		:	int					     =     ids_tensor([self.batch_size, self.seq_length]     ,     self.n_langs		)

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

        _snake_case		:	Union[str, Any]					     =     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		)		->						str:






        """simple docstring"""





        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     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						Tuple:






        """simple docstring"""





        _snake_case		:	List[Any]					     =     FlaubertModel(config=lowercase__		)
        model.to(lowercase__		)
        model.eval()
        _snake_case		:	Dict					     =     model(lowercase__     ,     lengths=lowercase__     ,     langs=lowercase__		)
        _snake_case		:	Dict					     =     model(lowercase__     ,     langs=lowercase__		)
        _snake_case		:	Union[str, Any]					     =     model(lowercase__		)
        self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.hidden_size)		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						Any:






        """simple docstring"""





        _snake_case		:	List[str]					     =     FlaubertWithLMHeadModel(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:	Optional[int]					     =     model(lowercase__     ,     token_type_ids=lowercase__     ,     labels=lowercase__		)
        self.parent.assertEqual(result.loss.shape     ,     ()		)
        self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.vocab_size)		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						Tuple:






        """simple docstring"""





        _snake_case		:	List[Any]					     =     FlaubertForQuestionAnsweringSimple(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:	List[str]					     =     model(lowercase__		)

        _snake_case		:	Tuple					     =     model(lowercase__     ,     start_positions=lowercase__     ,     end_positions=lowercase__		)
        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     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						str:






        """simple docstring"""





        _snake_case		:	Union[str, Any]					     =     FlaubertForQuestionAnswering(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:	int					     =     model(lowercase__		)

        _snake_case		:	Dict					     =     model(
            lowercase__     ,     start_positions=lowercase__     ,     end_positions=lowercase__     ,     cls_index=lowercase__     ,     is_impossible=lowercase__     ,     p_mask=lowercase__     ,     )

        _snake_case		:	str					     =     model(
            lowercase__     ,     start_positions=lowercase__     ,     end_positions=lowercase__     ,     cls_index=lowercase__     ,     is_impossible=lowercase__     ,     )

        ((_snake_case)     ,							)		:	Tuple					     =     result_with_labels.to_tuple()

        _snake_case		:	List[Any]					     =     model(lowercase__     ,     start_positions=lowercase__     ,     end_positions=lowercase__		)

        ((_snake_case)     ,							)		:	Dict					     =     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     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						Dict:






        """simple docstring"""





        _snake_case		:	Any					     =     FlaubertForSequenceClassification(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:	List[str]					     =     model(lowercase__		)
        _snake_case		:	str					     =     model(lowercase__     ,     labels=lowercase__		)

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







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	Tuple					     =     self.num_labels
        _snake_case		:	Union[str, Any]					     =     FlaubertForTokenClassification(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:	Optional[int]					     =     model(lowercase__     ,     attention_mask=lowercase__     ,     labels=lowercase__		)
        self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.num_labels)		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     )		->						int:






        """simple docstring"""





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







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	Any					     =     self.prepare_config_and_inputs()
        (
            (
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							(
            _snake_case
        )     ,							
        )		:	Optional[int]					     =     config_and_inputs
        _snake_case		:	Dict					     =     {
            '''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						(a__						,      a__						,      unittest.TestCase					):
  _lowercase  :	Optional[int]       					=				(
      (
          FlaubertModel,
          FlaubertWithLMHeadModel,
          FlaubertForQuestionAnswering,
          FlaubertForQuestionAnsweringSimple,
          FlaubertForSequenceClassification,
          FlaubertForTokenClassification,
          FlaubertForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  _lowercase  :	int       					=				(
      {
          """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     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__		)		->						List[str]:






        """simple docstring"""





        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     ,     lowercase__     ,     lowercase__     ,     lowercase__=False		)		->						List[Any]:






        """simple docstring"""





        _snake_case		:	Optional[int]					     =     super()._prepare_for_class(lowercase__     ,     lowercase__     ,     return_labels=lowercase__		)

        if return_labels:
              if model_class.__name__ == "FlaubertForQuestionAnswering":
                    _snake_case		:	List[Any]					     =     torch.zeros(
                        self.model_tester.batch_size     ,     dtype=torch.long     ,     device=lowercase__		)
                    _snake_case		:	Dict					     =     torch.zeros(
                        self.model_tester.batch_size     ,     dtype=torch.long     ,     device=lowercase__		)

        return inputs_dict







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	Dict					     =     FlaubertModelTester(self		)
        _snake_case		:	Optional[int]					     =     ConfigTester(self     ,     config_class=lowercase__     ,     emb_dim=37		)







  def 				UpperCAmelCase_				(		self		)		->						Optional[int]:






        """simple docstring"""





        self.config_tester.run_common_tests()







  def 				UpperCAmelCase_				(		self		)		->						Any:






        """simple docstring"""





        _snake_case		:	Union[str, Any]					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_model(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





        _snake_case		:	Optional[Any]					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_lm_head(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						Any:






        """simple docstring"""





        _snake_case		:	List[str]					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_simple_qa(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        _snake_case		:	int					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_qa(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	str					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	Any					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_token_classif(*lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        _snake_case		:	Tuple					     =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Optional[int]:






        """simple docstring"""





        for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:	List[str]					     =     FlaubertModel.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)







  @slow
  @require_torch_gpu
  def 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





        _snake_case     ,							_snake_case		:	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		:	List[str]					     =     True
              _snake_case		:	List[Any]					     =     model_class(config=lowercase__		)

              _snake_case		:	str					     =     self._prepare_for_class(lowercase__     ,     lowercase__		)
              _snake_case		:	int					     =     torch.jit.trace(
                  lowercase__     ,     (inputs_dict['''input_ids'''].to('''cpu'''		), inputs_dict['''attention_mask'''].to('''cpu'''		))		)

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





@require_torch
class lowerCamelCase						(unittest.TestCase					):







  @slow
  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        _snake_case		:	Tuple					     =     FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''		)
        _snake_case		:	int					     =     torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
        with torch.no_grad():
              _snake_case		:	str					     =     model(lowercase__		)[0]
        _snake_case		:	int					     =     torch.Size((1, 11, 768)		)
        self.assertEqual(output.shape     ,     lowercase__		)
        _snake_case		:	List[Any]					     =     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]     ,     lowercase__     ,     atol=1E-4		)		)









'''simple docstring'''

# Copyright 2023 The HuggingFace 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.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase					:		Dict		=							{'configuration_timm_backbone': ['TimmBackboneConfig']}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase					:		Union[str, Any]		=							['TimmBackbone']


if TYPE_CHECKING:
   from .configuration_timm_backbone import TimmBackboneConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_timm_backbone import TimmBackbone

else:
   import sys

   UpperCAmelCase					:		Dict		=							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


