


import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, 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 MaskFormerForInstanceSegmentation, MaskFormerModel

	if is_vision_available():
		from transformers import MaskFormerImageProcessor

if is_vision_available():
	from PIL import Image




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=2       ,  lowercase_=True       ,  lowercase_=False       ,  lowercase_=10       ,  lowercase_=3       ,  lowercase_=32 * 4       ,  lowercase_=32 * 6       ,  lowercase_=4       ,  lowercase_=32       ,  )  -> Union[str, Any]:
											a__      =parent
											a__      =batch_size
											a__      =is_training
											a__      =use_auxiliary_loss
											a__      =num_queries
											a__      =num_channels
											a__      =min_size
											a__      =max_size
											a__      =num_labels
											a__      =mask_feature_size





				def 						__UpperCamelCase    (				self)  -> int:
											a__      =floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to(
											    lowercase_)

											a__      =torch.ones([self.batch_size, self.min_size, self.max_size]       ,  device=lowercase_)

											a__      =(
											    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,  device=lowercase_) > 0.5
											).float()
											a__      =(torch.rand((self.batch_size, self.num_labels)       ,  device=lowercase_) > 0.5).long()

											a__      =self.get_config()
											return config, pixel_values, pixel_mask, mask_labels, class_labels





				def 						__UpperCamelCase    (				self)  -> str:
											return MaskFormerConfig.from_backbone_and_decoder_configs(
											    backbone_config=SwinConfig(
											        depths=[1, 1, 1, 1]       ,  )       ,  decoder_config=DetrConfig(
											        decoder_ffn_dim=128       ,  num_queries=self.num_queries       ,  decoder_attention_heads=2       ,  d_model=self.mask_feature_size       ,  )       ,  mask_feature_size=self.mask_feature_size       ,  fpn_feature_size=self.mask_feature_size       ,  num_channels=self.num_channels       ,  num_labels=self.num_labels       ,  )





				def 						__UpperCamelCase    (				self)  -> Tuple:
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.prepare_config_and_inputs()
											a__      ={'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
											return config, inputs_dict





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_)  -> str:
											a__      =output.encoder_hidden_states
											a__      =output.pixel_decoder_hidden_states
											a__      =output.transformer_decoder_hidden_states

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





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_=False)  -> List[str]:
											with torch.no_grad():
																		a__      =MaskFormerModel(config=lowercase_)
																		model.to(lowercase_)
																		model.eval()

																		a__      =model(pixel_values=lowercase_       ,  pixel_mask=lowercase_)
																		a__      =model(lowercase_       ,  output_hidden_states=lowercase_)
											# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
											# encoder and pixel decoder
											self.parent.assertEqual(
											    output.transformer_decoder_last_hidden_state.shape       ,  (self.batch_size, self.num_queries, self.mask_feature_size)       ,  )
											# 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(lowercase_       ,  lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> Union[str, Any]:
											a__      =MaskFormerForInstanceSegmentation(config=lowercase_)
											model.to(lowercase_)
											model.eval()

											def comm_check_on_output(lowercase_):
																		# 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():
																		a__      =model(pixel_values=lowercase_       ,  pixel_mask=lowercase_)
																		a__      =model(lowercase_)

																		comm_check_on_output(lowercase_)

																		a__      =model(
																		    pixel_values=lowercase_       ,  pixel_mask=lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_)

											comm_check_on_output(lowercase_)

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







