
import json
import logging
import math
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

from datasets import Dataset, load_dataset

import transformers
from transformers import (
    CONFIG_MAPPING,
    MODEL_FOR_MASKED_LM_MAPPING,
    AutoConfig,
    AutoModelForMaskedLM,
    AutoTokenizer,
    DataCollatorForWholeWordMask,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import get_last_checkpoint, is_main_process


__lowerCAmelCase			:     Dict           =   logging.getLogger(__name__)
__lowerCAmelCase			:     List[str]           =   list(MODEL_FOR_MASKED_LM_MAPPING.keys())
__lowerCAmelCase			:     Optional[Any]           =   tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)




@dataclass
class        UpperCAmelCase_			:


  '''simple docstring'''







  a__           =		field(
      default=_A  ,       metadata={
          """help""": (
              """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch."""
          )
      }  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(_A     )}  ,       )
  a__           =		field(
      default=_A  ,       metadata={
          """help""": (
              """Override some existing default config settings when a model is trained from scratch. Example: """
              """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index"""
          )
      }  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}     )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}     )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""}  ,       )
  a__           =		field(
      default="""main"""  ,       metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""}  ,       )
  a__           =		field(
      default=_A  ,       metadata={
          """help""": (
              """Will use the token generated when running `huggingface-cli login` (necessary to use this script """
              """with private models)."""
          )
      }  ,       )
  def 							_lowercase							( self			: List[Any]    )			->	int:







    """simple docstring"""






    if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None):
      raise ValueError(
          """--config_overrides can't be used in combination with --config_name or --model_name_or_path"""    )







