





import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
       import torch

       from transformers import (
           MODEL_MAPPING,
           SegformerForImageClassification,
           SegformerForSemanticSegmentation,
           SegformerModel,
       )
       from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import SegformerImageProcessor

class  __A(						__UpperCAmelCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.config_class(**self.inputs_dict      )
            self.parent.assertTrue(hasattr(A,       '''hidden_sizes'''      )      )
            self.parent.assertTrue(hasattr(A,       '''num_attention_heads'''      )      )
            self.parent.assertTrue(hasattr(A,       '''num_encoder_blocks'''      )      )






class  __A:

     def __init__(      self,       A,       A=13,       A=64,       A=3,       A=4,       A=[2, 2, 2, 2],       A=[8, 4, 2, 1],       A=[16, 32, 64, 128],       A=[1, 4, 8, 16],       A=[1, 2, 4, 8],       A=True,       A=True,       A="gelu",       A=0.1,       A=0.1,       A=0.02,       A=3,       A=None,       ):





            """simple docstring"""

            _UpperCamelCase              =						parent
            _UpperCamelCase              =						batch_size
            _UpperCamelCase              =						image_size
            _UpperCamelCase              =						num_channels
            _UpperCamelCase              =						num_encoder_blocks
            _UpperCamelCase              =						sr_ratios
            _UpperCamelCase              =						depths
            _UpperCamelCase              =						hidden_sizes
            _UpperCamelCase              =						downsampling_rates
            _UpperCamelCase              =						num_attention_heads
            _UpperCamelCase              =						is_training
            _UpperCamelCase              =						use_labels
            _UpperCamelCase              =						hidden_act
            _UpperCamelCase              =						hidden_dropout_prob
            _UpperCamelCase              =						attention_probs_dropout_prob
            _UpperCamelCase              =						initializer_range
            _UpperCamelCase              =						num_labels
            _UpperCamelCase              =						scope

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

            _UpperCamelCase              =						None
            if self.use_labels:
                   _UpperCamelCase              =						ids_tensor([self.batch_size, self.image_size, self.image_size],       self.num_labels      )

            _UpperCamelCase              =						self.get_config()
            return config, pixel_values, labels

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            return SegformerConfig(
                image_size=self.image_size,       num_channels=self.num_channels,       num_encoder_blocks=self.num_encoder_blocks,       depths=self.depths,       hidden_sizes=self.hidden_sizes,       num_attention_heads=self.num_attention_heads,       hidden_act=self.hidden_act,       hidden_dropout_prob=self.hidden_dropout_prob,       attention_probs_dropout_prob=self.attention_probs_dropout_prob,       initializer_range=self.initializer_range,       )

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						SegformerModel(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A      )
            _UpperCamelCase              =						_UpperCamelCase              =						self.image_size // (self.downsampling_rates[-1] * 2)
            self.parent.assertEqual(
                result.last_hidden_state.shape,       (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)      )

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						self.num_labels
            _UpperCamelCase              =						SegformerForSemanticSegmentation(A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						model(A      )
            self.parent.assertEqual(
                result.logits.shape,       (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)      )
            _UpperCamelCase              =						model(A,       labels=A      )
            self.parent.assertEqual(
                result.logits.shape,       (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)      )
            self.parent.assertGreater(result.loss,       0.0      )

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						1
            _UpperCamelCase              =						SegformerForSemanticSegmentation(config=A      )
            model.to(A      )
            model.eval()
            _UpperCamelCase              =						torch.randint(0,       1,       (self.batch_size, self.image_size, self.image_size)      ).to(A      )
            _UpperCamelCase              =						model(A,       labels=A      )
            self.parent.assertGreater(result.loss,       0.0      )



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.prepare_config_and_inputs()
            _UpperCamelCase       ,		_UpperCamelCase       ,		_UpperCamelCase              =						config_and_inputs
            _UpperCamelCase              =						{'''pixel_values''': pixel_values}
            return config, inputs_dict






@require_torch
class  __A(						__UpperCAmelCase						,			__UpperCAmelCase						,			unittest.TestCase ):
     __A	  =       (
         (
             SegformerModel,
             SegformerForSemanticSegmentation,
             SegformerForImageClassification,
         )
         if is_torch_available()
         else ()
     )
     __A	  =       (
         {
             "feature-extraction": SegformerModel,
             "image-classification": SegformerForImageClassification,
             "image-segmentation": SegformerForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     __A	  =       True
     __A	  =       False
     __A	  =       False
     __A	  =       False

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SegformerModelTester(self      )
            _UpperCamelCase              =						SegformerConfigTester(self,       config_class=A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            self.config_tester.run_common_tests()

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_binary_image_segmentation(*A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_segmentation(*A      )

     @unittest.skip('''SegFormer does not use inputs_embeds'''      )
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            pass

     @unittest.skip('''SegFormer does not have get_input_embeddings method and get_output_embeddings methods'''      )
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            pass

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase       ,		_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs_for_common()

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

                   _UpperCamelCase              =						['''pixel_values''']
                   self.assertListEqual(arg_names[:1],       A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase       ,		_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs_for_common()
            _UpperCamelCase              =						True

            for model_class in self.all_model_classes:
                   _UpperCamelCase              =						True
                   _UpperCamelCase              =						False
                   _UpperCamelCase              =						True
                   _UpperCamelCase              =						model_class(A      )
                   model.to(A      )
                   model.eval()
                   with torch.no_grad():
                          _UpperCamelCase              =						model(**self._prepare_for_class(A,       A      )      )
                   _UpperCamelCase              =						outputs.attentions

                   _UpperCamelCase              =						sum(self.model_tester.depths      )
                   self.assertEqual(len(A      ),       A      )

                   # check that output_attentions also work using config
                   del inputs_dict["output_attentions"]
                   _UpperCamelCase              =						True
                   _UpperCamelCase              =						model_class(A      )
                   model.to(A      )
                   model.eval()
                   with torch.no_grad():
                          _UpperCamelCase              =						model(**self._prepare_for_class(A,       A      )      )
                   _UpperCamelCase              =						outputs.attentions

                   self.assertEqual(len(A      ),       A      )

                   # verify the first attentions (first block, first layer)
                   _UpperCamelCase              =						(self.model_tester.image_size // 4) ** 2
                   _UpperCamelCase              =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                   self.assertListEqual(
                       list(attentions[0].shape[-3:]      ),       [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len],       )

                   # verify the last attentions (last block, last layer)
                   _UpperCamelCase              =						(self.model_tester.image_size // 32) ** 2
                   _UpperCamelCase              =						(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
                   self.assertListEqual(
                       list(attentions[-1].shape[-3:]      ),       [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len],       )
                   _UpperCamelCase              =						len(A      )

                   # Check attention is always last and order is fine
                   _UpperCamelCase              =						True
                   _UpperCamelCase              =						True
                   _UpperCamelCase              =						model_class(A      )
                   model.to(A      )
                   model.eval()
                   with torch.no_grad():
                          _UpperCamelCase              =						model(**self._prepare_for_class(A,       A      )      )

                   self.assertEqual(out_len + 1,       len(A      )      )

                   _UpperCamelCase              =						outputs.attentions

                   self.assertEqual(len(A      ),       A      )
                   # verify the first attentions (first block, first layer)
                   _UpperCamelCase              =						(self.model_tester.image_size // 4) ** 2
                   _UpperCamelCase              =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]      ),       [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len],       )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            def check_hidden_states_output(A,       A,       A      ):
                   _UpperCamelCase              =						model_class(A      )
                   model.to(A      )
                   model.eval()

                   with torch.no_grad():
                          _UpperCamelCase              =						model(**self._prepare_for_class(A,       A      )      )

                   _UpperCamelCase              =						outputs.hidden_states

                   _UpperCamelCase              =						self.model_tester.num_encoder_blocks
                   self.assertEqual(len(A      ),       A      )

                   # verify the first hidden states (first block)
                   self.assertListEqual(
                       list(hidden_states[0].shape[-3:]      ),       [
                           self.model_tester.hidden_sizes[0],
                           self.model_tester.image_size // 4,
                           self.model_tester.image_size // 4,
                       ],       )

            _UpperCamelCase       ,		_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _UpperCamelCase              =						True
                   check_hidden_states_output(A,       A,       A      )

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   _UpperCamelCase              =						True

                   check_hidden_states_output(A,       A,       A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            if not self.model_tester.is_training:
                   return

            _UpperCamelCase       ,		_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs_for_common()
            _UpperCamelCase              =						True

            for model_class in self.all_model_classes:
                   if model_class in get_values(A      ):
                          continue

                   _UpperCamelCase              =						model_class(A      )
                   model.to(A      )
                   model.train()
                   _UpperCamelCase              =						self._prepare_for_class(A,       A,       return_labels=A      )
                   _UpperCamelCase              =						model(**A      ).loss
                   loss.backward()

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





            """simple docstring"""

            pass



     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _UpperCamelCase              =						SegformerModel.from_pretrained(A      )
                   self.assertIsNotNone(A      )






def     SCREAMING_SNAKE_CASE			(      ):
       _UpperCamelCase              =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
       return image

@require_torch
class  __A(						unittest.TestCase ):

     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SegformerImageProcessor(
                image_scale=(512, 512),       keep_ratio=A,       align=A,       do_random_crop=A      )
            _UpperCamelCase              =						SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''      ).to(
                A      )

            _UpperCamelCase              =						prepare_img()
            _UpperCamelCase              =						image_processor(images=A,       return_tensors='''pt'''      )
            _UpperCamelCase              =						encoded_inputs.pixel_values.to(A      )

            with torch.no_grad():
                   _UpperCamelCase              =						model(A      )

            _UpperCamelCase              =						torch.Size((1, model.config.num_labels, 128, 128)      )
            self.assertEqual(outputs.logits.shape,       A      )

            _UpperCamelCase              =						torch.tensor(
                [
                    [[-4.6_310, -5.5_232, -6.2_356], [-5.1_921, -6.1_444, -6.5_996], [-5.4_424, -6.2_790, -6.7_574]],
                    [[-12.1_391, -13.3_122, -13.9_554], [-12.8_732, -13.9_352, -14.3_563], [-12.9_438, -13.8_226, -14.2_513]],
                    [[-12.5_134, -13.4_686, -14.4_915], [-12.8_669, -14.4_343, -14.7_758], [-13.2_523, -14.5_819, -15.0_694]],
                ]      ).to(A      )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3],       A,       atol=1E-4      )      )

     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SegformerImageProcessor(
                image_scale=(512, 512),       keep_ratio=A,       align=A,       do_random_crop=A      )
            _UpperCamelCase              =						SegformerForSemanticSegmentation.from_pretrained(
                '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''      ).to(A      )

            _UpperCamelCase              =						prepare_img()
            _UpperCamelCase              =						image_processor(images=A,       return_tensors='''pt'''      )
            _UpperCamelCase              =						encoded_inputs.pixel_values.to(A      )

            with torch.no_grad():
                   _UpperCamelCase              =						model(A      )

            _UpperCamelCase              =						torch.Size((1, model.config.num_labels, 128, 128)      )
            self.assertEqual(outputs.logits.shape,       A      )

            _UpperCamelCase              =						torch.tensor(
                [
                    [[-13.5_748, -13.9_111, -12.6_500], [-14.3_500, -15.3_683, -14.2_328], [-14.7_532, -16.0_424, -15.6_087]],
                    [[-17.1_651, -15.8_725, -12.9_653], [-17.2_580, -17.3_718, -14.8_223], [-16.6_058, -16.8_783, -16.7_452]],
                    [[-3.6_456, -3.0_209, -1.4_203], [-3.0_797, -3.1_959, -2.0_000], [-1.8_757, -1.9_217, -1.6_997]],
                ]      ).to(A      )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3],       A,       atol=1E-1      )      )



     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						SegformerImageProcessor(
                image_scale=(512, 512),       keep_ratio=A,       align=A,       do_random_crop=A      )
            _UpperCamelCase              =						SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''      ).to(
                A      )

            _UpperCamelCase              =						prepare_img()
            _UpperCamelCase              =						image_processor(images=A,       return_tensors='''pt'''      )
            _UpperCamelCase              =						encoded_inputs.pixel_values.to(A      )

            with torch.no_grad():
                   _UpperCamelCase              =						model(A      )

            _UpperCamelCase              =						outputs.logits.detach().cpu()

            _UpperCamelCase              =						image_processor.post_process_semantic_segmentation(outputs=A,       target_sizes=[(500, 300)]      )
            _UpperCamelCase              =						torch.Size((500, 300)      )
            self.assertEqual(segmentation[0].shape,       A      )

            _UpperCamelCase              =						image_processor.post_process_semantic_segmentation(outputs=A      )
            _UpperCamelCase              =						torch.Size((128, 128)      )
            self.assertEqual(segmentation[0].shape,       A      )













import logging
import os
from typing import List, TextIO, Union

from conllu import parse_incr
from utils_ner import InputExample, Split, TokenClassificationTask


lowercase    : Optional[Any]    	=  logging.getLogger(__name__)

class  __A(						__UpperCAmelCase ):

     def __init__(      self,       A=-1      ):





            """simple docstring"""

            _UpperCamelCase              =						label_idx

     def 	_UpperCamelCase     (      self,       A,       A      ):





            """simple docstring"""

            if isinstance(A,       A      ):
                   _UpperCamelCase              =						mode.value
            _UpperCamelCase              =						os.path.join(A,       F'''{mode}.txt'''      )
            _UpperCamelCase              =						1
            _UpperCamelCase              =						[]
            with open(A,       encoding='''utf-8'''      ) as f:
                   _UpperCamelCase              =						[]
                   _UpperCamelCase              =						[]
                   for line in f:
                          if line.startswith('''-DOCSTART-'''      ) or line == "" or line == "\n":
                                 if words:
                                        examples.append(InputExample(guid=F'''{mode}-{guid_index}''',       words=A,       labels=A      )      )
                                        guid_index += 1
                                        _UpperCamelCase              =						[]
                                        _UpperCamelCase              =						[]
                          else:
                                 _UpperCamelCase              =						line.split(''' '''      )
                                 words.append(splits[0]      )
                                 if len(A      ) > 1:
                                        labels.append(splits[self.label_idx].replace('''\n''',       ''''''      )      )
                                 else:
                                        # Examples could have no label for mode = "test"
                                        labels.append('''O'''      )
                   if words:
                          examples.append(InputExample(guid=F'''{mode}-{guid_index}''',       words=A,       labels=A      )      )
            return examples

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						0
            for line in test_input_reader:
                   if line.startswith('''-DOCSTART-'''      ) or line == "" or line == "\n":
                          writer.write(A      )
                          if not preds_list[example_id]:
                                 example_id += 1
                   elif preds_list[example_id]:
                          _UpperCamelCase              =						line.split()[0] + ''' ''' + preds_list[example_id].pop(0      ) + '''\n'''
                          writer.write(A      )
                   else:
                          logger.warning('''Maximum sequence length exceeded: No prediction for \'%s\'.''',       line.split()[0]      )



     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            if path:
                   with open(A,       '''r'''      ) as f:
                          _UpperCamelCase              =						f.read().splitlines()
                   if "O" not in labels:
                          _UpperCamelCase              =						['''O'''] + labels
                   return labels
            else:
                   return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]






class  __A(						__UpperCAmelCase ):

     def __init__(      self      ):





            """simple docstring"""

            super().__init__(label_idx=-2      )



     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            if path:
                   with open(A,       '''r'''      ) as f:
                          _UpperCamelCase              =						f.read().splitlines()
                   if "O" not in labels:
                          _UpperCamelCase              =						['''O'''] + labels
                   return labels
            else:
                   return [
                       "O",
                       "B-ADVP",
                       "B-INTJ",
                       "B-LST",
                       "B-PRT",
                       "B-NP",
                       "B-SBAR",
                       "B-VP",
                       "B-ADJP",
                       "B-CONJP",
                       "B-PP",
                       "I-ADVP",
                       "I-INTJ",
                       "I-LST",
                       "I-PRT",
                       "I-NP",
                       "I-SBAR",
                       "I-VP",
                       "I-ADJP",
                       "I-CONJP",
                       "I-PP",
                   ]






class  __A(						__UpperCAmelCase ):

     def 	_UpperCamelCase     (      self,       A,       A      ):





            """simple docstring"""

            if isinstance(A,       A      ):
                   _UpperCamelCase              =						mode.value
            _UpperCamelCase              =						os.path.join(A,       F'''{mode}.txt'''      )
            _UpperCamelCase              =						1
            _UpperCamelCase              =						[]

            with open(A,       encoding='''utf-8'''      ) as f:
                   for sentence in parse_incr(A      ):
                          _UpperCamelCase              =						[]
                          _UpperCamelCase              =						[]
                          for token in sentence:
                                 words.append(token['''form''']      )
                                 labels.append(token['''upos''']      )
                          assert len(A      ) == len(A      )
                          if words:
                                 examples.append(InputExample(guid=F'''{mode}-{guid_index}''',       words=A,       labels=A      )      )
                                 guid_index += 1
            return examples

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						0
            for sentence in parse_incr(A      ):
                   _UpperCamelCase              =						preds_list[example_id]
                   _UpperCamelCase              =						''''''
                   for token in sentence:
                          out += F'''{token["form"]} ({token["upos"]}|{s_p.pop(0      )}) '''
                   out += "\n"
                   writer.write(A      )
                   example_id += 1



     def 	_UpperCamelCase     (      self,       A      ):





            """simple docstring"""

            if path:
                   with open(A,       '''r'''      ) as f:
                          return f.read().splitlines()
            else:
                   return [
                       "ADJ",
                       "ADP",
                       "ADV",
                       "AUX",
                       "CCONJ",
                       "DET",
                       "INTJ",
                       "NOUN",
                       "NUM",
                       "PART",
                       "PRON",
                       "PROPN",
                       "PUNCT",
                       "SCONJ",
                       "SYM",
                       "VERB",
                       "X",
                   ]