@require_torch
class       lowercase_					(lowercase__      ,					lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
				snake_case	    =(
				    {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
				    if is_torch_available()
				    else {}
				)

				snake_case	    =False
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											a__      =MaskFormerModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_       ,  has_text_modality=lowercase_)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											self.config_tester.run_common_tests()





				def 						__UpperCamelCase    (				self)  -> Any:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskformer_model(lowercase_       ,  **lowercase_       ,  output_hidden_states=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Any:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*lowercase_)





				@unittest.skip(reason='MaskFormer does not use inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											pass





				@unittest.skip(reason='MaskFormer does not have a get_input_embeddings method')
				def 						__UpperCamelCase    (				self)  -> Dict:
											pass





				@unittest.skip(reason='MaskFormer is not a generative model')
				def 						__UpperCamelCase    (				self)  -> Dict:
											pass





				@unittest.skip(reason='MaskFormer does not use token embeddings')
				def 						__UpperCamelCase    (				self)  -> Any:
											pass





				@require_torch_multi_gpu
				@unittest.skip(
				    reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





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





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

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

																		a__      =['pixel_values']
																		self.assertListEqual(arg_names[:1]       ,  lowercase_)





				@slow
				def 						__UpperCamelCase    (				self)  -> Any:
											for model_name in ["facebook/maskformer-swin-small-coco"]:
																		a__      =MaskFormerModel.from_pretrained(lowercase_)
																		self.assertIsNotNone(lowercase_)





				def 						__UpperCamelCase    (				self)  -> int:
											a__      =(self.model_tester.min_size,) * 2
											a__      ={
											    'pixel_values': torch.randn((2, 3, *size)       ,  device=lowercase_),
											    'mask_labels': torch.randn((2, 10, *size)       ,  device=lowercase_),
											    'class_labels': torch.zeros(2       ,  10       ,  device=lowercase_).long(),
											}

											a__      =MaskFormerForInstanceSegmentation(MaskFormerConfig()).to(lowercase_)
											a__      =model(**lowercase_)
											self.assertTrue(outputs.loss is not None)





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskformer_model(lowercase_       ,  **lowercase_       ,  output_hidden_states=lowercase_)





				def 						__UpperCamelCase    (				self)  -> int:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		a__      =model_class(lowercase_).to(lowercase_)
																		a__      =model(**lowercase_       ,  output_attentions=lowercase_)
																		self.assertTrue(outputs.attentions is not None)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											if not self.model_tester.is_training:
																		return
											# only MaskFormerForInstanceSegmentation has the loss
											a__      =self.all_model_classes[1]
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.model_tester.prepare_config_and_inputs()

											a__      =model_class(lowercase_)
											model.to(lowercase_)
											model.train()

											a__      =model(lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_).loss
											loss.backward()





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											# only MaskFormerForInstanceSegmentation has the loss
											a__      =self.all_model_classes[1]
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.model_tester.prepare_config_and_inputs()
											a__      =True
											a__      =True

											a__      =model_class(lowercase_)
											model.to(lowercase_)
											model.train()

											a__      =model(lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_)

											a__      =outputs.encoder_hidden_states[0]
											encoder_hidden_states.retain_grad()

											a__      =outputs.pixel_decoder_hidden_states[0]
											pixel_decoder_hidden_states.retain_grad()
											# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
											a__      =outputs.transformer_decoder_hidden_states[0]
											transformer_decoder_hidden_states.retain_grad()

											a__      =outputs.attentions[0]
											attentions.retain_grad()

											outputs.loss.backward(retain_graph=lowercase_)

											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:						Optional[int]   				=       1e-4






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







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





				@cached_property
				def 						__UpperCamelCase    (				self)  -> Tuple:
											return (
											    MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco')
											    if is_vision_available()
											    else None
											)





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco').to(lowercase_)
											a__      =self.default_image_processor
											a__      =prepare_img()
											a__      =image_processor(lowercase_       ,  return_tensors='pt').to(lowercase_)
											a__      =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(lowercase_       ,  (1, 3, 800, 1088))

											with torch.no_grad():
																		a__      =model(**lowercase_)

											a__      =torch.tensor(
											    [[-0.04_82, 0.92_28, 0.49_51], [-0.25_47, 0.80_17, 0.85_27], [-0.00_69, 0.33_85, -0.00_89]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))

											a__      =torch.tensor(
											    [[-0.84_22, -0.84_34, -0.97_18], [-1.01_44, -0.55_65, -0.41_95], [-1.00_38, -0.44_84, -0.19_61]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))

											a__      =torch.tensor(
											    [[0.28_52, -0.01_59, 0.97_35], [0.62_54, 0.18_58, 0.85_29], [-0.06_80, -0.41_16, 1.84_13]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))





				def 						__UpperCamelCase    (				self)  -> Dict:
											a__      =(
											    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco')
											    .to(lowercase_)
											    .eval()
											)
											a__      =self.default_image_processor
											a__      =prepare_img()
											a__      =image_processor(lowercase_       ,  return_tensors='pt').to(lowercase_)
											a__      =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(lowercase_       ,  (1, 3, 800, 1088))

											with torch.no_grad():
																		a__      =model(**lowercase_)
											# masks_queries_logits
											a__      =outputs.masks_queries_logits
											self.assertEqual(
											    masks_queries_logits.shape       ,  (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)       ,  )
											a__      =[
											    [-1.3_73_71_24, -1.7_72_49_37, -1.9_36_42_33],
											    [-1.5_97_72_81, -1.9_86_79_39, -2.1_52_36_95],
											    [-1.5_79_53_98, -1.9_26_98_32, -2.09_39_42],
											]
											a__      =torch.tensor(lowercase_).to(lowercase_)
											self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))
											# class_queries_logits
											a__      =outputs.class_queries_logits
											self.assertEqual(
											    class_queries_logits.shape       ,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1))
											a__      =torch.tensor(
											    [
											        [1.6512e00, -5.2572e00, -3.3519e00],
											        [3.6169e-02, -5.9025e00, -2.9313e00],
											        [1.0766e-04, -7.7630e00, -5.1263e00],
											    ]).to(lowercase_)
											self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =(
											    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff')
											    .to(lowercase_)
											    .eval()
											)
											a__      =self.default_image_processor
											a__      =prepare_img()
											a__      =image_processor(lowercase_       ,  return_tensors='pt').to(lowercase_)
											a__      =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(lowercase_       ,  (1, 3, 800, 1088))

											with torch.no_grad():
																		a__      =model(**lowercase_)
											# masks_queries_logits
											a__      =outputs.masks_queries_logits
											self.assertEqual(
											    masks_queries_logits.shape       ,  (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)       ,  )
											a__      =[[-0.90_46, -2.63_66, -4.60_62], [-3.41_79, -5.78_90, -8.80_57], [-4.91_79, -7.65_60, -10.77_11]]
											a__      =torch.tensor(lowercase_).to(lowercase_)
											self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))
											# class_queries_logits
											a__      =outputs.class_queries_logits
											self.assertEqual(
											    class_queries_logits.shape       ,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1))
											a__      =torch.tensor(
											    [[4.71_88, -3.25_85, -2.88_57], [6.68_71, -2.91_81, -1.24_87], [7.24_49, -2.27_64, -2.18_74]]).to(lowercase_)
											self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =(
											    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco')
											    .to(lowercase_)
											    .eval()
											)
											a__      =self.default_image_processor

											a__      =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'       ,  )

											a__      =inputs['pixel_values'].to(lowercase_)
											a__      =[el.to(lowercase_) for el in inputs['mask_labels']]
											a__      =[el.to(lowercase_) for el in inputs['class_labels']]

											with torch.no_grad():
																		a__      =model(**lowercase_)

											self.assertTrue(outputs.loss is not None)