@dataclass
class        UpperCAmelCase_			:


  '''simple docstring'''







  a__           =		field(
      default=_A  ,       metadata={"""help""": """The name of the dataset to use (via the datasets library)."""}     )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""}     )
  a__           =		field(default=_A  ,       metadata={"""help""": """The input training data file (a text file)."""}     )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""}  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""}  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""}  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """Overwrite the cached training and evaluation sets"""}     )
  a__           =		field(
      default=5  ,       metadata={
          """help""": """The percentage of the train set used as validation set in case there's no validation split"""
      }  ,       )
  a__           =		field(
      default=_A  ,       metadata={
          """help""": (
              """The maximum total input sequence length after tokenization. Sequences longer """
              """than this will be truncated. Default to the max input length of the model."""
          )
      }  ,       )
  a__           =		field(
      default=_A  ,       metadata={"""help""": """The number of processes to use for the preprocessing."""}  ,       )
  a__           =		field(
      default=0.15  ,       metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""}     )
  a__           =		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."""
          )
      }  ,       )
  def 							_lowercase							( self			: Any    )			->	Optional[Any]:







    """simple docstring"""






    if self.train_file is not None:
      __magic_name__				     =       self.train_file.split("""."""    )[-1]
      assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
    if self.validation_file is not None:
      __magic_name__				     =       self.validation_file.split("""."""    )[-1]
      assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file."







def a__    (       A_,     A_					):






  '''simple docstring'''





  with open(A_,     """r""",     encoding="""utf-8"""					) as f:
    __magic_name__				     =       [json.loads(A_					) for line in f.read().splitlines() if (len(A_					) > 0 and not line.isspace())]
  assert len(A_					) == len(A_					)

  __magic_name__				     =       {c: dataset[c] for c in dataset.column_names}
  __magic_name__				     =       refs
  return Dataset.from_dict(A_					)

def a__    (       ):






  '''simple docstring'''





  __magic_name__				     =       HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)					)
  if len(sys.argv					) == 2 and sys.argv[1].endswith(""".json"""					):
    # If we pass only one argument to the script and it's the path to a json file,
    # let's parse it to get our arguments.
    __magic_name__     ,    __magic_name__     ,    __magic_name__				     =       parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]					)					)
  else:
    __magic_name__     ,    __magic_name__     ,    __magic_name__				     =       parser.parse_args_into_dataclasses()

  # Detecting last checkpoint.
  __magic_name__				     =       None
  if os.path.isdir(training_args.output_dir					) and training_args.do_train and not training_args.overwrite_output_dir:
    __magic_name__				     =       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."""					)

    # 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					)],     )
  logger.setLevel(logging.INFO if is_main_process(training_args.local_rank					) else logging.WARN					)

  # 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}'''					)
  # Set the verbosity to info of the Transformers logger (on main process only):
  if is_main_process(training_args.local_rank					):
    transformers.utils.logging.set_verbosity_info()
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()
  logger.info("""Training/evaluation parameters %s""",     A_					)

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

  # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below)
  # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/
  # (the dataset will be downloaded automatically from the datasets Hub).
  #
  # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called
  # 'text' is found. You can easily tweak this behavior (see below).
  #
  # In distributed training, the load_dataset function guarantee that only one local process can concurrently
  # download the dataset.
  if data_args.dataset_name is not None:
    # Downloading and loading a dataset from the hub.
    __magic_name__				     =       load_dataset(data_args.dataset_name,     data_args.dataset_config_name					)
    if "validation" not in datasets.keys():
      __magic_name__				     =       load_dataset(
          data_args.dataset_name,     data_args.dataset_config_name,     split=f'''train[:{data_args.validation_split_percentage}%]''',     )
      __magic_name__				     =       load_dataset(
          data_args.dataset_name,     data_args.dataset_config_name,     split=f'''train[{data_args.validation_split_percentage}%:]''',     )
  else:
    __magic_name__				     =       {}
    if data_args.train_file is not None:
      __magic_name__				     =       data_args.train_file
    if data_args.validation_file is not None:
      __magic_name__				     =       data_args.validation_file
    __magic_name__				     =       data_args.train_file.split("""."""					)[-1]
    if extension == "txt":
      __magic_name__				     =       """text"""
    __magic_name__				     =       load_dataset(A_,     data_files=A_					)
  # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
  # https://huggingface.co/docs/datasets/loading_datasets.html.

  # Load pretrained model and tokenizer
  #
  # Distributed training:
  # The .from_pretrained methods guarantee that only one local process can concurrently
  # download model & vocab.
  __magic_name__				     =       {
      """cache_dir""": model_args.cache_dir,
      """revision""": model_args.model_revision,
      """use_auth_token""": True if model_args.use_auth_token else None,
  }
  if model_args.config_name:
    __magic_name__				     =       AutoConfig.from_pretrained(model_args.config_name,     **A_					)
  elif model_args.model_name_or_path:
    __magic_name__				     =       AutoConfig.from_pretrained(model_args.model_name_or_path,     **A_					)
  else:
    __magic_name__				     =       CONFIG_MAPPING[model_args.model_type]()
    logger.warning("""You are instantiating a new config instance from scratch."""					)
    if model_args.config_overrides is not None:
      logger.info(f'''Overriding config: {model_args.config_overrides}'''					)
      config.update_from_string(model_args.config_overrides					)
      logger.info(f'''New config: {config}'''					)

  __magic_name__				     =       {
      """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,
  }
  if model_args.tokenizer_name:
    __magic_name__				     =       AutoTokenizer.from_pretrained(model_args.tokenizer_name,     **A_					)
  elif model_args.model_name_or_path:
    __magic_name__				     =       AutoTokenizer.from_pretrained(model_args.model_name_or_path,     **A_					)
  else:
    raise ValueError(
        """You are instantiating a new tokenizer from scratch. This is not supported by this script."""
        """You can do it from another script, save it, and load it from here, using --tokenizer_name."""					)

  if model_args.model_name_or_path:
    __magic_name__				     =       AutoModelForMaskedLM.from_pretrained(
        model_args.model_name_or_path,     from_tf=bool(""".ckpt""" in model_args.model_name_or_path					),     config=A_,     cache_dir=model_args.cache_dir,     revision=model_args.model_revision,     use_auth_token=True if model_args.use_auth_token else None,     )
  else:
    logger.info("""Training new model from scratch"""					)
    __magic_name__				     =       AutoModelForMaskedLM.from_config(A_					)

  model.resize_token_embeddings(len(A_					)					)

  # Preprocessing the datasets.
  # First we tokenize all the texts.
  if training_args.do_train:
    __magic_name__				     =       datasets["""train"""].column_names
  else:
    __magic_name__				     =       datasets["""validation"""].column_names
  __magic_name__				     =       """text""" if """text""" in column_names else column_names[0]

  __magic_name__				     =       """max_length""" if data_args.pad_to_max_length else False

  def tokenize_function(A_					):
    # Remove empty lines
    __magic_name__				     =       [line for line in examples["""text"""] if len(A_					) > 0 and not line.isspace()]
    return tokenizer(examples["""text"""],     padding=A_,     truncation=A_,     max_length=data_args.max_seq_length					)

  __magic_name__				     =       datasets.map(
      A_,     batched=A_,     num_proc=data_args.preprocessing_num_workers,     remove_columns=[text_column_name],     load_from_cache_file=not data_args.overwrite_cache,     )

  # Add the chinese references if provided
  if data_args.train_ref_file is not None:
    __magic_name__				     =       add_chinese_references(tokenized_datasets["""train"""],     data_args.train_ref_file					)
  if data_args.validation_ref_file is not None:
    __magic_name__				     =       add_chinese_references(
        tokenized_datasets["""validation"""],     data_args.validation_ref_file					)
  # If we have ref files, need to avoid it removed by trainer
  __magic_name__				     =       data_args.train_ref_file or data_args.validation_ref_file
  if has_ref:
    __magic_name__				     =       False

  # Data collator
  # This one will take care of randomly masking the tokens.
  __magic_name__				     =       DataCollatorForWholeWordMask(tokenizer=A_,     mlm_probability=data_args.mlm_probability					)

  # Initialize our Trainer
  __magic_name__				     =       Trainer(
      model=A_,     args=A_,     train_dataset=tokenized_datasets["""train"""] if training_args.do_train else None,     eval_dataset=tokenized_datasets["""validation"""] if training_args.do_eval else None,     tokenizer=A_,     data_collator=A_,     )

  # Training
  if training_args.do_train:
    if last_checkpoint is not None:
      __magic_name__				     =       last_checkpoint
    elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path					):
      __magic_name__				     =       model_args.model_name_or_path
    else:
      __magic_name__				     =       None
    __magic_name__				     =       trainer.train(resume_from_checkpoint=A_					)
    trainer.save_model()  # Saves the tokenizer too for easy upload

    __magic_name__				     =       os.path.join(training_args.output_dir,     """train_results.txt"""					)
    if trainer.is_world_process_zero():
      with open(A_,     """w"""					) as writer:
        logger.info("""***** Train results *****"""					)
        for key, value in sorted(train_result.metrics.items()					):
          logger.info(f'''  {key} = {value}'''					)
          writer.write(f'''{key} = {value}\n'''					)

            # Need to save the state, since Trainer.save_model saves only the tokenizer with the model
      trainer.state.save_to_json(os.path.join(training_args.output_dir,     """trainer_state.json"""					)					)

    # Evaluation
  __magic_name__				     =       {}
  if training_args.do_eval:
    logger.info("""*** Evaluate ***"""					)

    __magic_name__				     =       trainer.evaluate()

    __magic_name__				     =       math.exp(eval_output["""eval_loss"""]					)
    __magic_name__				     =       perplexity

    __magic_name__				     =       os.path.join(training_args.output_dir,     """eval_results_mlm_wwm.txt"""					)
    if trainer.is_world_process_zero():
      with open(A_,     """w"""					) as writer:
        logger.info("""***** Eval results *****"""					)
        for key, value in sorted(results.items()					):
          logger.info(f'''  {key} = {value}'''					)
          writer.write(f'''{key} = {value}\n'''					)

  return results

def a__    (       A_					):






  '''simple docstring'''





  main()


if __name__ == "__main__":
    main()



import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
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 torch import nn

    from transformers import MaskFormerSwinBackbone
    from transformers.models.maskformer import MaskFormerSwinModel




class        UpperCAmelCase_			:


  '''simple docstring'''







  def __init__( self			: Optional[int]			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Tuple=13			,							UpperCamelCase__			: Optional[Any]=32			,							UpperCamelCase__			: Dict=2			,							UpperCamelCase__			: Dict=3			,							UpperCamelCase__			: Union[str, Any]=16			,							UpperCamelCase__			: Any=[1, 2, 1]			,							UpperCamelCase__			: int=[2, 2, 4]			,							UpperCamelCase__			: int=2			,							UpperCamelCase__			: Optional[int]=2.0			,							UpperCamelCase__			: Optional[int]=True			,							UpperCamelCase__			: Any=0.0			,							UpperCamelCase__			: str=0.0			,							UpperCamelCase__			: Optional[Any]=0.1			,							UpperCamelCase__			: Tuple="gelu"			,							UpperCamelCase__			: Optional[Any]=False			,							UpperCamelCase__			: Any=True			,							UpperCamelCase__			: List[str]=0.02			,							UpperCamelCase__			: Union[str, Any]=1E-5			,							UpperCamelCase__			: str=True			,							UpperCamelCase__			: List[str]=None			,							UpperCamelCase__			: Union[str, Any]=True			,							UpperCamelCase__			: Tuple=10			,							UpperCamelCase__			: Dict=8			,							UpperCamelCase__			: Tuple=["stage1", "stage2", "stage3"]			,							UpperCamelCase__			: Tuple=[1, 2, 3]			,							)			->	Dict:







    """simple docstring"""






    __magic_name__				     =       parent
    __magic_name__				     =       batch_size
    __magic_name__				     =       image_size
    __magic_name__				     =       patch_size
    __magic_name__				     =       num_channels
    __magic_name__				     =       embed_dim
    __magic_name__				     =       depths
    __magic_name__				     =       num_heads
    __magic_name__				     =       window_size
    __magic_name__				     =       mlp_ratio
    __magic_name__				     =       qkv_bias
    __magic_name__				     =       hidden_dropout_prob
    __magic_name__				     =       attention_probs_dropout_prob
    __magic_name__				     =       drop_path_rate
    __magic_name__				     =       hidden_act
    __magic_name__				     =       use_absolute_embeddings
    __magic_name__				     =       patch_norm
    __magic_name__				     =       layer_norm_eps
    __magic_name__				     =       initializer_range
    __magic_name__				     =       is_training
    __magic_name__				     =       scope
    __magic_name__				     =       use_labels
    __magic_name__				     =       type_sequence_label_size
    __magic_name__				     =       encoder_stride
    __magic_name__				     =       out_features
    __magic_name__				     =       out_indices
  def 							_lowercase							( self			: str    )			->	Optional[int]:







    """simple docstring"""






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

    __magic_name__				     =       None
    if self.use_labels:
      __magic_name__				     =       ids_tensor([self.batch_size]			,							self.type_sequence_label_size    )

    __magic_name__				     =       self.get_config()

    return config, pixel_values, labels
  def 							_lowercase							( self			: Tuple    )			->	str:







    """simple docstring"""






    return MaskFormerSwinConfig(
        image_size=self.image_size			,							patch_size=self.patch_size			,							num_channels=self.num_channels			,							embed_dim=self.embed_dim			,							depths=self.depths			,							num_heads=self.num_heads			,							window_size=self.window_size			,							mlp_ratio=self.mlp_ratio			,							qkv_bias=self.qkv_bias			,							hidden_dropout_prob=self.hidden_dropout_prob			,							attention_probs_dropout_prob=self.attention_probs_dropout_prob			,							drop_path_rate=self.drop_path_rate			,							hidden_act=self.hidden_act			,							use_absolute_embeddings=self.use_absolute_embeddings			,							path_norm=self.patch_norm			,							layer_norm_eps=self.layer_norm_eps			,							initializer_range=self.initializer_range			,							encoder_stride=self.encoder_stride			,							out_features=self.out_features			,							out_indices=self.out_indices			,							)
  def 							_lowercase							( self			: Optional[int]			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Optional[int]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       MaskFormerSwinModel(config=UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()
    __magic_name__				     =       model(UpperCamelCase__    )

    __magic_name__				     =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
    __magic_name__				     =       int(config.embed_dim * 2 ** (len(config.depths    ) - 1)    )

    self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, expected_seq_len, expected_dim)    )
  def 							_lowercase							( self			: List[str]			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: List[str]    )			->	Tuple:







    """simple docstring"""






    __magic_name__				     =       MaskFormerSwinBackbone(config=UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()
    __magic_name__				     =       model(UpperCamelCase__    )

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps    )			,							len(config.out_features    )    )
    self.parent.assertListEqual(list(result.feature_maps[0].shape    )			,							[13, 16, 16, 16]    )

    # verify channels
    self.parent.assertEqual(len(model.channels    )			,							len(config.out_features    )    )
    self.parent.assertListEqual(model.channels			,							[16, 32, 64]    )

    # verify ValueError
    with self.parent.assertRaises(UpperCamelCase__    ):
      __magic_name__				     =       ["""stem"""]
      __magic_name__				     =       MaskFormerSwinBackbone(config=UpperCamelCase__    )





  def 							_lowercase							( self			: Any    )			->	Any:







    """simple docstring"""






    __magic_name__				     =       self.prepare_config_and_inputs()
    __magic_name__     ,    __magic_name__     ,    __magic_name__				     =       config_and_inputs
    __magic_name__				     =       {"""pixel_values""": pixel_values}
    return config, inputs_dict







@require_torch
class        UpperCAmelCase_			( _A  ,       _A  ,       unittest.TestCase     ):


  '''simple docstring'''







  a__           =		(
      (
          MaskFormerSwinModel,
          MaskFormerSwinBackbone,
      )
      if is_torch_available()
      else ()
  )
  a__           =		{"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
  a__           =		False
  a__           =		False
  a__           =		False
  a__           =		False
  a__           =		False
  def 							_lowercase							( self			: Any    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       MaskFormerSwinModelTester(self    )
    __magic_name__				     =       ConfigTester(self			,							config_class=UpperCamelCase__			,							embed_dim=37    )
  @require_torch_multi_gpu
  @unittest.skip(
      reason=(
          """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"""
          """ `nn.DataParallel`"""
      )    )
  def 							_lowercase							( self			: List[str]    )			->	Optional[int]:







    """simple docstring"""






    pass
  def 							_lowercase							( self			: str    )			->	Dict:







    """simple docstring"""






    self.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()
  def 							_lowercase							( self			: Optional[int]    )			->	List[str]:







    """simple docstring"""






    return
  def 							_lowercase							( self			: str    )			->	str:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__    )
  def 							_lowercase							( self			: int    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_backbone(*UpperCamelCase__    )
  @unittest.skip("""Swin does not use inputs_embeds"""    )
  def 							_lowercase							( self			: Any    )			->	int:







    """simple docstring"""






    pass
  @unittest.skip("""Swin does not support feedforward chunking"""    )
  def 							_lowercase							( self			: str    )			->	List[Any]:







    """simple docstring"""






    pass
  def 							_lowercase							( self			: Union[str, Any]    )			->	Dict:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __magic_name__				     =       model_class(UpperCamelCase__    )
      self.assertIsInstance(model.get_input_embeddings()			,							(nn.Module)    )
      __magic_name__				     =       model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(UpperCamelCase__			,							nn.Linear    )    )
  def 							_lowercase							( self			: Tuple    )			->	Dict:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()

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

      __magic_name__				     =       ["""pixel_values"""]
      self.assertListEqual(arg_names[:1]			,							UpperCamelCase__    )
  @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions"""    )
  def 							_lowercase							( self			: Tuple    )			->	int:







    """simple docstring"""






    pass
  @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone"""    )
  def 							_lowercase							( self			: List[str]    )			->	Dict:







    """simple docstring"""






    pass
  def 							_lowercase							( self			: Union[str, Any]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: Union[str, Any]    )			->	Any:







    """simple docstring"""






    __magic_name__				     =       model_class(UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()

    with torch.no_grad():
      __magic_name__				     =       model(**self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )    )

    __magic_name__				     =       outputs.hidden_states

    __magic_name__				     =       getattr(
        self.model_tester			,							"""expected_num_hidden_layers"""			,							len(self.model_tester.depths    ) + 1    )
    self.assertEqual(len(UpperCamelCase__    )			,							UpperCamelCase__    )

    # Swin has a different seq_length
    __magic_name__				     =       (
        config.patch_size
        if isinstance(config.patch_size			,							collections.abc.Iterable    )
        else (config.patch_size, config.patch_size)
    )

    __magic_name__				     =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

    self.assertListEqual(
        list(hidden_states[0].shape[-2:]    )			,							[num_patches, self.model_tester.embed_dim]			,							)
  def 							_lowercase							( self			: Dict    )			->	Dict:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()

    __magic_name__				     =       (
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size			,							collections.abc.Iterable    )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )

    for model_class in self.all_model_classes:
      __magic_name__				     =       True
      self.check_hidden_states_output(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__    )

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

      self.check_hidden_states_output(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__    )
  def 							_lowercase							( self			: Optional[Any]    )			->	Union[str, Any]:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()
    __magic_name__				     =       3

    __magic_name__				     =       (
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size			,							collections.abc.Iterable    )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )
    __magic_name__				     =       (
        config.patch_size
        if isinstance(config.patch_size			,							collections.abc.Iterable    )
        else (config.patch_size, config.patch_size)
    )

    __magic_name__				     =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
    __magic_name__				     =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

    for model_class in self.all_model_classes:
      __magic_name__				     =       True
      self.check_hidden_states_output(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							(padded_height, padded_width)    )

      # check that output_hidden_states also work using config
      del inputs_dict["output_hidden_states"]
      __magic_name__				     =       True
      self.check_hidden_states_output(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							(padded_height, padded_width)    )
  @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints"""    )
  def 							_lowercase							( self			: Optional[int]    )			->	int:







    """simple docstring"""






    pass
  @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""    )
  def 							_lowercase							( self			: List[str]    )			->	List[Any]:







    """simple docstring"""






    pass
  @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""    )
  def 							_lowercase							( self			: Dict    )			->	Optional[Any]:







    """simple docstring"""






    pass





  def 							_lowercase							( self			: Dict    )			->	Any:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()

    def set_nan_tensor_to_zero(UpperCamelCase__			: Union[str, Any]    ):
      __magic_name__				     =       0
      return t

    def check_equivalence(UpperCamelCase__			: str			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: int={}    ):
      with torch.no_grad():
        __magic_name__				     =       model(**UpperCamelCase__			,							return_dict=UpperCamelCase__			,							**UpperCamelCase__    )
        __magic_name__				     =       model(**UpperCamelCase__			,							return_dict=UpperCamelCase__			,							**UpperCamelCase__    ).to_tuple()

        def recursive_check(UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: List[Any]    ):
          if isinstance(UpperCamelCase__			,							(List, Tuple)    ):
            for tuple_iterable_value, dict_iterable_value in zip(UpperCamelCase__			,							UpperCamelCase__    ):
              recursive_check(UpperCamelCase__			,							UpperCamelCase__    )
          elif isinstance(UpperCamelCase__			,							UpperCamelCase__    ):
            for tuple_iterable_value, dict_iterable_value in zip(
                tuple_object.values()			,							dict_object.values()    ):
              recursive_check(UpperCamelCase__			,							UpperCamelCase__    )
          elif tuple_object is None:
            return
          else:
            self.assertTrue(
                torch.allclose(
                    set_nan_tensor_to_zero(UpperCamelCase__    )			,							set_nan_tensor_to_zero(UpperCamelCase__    )			,							atol=1E-5    )			,							msg=(
                    """Tuple and dict output are not equal. Difference:"""
                    F''' {torch.max(torch.abs(tuple_object - dict_object    )    )}. Tuple has `nan`:'''
                    F''' {torch.isnan(UpperCamelCase__    ).any()} and `inf`: {torch.isinf(UpperCamelCase__    )}. Dict has'''
                    F''' `nan`: {torch.isnan(UpperCamelCase__    ).any()} and `inf`: {torch.isinf(UpperCamelCase__    )}.'''
                )			,							)

        recursive_check(UpperCamelCase__			,							UpperCamelCase__    )

    for model_class in self.all_model_classes:
      __magic_name__				     =       model_class(UpperCamelCase__    )
      model.to(UpperCamelCase__    )
      model.eval()

      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )
      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )
      check_equivalence(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__    )

      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__			,							return_labels=UpperCamelCase__    )
      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__			,							return_labels=UpperCamelCase__    )
      check_equivalence(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__    )

      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )
      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )
      check_equivalence(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							{"""output_hidden_states""": True}    )

      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__			,							return_labels=UpperCamelCase__    )
      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__			,							return_labels=UpperCamelCase__    )
      check_equivalence(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__			,							{"""output_hidden_states""": True}    )







