'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers.models.distilbert.modeling_tf_distilbert import (
          TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFDistilBertForMaskedLM,
          TFDistilBertForMultipleChoice,
          TFDistilBertForQuestionAnswering,
          TFDistilBertForSequenceClassification,
          TFDistilBertForTokenClassification,
          TFDistilBertModel,
      )





class      lowerCAmelCase						:



     def __init__(					self				,      lowerCAmelCase__				,      ):
            _A=	parent
            _A=	13
            _A=	7
            _A=	True
            _A=	True
            _A=	False
            _A=	True
            _A=	99
            _A=	32
            _A=	2
            _A=	4
            _A=	37
            _A=	'gelu'
            _A=	0.1
            _A=	0.1
            _A=	512
            _A=	16
            _A=	2
            _A=	0.02
            _A=	3
            _A=	4
            _A=	None



     def 			a__  (					self       ):
            _A=	ids_tensor([self.batch_size, self.seq_length]				,      self.vocab_size       )

            _A=	None
            if self.use_input_mask:
                   _A=	random_attention_mask([self.batch_size, self.seq_length]       )

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

            _A=	DistilBertConfig(
                vocab_size=self.vocab_size				,      dim=self.hidden_size				,      n_layers=self.num_hidden_layers				,      n_heads=self.num_attention_heads				,      hidden_dim=self.intermediate_size				,      hidden_act=self.hidden_act				,      dropout=self.hidden_dropout_prob				,      attention_dropout=self.attention_probs_dropout_prob				,      max_position_embeddings=self.max_position_embeddings				,      initializer_range=self.initializer_range				,      )

            return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	TFDistilBertModel(config=UpperCamelCase__       )
            _A=	{'input_ids': input_ids, 'attention_mask': input_mask}

            _A=	model(UpperCamelCase__       )

            _A=	[input_ids, input_mask]

            _A=	model(UpperCamelCase__       )

            self.parent.assertEqual(result.last_hidden_state.shape				,      (self.batch_size, self.seq_length, self.hidden_size)       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	TFDistilBertForMaskedLM(config=UpperCamelCase__       )
            _A=	{'input_ids': input_ids, 'attention_mask': input_mask}
            _A=	model(UpperCamelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.vocab_size)       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	TFDistilBertForQuestionAnswering(config=UpperCamelCase__       )
            _A=	{
                'input_ids': input_ids,
                'attention_mask': input_mask,
            }
            _A=	model(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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_labels
            _A=	TFDistilBertForSequenceClassification(UpperCamelCase__       )
            _A=	{'input_ids': input_ids, 'attention_mask': input_mask}
            _A=	model(UpperCamelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_labels)       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_choices
            _A=	TFDistilBertForMultipleChoice(UpperCamelCase__       )
            _A=	tf.tile(tf.expand_dims(UpperCamelCase__				,      1       )				,      (1, self.num_choices, 1)       )
            _A=	tf.tile(tf.expand_dims(UpperCamelCase__				,      1       )				,      (1, self.num_choices, 1)       )
            _A=	{
                'input_ids': multiple_choice_inputs_ids,
                'attention_mask': multiple_choice_input_mask,
            }
            _A=	model(UpperCamelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_choices)       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_labels
            _A=	TFDistilBertForTokenClassification(UpperCamelCase__       )
            _A=	{'input_ids': input_ids, 'attention_mask': input_mask}
            _A=	model(UpperCamelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.seq_length, self.num_labels)       )





     def 			a__  (					self       ):
            _A=	self.prepare_config_and_inputs()
            ((_A),			(_A),			(_A),			(_A),			(_A),			(_A))=	config_and_inputs
            _A=	{'input_ids': input_ids, 'attention_mask': input_mask}
            return config, inputs_dict





@require_tf
class      lowerCAmelCase						(					_a			,		_a			,		unittest.TestCase			):
     _SCREAMING_SNAKE_CASE     :     Optional[int]		=(
         (
             TFDistilBertModel,
             TFDistilBertForMaskedLM,
             TFDistilBertForQuestionAnswering,
             TFDistilBertForSequenceClassification,
             TFDistilBertForTokenClassification,
             TFDistilBertForMultipleChoice,
         )
         if is_tf_available()
         else None
     )
     _SCREAMING_SNAKE_CASE     :     Union[str, Any]		=(
         {
             "feature-extraction": TFDistilBertModel,
             "fill-mask": TFDistilBertForMaskedLM,
             "question-answering": TFDistilBertForQuestionAnswering,
             "text-classification": TFDistilBertForSequenceClassification,
             "token-classification": TFDistilBertForTokenClassification,
             "zero-shot": TFDistilBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     _SCREAMING_SNAKE_CASE     :     List[Any]		=False
     _SCREAMING_SNAKE_CASE     :     int		=False



     def 			a__  (					self       ):
            _A=	TFDistilBertModelTester(self       )
            _A=	ConfigTester(self				,      config_class=UpperCamelCase__				,      dim=37       )



     def 			a__  (					self       ):
            self.config_tester.run_common_tests()



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__       )





     @slow
     def 			a__  (					self       ):





            for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]       ):
                   _A=	TFDistilBertModel.from_pretrained(UpperCamelCase__       )
                   self.assertIsNotNone(UpperCamelCase__       )





