



'''simple docstring'''

import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
							from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import DPTImageProcessor

class 	__magic_name__						:







							def __init__(							self					:       Union[str, Any]	,_UpperCAmelCase					:       Dict	,_UpperCAmelCase					:       Tuple=2	,_UpperCAmelCase					:       Optional[int]=32	,_UpperCAmelCase					:       Any=16	,_UpperCAmelCase					:       Tuple=3	,_UpperCAmelCase					:       Any=True	,_UpperCAmelCase					:       List[Any]=True	,_UpperCAmelCase					:       Union[str, Any]=32	,_UpperCAmelCase					:       Optional[int]=4	,_UpperCAmelCase					:       str=[0, 1, 2, 3]	,_UpperCAmelCase					:       Optional[Any]=4	,_UpperCAmelCase					:       int=37	,_UpperCAmelCase					:       int="gelu"	,_UpperCAmelCase					:       Tuple=0.1	,_UpperCAmelCase					:       Dict=0.1	,_UpperCAmelCase					:       List[Any]=0.02	,_UpperCAmelCase					:       str=3	,_UpperCAmelCase					:       Dict=[1, 384, 24, 24]	,_UpperCAmelCase					:       str=True	,_UpperCAmelCase					:       Any=None	,):
									_a     :      Optional[Any]        =							parent
									_a     :      Any        =							batch_size
									_a     :      str        =							image_size
									_a     :      Any        =							patch_size
									_a     :      Dict        =							num_channels
									_a     :      int        =							is_training
									_a     :      str        =							use_labels
									_a     :      List[Any]        =							hidden_size
									_a     :      Dict        =							num_hidden_layers
									_a     :      int        =							backbone_out_indices
									_a     :      Any        =							num_attention_heads
									_a     :      Dict        =							intermediate_size
									_a     :      Dict        =							hidden_act
									_a     :      Optional[int]        =							hidden_dropout_prob
									_a     :      Dict        =							attention_probs_dropout_prob
									_a     :      Union[str, Any]        =							initializer_range
									_a     :      Tuple        =							num_labels
									_a     :      Any        =							backbone_featmap_shape
									_a     :      Any        =							scope
									_a     :      Dict        =							is_hybrid
									# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
									_a     :      List[str]        =							(image_size // patch_size) ** 2
									_a     :      Tuple        =							num_patches + 1







							def 							__lowercase			(							self					:       Optional[Any]		):
									_a     :      Dict        =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

									_a     :      Dict        =							None
									if self.use_labels:
											_a     :      Optional[int]        =							ids_tensor([self.batch_size, self.image_size, self.image_size]	,self.num_labels		)

									_a     :      Any        =							self.get_config()

									return config, pixel_values, labels







							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      Union[str, Any]        =							{
									    'global_padding': 'same',
									    'layer_type': 'bottleneck',
									    'depths': [3, 4, 9],
									    'out_features': ['stage1', 'stage2', 'stage3'],
									    'embedding_dynamic_padding': True,
									    'hidden_sizes': [96, 192, 384, 768],
									    'num_groups': 2,
									}

									return DPTConfig(
									    image_size=self.image_size	,patch_size=self.patch_size	,num_channels=self.num_channels	,hidden_size=self.hidden_size	,num_hidden_layers=self.num_hidden_layers	,backbone_out_indices=self.backbone_out_indices	,num_attention_heads=self.num_attention_heads	,intermediate_size=self.intermediate_size	,hidden_act=self.hidden_act	,hidden_dropout_prob=self.hidden_dropout_prob	,attention_probs_dropout_prob=self.attention_probs_dropout_prob	,is_decoder=_UpperCAmelCase	,initializer_range=self.initializer_range	,is_hybrid=self.is_hybrid	,backbone_config=_UpperCAmelCase	,backbone_featmap_shape=self.backbone_featmap_shape	,)







							def 							__lowercase			(							self					:       Any	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       Union[str, Any]	,_UpperCAmelCase					:       Optional[int]		):
									_a     :      Tuple        =							DPTModel(config=_UpperCAmelCase		)
									model.to(_UpperCAmelCase		)
									model.eval()
									_a     :      Optional[int]        =							model(_UpperCAmelCase		)
									self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size)		)







							def 							__lowercase			(							self					:       str	,_UpperCAmelCase					:       Dict	,_UpperCAmelCase					:       List[str]	,_UpperCAmelCase					:       Union[str, Any]		):
									_a     :      Dict        =							self.num_labels
									_a     :      List[Any]        =							DPTForDepthEstimation(_UpperCAmelCase		)
									model.to(_UpperCAmelCase		)
									model.eval()
									_a     :      Union[str, Any]        =							model(_UpperCAmelCase		)
									self.parent.assertEqual(result.predicted_depth.shape	,(self.batch_size, self.image_size, self.image_size)		)







							def 							__lowercase			(							self					:       str	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       str		):
									_a     :      Union[str, Any]        =							self.num_labels
									_a     :      Union[str, Any]        =							DPTForSemanticSegmentation(_UpperCAmelCase		)
									model.to(_UpperCAmelCase		)
									model.eval()
									_a     :      Tuple        =							model(_UpperCAmelCase	,labels=_UpperCAmelCase		)
									self.parent.assertEqual(
									    result.logits.shape	,(self.batch_size, self.num_labels, self.image_size, self.image_size)		)







							def 							__lowercase			(							self					:       Dict		):
									_a     :      Dict        =							self.prepare_config_and_inputs()
									_a       ,					_a       ,					_a     :      Any        =							config_and_inputs
									_a     :      Optional[Any]        =							{'pixel_values': pixel_values}
									return config, inputs_dict

