'''simple docstring'''


import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


snake_case__	:     Optional[int]      =    namedtuple('''ExampleNamedTuple''', '''a b c''')






class 	__SCREAMING_SNAKE_CASE		(			unittest.TestCase			):







  '''simple docstring'''






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = torch.randn(5 ,						2		)
       UpperCAmelCase_				:  Optional[Any]  = torch.device('cuda'		) if torch.cuda.is_available() else torch.device('cpu'		)

       UpperCAmelCase_				:  Dict  = send_to_device(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.cpu() ,						snake_case_		)		)

       UpperCAmelCase_				:  Tuple  = send_to_device((tensor, [tensor, tensor], 1) ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta[0].cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta[1] ,						snake_case_		)
       self.assertTrue(torch.equal(resulta[1][0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta[1][1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta[2] ,						1		)

       UpperCAmelCase_				:  List[Any]  = send_to_device({'a': tensor, 'b': [tensor, tensor], 'c': 1} ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta['a'].cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta['b'] ,						snake_case_		)
       self.assertTrue(torch.equal(resulta['b'][0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta['b'][1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta['c'] ,						1		)

       UpperCAmelCase_				:  Tuple  = send_to_device(ExampleNamedTuple(a=snake_case_ ,						b=[tensor, tensor] ,						c=1		) ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.a.cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta.b ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.b[0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta.b[1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta.c ,						1		)

       UpperCAmelCase_				:  int  = send_to_device(UserDict({'a': tensor, 'b': [tensor, tensor], 'c': 1}		) ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta['a'].cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta['b'] ,						snake_case_		)
       self.assertTrue(torch.equal(resulta['b'][0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta['b'][1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta['c'] ,						1		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       with self.assertRaises(snake_case_		) as cm:
            UpperCAmelCase_				:  Optional[Any]  = recursively_apply(torch.tensor ,						(torch.tensor(1		), 1) ,						error_on_other_type=snake_case_		)
       self.assertEqual(
           str(cm.exception		) ,						'Unsupported types (<class \'int\'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed.' ,						)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Dict  = torch.tensor([1, 2, 3, 4, 5]		)
       self.assertEqual(listify(snake_case_		) ,						[1, 2, 3, 4, 5]		)

       UpperCAmelCase_				:  List[str]  = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]		)
       self.assertEqual(listify(snake_case_		) ,						[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]		)

       UpperCAmelCase_				:  int  = torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]		)
       self.assertEqual(
           listify(snake_case_		) ,						[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       with patch_environment(aa=1 ,						BB=2		):
            self.assertEqual(os.environ.get('AA'		) ,						'1'		)
            self.assertEqual(os.environ.get('BB'		) ,						'2'		)

       self.assertNotIn('AA' ,						os.environ		)
       self.assertNotIn('BB' ,						os.environ		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[int]  = RegressionModel()
       UpperCAmelCase_				:  Optional[Any]  = model.forward
       UpperCAmelCase_				:  List[Any]  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  Any  = extract_model_from_parallel(snake_case_ ,						keep_fpaa_wrapper=snake_case_		)
       UpperCAmelCase_				:  Optional[Any]  = pickle.dumps(snake_case_		)


  @require_cuda
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Tuple  = RegressionModel()
       UpperCAmelCase_				:  Tuple  = model.forward
       UpperCAmelCase_				:  Optional[Any]  = torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
       UpperCAmelCase_				:  int  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  Optional[Any]  = extract_model_from_parallel(snake_case_ ,						keep_fpaa_wrapper=snake_case_		)
       UpperCAmelCase_				:  List[Any]  = pickle.dumps(snake_case_		)


  @require_cuda
  @require_torch_min_version(version='2.0'		)
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Tuple  = RegressionModel()
       UpperCAmelCase_				:  str  = model.forward
       UpperCAmelCase_				:  str  = torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
       UpperCAmelCase_				:  List[str]  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  List[Any]  = torch.compile(model.forward ,						backend='inductor'		)
       UpperCAmelCase_				:  List[Any]  = torch.randn(4 ,						1_0		).cuda()
       UpperCAmelCase_				:  List[str]  = model(snake_case_		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = RegressionModel()
       # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
       UpperCAmelCase_				:  List[Any]  = torch.nn.parallel.DataParallel(snake_case_		)
       UpperCAmelCase_				:  List[str]  = extract_model_from_parallel(snake_case_		)

       self.assertEqual(snake_case_ ,						snake_case_		)


  @require_torch_min_version(version='2.0'		)
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[Any]  = RegressionModel()
       UpperCAmelCase_				:  List[str]  = torch.compile(snake_case_		)

       # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
       UpperCAmelCase_				:  Optional[int]  = torch.nn.parallel.DataParallel(snake_case_		)
       UpperCAmelCase_				:  List[str]  = torch.compile(snake_case_		)
       UpperCAmelCase_				:  Union[str, Any]  = extract_model_from_parallel(snake_case_		)

       self.assertEqual(compiled_model._orig_mod ,						compiled_model_unwrapped._orig_mod		)






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       self.assertEqual(find_device([1, 'a', torch.tensor([1, 2, 3]		)]		) ,						torch.device('cpu'		)		)
       self.assertEqual(find_device({'a': 1, 'b': torch.tensor([1, 2, 3]		)}		) ,						torch.device('cpu'		)		)
       self.assertIsNone(find_device([1, 'a']		)		)











import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_          =				logging.get_logger(__name__)

UpperCAmelCase_          =				{
    "facebook/sam-vit-huge": "https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json",
    "facebook/sam-vit-large": "https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json",
    "facebook/sam-vit-base": "https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json",
}





class 		UpperCAmelCase    (    snake_case_					):





   def __init__(						self							,  _lowerCAmelCase=256							,  _lowerCAmelCase=1_024							,  _lowerCAmelCase=16							,  _lowerCAmelCase=16							,  _lowerCAmelCase=4							,  _lowerCAmelCase="gelu"							,  _lowerCAmelCase=1E-6							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	hidden_size
         _lowerCAmelCase			      =	image_size
         _lowerCAmelCase			      =	patch_size
         _lowerCAmelCase			      =	image_size // patch_size
         _lowerCAmelCase			      =	mask_input_channels
         _lowerCAmelCase			      =	num_point_embeddings
         _lowerCAmelCase			      =	hidden_act
         _lowerCAmelCase			      =	layer_norm_eps






class 		UpperCAmelCase    (    snake_case_					):





   def __init__(						self							,  _lowerCAmelCase=256							,  _lowerCAmelCase="relu"							,  _lowerCAmelCase=2_048							,  _lowerCAmelCase=2							,  _lowerCAmelCase=8							,  _lowerCAmelCase=2							,  _lowerCAmelCase=3							,  _lowerCAmelCase=3							,  _lowerCAmelCase=256							,  _lowerCAmelCase=1E-6							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	hidden_size
         _lowerCAmelCase			      =	hidden_act
         _lowerCAmelCase			      =	mlp_dim
         _lowerCAmelCase			      =	num_hidden_layers
         _lowerCAmelCase			      =	num_attention_heads
         _lowerCAmelCase			      =	attention_downsample_rate
         _lowerCAmelCase			      =	num_multimask_outputs
         _lowerCAmelCase			      =	iou_head_depth
         _lowerCAmelCase			      =	iou_head_hidden_dim
         _lowerCAmelCase			      =	layer_norm_eps






class 		UpperCAmelCase    (    snake_case_					):





   def __init__(						self							,  _lowerCAmelCase=768							,  _lowerCAmelCase=256							,  _lowerCAmelCase=12							,  _lowerCAmelCase=12							,  _lowerCAmelCase=3							,  _lowerCAmelCase=1_024							,  _lowerCAmelCase=16							,  _lowerCAmelCase="gelu"							,  _lowerCAmelCase=1E-06							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=1E-10							,  _lowerCAmelCase=True							,  _lowerCAmelCase=4.0							,  _lowerCAmelCase=True							,  _lowerCAmelCase=True							,  _lowerCAmelCase=14							,  _lowerCAmelCase=[2, 5, 8, 11]							,  _lowerCAmelCase=128							,  _lowerCAmelCase=None							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )

         _lowerCAmelCase			      =	hidden_size
         _lowerCAmelCase			      =	output_channels
         _lowerCAmelCase			      =	num_hidden_layers
         _lowerCAmelCase			      =	num_attention_heads
         _lowerCAmelCase			      =	num_channels
         _lowerCAmelCase			      =	image_size
         _lowerCAmelCase			      =	patch_size
         _lowerCAmelCase			      =	hidden_act
         _lowerCAmelCase			      =	layer_norm_eps
         _lowerCAmelCase			      =	attention_dropout
         _lowerCAmelCase			      =	initializer_range
         _lowerCAmelCase			      =	qkv_bias
         _lowerCAmelCase			      =	mlp_ratio
         _lowerCAmelCase			      =	use_abs_pos
         _lowerCAmelCase			      =	use_rel_pos
         _lowerCAmelCase			      =	window_size
         _lowerCAmelCase			      =	global_attn_indexes
         _lowerCAmelCase			      =	num_pos_feats
         _lowerCAmelCase			      =	int(hidden_size * mlp_ratio      ) if mlp_dim is None else mlp_dim






class 		UpperCAmelCase    (    snake_case_					):
   SCREAMING_SNAKE_CASE__					      =  '''sam'''
   SCREAMING_SNAKE_CASE__					      =  True





   def __init__(						self							,  _lowerCAmelCase=None							,  _lowerCAmelCase=None							,  _lowerCAmelCase=None							,  _lowerCAmelCase=0.02							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	vision_config if vision_config is not None else {}
         _lowerCAmelCase			      =	prompt_encoder_config if prompt_encoder_config is not None else {}
         _lowerCAmelCase			      =	mask_decoder_config if mask_decoder_config is not None else {}

         if isinstance(_lowerCAmelCase							,  _lowerCAmelCase      ):
               _lowerCAmelCase			      =	vision_config.to_dict()
         if isinstance(_lowerCAmelCase							,  _lowerCAmelCase      ):
               _lowerCAmelCase			      =	prompt_encoder_config.to_dict()
         if isinstance(_lowerCAmelCase							,  _lowerCAmelCase      ):
               _lowerCAmelCase			      =	mask_decoder_config.to_dict()

         _lowerCAmelCase			      =	SamVisionConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	SamPromptEncoderConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	SamMaskDecoderConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	initializer_range





   def 						__lowerCAmelCase				(						self      ):
         _lowerCAmelCase			      =	copy.deepcopy(self.__dict__      )
         _lowerCAmelCase			      =	self.vision_config.to_dict()
         _lowerCAmelCase			      =	self.prompt_encoder_config.to_dict()
         _lowerCAmelCase			      =	self.mask_decoder_config.to_dict()
         _lowerCAmelCase			      =	self.__class__.model_type
         return output