@require_torch
class        UpperCAmelCase_			( unittest.TestCase  ,       _A     ):


  '''simple docstring'''







  a__           =		(MaskFormerSwinBackbone,) if is_torch_available() else ()
  a__           =		MaskFormerSwinConfig
  def 							_lowercase							( self			: Union[str, Any]    )			->	List[Any]:







    """simple docstring"""






    __magic_name__				     =       MaskFormerSwinModelTester(self    )





  def 							_lowercase							( self			: List[str]    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()
    __magic_name__				     =       inputs_dict["""pixel_values"""].shape[0]

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

      __magic_name__				     =       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__				     =       backbone(**UpperCamelCase__			,							output_hidden_states=UpperCamelCase__    )
      self.assertIsNotNone(outputs.hidden_states    )
      self.assertTrue(len(outputs.hidden_states    )			,							len(backbone.stage_names    )    )
      # We skip the stem layer
      for hidden_states, n_channels in zip(outputs.hidden_states[1:]			,							backbone.channels    ):
        for hidden_state in hidden_states:
          # Hidden states are in the format (batch_size, (height * width), n_channels)
          __magic_name__     ,    __magic_name__     ,    __magic_name__				     =       hidden_state.shape
          self.assertTrue((h_batch_size, h_n_channels)			,							(batch_size, n_channels)    )

            # Test output_attentions=True
      if self.has_attentions:
        __magic_name__				     =       backbone(**UpperCamelCase__			,							output_attentions=UpperCamelCase__    )
        self.assertIsNotNone(outputs.attentions    )

