





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       __A							:




      """simple docstring"""







      def __init__(		self   ,				a__   ,				a__=2   ,				a__=True   ,				a__=False   ,				a__=10   ,				a__=3   ,				a__=32 * 8   ,				a__=32 * 8   ,				a__=4   ,				a__=64   ,				):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  Tuple						=		torch.ones([self.batch_size, self.min_size, self.max_size]   ,				device=a__)

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

             _lowerCamelCase				:  Dict						=		self.get_config()
             return config, pixel_values, pixel_mask, mask_labels, class_labels


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  str						=		MaskaFormerConfig(
                 hidden_size=self.hidden_dim   ,				)
             _lowerCamelCase				:  List[str]						=		self.num_queries
             _lowerCamelCase				:  Optional[int]						=		self.num_labels

             _lowerCamelCase				:  int						=		[1, 1, 1, 1]
             _lowerCamelCase				:  List[Any]						=		self.num_channels

             _lowerCamelCase				:  int						=		64
             _lowerCamelCase				:  Optional[Any]						=		128
             _lowerCamelCase				:  str						=		self.hidden_dim
             _lowerCamelCase				:  List[Any]						=		self.hidden_dim
             _lowerCamelCase				:  str						=		self.hidden_dim
             return config


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  Union[str, Any]						=		self.prepare_config_and_inputs()
             _lowerCamelCase				:  int						=		{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
             return config, inputs_dict


      def 					__snake_case      (		self   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		output.encoder_hidden_states
             _lowerCamelCase				:  str						=		output.pixel_decoder_hidden_states
             _lowerCamelCase				:  int						=		output.transformer_decoder_hidden_states

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


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__=False):



             """simple docstring"""





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

                    _lowerCamelCase				:  Union[str, Any]						=		model(pixel_values=a__   ,				pixel_mask=a__)
                    _lowerCamelCase				:  List[Any]						=		model(a__   ,				output_hidden_states=a__)

             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(a__   ,				a__)



      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		MaskaFormerForUniversalSegmentation(config=a__)
             model.to(a__)
             model.eval()

             def comm_check_on_output(a__):
                    # 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=a__   ,				pixel_mask=a__)
                    _lowerCamelCase				:  Optional[int]						=		model(a__)

                    comm_check_on_output(a__)

                    _lowerCamelCase				:  Optional[int]						=		model(
                        pixel_values=a__   ,				pixel_mask=a__   ,				mask_labels=a__   ,				class_labels=a__)

             comm_check_on_output(a__)

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







@require_torch
class       __A							(   lowerCamelCase__			,lowerCamelCase__			,unittest.TestCase	):




      """simple docstring"""







      UpperCAmelCase__							=   (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
      UpperCAmelCase__							=   {"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False
      UpperCAmelCase__							=   False


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		MaskaFormerModelTester(self)
             _lowerCamelCase				:  List[str]						=		ConfigTester(self   ,				config_class=a__   ,				has_text_modality=a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             self.config_tester.run_common_tests()


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  int						=		self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.create_and_check_maskaformer_model(a__   ,				**a__   ,				output_hidden_states=a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*a__)


      @unittest.skip(reason='''Mask2Former does not use inputs_embeds''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''Mask2Former is not a generative model''')
      def 					__snake_case      (		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''Mask2Former does not use token embeddings''')
      def 					__snake_case      (		self):



             """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):



             """simple docstring"""





             pass


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



             """simple docstring"""





             pass


      def 					__snake_case      (		self):



             """simple docstring"""





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

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

                    _lowerCamelCase				:  Optional[int]						=		['''pixel_values''']
                    self.assertListEqual(arg_names[:1]   ,				a__)


      @slow
      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		(self.model_tester.min_size,) * 2
             _lowerCamelCase				:  Dict						=		{
                 '''pixel_values''': torch.randn((2, 3, *size)   ,				device=a__),
                 '''mask_labels''': torch.randn((2, 10, *size)   ,				device=a__),
                 '''class_labels''': torch.zeros(2   ,				10   ,				device=a__).long(),
             }
             _lowerCamelCase				:  Dict						=		self.model_tester.get_config()

             _lowerCamelCase				:  int						=		MaskaFormerForUniversalSegmentation(a__).to(a__)
             _lowerCamelCase				:  List[Any]						=		model(**a__)
             self.assertTrue(outputs.loss is not None)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase,	_lowerCamelCase				:  Union[str, Any]						=		self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.create_and_check_maskaformer_model(a__   ,				**a__   ,				output_hidden_states=a__)


      def 					__snake_case      (		self):



             """simple docstring"""





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

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


      def 					__snake_case      (		self):



             """simple docstring"""





             if not self.model_tester.is_training:
                    return

             _lowerCamelCase				:  List[str]						=		self.all_model_classes[1]
             _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  Any						=		self.model_tester.prepare_config_and_inputs()

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

             _lowerCamelCase				:  Optional[Any]						=		model(a__   ,				mask_labels=a__   ,				class_labels=a__).loss
             loss.backward()



      def 					__snake_case      (		self):



             """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				:  Dict						=		True
             _lowerCamelCase				:  List[Any]						=		True

             _lowerCamelCase				:  Dict						=		model_class(a__).to(a__)
             model.train()

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

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

             _lowerCamelCase				:  Optional[int]						=		outputs.pixel_decoder_hidden_states[0]
             pixel_decoder_hidden_states.retain_grad()

             _lowerCamelCase				:  Tuple						=		outputs.transformer_decoder_hidden_states[0]
             transformer_decoder_hidden_states.retain_grad()

             _lowerCamelCase				:  Union[str, Any]						=		outputs.attentions[0]
             attentions.retain_grad()

             outputs.loss.backward(retain_graph=a__)

             self.assertIsNotNone(encoder_hidden_states.grad)
             self.assertIsNotNone(pixel_decoder_hidden_states.grad)
             self.assertIsNotNone(transformer_decoder_hidden_states.grad)
             self.assertIsNotNone(attentions.grad)
_lowerCamelCase      							=   1E-4




def  __UpperCAmelCase(   ):
       _lowerCamelCase				:  List[Any]						=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
       return image







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




      """simple docstring"""







      @cached_property
      def 					__snake_case      (		self):



             """simple docstring"""





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


      @cached_property
      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





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

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

             _lowerCamelCase				:  Union[str, Any]						=		torch.tensor(
                 [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,				a__   ,				atol=a__))

             _lowerCamelCase				:  Optional[Any]						=		torch.tensor(
                 [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,				a__   ,				atol=a__))

             _lowerCamelCase				:  List[Any]						=		torch.tensor(
                 [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]).to(a__)
             self.assertTrue(
                 torch.allclose(
                     outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,				a__   ,				atol=a__))


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Tuple						=		MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(a__).eval()
             _lowerCamelCase				:  Optional[int]						=		self.default_image_processor
             _lowerCamelCase				:  List[str]						=		prepare_img()
             _lowerCamelCase				:  Optional[int]						=		image_processor(a__   ,				return_tensors='''pt''').to(a__)
             _lowerCamelCase				:  str						=		inputs['''pixel_values'''].shape
             # check size is divisible by 32
             self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0)
             # check size
             self.assertEqual(a__   ,				(1, 3, 384, 384))

             with torch.no_grad():
                    _lowerCamelCase				:  Union[str, Any]						=		model(**a__)
             # 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				:  Optional[int]						=		[
                 [-8.7839, -9.0056, -8.8121],
                 [-7.4104, -7.0313, -6.5401],
                 [-6.6105, -6.3427, -6.4675],
             ]
             _lowerCamelCase				:  Optional[Any]						=		torch.tensor(a__).to(a__)
             self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,				a__   ,				atol=a__))
             # class_queries_logits
             _lowerCamelCase				:  int						=		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.8324, -8.0835, -4.1922],
                     [0.8450, -9.0050, -3.6053],
                     [0.3045, -7.7293, -3.0275],
                 ]).to(a__)
             self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,				a__   ,				atol=a__))



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[int]						=		MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(a__).eval()
             _lowerCamelCase				:  Dict						=		self.default_image_processor

             _lowerCamelCase				:  Optional[Any]						=		image_processor(
                 [np.zeros((3, 800, 1333)), np.zeros((3, 800, 1333))]   ,				segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)]   ,				return_tensors='''pt'''   ,				)

             _lowerCamelCase				:  List[str]						=		inputs['''pixel_values'''].to(a__)
             _lowerCamelCase				:  Optional[int]						=		[el.to(a__) for el in inputs['''mask_labels''']]
             _lowerCamelCase				:  Optional[Any]						=		[el.to(a__) for el in inputs['''class_labels''']]

             with torch.no_grad():
                    _lowerCamelCase				:  List[Any]						=		model(**a__)

             self.assertTrue(outputs.loss is not None)








import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
       from sagemaker import Session, TrainingJobAnalytics
       from sagemaker.huggingface import HuggingFace







@pytest.mark.skipif(
    literal_eval(os.getenv("""TEST_SAGEMAKER"""			,"""False"""	)	) is not True			,reason="""Skipping test because should only be run when releasing minor transformers version"""			,)
@pytest.mark.usefixtures("""sm_env"""	)
@parameterized_class(
    [
        {
            """framework""": """pytorch""",
            """script""": """run_glue.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.6, """eval_loss""": 0.9},
        },
        {
            """framework""": """tensorflow""",
            """script""": """run_tf.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.3, """eval_loss""": 0.9},
        },
    ]	)
class       __A							(   unittest.TestCase	):




      """simple docstring"""







      def 					__snake_case      (		self):



             """simple docstring"""





             if self.framework == "pytorch":
                    subprocess.run(
                        F"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split()   ,				encoding='''utf-8'''   ,				check=a__   ,				)
             assert hasattr(self   ,				'''env''')


      def 					__snake_case      (		self   ,				a__=1):



             """simple docstring"""





             return HuggingFace(
                 entry_point=self.script   ,				source_dir=self.env.test_path   ,				role=self.env.role   ,				image_uri=self.env.image_uri   ,				base_job_name=F"""{self.env.base_job_name}-single"""   ,				instance_count=a__   ,				instance_type=self.instance_type   ,				debugger_hook_config=a__   ,				hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path}   ,				metric_definitions=self.env.metric_definitions   ,				py_version='''py36'''   ,				)


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             TrainingJobAnalytics(a__).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""")



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.create_estimator()

             # run training
             estimator.fit()

             # result dataframe
             _lowerCamelCase				:  Tuple						=		TrainingJobAnalytics(estimator.latest_training_job.name).dataframe()

             # extract kpis
             _lowerCamelCase				:  Tuple						=		list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''])
             _lowerCamelCase				:  Dict						=		list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''])
             # get train time from SageMaker job, this includes starting, preprocessing, stopping
             _lowerCamelCase				:  Optional[Any]						=		(
                 Session().describe_training_job(estimator.latest_training_job.name).get('''TrainingTimeInSeconds'''   ,				99_9999)
             )

             # assert kpis
             assert train_runtime <= self.results["train_runtime"]
             assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy)
             assert all(t <= self.results['''eval_loss'''] for t in eval_loss)

             # dump tests result into json file to share in PR
             with open(F"""{estimator.latest_training_job.name}.json"""   ,				'''w''') as outfile:
                    json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss}   ,				a__)

