


import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType



@require_torch
class     snake_case__  :
				__lowerCAmelCase		=      ()
				__lowerCAmelCase		=      True
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[int]				=      self.config_class

										# test default config
										__magic_name__				:							Any				=      config_class()
										self.assertIsNotNone(_UpperCAmelCase      )
										__magic_name__				:							Any				=      ['stem'] + [F"stage{idx}" for idx in range(1     ,      len(config.depths      ) + 1      )]
										self.assertEqual(config.stage_names     ,      _UpperCAmelCase      )
										self.assertTrue(set(config.out_features      ).issubset(set(config.stage_names      )      )      )

										# Test out_features and out_indices are correctly set
										# out_features and out_indices both None
										__magic_name__				:							List[str]				=      config_class(out_features=_UpperCAmelCase     ,      out_indices=_UpperCAmelCase      )
										self.assertEqual(config.out_features     ,      [config.stage_names[-1]]      )
										self.assertEqual(config.out_indices     ,      [len(config.stage_names      ) - 1]      )

										# out_features and out_indices both set
										__magic_name__				:							Any				=      config_class(out_features=['stem', 'stage1']     ,      out_indices=[0, 1]      )
										self.assertEqual(config.out_features     ,      ['stem', 'stage1']      )
										self.assertEqual(config.out_indices     ,      [0, 1]      )

										# Only out_features set
										__magic_name__				:							Optional[int]				=      config_class(out_features=['stage1', 'stage3']      )
										self.assertEqual(config.out_features     ,      ['stage1', 'stage3']      )
										self.assertEqual(config.out_indices     ,      [1, 3]      )

										# Only out_indices set
										__magic_name__				:							Optional[int]				=      config_class(out_indices=[0, 2]      )
										self.assertEqual(config.out_features     ,      [config.stage_names[0], config.stage_names[2]]      )
										self.assertEqual(config.out_indices     ,      [0, 2]      )

										# Error raised when out_indices do not correspond to out_features
										with self.assertRaises(_UpperCAmelCase      ):
																__magic_name__				:							Any				=      config_class(out_features=['stage1', 'stage2']     ,      out_indices=[0, 2]      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							Union[str, Any]				=      self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__				:							Optional[Any]				=      model_class(_UpperCAmelCase      )
																__magic_name__				:							Optional[Any]				=      inspect.signature(model.forward      )
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__magic_name__				:							Tuple				=      [*signature.parameters.keys()]
																__magic_name__				:							int				=      ['pixel_values']
																self.assertListEqual(arg_names[:1]     ,      _UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							Union[str, Any]				=      self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__				:							int				=      model_class(_UpperCAmelCase      )
																self.assertEqual(len(model.channels      )     ,      len(config.out_features      )      )
																__magic_name__				:							List[str]				=      model.num_features
																__magic_name__				:							Optional[int]				=      [config.stage_names.index(_UpperCAmelCase      ) for feat in config.out_features]
																__magic_name__				:							Dict				=      [num_features[idx] for idx in out_indices]
																self.assertListEqual(model.channels     ,      _UpperCAmelCase      )

																__magic_name__				:							Optional[int]				=      copy.deepcopy(_UpperCAmelCase      )
																__magic_name__				:							int				=      None
																__magic_name__				:							str				=      model_class(_UpperCAmelCase      )
																self.assertEqual(len(model.channels      )     ,      1      )
																self.assertListEqual(model.channels     ,      [num_features[-1]]      )

																__magic_name__				:							str				=      copy.deepcopy(_UpperCAmelCase      )
																__magic_name__				:							str				=      None
																__magic_name__				:							Optional[Any]				=      model_class(_UpperCAmelCase      )
																self.assertEqual(len(model.channels      )     ,      1      )
																self.assertListEqual(model.channels     ,      [num_features[-1]]      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							int				=      self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__				:							Tuple				=      model_class(_UpperCAmelCase      )
																model.to(_UpperCAmelCase      )
																model.eval()
																__magic_name__				:							Optional[Any]				=      model(**_UpperCAmelCase      )

																self.assertEqual(len(result.feature_maps      )     ,      len(config.out_features      )      )
																self.assertEqual(len(model.channels      )     ,      len(config.out_features      )      )
																self.assertEqual(len(result.feature_maps      )     ,      len(config.out_indices      )      )
																self.assertEqual(len(model.channels      )     ,      len(config.out_indices      )      )

																# Check output of last stage is taken if out_features=None, out_indices=None
																__magic_name__				:							List[str]				=      copy.deepcopy(_UpperCAmelCase      )
																__magic_name__				:							List[str]				=      None
																__magic_name__				:							Any				=      model_class(_UpperCAmelCase      )
																model.to(_UpperCAmelCase      )
																model.eval()
																__magic_name__				:							Optional[int]				=      model(**_UpperCAmelCase      )

																self.assertEqual(len(result.feature_maps      )     ,      1      )
																self.assertEqual(len(model.channels      )     ,      1      )

																__magic_name__				:							Union[str, Any]				=      copy.deepcopy(_UpperCAmelCase      )
																__magic_name__				:							Optional[int]				=      None
																__magic_name__				:							int				=      model_class(_UpperCAmelCase      )
																model.to(_UpperCAmelCase      )
																model.eval()
																__magic_name__				:							Tuple				=      model(**_UpperCAmelCase      )

																self.assertEqual(len(result.feature_maps      )     ,      1      )
																self.assertEqual(len(model.channels      )     ,      1      )

																# Check backbone can be initialized with fresh weights
																__magic_name__				:							Union[str, Any]				=      copy.deepcopy(_UpperCAmelCase      )
																__magic_name__				:							Optional[int]				=      False
																__magic_name__				:							Union[str, Any]				=      model_class(_UpperCAmelCase      )
																model.to(_UpperCAmelCase      )
																model.eval()
																__magic_name__				:							int				=      model(**_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							Optional[Any]				=      self.model_tester.prepare_config_and_inputs_for_common()

										for backbone_class in self.all_model_classes:
																__magic_name__				:							str				=      backbone_class(_UpperCAmelCase      )

																self.assertTrue(hasattr(_UpperCAmelCase     ,      'backbone_type'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'stage_names'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'num_features'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'out_indices'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'out_features'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'out_feature_channels'      )      )
																self.assertTrue(hasattr(_UpperCAmelCase     ,      'channels'      )      )

																self.assertIsInstance(backbone.backbone_type     ,      _UpperCAmelCase      )
																# Verify num_features has been initialized in the backbone init
																self.assertIsNotNone(backbone.num_features      )
																self.assertTrue(len(backbone.channels      ) == len(backbone.out_indices      )      )
																self.assertTrue(len(backbone.stage_names      ) == len(backbone.num_features      )      )
																self.assertTrue(len(backbone.channels      ) <= len(backbone.num_features      )      )
																self.assertTrue(len(backbone.out_feature_channels      ) == len(backbone.stage_names      )      )





				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							Any				=      self.model_tester.prepare_config_and_inputs_for_common()
										__magic_name__				:							Any				=      inputs_dict['pixel_values'].shape[0]

										for backbone_class in self.all_model_classes:
																__magic_name__				:							Tuple				=      backbone_class(_UpperCAmelCase      )
																backbone.to(_UpperCAmelCase      )
																backbone.eval()

																__magic_name__				:							Dict				=      backbone(**_UpperCAmelCase      )

																# Test default outputs and verify feature maps
																self.assertIsInstance(outputs.feature_maps     ,      _UpperCAmelCase      )
																self.assertTrue(len(outputs.feature_maps      ) == len(backbone.channels      )      )
																for feature_map, n_channels in zip(outputs.feature_maps     ,      backbone.channels      ):
																						self.assertTrue(feature_map.shape[:2]     ,      (batch_size, n_channels)      )
																self.assertIsNone(outputs.hidden_states      )
																self.assertIsNone(outputs.attentions      )

																# Test output_hidden_states=True
																__magic_name__				:							List[str]				=      backbone(**_UpperCAmelCase     ,      output_hidden_states=_UpperCAmelCase      )
																self.assertIsNotNone(outputs.hidden_states      )
																self.assertTrue(len(outputs.hidden_states      )     ,      len(backbone.stage_names      )      )
																for hidden_state, n_channels in zip(outputs.hidden_states     ,      backbone.channels      ):
																						self.assertTrue(hidden_state.shape[:2]     ,      (batch_size, n_channels)      )

																# Test output_attentions=True
																if self.has_attentions:
																						__magic_name__				:							List[Any]				=      backbone(**_UpperCAmelCase     ,      output_attentions=_UpperCAmelCase      )
																						self.assertIsNotNone(outputs.attentions      )






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


snake_case__     							= {
    'configuration_flaubert': ['FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FlaubertConfig', 'FlaubertOnnxConfig'],
    'tokenization_flaubert': ['FlaubertTokenizer'],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			snake_case__     							= [
			    'FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'FlaubertForMultipleChoice',
			    'FlaubertForQuestionAnswering',
			    'FlaubertForQuestionAnsweringSimple',
			    'FlaubertForSequenceClassification',
			    'FlaubertForTokenClassification',
			    'FlaubertModel',
			    'FlaubertWithLMHeadModel',
			    'FlaubertPreTrainedModel',
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			snake_case__     							= [
			    'TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'TFFlaubertForMultipleChoice',
			    'TFFlaubertForQuestionAnsweringSimple',
			    'TFFlaubertForSequenceClassification',
			    'TFFlaubertForTokenClassification',
			    'TFFlaubertModel',
			    'TFFlaubertPreTrainedModel',
			    'TFFlaubertWithLMHeadModel',
			]


if TYPE_CHECKING:
			from .configuration_flaubert import FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, FlaubertConfig, FlaubertOnnxConfig
			from .tokenization_flaubert import FlaubertTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_flaubert import (
						    FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    FlaubertForMultipleChoice,
						    FlaubertForQuestionAnswering,
						    FlaubertForQuestionAnsweringSimple,
						    FlaubertForSequenceClassification,
						    FlaubertForTokenClassification,
						    FlaubertModel,
						    FlaubertPreTrainedModel,
						    FlaubertWithLMHeadModel,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_flaubert import (
						    TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFFlaubertForMultipleChoice,
						    TFFlaubertForQuestionAnsweringSimple,
						    TFFlaubertForSequenceClassification,
						    TFFlaubertForTokenClassification,
						    TFFlaubertModel,
						    TFFlaubertPreTrainedModel,
						    TFFlaubertWithLMHeadModel,
						)

else:
			import sys

			snake_case__     							= _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


