




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






   '''simple docstring'''





   def __init__( self			:   List[str]       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   Any=2       ,						lowerCamelCase_			:   List[Any]=True       ,						lowerCamelCase_			:   Optional[int]=False       ,						lowerCamelCase_			:   str=10       ,						lowerCamelCase_			:   Any=3       ,						lowerCamelCase_			:   Dict=32 * 8       ,						lowerCamelCase_			:   int=32 * 8       ,						lowerCamelCase_			:   Union[str, Any]=4       ,						lowerCamelCase_			:   Optional[int]=64       ,						)						->   Tuple:
        __magic_name__							:	List[Any]    							=							parent
        __magic_name__							:	Any    							=							batch_size
        __magic_name__							:	List[str]    							=							is_training
        __magic_name__							:	Union[str, Any]    							=							use_auxiliary_loss
        __magic_name__							:	str    							=							num_queries
        __magic_name__							:	Dict    							=							num_channels
        __magic_name__							:	str    							=							min_size
        __magic_name__							:	Dict    							=							max_size
        __magic_name__							:	Any    							=							num_labels
        __magic_name__							:	Tuple    							=							hidden_dim
        __magic_name__							:	List[str]    							=							hidden_dim

   def 							UpperCAmelCase__       ( self			:   int  )						->   Tuple:
        __magic_name__							:	int    							=							floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]  ).to(
            UpperCamelCase__  )

        __magic_name__							:	Dict    							=							torch.ones([self.batch_size, self.min_size, self.max_size]       ,						device=UpperCamelCase__  )

        __magic_name__							:	Any    							=							(
            torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,						device=UpperCamelCase__  ) > 0.5
        ).float()
        __magic_name__							:	Tuple    							=							(torch.rand((self.batch_size, self.num_labels)       ,						device=UpperCamelCase__  ) > 0.5).long()

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

   def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   List[str]:
        __magic_name__							:	int    							=							MaskaFormerConfig(
            hidden_size=self.hidden_dim       ,						)
        __magic_name__							:	Optional[Any]    							=							self.num_queries
        __magic_name__							:	List[Any]    							=							self.num_labels

        __magic_name__							:	Optional[int]    							=							[1, 1, 1, 1]
        __magic_name__							:	Tuple    							=							self.num_channels

        __magic_name__							:	Any    							=							64
        __magic_name__							:	List[str]    							=							128
        __magic_name__							:	Dict    							=							self.hidden_dim
        __magic_name__							:	List[str]    							=							self.hidden_dim
        __magic_name__							:	int    							=							self.hidden_dim
        return config

   def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Dict:
        __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							:	List[str]    							=							self.prepare_config_and_inputs()
        __magic_name__							:	Tuple    							=							{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
        return config, inputs_dict

   def 							UpperCAmelCase__       ( self			:   Any       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   List[str]  )						->   str:
        __magic_name__							:	Any    							=							output.encoder_hidden_states
        __magic_name__							:	List[Any]    							=							output.pixel_decoder_hidden_states
        __magic_name__							:	Tuple    							=							output.transformer_decoder_hidden_states

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

   def 							UpperCAmelCase__       ( self			:   List[Any]       ,						lowerCamelCase_			:   List[str]       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   Tuple=False  )						->   Dict:





        with torch.no_grad():
             __magic_name__							:	int    							=							MaskaFormerModel(config=UpperCamelCase__  )
             model.to(UpperCamelCase__  )
             model.eval()

             __magic_name__							:	Tuple    							=							model(pixel_values=UpperCamelCase__       ,						pixel_mask=UpperCamelCase__  )
             __magic_name__							:	Dict    							=							model(UpperCamelCase__       ,						output_hidden_states=UpperCamelCase__  )

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


   def 							UpperCAmelCase__       ( self			:   List[Any]       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   str       ,						lowerCamelCase_			:   str       ,						lowerCamelCase_			:   Optional[Any]       ,						lowerCamelCase_			:   str  )						->   int:
        __magic_name__							:	Optional[int]    							=							MaskaFormerForUniversalSegmentation(config=UpperCamelCase__  )
        model.to(UpperCamelCase__  )
        model.eval()

        def comm_check_on_output(lowerCamelCase_			:   Optional[Any]  ):
             # 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():
             __magic_name__							:	Any    							=							model(pixel_values=UpperCamelCase__       ,						pixel_mask=UpperCamelCase__  )
             __magic_name__							:	Union[str, Any]    							=							model(UpperCamelCase__  )

             comm_check_on_output(UpperCamelCase__  )

             __magic_name__							:	int    							=							model(
                 pixel_values=UpperCamelCase__       ,						pixel_mask=UpperCamelCase__       ,						mask_labels=UpperCamelCase__       ,						class_labels=UpperCamelCase__  )

        comm_check_on_output(UpperCamelCase__  )

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






@require_torch
class    lowerCamelCase    (		_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 							UpperCAmelCase__       ( self			:   str  )						->   List[str]:
        __magic_name__							:	List[str]    							=							MaskaFormerModelTester(self  )
        __magic_name__							:	Optional[Any]    							=							ConfigTester(self       ,						config_class=UpperCamelCase__       ,						has_text_modality=UpperCamelCase__  )

   def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Dict:
        self.config_tester.run_common_tests()

   def 							UpperCAmelCase__       ( self			:   int  )						->   Optional[int]:
        __magic_name__							,    __magic_name__							:	List[str]    							=							self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskaformer_model(UpperCamelCase__       ,						**UpperCamelCase__       ,						output_hidden_states=UpperCamelCase__  )

   def 							UpperCAmelCase__       ( self			:   Dict  )						->   Optional[int]:
        __magic_name__							:	Union[str, Any]    							=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*UpperCamelCase__  )

   @unittest.skip(reason='''Mask2Former does not use inputs_embeds'''  )
   def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Union[str, Any]:
        pass

   @unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''  )
   def 							UpperCAmelCase__       ( self			:   Any  )						->   Any:
        pass

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

   @unittest.skip(reason='''Mask2Former does not use token embeddings'''  )
   def 							UpperCAmelCase__       ( self			:   Dict  )						->   Union[str, Any]:
        pass

   @require_torch_multi_gpu
   @unittest.skip(
       reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''  )
   def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Dict:
        pass

   @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
   def 							UpperCAmelCase__       ( self			:   str  )						->   Union[str, Any]:
        pass

   def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Dict:
        __magic_name__							,    __magic_name__							:	Optional[int]    							=							self.model_tester.prepare_config_and_inputs_for_common()

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

             __magic_name__							:	List[str]    							=							['''pixel_values''']
             self.assertListEqual(arg_names[:1]       ,						UpperCamelCase__  )

   @slow
   def 							UpperCAmelCase__       ( self			:   str  )						->   Any:





        for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
             __magic_name__							:	int    							=							MaskaFormerModel.from_pretrained(UpperCamelCase__  )
             self.assertIsNotNone(UpperCamelCase__  )

   def 							UpperCAmelCase__       ( self			:   str  )						->   Dict:
        __magic_name__							:	Optional[int]    							=							(self.model_tester.min_size,) * 2
        __magic_name__							:	List[Any]    							=							{
            '''pixel_values''': torch.randn((2, 3, *size)       ,						device=UpperCamelCase__  ),
            '''mask_labels''': torch.randn((2, 10, *size)       ,						device=UpperCamelCase__  ),
            '''class_labels''': torch.zeros(2       ,						10       ,						device=UpperCamelCase__  ).long(),
        }
        __magic_name__							:	Optional[Any]    							=							self.model_tester.get_config()

        __magic_name__							:	Optional[int]    							=							MaskaFormerForUniversalSegmentation(UpperCamelCase__  ).to(UpperCamelCase__  )
        __magic_name__							:	Dict    							=							model(**UpperCamelCase__  )
        self.assertTrue(outputs.loss is not None  )

   def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Optional[int]:
        __magic_name__							,    __magic_name__							:	str    							=							self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskaformer_model(UpperCamelCase__       ,						**UpperCamelCase__       ,						output_hidden_states=UpperCamelCase__  )

   def 							UpperCAmelCase__       ( self			:   str  )						->   Dict:
        __magic_name__							,    __magic_name__							:	Optional[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             __magic_name__							:	Dict    							=							model_class(UpperCamelCase__  ).to(UpperCamelCase__  )
             __magic_name__							:	Optional[Any]    							=							model(**UpperCamelCase__       ,						output_attentions=UpperCamelCase__  )
             self.assertTrue(outputs.attentions is not None  )

   def 							UpperCAmelCase__       ( self			:   int  )						->   List[str]:





        if not self.model_tester.is_training:
             return

        __magic_name__							:	Any    							=							self.all_model_classes[1]
        __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							:	Dict    							=							self.model_tester.prepare_config_and_inputs()

        __magic_name__							:	Dict    							=							model_class(UpperCamelCase__  )
        model.to(UpperCamelCase__  )
        model.train()

        __magic_name__							:	List[str]    							=							model(UpperCamelCase__       ,						mask_labels=UpperCamelCase__       ,						class_labels=UpperCamelCase__  ).loss
        loss.backward()


   def 							UpperCAmelCase__       ( self			:   int  )						->   Dict:
        __magic_name__							:	Optional[Any]    							=							self.all_model_classes[1]
        __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							:	Union[str, Any]    							=							self.model_tester.prepare_config_and_inputs()
        __magic_name__							:	Union[str, Any]    							=							True
        __magic_name__							:	str    							=							True

        __magic_name__							:	Tuple    							=							model_class(UpperCamelCase__  ).to(UpperCamelCase__  )
        model.train()

        __magic_name__							:	Dict    							=							model(UpperCamelCase__       ,						mask_labels=UpperCamelCase__       ,						class_labels=UpperCamelCase__  )

        __magic_name__							:	Optional[int]    							=							outputs.encoder_hidden_states[0]
        encoder_hidden_states.retain_grad()

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

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

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

        outputs.loss.backward(retain_graph=UpperCamelCase__  )

        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   :						List[Any]				=			1E-4







def 		lowercase__   (		):





     '''simple docstring'''






     __magic_name__							:	List[str]    							=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
     return image


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






   '''simple docstring'''





   @cached_property
   def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Any:
        return "facebook/mask2former-swin-small-coco-instance"

   @cached_property
   def 							UpperCAmelCase__       ( self			:   Any  )						->   str:
        return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints  ) if is_vision_available() else None

   def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   List[str]:
        __magic_name__							:	List[Any]    							=							MaskaFormerModel.from_pretrained(self.model_checkpoints  ).to(UpperCamelCase__  )
        __magic_name__							:	Dict    							=							self.default_image_processor
        __magic_name__							:	Any    							=							prepare_img()
        __magic_name__							:	Optional[int]    							=							image_processor(UpperCamelCase__       ,						return_tensors='''pt'''  ).to(UpperCamelCase__  )
        __magic_name__							:	Tuple    							=							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(UpperCamelCase__       ,						(1, 3, 384, 384)  )

        with torch.no_grad():
             __magic_name__							:	Any    							=							model(**UpperCamelCase__  )

        __magic_name__							:	Any    							=							torch.tensor(
            [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]]  ).to(UpperCamelCase__  )
        self.assertTrue(
            torch.allclose(
                outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,						UpperCamelCase__       ,						atol=UpperCamelCase__  )  )

        __magic_name__							:	Any    							=							torch.tensor(
            [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]]  ).to(UpperCamelCase__  )
        self.assertTrue(
            torch.allclose(
                outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,						UpperCamelCase__       ,						atol=UpperCamelCase__  )  )

        __magic_name__							:	str    							=							torch.tensor(
            [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]]  ).to(UpperCamelCase__  )
        self.assertTrue(
            torch.allclose(
                outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,						UpperCamelCase__       ,						atol=UpperCamelCase__  )  )

   def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Union[str, Any]:
        __magic_name__							:	Any    							=							MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints  ).to(UpperCamelCase__  ).eval()
        __magic_name__							:	Union[str, Any]    							=							self.default_image_processor
        __magic_name__							:	int    							=							prepare_img()
        __magic_name__							:	Dict    							=							image_processor(UpperCamelCase__       ,						return_tensors='''pt'''  ).to(UpperCamelCase__  )
        __magic_name__							:	Dict    							=							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(UpperCamelCase__       ,						(1, 3, 384, 384)  )

        with torch.no_grad():
             __magic_name__							:	Union[str, Any]    							=							model(**UpperCamelCase__  )
        # masks_queries_logits
        __magic_name__							:	Any    							=							outputs.masks_queries_logits
        self.assertEqual(
            masks_queries_logits.shape       ,						(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)  )
        __magic_name__							:	List[str]    							=							[
            [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1],
            [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1],
            [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5],
        ]
        __magic_name__							:	List[str]    							=							torch.tensor(UpperCamelCase__  ).to(UpperCamelCase__  )
        self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,						UpperCamelCase__       ,						atol=UpperCamelCase__  )  )
        # class_queries_logits
        __magic_name__							:	List[str]    							=							outputs.class_queries_logits
        self.assertEqual(class_queries_logits.shape       ,						(1, model.config.num_queries, model.config.num_labels + 1)  )
        __magic_name__							:	List[str]    							=							torch.tensor(
            [
                [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2],
                [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3],
                [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5],
            ]  ).to(UpperCamelCase__  )
        self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,						UpperCamelCase__       ,						atol=UpperCamelCase__  )  )


   def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   List[Any]:
        __magic_name__							:	List[Any]    							=							MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints  ).to(UpperCamelCase__  ).eval()
        __magic_name__							:	int    							=							self.default_image_processor

        __magic_name__							:	Optional[int]    							=							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'''       ,						)

        __magic_name__							:	Tuple    							=							inputs['''pixel_values'''].to(UpperCamelCase__  )
        __magic_name__							:	Optional[int]    							=							[el.to(UpperCamelCase__  ) for el in inputs['''mask_labels''']]
        __magic_name__							:	List[str]    							=							[el.to(UpperCamelCase__  ) for el in inputs['''class_labels''']]

        with torch.no_grad():
             __magic_name__							:	Tuple    							=							model(**UpperCamelCase__  )

        self.assertTrue(outputs.loss is not None  )









import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


__lowerCamelCase   :						List[Any]				=			logging.get_logger(__name__)







def 		lowercase__   (		):





					'''simple docstring'''






					__magic_name__							:	Any    							=							os.getenv('''SM_HP_MP_PARAMETERS'''							,'''{}'''				)
					try:
										# Parse it and check the field "partitions" is included, it is required for model parallel.
										__magic_name__							:	Tuple    							=							json.loads(__A				)
										if "partitions" not in smp_options:
															return False
					except json.JSONDecodeError:
										return False

					# Get the sagemaker specific framework parameters from mpi_options variable.
					__magic_name__							:	int    							=							os.getenv('''SM_FRAMEWORK_PARAMS'''							,'''{}'''				)
					try:
										# Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
										__magic_name__							:	List[Any]    							=							json.loads(__A				)
										if not mpi_options.get('''sagemaker_mpi_enabled'''							,__A				):
															return False
					except json.JSONDecodeError:
										return False
					# Lastly, check if the `smdistributed` module is present.
					return importlib.util.find_spec('''smdistributed'''				) is not None


if is_sagemaker_model_parallel_available():
	import smdistributed.modelparallel.torch as smp

	smp.init()


@dataclass
class    lowerCamelCase    (		_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								=field(
			    default=''''''				,metadata={'''help''': '''Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'''}				,)

			def 							UpperCAmelCase__       ( self			:   str  )						->   List[str]:
								super().__post_init__()
								warnings.warn(
								    '''`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '''
								    '''`TrainingArguments` instead.'''       ,						lowerCamelCase_       ,						)

			@cached_property
			def 							UpperCAmelCase__       ( self			:   int  )						->   "torch.device":
								logger.info('''PyTorch: setting up devices'''  )
								if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
													logger.warning(
													    '''torch.distributed process group is initialized, but local_rank == -1. '''
													    '''In order to use Torch DDP, launch your script with `python -m torch.distributed.launch'''  )
								if self.no_cuda:
													__magic_name__							:	Optional[int]    							=							torch.device('''cpu'''  )
													__magic_name__							:	int    							=							0
								elif is_sagemaker_model_parallel_available():
													__magic_name__							:	Any    							=							smp.local_rank()
													__magic_name__							:	int    							=							torch.device('''cuda'''       ,						lowerCamelCase_  )
													__magic_name__							:	Optional[int]    							=							1
								elif is_sagemaker_dp_enabled():
													import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

													torch.distributed.init_process_group(backend='''smddp'''       ,						timeout=self.ddp_timeout_delta  )
													__magic_name__							:	Optional[int]    							=							int(os.getenv('''SMDATAPARALLEL_LOCAL_RANK'''  )  )
													__magic_name__							:	Optional[int]    							=							torch.device('''cuda'''       ,						self.local_rank  )
													__magic_name__							:	str    							=							1
								elif self.local_rank == -1:
													# if n_gpu is > 1 we'll use nn.DataParallel.
													# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
													# Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
													# trigger an error that a device index is missing. Index 0 takes into account the
													# GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
													# will use the first GPU in that env, i.e. GPU#1
													__magic_name__							:	List[str]    							=							torch.device('''cuda:0''' if torch.cuda.is_available() else '''cpu'''  )
													# Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
													# the default value.
													__magic_name__							:	Dict    							=							torch.cuda.device_count()
								else:
													# Here, we'll use torch.distributed.
													# Initializes the distributed backend which will take care of synchronizing nodes/GPUs
													if not torch.distributed.is_initialized():
																		torch.distributed.init_process_group(backend='''nccl'''       ,						timeout=self.ddp_timeout_delta  )
													__magic_name__							:	str    							=							torch.device('''cuda'''       ,						self.local_rank  )
													__magic_name__							:	Dict    							=							1

								if device.type == "cuda":
													torch.cuda.set_device(lowerCamelCase_  )

								return device

			@property
			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   int:
								if is_sagemaker_model_parallel_available():
													return smp.dp_size()

								return super().world_size

			@property
			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   List[str]:
								return not is_sagemaker_model_parallel_available()


			@property
			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Tuple:
								return False