@require_torch
class 	__magic_name__						(    _UpperCamelCase			,						_UpperCamelCase			,						unittest.TestCase   ):
							lowerCAmelCase       :    List[Any]   				=					(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
							lowerCAmelCase       :    Tuple   				=					(
							    {
							        'depth-estimation': DPTForDepthEstimation,
							        'feature-extraction': DPTModel,
							        'image-segmentation': DPTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							lowerCAmelCase       :    Dict   				=					False
							lowerCAmelCase       :    Any   				=					False
							lowerCAmelCase       :    Optional[Any]   				=					False







							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      Union[str, Any]        =							DPTModelTester(self		)
									_a     :      List[Any]        =							ConfigTester(self	,config_class=_UpperCAmelCase	,has_text_modality=_UpperCAmelCase	,hidden_size=37		)







							def 							__lowercase			(							self					:       Dict		):
									self.config_tester.run_common_tests()







							@unittest.skip(reason='DPT does not use inputs_embeds'		)
							def 							__lowercase			(							self					:       List[Any]		):
									pass







							def 							__lowercase			(							self					:       str		):
									_a       ,					_a     :      List[str]        =							self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a     :      str        =							model_class(_UpperCAmelCase		)
											self.assertIsInstance(model.get_input_embeddings()	,(nn.Module)		)
											_a     :      List[Any]        =							model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(_UpperCAmelCase	,nn.Linear		)		)







							def 							__lowercase			(							self					:       Dict		):
									_a       ,					_a     :      List[str]        =							self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a     :      str        =							model_class(_UpperCAmelCase		)
											_a     :      List[str]        =							inspect.signature(model.forward		)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											_a     :      str        =							[*signature.parameters.keys()]

											_a     :      Optional[Any]        =							['pixel_values']
											self.assertListEqual(arg_names[:1]	,_UpperCAmelCase		)







							def 							__lowercase			(							self					:       Any		):
									_a     :      List[str]        =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase		)







							def 							__lowercase			(							self					:       List[str]		):
									_a     :      Dict        =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_depth_estimation(*_UpperCAmelCase		)







							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      List[Any]        =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCAmelCase		)







							def 							__lowercase			(							self					:       str		):
									for model_class in self.all_model_classes:
											if model_class.__name__ == "DPTForDepthEstimation":
													continue

											_a       ,					_a     :      Optional[int]        =							self.model_tester.prepare_config_and_inputs_for_common()
											_a     :      Optional[Any]        =							True

											if model_class in get_values(_UpperCAmelCase		):
													continue

											_a     :      List[Any]        =							model_class(_UpperCAmelCase		)
											model.to(_UpperCAmelCase		)
											model.train()
											_a     :      int        =							self._prepare_for_class(_UpperCAmelCase	,_UpperCAmelCase	,return_labels=_UpperCAmelCase		)
											_a     :      str        =							model(**_UpperCAmelCase		).loss
											loss.backward()







							def 							__lowercase			(							self					:       Optional[Any]		):
									for model_class in self.all_model_classes:
											if model_class.__name__ == "DPTForDepthEstimation":
													continue

											_a       ,					_a     :      Tuple        =							self.model_tester.prepare_config_and_inputs_for_common()
											_a     :      Optional[int]        =							False
											_a     :      Optional[int]        =							True

											if model_class in get_values(_UpperCAmelCase		) or not model_class.supports_gradient_checkpointing:
													continue
											_a     :      Any        =							model_class(_UpperCAmelCase		)
											model.to(_UpperCAmelCase		)
											model.gradient_checkpointing_enable()
											model.train()
											_a     :      Optional[Any]        =							self._prepare_for_class(_UpperCAmelCase	,_UpperCAmelCase	,return_labels=_UpperCAmelCase		)
											_a     :      str        =							model(**_UpperCAmelCase		).loss
											loss.backward()







							def 							__lowercase			(							self					:       Optional[Any]		):
									_a       ,					_a     :      Optional[Any]        =							self.model_tester.prepare_config_and_inputs_for_common()

									_a     :      List[str]        =							_config_zero_init(_UpperCAmelCase		)
									for model_class in self.all_model_classes:
											_a     :      Optional[Any]        =							model_class(config=_UpperCAmelCase		)
											# Skip the check for the backbone
											_a     :      Optional[Any]        =							[]
											for name, module in model.named_modules():
													if module.__class__.__name__ == "DPTViTHybridEmbeddings":
															_a     :      int        =							[F"""{name}.{key}""" for key in module.state_dict().keys()]
															break

											for name, param in model.named_parameters():
													if param.requires_grad:
															if name in backbone_params:
																	continue
															self.assertIn(
															    ((param.data.mean() * 1E9).round() / 1E9).item()	,[0.0, 1.0]	,msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""	,)







							@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
							def 							__lowercase			(							self					:       Tuple		):
									pass







							@slow
							def 							__lowercase			(							self					:       Tuple		):
									for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
											_a     :      int        =							DPTModel.from_pretrained(_UpperCAmelCase		)
											self.assertIsNotNone(_UpperCAmelCase		)







							def 							__lowercase			(							self					:       str		):
									# We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type
									_a       ,					_a     :      Any        =							self.model_tester.prepare_config_and_inputs_for_common()
									_a     :      int        =							'add'
									with self.assertRaises(_UpperCAmelCase		):
											_a     :      Dict        =							DPTForDepthEstimation(_UpperCAmelCase		)



