





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




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




 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size		)
     _lowerCAmelCase				=      random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

     _lowerCAmelCase				=      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		)    ->							Tuple:
     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			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Optional[Any]:
     _lowerCAmelCase				=      FlaubertModel(config=__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()
     _lowerCAmelCase				=      model(__UpperCamelCase			,     lengths=__UpperCamelCase			,     langs=__UpperCamelCase		)
     _lowerCAmelCase				=      model(__UpperCamelCase			,     langs=__UpperCamelCase		)
     _lowerCAmelCase				=      model(__UpperCamelCase		)
     self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size)		)




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Dict:
     _lowerCAmelCase				=      FlaubertWithLMHeadModel(__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							str:
     _lowerCAmelCase				=      FlaubertForQuestionAnsweringSimple(__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()

     _lowerCAmelCase				=      model(__UpperCamelCase		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Any:
     _lowerCAmelCase				=      FlaubertForQuestionAnswering(__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()

     _lowerCAmelCase				=      model(__UpperCamelCase		)

     _lowerCAmelCase				=      model(
         __UpperCamelCase			,     start_positions=__UpperCamelCase			,     end_positions=__UpperCamelCase			,     cls_index=__UpperCamelCase			,     is_impossible=__UpperCamelCase			,     p_mask=__UpperCamelCase			,     )

     _lowerCAmelCase				=      model(
         __UpperCamelCase			,     start_positions=__UpperCamelCase			,     end_positions=__UpperCamelCase			,     cls_index=__UpperCamelCase			,     is_impossible=__UpperCamelCase			,     )

     ((_lowerCAmelCase   )			,			   )				=      result_with_labels.to_tuple()

     _lowerCAmelCase				=      model(__UpperCamelCase			,     start_positions=__UpperCamelCase			,     end_positions=__UpperCamelCase		)

     ((_lowerCAmelCase   )			,			   )				=      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			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Optional[Any]:
     _lowerCAmelCase				=      FlaubertForSequenceClassification(__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()

     _lowerCAmelCase				=      model(__UpperCamelCase		)
     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Optional[int]:
     _lowerCAmelCase				=      self.num_labels
     _lowerCAmelCase				=      FlaubertForTokenClassification(__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()

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




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     )    ->							Dict:
     _lowerCAmelCase				=      self.num_choices
     _lowerCAmelCase				=      FlaubertForMultipleChoice(config=__UpperCamelCase		)
     model.to(__UpperCamelCase		)
     model.eval()
     _lowerCAmelCase				=      input_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
     _lowerCAmelCase				=      token_type_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
     _lowerCAmelCase				=      input_mask.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
     _lowerCAmelCase				=      model(
         __UpperCamelCase			,     attention_mask=__UpperCamelCase			,     token_type_ids=__UpperCamelCase			,     labels=__UpperCamelCase			,     )
     self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_choices)		)





 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     (
         (
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			(
         _lowerCAmelCase   
     )			,			   
     )				=      config_and_inputs
     _lowerCAmelCase				=      {
         "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_		(		__magic_name__						,__magic_name__						,unittest.TestCase		):
 __lowerCamelCase						:							str         =					(
     (
         FlaubertModel,
         FlaubertWithLMHeadModel,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __lowerCamelCase						:							str         =					(
     {
         "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			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Optional[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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=False		)    ->							Dict:
     _lowerCAmelCase				=      super()._prepare_for_class(__UpperCamelCase			,     __UpperCamelCase			,     return_labels=__UpperCamelCase		)

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

     return inputs_dict




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      FlaubertModelTester(self		)
     _lowerCAmelCase				=      ConfigTester(self			,     config_class=__UpperCamelCase			,     emb_dim=37		)




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




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							List[str]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							List[str]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*__UpperCamelCase		)




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*__UpperCamelCase		)




 @slow
 def 							_snake_case   (	self		)    ->							str:
     for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase				=      FlaubertModel.from_pretrained(__UpperCamelCase		)
         self.assertIsNotNone(__UpperCamelCase		)





 @slow
 @require_torch_gpu
 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase			,			_lowerCAmelCase				=      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

         _lowerCAmelCase				=      True
         _lowerCAmelCase				=      model_class(config=__UpperCamelCase		)

         _lowerCAmelCase				=      self._prepare_for_class(__UpperCamelCase			,     __UpperCamelCase		)
         _lowerCAmelCase				=      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"		)		)
             _lowerCAmelCase				=      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    lowerCAmelCase_		(		unittest.TestCase		):




 @slow
 def 							_snake_case   (	self		)    ->							int:
     _lowerCAmelCase				=      FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"		)
     _lowerCAmelCase				=      torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
     with torch.no_grad():
         _lowerCAmelCase				=      model(__UpperCamelCase		)[0]
     _lowerCAmelCase				=      torch.Size((1, 11, 768)		)
     self.assertEqual(output.shape			,     __UpperCamelCase		)
     _lowerCAmelCase				=      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]			,     __UpperCamelCase			,     atol=1E-4		)		)











'''simple docstring'''


from typing import List, Union

import numpy as np

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       import torch

       from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING

_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

@add_end_docstrings(__magic_name__		)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     *_lowerCAmelCase			,     **_lowerCAmelCase		)    ->							List[Any]:
     super().__init__(*_lowerCAmelCase			,     **_lowerCAmelCase		)
     requires_backends(self			,     "vision"		)
     self.check_model_type(_lowerCAmelCase		)




 def __call__(	self			,     _lowerCAmelCase			,     **_lowerCAmelCase		)    ->							str:
     return super().__call__(_lowerCAmelCase			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Tuple:
     return {}, {}, {}




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      load_image(_lowerCAmelCase		)
     _lowerCAmelCase				=      image.size
     _lowerCAmelCase				=      self.image_processor(images=_lowerCAmelCase			,     return_tensors=self.framework		)
     return model_inputs




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Tuple:
     _lowerCAmelCase				=      self.model(**_lowerCAmelCase		)
     return model_outputs





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Optional[Any]:
     _lowerCAmelCase				=      model_outputs.predicted_depth
     _lowerCAmelCase				=      torch.nn.functional.interpolate(
         predicted_depth.unsqueeze(1		)			,     size=self.image_size[::-1]			,     mode="bicubic"			,     align_corners=_lowerCAmelCase		)
     _lowerCAmelCase				=      prediction.squeeze().cpu().numpy()
     _lowerCAmelCase				=      (output * 255 / np.max(_lowerCAmelCase		)).astype("uint8"		)
     _lowerCAmelCase				=      Image.fromarray(_lowerCAmelCase		)
     _lowerCAmelCase				=      {}
     _lowerCAmelCase				=      predicted_depth
     _lowerCAmelCase				=      depth
     return output_dict