@require_tf
class      lowerCAmelCase						(					unittest.TestCase			):



     @slow
     def 			a__  (					self       ):
            _A=	TFDistilBertModel.from_pretrained('distilbert-base-uncased'       )
            _A=	tf.constant([[0, 1, 2, 3, 4, 5]]       )
            _A=	model(UpperCamelCase__       )[0]

            _A=	[1, 6, 768]
            self.assertEqual(output.shape				,      UpperCamelCase__       )

            _A=	tf.constant(
                [
                    [
                        [0.19261885, -0.13732955, 0.4119799],
                        [0.22150156, -0.07422661, 0.39037204],
                        [0.22756018, -0.0896414, 0.3701467],
                    ]
                ]       )
            tf.debugging.assert_near(output[:, :3, :3]				,      UpperCamelCase__				,      atol=1E-4       )
import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_								=							logging.get_logger(__name__)

UpperCAmelCase_								=							{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}





class      lowerCAmelCase						(					_a			):
     _SCREAMING_SNAKE_CASE     :     Tuple		="""segformer"""



     def __init__(					self				,      lowerCAmelCase__=3				,      lowerCAmelCase__=4				,      lowerCAmelCase__=[2, 2, 2, 2]				,      lowerCAmelCase__=[8, 4, 2, 1]				,      lowerCAmelCase__=[32, 64, 160, 256]				,      lowerCAmelCase__=[7, 3, 3, 3]				,      lowerCAmelCase__=[4, 2, 2, 2]				,      lowerCAmelCase__=[1, 2, 5, 8]				,      lowerCAmelCase__=[4, 4, 4, 4]				,      lowerCAmelCase__="gelu"				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.02				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=1E-6				,      lowerCAmelCase__=256				,      lowerCAmelCase__=255				,      **lowerCAmelCase__				,      ):
            super().__init__(**lowerCAmelCase__       )

            if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
                   warnings.warn(
                       'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
                       ' removed, as the behaviour will default to that of reshape_last_stage = True.'				,      lowerCAmelCase__				,      )

            _A=	num_channels
            _A=	num_encoder_blocks
            _A=	depths
            _A=	sr_ratios
            _A=	hidden_sizes
            _A=	patch_sizes
            _A=	strides
            _A=	mlp_ratios
            _A=	num_attention_heads
            _A=	hidden_act
            _A=	hidden_dropout_prob
            _A=	attention_probs_dropout_prob
            _A=	classifier_dropout_prob
            _A=	initializer_range
            _A=	drop_path_rate
            _A=	layer_norm_eps
            _A=	decoder_hidden_size
            _A=	kwargs.get('reshape_last_stage'				,      lowerCAmelCase__       )
            _A=	semantic_loss_ignore_index





class      lowerCAmelCase						(					_a			):
     _SCREAMING_SNAKE_CASE     :     List[str]		=version.parse("""1.11"""			)



     @property
     def 			a__  (					self       ):
            return OrderedDict(
                [
                    ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                ]       )



     @property
     def 			a__  (					self       ):
            return 1E-4





     @property
     def 			a__  (					self       ):
            return 12