def      __lowerCamelCase     (      )			->      Tuple:
		_a     :      List[Any]        =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
		return image

@require_torch
@require_vision
@slow
class 	__magic_name__						(    unittest.TestCase   ):







							def 							__lowercase			(							self					:       str		):
									_a     :      Optional[int]        =							DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas'		)
									_a     :      List[Any]        =							DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas'		).to(_UpperCAmelCase		)

									_a     :      Optional[int]        =							prepare_img()
									_a     :      str        =							image_processor(images=_UpperCAmelCase	,return_tensors='pt'		).to(_UpperCAmelCase		)

									# forward pass
									with torch.no_grad():
											_a     :      int        =							model(**_UpperCAmelCase		)
											_a     :      Union[str, Any]        =							outputs.predicted_depth

									# verify the predicted depth
									_a     :      List[str]        =							torch.Size((1, 384, 384)		)
									self.assertEqual(predicted_depth.shape	,_UpperCAmelCase		)

									_a     :      Optional[Any]        =							torch.tensor(
									    [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]]		).to(_UpperCAmelCase		)

									self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100	,_UpperCAmelCase	,atol=1E-4		)		)











"""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					(					_UpperCAmelCase					):





      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      ,						)		->     int:
            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 )		->     int:
            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 )		->     List[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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Any:
            lowerCAmelCase  						=       FlaubertModel(config=lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       model(lowercase      ,						lengths=lowercase      ,						langs=lowercase )
            lowerCAmelCase  						=       model(lowercase      ,						langs=lowercase )
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Tuple:
            lowerCAmelCase  						=       FlaubertWithLMHeadModel(lowercase )
            model.to(lowercase )
            model.eval()

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

            lowerCAmelCase  						=       model(lowercase )

            lowerCAmelCase  						=       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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Dict:
            lowerCAmelCase  						=       FlaubertForQuestionAnswering(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       model(lowercase )

            lowerCAmelCase  						=       model(
                lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase      ,						cls_index=lowercase      ,						is_impossible=lowercase      ,						p_mask=lowercase      ,						)

            lowerCAmelCase  						=       model(
                lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase      ,						cls_index=lowercase      ,						is_impossible=lowercase      ,						)

            ((lowerCAmelCase)						,		)  						=       result_with_labels.to_tuple()

            lowerCAmelCase  						=       model(lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase )

            ((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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     int:
            lowerCAmelCase  						=       FlaubertForSequenceClassification(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       model(lowercase )
            lowerCAmelCase  						=       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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     int:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       FlaubertForTokenClassification(lowercase )
            model.to(lowercase )
            model.eval()

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





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Tuple:
            lowerCAmelCase  						=       self.num_choices
            lowerCAmelCase  						=       FlaubertForMultipleChoice(config=lowercase )
            model.to(lowercase )
            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(
                lowercase      ,						attention_mask=lowercase      ,						token_type_ids=lowercase      ,						labels=lowercase      ,						)
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_choices) )





      def     _snake_case			(       self )		->     List[Any]:
            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 		lowercase					(					_UpperCAmelCase     ,  _UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _SCREAMING_SNAKE_CASE					=      (
          {
              '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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Optional[int]:
            if (
                pipeline_test_casse_name == "QAPipelineTests"
                and tokenizer_name is not None
                and not tokenizer_name.endswith("""Fast""" )
            ):
                  # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                  # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                  # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                  return True

            return False





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase=False )		->     Optional[Any]:
            lowerCAmelCase  						=       super()._prepare_for_class(lowercase      ,						lowercase      ,						return_labels=lowercase )

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

            return inputs_dict





      def     _snake_case			(       self )		->     List[str]:
            lowerCAmelCase  						=       FlaubertModelTester(self )
            lowerCAmelCase  						=       ConfigTester(self      ,						config_class=lowercase      ,						emb_dim=37 )





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





      def     _snake_case			(       self )		->     Tuple:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_model(*lowercase )





      def     _snake_case			(       self )		->     Tuple:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_lm_head(*lowercase )





      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_simple_qa(*lowercase )





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





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_token_classif(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase )





      @slow
      def     _snake_case			(       self )		->     Tuple:
            for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase  						=       FlaubertModel.from_pretrained(lowercase )
                  self.assertIsNotNone(lowercase )





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

                  lowerCAmelCase  						=       self._prepare_for_class(lowercase      ,						lowercase )
                  lowerCAmelCase  						=       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""" ) )
                        lowerCAmelCase  						=       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 		lowercase					(					unittest.TestCase					):





      @slow
      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased""" )
            lowerCAmelCase  						=       torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] )
            with torch.no_grad():
                  lowerCAmelCase  						=       model(lowercase )[0]
            lowerCAmelCase  						=       torch.Size((1, 11, 768) )
            self.assertEqual(output.shape      ,						lowercase )
            lowerCAmelCase  						=       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 ) )