from .constants import (
    MODEL_NAME,
    OPTIMIZER_NAME,
    RNG_STATE_NAME,
    SAFE_WEIGHTS_INDEX_NAME,
    SAFE_WEIGHTS_NAME,
    SCALER_NAME,
    SCHEDULER_NAME,
    TORCH_LAUNCH_PARAMS,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
)
from .dataclasses import (
    BnbQuantizationConfig,
    ComputeEnvironment,
    CustomDtype,
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    DynamoBackend,
    FPaRecipeKwargs,
    FullyShardedDataParallelPlugin,
    GradientAccumulationPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    KwargsHandler,
    LoggerType,
    MegatronLMPlugin,
    PrecisionType,
    ProjectConfiguration,
    RNGType,
    SageMakerDistributedType,
    TensorInformation,
    TorchDynamoPlugin,
)
from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env
from .imports import (
    get_ccl_version,
    is_abit_bnb_available,
    is_abit_bnb_available,
    is_aim_available,
    is_bfaa_available,
    is_bnb_available,
    is_botoa_available,
    is_ccl_available,
    is_comet_ml_available,
    is_datasets_available,
    is_deepspeed_available,
    is_fpa_available,
    is_ipex_available,
    is_megatron_lm_available,
    is_mlflow_available,
    is_mps_available,
    is_npu_available,
    is_rich_available,
    is_safetensors_available,
    is_sagemaker_available,
    is_tensorboard_available,
    is_tpu_available,
    is_transformers_available,
    is_wandb_available,
    is_xpu_available,
)
from .modeling import (
    check_device_map,
    check_tied_parameters_in_config,
    check_tied_parameters_on_same_device,
    compute_module_sizes,
    convert_file_size_to_int,
    dtype_byte_size,
    find_tied_parameters,
    get_balanced_memory,
    get_max_layer_size,
    get_max_memory,
    get_mixed_precision_context_manager,
    id_tensor_storage,
    infer_auto_device_map,
    load_checkpoint_in_model,
    load_offloaded_weights,
    load_state_dict,
    named_module_tensors,
    retie_parameters,
    set_module_tensor_to_device,
    shard_checkpoint,
)
from .offload import (
    OffloadedWeightsLoader,
    PrefixedDataset,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
    save_offload_index,
)
from .operations import (
    broadcast,
    broadcast_object_list,
    concatenate,
    convert_outputs_to_fpaa,
    convert_to_fpaa,
    find_batch_size,
    find_device,
    gather,
    gather_object,
    get_data_structure,
    honor_type,
    initialize_tensors,
    is_namedtuple,
    is_tensor_information,
    is_torch_tensor,
    listify,
    pad_across_processes,
    recursively_apply,
    reduce,
    send_to_device,
    slice_tensors,
)
from .versions import compare_versions, is_torch_version


