


import logging
import os
import random
import sys
from dataclasses import dataclass, field
from typing import Optional

import datasets
import evaluate
import numpy as np
from datasets import load_dataset

import transformers
from transformers import (
    AutoConfig,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    default_data_collator,
    set_seed,
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version


# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version('4.31.0')

require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/text-classification/requirements.txt')

__UpperCamelCase    :						int     =							logging.getLogger(__name__)



@dataclass
class 							_UpperCamelCase :

   '''simple docstring'''







   a_				:							Optional[int]    = field(
       default=128,metadata={
           "help": (
               "The maximum total input sequence length after tokenization. Sequences longer "
               "than this will be truncated, sequences shorter will be padded."
           )
       },)
   a_				:							bool    = field(
       default=A,metadata={"help": "Overwrite the cached preprocessed datasets or not."}						)
   a_				:							bool    = field(
       default=A,metadata={
           "help": (
               "Whether to pad all samples to `max_seq_length`. "
               "If False, will pad the samples dynamically when batching to the maximum length in the batch."
           )
       },)
   a_				:							Optional[int]    = field(
       default=A,metadata={
           "help": (
               "For debugging purposes or quicker training, truncate the number of training examples to this "
               "value if set."
           )
       },)
   a_				:							Optional[int]    = field(
       default=A,metadata={
           "help": (
               "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
               "value if set."
           )
       },)
   a_				:							Optional[int]    = field(
       default=A,metadata={
           "help": (
               "For debugging purposes or quicker training, truncate the number of prediction examples to this "
               "value if set."
           )
       },)



@dataclass
class 							_UpperCamelCase :

   '''simple docstring'''







   a_				:							str    = field(
       default=A,metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}						)
   a_				:							str    = field(
       default=A,metadata={"help": "Evaluation language. Also train language if `train_language` is set to None."}						)
   a_				:							Optional[str]    = field(
       default=A,metadata={"help": "Train language if it is different from the evaluation language."}						)
   a_				:							Optional[str]    = field(
       default=A,metadata={"help": "Pretrained config name or path if not the same as model_name"}						)
   a_				:							Optional[str]    = field(
       default=A,metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}						)
   a_				:							Optional[str]    = field(
       default=A,metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},)
   a_				:							Optional[bool]    = field(
       default=A,metadata={"help": "arg to indicate if tokenizer should do lower case in AutoTokenizer.from_pretrained()"},)
   a_				:							bool    = field(
       default=A,metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."},)
   a_				:							str    = field(
       default="main",metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},)
   a_				:							bool    = field(
       default=A,metadata={
           "help": (
               "Will use the token generated when running `huggingface-cli login` (necessary to use this script "
               "with private models)."
           )
       },)
   a_				:							bool    = field(
       default=A,metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."},)





def   _UpperCAmelCase       (     ):
      """simple docstring"""




      __lowerCamelCase     :      int       						=						HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)      )
      __lowerCamelCase     :      Tuple       						=						parser.parse_args_into_dataclasses()

      # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The
      # information sent is the one passed as arguments along with your Python/PyTorch versions.
      send_example_telemetry("""run_xnli"""						,	UpperCAmelCase      )

      # Setup logging
      logging.basicConfig(
          format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s"""						,	datefmt="""%m/%d/%Y %H:%M:%S"""						,	handlers=[logging.StreamHandler(sys.stdout      )]						,	)

      if training_args.should_log:
            # The default of training_args.log_level is passive, so we set log level at info here to have that default.
            transformers.utils.logging.set_verbosity_info()

      __lowerCamelCase     :      Dict       						=						training_args.get_process_log_level()
      logger.setLevel(UpperCAmelCase      )
      datasets.utils.logging.set_verbosity(UpperCAmelCase      )
      transformers.utils.logging.set_verbosity(UpperCAmelCase      )
      transformers.utils.logging.enable_default_handler()
      transformers.utils.logging.enable_explicit_format()

      # Log on each process the small summary:
      logger.warning(
          f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"""
          + f"""distributed training: {bool(training_args.local_rank != -1      )}, 16-bits training: {training_args.fpaa}"""      )
      logger.info(f"""Training/evaluation parameters {training_args}"""      )

      # Detecting last checkpoint.
      __lowerCamelCase     :      Tuple       						=						None
      if os.path.isdir(training_args.output_dir      ) and training_args.do_train and not training_args.overwrite_output_dir:
            __lowerCamelCase     :      List[Any]       						=						get_last_checkpoint(training_args.output_dir      )
            if last_checkpoint is None and len(os.listdir(training_args.output_dir      )      ) > 0:
                  raise ValueError(
                      f"""Output directory ({training_args.output_dir}) already exists and is not empty. """
                      """Use --overwrite_output_dir to overcome."""      )
            elif last_checkpoint is not None:
                  logger.info(
                      f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """
                      """the `--output_dir` or add `--overwrite_output_dir` to train from scratch."""      )

    # Set seed before initializing model.
      set_seed(training_args.seed      )

      # In distributed training, the load_dataset function guarantees that only one local process can concurrently
      # download the dataset.
      # Downloading and loading xnli dataset from the hub.
      if training_args.do_train:
            if model_args.train_language is None:
                  __lowerCamelCase     :      Union[str, Any]       						=						load_dataset(
                      """xnli"""						,	model_args.language						,	split="""train"""						,	cache_dir=model_args.cache_dir						,	use_auth_token=True if model_args.use_auth_token else None						,	)
            else:
                  __lowerCamelCase     :      str       						=						load_dataset(
                      """xnli"""						,	model_args.train_language						,	split="""train"""						,	cache_dir=model_args.cache_dir						,	use_auth_token=True if model_args.use_auth_token else None						,	)
            __lowerCamelCase     :      List[Any]       						=						train_dataset.features["""label"""].names

      if training_args.do_eval:
            __lowerCamelCase     :      List[Any]       						=						load_dataset(
                """xnli"""						,	model_args.language						,	split="""validation"""						,	cache_dir=model_args.cache_dir						,	use_auth_token=True if model_args.use_auth_token else None						,	)
            __lowerCamelCase     :      List[Any]       						=						eval_dataset.features["""label"""].names

      if training_args.do_predict:
            __lowerCamelCase     :      Any       						=						load_dataset(
                """xnli"""						,	model_args.language						,	split="""test"""						,	cache_dir=model_args.cache_dir						,	use_auth_token=True if model_args.use_auth_token else None						,	)
            __lowerCamelCase     :      Tuple       						=						predict_dataset.features["""label"""].names

      # Labels
      __lowerCamelCase     :      Dict       						=						len(UpperCAmelCase      )

      # Load pretrained model and tokenizer
      # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently
      # download model & vocab.
      __lowerCamelCase     :      Dict       						=						AutoConfig.from_pretrained(
          model_args.config_name if model_args.config_name else model_args.model_name_or_path						,	num_labels=UpperCAmelCase						,	idalabel={str(UpperCAmelCase      ): label for i, label in enumerate(UpperCAmelCase      )}						,	labelaid={label: i for i, label in enumerate(UpperCAmelCase      )}						,	finetuning_task="""xnli"""						,	cache_dir=model_args.cache_dir						,	revision=model_args.model_revision						,	use_auth_token=True if model_args.use_auth_token else None						,	)
      __lowerCamelCase     :      Union[str, Any]       						=						AutoTokenizer.from_pretrained(
          model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path						,	do_lower_case=model_args.do_lower_case						,	cache_dir=model_args.cache_dir						,	use_fast=model_args.use_fast_tokenizer						,	revision=model_args.model_revision						,	use_auth_token=True if model_args.use_auth_token else None						,	)
      __lowerCamelCase     :      Any       						=						AutoModelForSequenceClassification.from_pretrained(
          model_args.model_name_or_path						,	from_tf=bool(""".ckpt""" in model_args.model_name_or_path      )						,	config=UpperCAmelCase						,	cache_dir=model_args.cache_dir						,	revision=model_args.model_revision						,	use_auth_token=True if model_args.use_auth_token else None						,	ignore_mismatched_sizes=model_args.ignore_mismatched_sizes						,	)

      # Preprocessing the datasets
      # Padding strategy
      if data_args.pad_to_max_length:
            __lowerCamelCase     :      Dict       						=						"""max_length"""
      else:
            # We will pad later, dynamically at batch creation, to the max sequence length in each batch
            __lowerCamelCase     :      Optional[Any]       						=						False

      def preprocess_function(UpperCAmelCase      :			Optional[int]      ):
            # Tokenize the texts
            return tokenizer(
                examples["""premise"""]						,	examples["""hypothesis"""]						,	padding=UpperCAmelCase						,	max_length=data_args.max_seq_length						,	truncation=UpperCAmelCase						,	)

      if training_args.do_train:
            if data_args.max_train_samples is not None:
                  __lowerCamelCase     :      List[Any]       						=						min(len(UpperCAmelCase      )						,	data_args.max_train_samples      )
                  __lowerCamelCase     :      Optional[Any]       						=						train_dataset.select(range(UpperCAmelCase      )      )
            with training_args.main_process_first(desc="""train dataset map pre-processing"""      ):
                  __lowerCamelCase     :      Any       						=						train_dataset.map(
                      UpperCAmelCase						,	batched=UpperCAmelCase						,	load_from_cache_file=not data_args.overwrite_cache						,	desc="""Running tokenizer on train dataset"""						,	)
            # Log a few random samples from the training set:
            for index in random.sample(range(len(UpperCAmelCase      )      )						,	3      ):
                  logger.info(f"""Sample {index} of the training set: {train_dataset[index]}."""      )

      if training_args.do_eval:
            if data_args.max_eval_samples is not None:
                  __lowerCamelCase     :      List[str]       						=						min(len(UpperCAmelCase      )						,	data_args.max_eval_samples      )
                  __lowerCamelCase     :      Tuple       						=						eval_dataset.select(range(UpperCAmelCase      )      )
            with training_args.main_process_first(desc="""validation dataset map pre-processing"""      ):
                  __lowerCamelCase     :      Tuple       						=						eval_dataset.map(
                      UpperCAmelCase						,	batched=UpperCAmelCase						,	load_from_cache_file=not data_args.overwrite_cache						,	desc="""Running tokenizer on validation dataset"""						,	)

      if training_args.do_predict:
            if data_args.max_predict_samples is not None:
                  __lowerCamelCase     :      int       						=						min(len(UpperCAmelCase      )						,	data_args.max_predict_samples      )
                  __lowerCamelCase     :      str       						=						predict_dataset.select(range(UpperCAmelCase      )      )
            with training_args.main_process_first(desc="""prediction dataset map pre-processing"""      ):
                  __lowerCamelCase     :      Optional[Any]       						=						predict_dataset.map(
                      UpperCAmelCase						,	batched=UpperCAmelCase						,	load_from_cache_file=not data_args.overwrite_cache						,	desc="""Running tokenizer on prediction dataset"""						,	)

    # Get the metric function
      __lowerCamelCase     :      Optional[int]       						=						evaluate.load("""xnli"""      )

      # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
      # predictions and label_ids field) and has to return a dictionary string to float.
      def compute_metrics(UpperCAmelCase      :			EvalPrediction      ):
            __lowerCamelCase     :      Union[str, Any]       						=						p.predictions[0] if isinstance(p.predictions						,	UpperCAmelCase      ) else p.predictions
            __lowerCamelCase     :      Dict       						=						np.argmax(UpperCAmelCase						,	axis=1      )
            return metric.compute(predictions=UpperCAmelCase						,	references=p.label_ids      )

      # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding.
      if data_args.pad_to_max_length:
            __lowerCamelCase     :      List[Any]       						=						default_data_collator
      elif training_args.fpaa:
            __lowerCamelCase     :      List[str]       						=						DataCollatorWithPadding(UpperCAmelCase						,	pad_to_multiple_of=8      )
      else:
            __lowerCamelCase     :      List[Any]       						=						None

      # Initialize our Trainer
      __lowerCamelCase     :      Dict       						=						Trainer(
          model=UpperCAmelCase						,	args=UpperCAmelCase						,	train_dataset=train_dataset if training_args.do_train else None						,	eval_dataset=eval_dataset if training_args.do_eval else None						,	compute_metrics=UpperCAmelCase						,	tokenizer=UpperCAmelCase						,	data_collator=UpperCAmelCase						,	)

      # Training
      if training_args.do_train:
            __lowerCamelCase     :      Optional[int]       						=						None
            if training_args.resume_from_checkpoint is not None:
                  __lowerCamelCase     :      Union[str, Any]       						=						training_args.resume_from_checkpoint
            elif last_checkpoint is not None:
                  __lowerCamelCase     :      Dict       						=						last_checkpoint
            __lowerCamelCase     :      List[str]       						=						trainer.train(resume_from_checkpoint=UpperCAmelCase      )
            __lowerCamelCase     :      Tuple       						=						train_result.metrics
            __lowerCamelCase     :      Optional[Any]       						=						(
                data_args.max_train_samples if data_args.max_train_samples is not None else len(UpperCAmelCase      )
            )
            __lowerCamelCase     :      str       						=						min(UpperCAmelCase						,	len(UpperCAmelCase      )      )

            trainer.save_model()  # Saves the tokenizer too for easy upload

            trainer.log_metrics("""train"""						,	UpperCAmelCase      )
            trainer.save_metrics("""train"""						,	UpperCAmelCase      )
            trainer.save_state()

      # Evaluation
      if training_args.do_eval:
            logger.info("""*** Evaluate ***"""      )
            __lowerCamelCase     :      Optional[Any]       						=						trainer.evaluate(eval_dataset=UpperCAmelCase      )

            __lowerCamelCase     :      List[Any]       						=						data_args.max_eval_samples if data_args.max_eval_samples is not None else len(UpperCAmelCase      )
            __lowerCamelCase     :      Optional[Any]       						=						min(UpperCAmelCase						,	len(UpperCAmelCase      )      )

            trainer.log_metrics("""eval"""						,	UpperCAmelCase      )
            trainer.save_metrics("""eval"""						,	UpperCAmelCase      )

      # Prediction
      if training_args.do_predict:
            logger.info("""*** Predict ***"""      )
            __lowerCamelCase     :      Dict       						=						trainer.predict(UpperCAmelCase						,	metric_key_prefix="""predict"""      )

            __lowerCamelCase     :      Optional[Any]       						=						(
                data_args.max_predict_samples if data_args.max_predict_samples is not None else len(UpperCAmelCase      )
            )
            __lowerCamelCase     :      Any       						=						min(UpperCAmelCase						,	len(UpperCAmelCase      )      )

            trainer.log_metrics("""predict"""						,	UpperCAmelCase      )
            trainer.save_metrics("""predict"""						,	UpperCAmelCase      )

            __lowerCamelCase     :      Union[str, Any]       						=						np.argmax(UpperCAmelCase						,	axis=1      )
            __lowerCamelCase     :      Dict       						=						os.path.join(training_args.output_dir						,	"""predictions.txt"""      )
            if trainer.is_world_process_zero():
                  with open(UpperCAmelCase						,	"""w"""      ) as writer:
                        writer.write("""index\tprediction\n"""      )
                        for index, item in enumerate(UpperCAmelCase      ):
                              __lowerCamelCase     :      List[str]       						=						label_list[item]
                              writer.write(f"""{index}\t{item}\n"""      )


if __name__ == "__main__":
    main()









import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

    if is_vision_available():
        from transformers import MaskaFormerImageProcessor

if is_vision_available():
    from PIL import Image



class 							_UpperCamelCase :

   '''simple docstring'''







   def __init__(  self  : int       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Tuple=2       ,  _lowerCamelCase  : Optional[int]=True       ,  _lowerCamelCase  : Union[str, Any]=False       ,  _lowerCamelCase  : Dict=1_0       ,  _lowerCamelCase  : Union[str, Any]=3       ,  _lowerCamelCase  : Tuple=3_2 * 8       ,  _lowerCamelCase  : int=3_2 * 8       ,  _lowerCamelCase  : str=4       ,  _lowerCamelCase  : List[Any]=6_4       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						parent
         __lowerCamelCase     :      Optional[int]       						=						batch_size
         __lowerCamelCase     :      List[str]       						=						is_training
         __lowerCamelCase     :      Dict       						=						use_auxiliary_loss
         __lowerCamelCase     :      Optional[int]       						=						num_queries
         __lowerCamelCase     :      Optional[Any]       						=						num_channels
         __lowerCamelCase     :      Dict       						=						min_size
         __lowerCamelCase     :      Optional[Any]       						=						max_size
         __lowerCamelCase     :      str       						=						num_labels
         __lowerCamelCase     :      Optional[Any]       						=						hidden_dim
         __lowerCamelCase     :      Optional[Any]       						=						hidden_dim

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]							).to(
             _lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						torch.ones([self.batch_size, self.min_size, self.max_size]       ,  device=_lowerCamelCase							)

         __lowerCamelCase     :      int       						=						(
             torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,  device=_lowerCamelCase							) > 0.5
         ).float()
         __lowerCamelCase     :      List[Any]       						=						(torch.rand((self.batch_size, self.num_labels)       ,  device=_lowerCamelCase							) > 0.5).long()

         __lowerCamelCase     :      Optional[Any]       						=						self.get_config()
         return config, pixel_values, pixel_mask, mask_labels, class_labels

   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						MaskaFormerConfig(
             hidden_size=self.hidden_dim       ,  )
         __lowerCamelCase     :      List[Any]       						=						self.num_queries
         __lowerCamelCase     :      List[str]       						=						self.num_labels

         __lowerCamelCase     :      List[str]       						=						[1, 1, 1, 1]
         __lowerCamelCase     :      Optional[int]       						=						self.num_channels

         __lowerCamelCase     :      Optional[int]       						=						6_4
         __lowerCamelCase     :      int       						=						1_2_8
         __lowerCamelCase     :      Any       						=						self.hidden_dim
         __lowerCamelCase     :      List[str]       						=						self.hidden_dim
         __lowerCamelCase     :      List[Any]       						=						self.hidden_dim
         return config

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      int       						=						self.prepare_config_and_inputs()
         __lowerCamelCase     :      Optional[int]       						=						{"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
         return config, inputs_dict

   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						output.encoder_hidden_states
         __lowerCamelCase     :      Any       						=						output.pixel_decoder_hidden_states
         __lowerCamelCase     :      Optional[Any]       						=						output.transformer_decoder_hidden_states

         self.parent.assertTrue(len(_lowerCamelCase							)       ,  len(config.backbone_config.depths							)							)
         self.parent.assertTrue(len(_lowerCamelCase							)       ,  len(config.backbone_config.depths							)							)
         self.parent.assertTrue(len(_lowerCamelCase							)       ,  config.decoder_layers							)

   def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : List[str]=False							):






         '''simple docstring'''



         with torch.no_grad():
               __lowerCamelCase     :      Any       						=						MaskaFormerModel(config=_lowerCamelCase							)
               model.to(_lowerCamelCase							)
               model.eval()

               __lowerCamelCase     :      Dict       						=						model(pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase							)
               __lowerCamelCase     :      Optional[Any]       						=						model(_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

         self.parent.assertEqual(
             output.transformer_decoder_last_hidden_state.shape       ,  (self.batch_size, self.num_queries, self.hidden_dim)       ,  )
         # let's ensure the other two hidden state exists
         self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None							)
         self.parent.assertTrue(output.encoder_last_hidden_state is not None							)

         if output_hidden_states:
               self.check_output_hidden_state(_lowerCamelCase       ,  _lowerCamelCase							)


   def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						MaskaFormerForUniversalSegmentation(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

         def comm_check_on_output(_lowerCamelCase  : Optional[int]							):
               # let's still check that all the required stuff is there
               self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None							)
               self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None							)
               self.parent.assertTrue(result.encoder_last_hidden_state is not None							)
               # okay, now we need to check the logits shape
               # due to the encoder compression, masks have a //4 spatial size
               self.parent.assertEqual(
                   result.masks_queries_logits.shape       ,  (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)       ,  )
               # + 1 for null class
               self.parent.assertEqual(
                   result.class_queries_logits.shape       ,  (self.batch_size, self.num_queries, self.num_labels + 1)							)

         with torch.no_grad():
               __lowerCamelCase     :      Optional[int]       						=						model(pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase							)
               __lowerCamelCase     :      Tuple       						=						model(_lowerCamelCase							)

               comm_check_on_output(_lowerCamelCase							)

               __lowerCamelCase     :      str       						=						model(
                   pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							)

         comm_check_on_output(_lowerCamelCase							)

         self.parent.assertTrue(result.loss is not None							)
         self.parent.assertEqual(result.loss.shape       ,  torch.Size([1]							)							)



@require_torch
class 							_UpperCamelCase (						A,A,unittest.TestCase						):

   '''simple docstring'''







   a_				:							Union[str, Any]    = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
   a_				:							Union[str, Any]    = {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

   a_				:							Optional[Any]    = False
   a_				:							int    = False
   a_				:							List[str]    = False
   a_				:							List[Any]    = False

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						MaskaFormerModelTester(self							)
         __lowerCamelCase     :      int       						=						ConfigTester(self       ,  config_class=_lowerCamelCase       ,  has_text_modality=_lowerCamelCase							)

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         self.config_tester.run_common_tests()

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Optional[Any]       						=						self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskaformer_model(_lowerCamelCase       ,  **_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_lowerCamelCase							)

   @unittest.skip(reason="""Mask2Former does not use inputs_embeds"""							)
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""							)
   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former is not a generative model"""							)
   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former does not use token embeddings"""							)
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         pass

   @require_torch_multi_gpu
   @unittest.skip(
       reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""							)
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         pass

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






         '''simple docstring'''



         pass

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Dict       						=						self.model_tester.prepare_config_and_inputs_for_common()

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

               __lowerCamelCase     :      List[Any]       						=						["""pixel_values"""]
               self.assertListEqual(arg_names[:1]       ,  _lowerCamelCase							)

   @slow
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
               __lowerCamelCase     :      Dict       						=						MaskaFormerModel.from_pretrained(_lowerCamelCase							)
               self.assertIsNotNone(_lowerCamelCase							)

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						(self.model_tester.min_size,) * 2
         __lowerCamelCase     :      Optional[Any]       						=						{
             """pixel_values""": torch.randn((2, 3, *size)       ,  device=_lowerCamelCase							),
             """mask_labels""": torch.randn((2, 1_0, *size)       ,  device=_lowerCamelCase							),
             """class_labels""": torch.zeros(2       ,  1_0       ,  device=_lowerCamelCase							).long(),
         }
         __lowerCamelCase     :      Any       						=						self.model_tester.get_config()

         __lowerCamelCase     :      Tuple       						=						MaskaFormerForUniversalSegmentation(_lowerCamelCase							).to(_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						model(**_lowerCamelCase							)
         self.assertTrue(outputs.loss is not None							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Any       						=						self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskaformer_model(_lowerCamelCase       ,  **_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      List[str]       						=						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               __lowerCamelCase     :      List[str]       						=						model_class(_lowerCamelCase							).to(_lowerCamelCase							)
               __lowerCamelCase     :      int       						=						model(**_lowerCamelCase       ,  output_attentions=_lowerCamelCase							)
               self.assertTrue(outputs.attentions is not None							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         if not self.model_tester.is_training:
               return

         __lowerCamelCase     :      str       						=						self.all_model_classes[1]
         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      Dict       						=						self.model_tester.prepare_config_and_inputs()

         __lowerCamelCase     :      List[Any]       						=						model_class(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.train()

         __lowerCamelCase     :      Tuple       						=						model(_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							).loss
         loss.backward()


   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.all_model_classes[1]
         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      Optional[int]       						=						self.model_tester.prepare_config_and_inputs()
         __lowerCamelCase     :      Optional[int]       						=						True
         __lowerCamelCase     :      Any       						=						True

         __lowerCamelCase     :      Any       						=						model_class(_lowerCamelCase							).to(_lowerCamelCase							)
         model.train()

         __lowerCamelCase     :      Optional[Any]       						=						model(_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							)

         __lowerCamelCase     :      Union[str, Any]       						=						outputs.encoder_hidden_states[0]
         encoder_hidden_states.retain_grad()

         __lowerCamelCase     :      Any       						=						outputs.pixel_decoder_hidden_states[0]
         pixel_decoder_hidden_states.retain_grad()

         __lowerCamelCase     :      Optional[Any]       						=						outputs.transformer_decoder_hidden_states[0]
         transformer_decoder_hidden_states.retain_grad()

         __lowerCamelCase     :      List[str]       						=						outputs.attentions[0]
         attentions.retain_grad()

         outputs.loss.backward(retain_graph=_lowerCamelCase							)

         self.assertIsNotNone(encoder_hidden_states.grad							)
         self.assertIsNotNone(pixel_decoder_hidden_states.grad							)
         self.assertIsNotNone(transformer_decoder_hidden_states.grad							)
         self.assertIsNotNone(attentions.grad							)

__UpperCamelCase    :						str     =							1E-4





def   _UpperCAmelCase       (     ):
      """simple docstring"""




      __lowerCamelCase     :      Union[str, Any]       						=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
      return image




@require_vision
@slow
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   @cached_property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return "facebook/mask2former-swin-small-coco-instance"

   @cached_property
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints							) if is_vision_available() else None

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						MaskaFormerModel.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						self.default_image_processor
         __lowerCamelCase     :      Dict       						=						prepare_img()
         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							).to(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						inputs["""pixel_values"""].shape
         # check size is divisible by 32
         self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0							)
         # check size
         self.assertEqual(_lowerCamelCase       ,  (1, 3, 3_8_4, 3_8_4)							)

         with torch.no_grad():
               __lowerCamelCase     :      str       						=						model(**_lowerCamelCase							)

         __lowerCamelCase     :      Dict       						=						torch.tensor(
             [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

         __lowerCamelCase     :      Union[str, Any]       						=						torch.tensor(
             [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

         __lowerCamelCase     :      Tuple       						=						torch.tensor(
             [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							).eval()
         __lowerCamelCase     :      Any       						=						self.default_image_processor
         __lowerCamelCase     :      Union[str, Any]       						=						prepare_img()
         __lowerCamelCase     :      int       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							).to(_lowerCamelCase							)
         __lowerCamelCase     :      int       						=						inputs["""pixel_values"""].shape
         # check size is divisible by 32
         self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0							)
         # check size
         self.assertEqual(_lowerCamelCase       ,  (1, 3, 3_8_4, 3_8_4)							)

         with torch.no_grad():
               __lowerCamelCase     :      Dict       						=						model(**_lowerCamelCase							)
         # masks_queries_logits
         __lowerCamelCase     :      Dict       						=						outputs.masks_queries_logits
         self.assertEqual(
             masks_queries_logits.shape       ,  (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)							)
         __lowerCamelCase     :      Any       						=						[
             [-8.7_839, -9.0_056, -8.8_121],
             [-7.4_104, -7.0_313, -6.5_401],
             [-6.6_105, -6.3_427, -6.4_675],
         ]
         __lowerCamelCase     :      List[str]       						=						torch.tensor(_lowerCamelCase							).to(_lowerCamelCase							)
         self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)
         # class_queries_logits
         __lowerCamelCase     :      Union[str, Any]       						=						outputs.class_queries_logits
         self.assertEqual(class_queries_logits.shape       ,  (1, model.config.num_queries, model.config.num_labels + 1)							)
         __lowerCamelCase     :      List[Any]       						=						torch.tensor(
             [
                 [1.8_324, -8.0_835, -4.1_922],
                 [0.8_450, -9.0_050, -3.6_053],
                 [0.3_045, -7.7_293, -3.0_275],
             ]							).to(_lowerCamelCase							)
         self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)


   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							).eval()
         __lowerCamelCase     :      Dict       						=						self.default_image_processor

         __lowerCamelCase     :      List[Any]       						=						image_processor(
             [np.zeros((3, 8_0_0, 1_3_3_3)							), np.zeros((3, 8_0_0, 1_3_3_3)							)]       ,  segmentation_maps=[np.zeros((3_8_4, 3_8_4)							).astype(np.floataa							), np.zeros((3_8_4, 3_8_4)							).astype(np.floataa							)]       ,  return_tensors="""pt"""       ,  )

         __lowerCamelCase     :      int       						=						inputs["""pixel_values"""].to(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						[el.to(_lowerCamelCase							) for el in inputs["""mask_labels"""]]
         __lowerCamelCase     :      Union[str, Any]       						=						[el.to(_lowerCamelCase							) for el in inputs["""class_labels"""]]

         with torch.no_grad():
               __lowerCamelCase     :      Dict       						=						model(**_lowerCamelCase							)

         self.assertTrue(outputs.loss is not None							)