if is_deepspeed_available():
	from .deepspeed import (
	    DeepSpeedEngineWrapper,
	    DeepSpeedOptimizerWrapper,
	    DeepSpeedSchedulerWrapper,
	    DummyOptim,
	    DummyScheduler,
	    HfDeepSpeedConfig,
	)

from .bnb import has_abit_bnb_layers, load_and_quantize_model
from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer
from .launch import (
    PrepareForLaunch,
    _filter_args,
    prepare_deepspeed_cmd_env,
    prepare_multi_gpu_env,
    prepare_sagemager_args_inputs,
    prepare_simple_launcher_cmd_env,
    prepare_tpu,
)
from .megatron_lm import (
    AbstractTrainStep,
    BertTrainStep,
    GPTTrainStep,
    MegatronEngine,
    MegatronLMDummyDataLoader,
    MegatronLMDummyScheduler,
    MegatronLMOptimizerWrapper,
    MegatronLMSchedulerWrapper,
    TaTrainStep,
    avg_losses_across_data_parallel_group,
    gather_across_data_parallel_groups,
)
from .megatron_lm import initialize as megatron_lm_initialize
from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader
from .megatron_lm import prepare_model as megatron_lm_prepare_model
from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer
from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler
from .memory import find_executable_batch_size, release_memory
from .other import (
    extract_model_from_parallel,
    get_pretty_name,
    is_port_in_use,
    merge_dicts,
    patch_environment,
    save,
    wait_for_everyone,
    write_basic_config,
)
from .random import set_seed, synchronize_rng_state, synchronize_rng_states
from .torch_xla import install_xla
from .tqdm import tqdm
from .transformer_engine import convert_model, has_transformer_engine_layers

