


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


lowerCamelCase		=	namedtuple('''ExampleNamedTuple''', '''a b c''')





class       _a							(       unittest.TestCase):




	def 							UpperCAmelCase__(      self		:	Union[str, Any]	)-> int:
			lowerCAmelCase__     :    Optional[int]					  =      torch.randn(5	,      2	)
			lowerCAmelCase__     :    Union[str, Any]					  =      torch.device('''cuda'''	) if torch.cuda.is_available() else torch.device('''cpu'''	)

			lowerCAmelCase__     :    str					  =      send_to_device(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta.cpu()	,      _SCREAMING_SNAKE_CASE	)	)

			lowerCAmelCase__     :    Dict					  =      send_to_device((tensor, [tensor, tensor], 1)	,      _SCREAMING_SNAKE_CASE	)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta[0].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertIsInstance(resulta[1]	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta[1][0].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertTrue(torch.equal(resulta[1][1].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertEqual(resulta[2]	,      1	)

			lowerCAmelCase__     :    Tuple					  =      send_to_device({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}	,      _SCREAMING_SNAKE_CASE	)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta['''a'''].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertIsInstance(resulta['''b''']	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta['''b'''][0].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertTrue(torch.equal(resulta['''b'''][1].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertEqual(resulta['''c''']	,      1	)

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

			lowerCAmelCase__     :    Optional[Any]					  =      send_to_device(UserDict({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}	)	,      _SCREAMING_SNAKE_CASE	)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta['''a'''].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertIsInstance(resulta['''b''']	,      _SCREAMING_SNAKE_CASE	)
			self.assertTrue(torch.equal(resulta['''b'''][0].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertTrue(torch.equal(resulta['''b'''][1].cpu()	,      _SCREAMING_SNAKE_CASE	)	)
			self.assertEqual(resulta['''c''']	,      1	)




	def 							UpperCAmelCase__(      self		:	int	)-> Union[str, Any]:
			with self.assertRaises(_SCREAMING_SNAKE_CASE	) as cm:
					lowerCAmelCase__     :    Any					  =      recursively_apply(torch.tensor	,      (torch.tensor(1	), 1)	,      error_on_other_type=_SCREAMING_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		:	Any	)-> Any:
			lowerCAmelCase__     :    str					  =      torch.tensor([1, 2, 3, 4, 5]	)
			self.assertEqual(listify(_SCREAMING_SNAKE_CASE	)	,      [1, 2, 3, 4, 5]	)

			lowerCAmelCase__     :    Union[str, Any]					  =      torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]	)
			self.assertEqual(listify(_SCREAMING_SNAKE_CASE	)	,      [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]	)

			lowerCAmelCase__     :    Optional[Any]					  =      torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]	)
			self.assertEqual(
			    listify(_SCREAMING_SNAKE_CASE	)	,      [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]	)




	def 							UpperCAmelCase__(      self		:	Tuple	)-> List[str]:
			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		:	Dict	)-> Optional[Any]:
			lowerCAmelCase__     :    Union[str, Any]					  =      RegressionModel()
			lowerCAmelCase__     :    str					  =      model.forward
			lowerCAmelCase__     :    Dict					  =      convert_outputs_to_fpaa(model.forward	)
			lowerCAmelCase__     :    List[str]					  =      extract_model_from_parallel(_SCREAMING_SNAKE_CASE	,      keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Optional[Any]					  =      pickle.dumps(_SCREAMING_SNAKE_CASE	)




	@require_cuda
	def 							UpperCAmelCase__(      self		:	Tuple	)-> Union[str, Any]:
			lowerCAmelCase__     :    Optional[Any]					  =      RegressionModel()
			lowerCAmelCase__     :    List[str]					  =      model.forward
			lowerCAmelCase__     :    Any					  =      torch.cuda.amp.autocast(dtype=torch.floataa	)(model.forward	)
			lowerCAmelCase__     :    Tuple					  =      convert_outputs_to_fpaa(model.forward	)
			lowerCAmelCase__     :    Optional[int]					  =      extract_model_from_parallel(_SCREAMING_SNAKE_CASE	,      keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Any					  =      pickle.dumps(_SCREAMING_SNAKE_CASE	)




	@require_cuda
	@require_torch_min_version(version='''2.0'''	)
	def 							UpperCAmelCase__(      self		:	Optional[int]	)-> Union[str, Any]:
			lowerCAmelCase__     :    str					  =      RegressionModel()
			lowerCAmelCase__     :    str					  =      model.forward
			lowerCAmelCase__     :    int					  =      torch.cuda.amp.autocast(dtype=torch.floataa	)(model.forward	)
			lowerCAmelCase__     :    Union[str, Any]					  =      convert_outputs_to_fpaa(model.forward	)
			lowerCAmelCase__     :    Dict					  =      torch.compile(model.forward	,      backend='''inductor'''	)
			lowerCAmelCase__     :    Optional[int]					  =      torch.randn(4	,      10	).cuda()
			lowerCAmelCase__     :    int					  =      model(_SCREAMING_SNAKE_CASE	)




	def 							UpperCAmelCase__(      self		:	int	)-> List[Any]:
			lowerCAmelCase__     :    Optional[int]					  =      RegressionModel()
			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			lowerCAmelCase__     :    str					  =      torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    int					  =      extract_model_from_parallel(_SCREAMING_SNAKE_CASE	)

			self.assertEqual(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	)




	@require_torch_min_version(version='''2.0'''	)
	def 							UpperCAmelCase__(      self		:	Optional[int]	)-> str:
			lowerCAmelCase__     :    Tuple					  =      RegressionModel()
			lowerCAmelCase__     :    Optional[int]					  =      torch.compile(_SCREAMING_SNAKE_CASE	)

			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			lowerCAmelCase__     :    str					  =      torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Dict					  =      torch.compile(_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    int					  =      extract_model_from_parallel(_SCREAMING_SNAKE_CASE	)

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




	def 							UpperCAmelCase__(      self		:	Optional[Any]	)-> Dict:
			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


lowerCamelCase		=	logging.get_logger(__name__)

lowerCamelCase		=	{
    '''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       _a							(       _lowercase):




	def __init__(      self		:	Union[str, Any]	,      _SCREAMING_SNAKE_CASE		:	Tuple=256	,      _SCREAMING_SNAKE_CASE		:	Optional[Any]=1024	,      _SCREAMING_SNAKE_CASE		:	List[Any]=16	,      _SCREAMING_SNAKE_CASE		:	Optional[int]=16	,      _SCREAMING_SNAKE_CASE		:	Union[str, Any]=4	,      _SCREAMING_SNAKE_CASE		:	Any="gelu"	,      _SCREAMING_SNAKE_CASE		:	Optional[Any]=1E-6	,      **_SCREAMING_SNAKE_CASE		:	List[Any]	,      )-> Tuple:
			super().__init__(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Any					  =      hidden_size
			lowerCAmelCase__     :    Tuple					  =      image_size
			lowerCAmelCase__     :    List[Any]					  =      patch_size
			lowerCAmelCase__     :    Any					  =      image_size // patch_size
			lowerCAmelCase__     :    Dict					  =      mask_input_channels
			lowerCAmelCase__     :    Dict					  =      num_point_embeddings
			lowerCAmelCase__     :    str					  =      hidden_act
			lowerCAmelCase__     :    List[Any]					  =      layer_norm_eps





class       _a							(       _lowercase):




	def __init__(      self		:	Tuple	,      _SCREAMING_SNAKE_CASE		:	Union[str, Any]=256	,      _SCREAMING_SNAKE_CASE		:	Dict="relu"	,      _SCREAMING_SNAKE_CASE		:	str=2048	,      _SCREAMING_SNAKE_CASE		:	List[Any]=2	,      _SCREAMING_SNAKE_CASE		:	Dict=8	,      _SCREAMING_SNAKE_CASE		:	str=2	,      _SCREAMING_SNAKE_CASE		:	Tuple=3	,      _SCREAMING_SNAKE_CASE		:	Optional[int]=3	,      _SCREAMING_SNAKE_CASE		:	Optional[Any]=256	,      _SCREAMING_SNAKE_CASE		:	Dict=1E-6	,      **_SCREAMING_SNAKE_CASE		:	Any	,      )-> List[Any]:
			super().__init__(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    List[str]					  =      hidden_size
			lowerCAmelCase__     :    Union[str, Any]					  =      hidden_act
			lowerCAmelCase__     :    str					  =      mlp_dim
			lowerCAmelCase__     :    Optional[int]					  =      num_hidden_layers
			lowerCAmelCase__     :    int					  =      num_attention_heads
			lowerCAmelCase__     :    List[str]					  =      attention_downsample_rate
			lowerCAmelCase__     :    str					  =      num_multimask_outputs
			lowerCAmelCase__     :    Optional[int]					  =      iou_head_depth
			lowerCAmelCase__     :    Union[str, Any]					  =      iou_head_hidden_dim
			lowerCAmelCase__     :    Tuple					  =      layer_norm_eps





class       _a							(       _lowercase):




	def __init__(      self		:	List[str]	,      _SCREAMING_SNAKE_CASE		:	int=768	,      _SCREAMING_SNAKE_CASE		:	str=256	,      _SCREAMING_SNAKE_CASE		:	int=12	,      _SCREAMING_SNAKE_CASE		:	Tuple=12	,      _SCREAMING_SNAKE_CASE		:	Dict=3	,      _SCREAMING_SNAKE_CASE		:	Optional[int]=1024	,      _SCREAMING_SNAKE_CASE		:	Tuple=16	,      _SCREAMING_SNAKE_CASE		:	List[Any]="gelu"	,      _SCREAMING_SNAKE_CASE		:	Optional[int]=1E-06	,      _SCREAMING_SNAKE_CASE		:	int=0.0	,      _SCREAMING_SNAKE_CASE		:	int=1E-10	,      _SCREAMING_SNAKE_CASE		:	List[Any]=True	,      _SCREAMING_SNAKE_CASE		:	Any=4.0	,      _SCREAMING_SNAKE_CASE		:	List[str]=True	,      _SCREAMING_SNAKE_CASE		:	int=True	,      _SCREAMING_SNAKE_CASE		:	List[str]=14	,      _SCREAMING_SNAKE_CASE		:	Dict=[2, 5, 8, 11]	,      _SCREAMING_SNAKE_CASE		:	List[Any]=128	,      _SCREAMING_SNAKE_CASE		:	Dict=None	,      **_SCREAMING_SNAKE_CASE		:	str	,      )-> Any:
			super().__init__(**_SCREAMING_SNAKE_CASE	)

			lowerCAmelCase__     :    Optional[Any]					  =      hidden_size
			lowerCAmelCase__     :    Any					  =      output_channels
			lowerCAmelCase__     :    Optional[int]					  =      num_hidden_layers
			lowerCAmelCase__     :    Dict					  =      num_attention_heads
			lowerCAmelCase__     :    List[Any]					  =      num_channels
			lowerCAmelCase__     :    int					  =      image_size
			lowerCAmelCase__     :    Tuple					  =      patch_size
			lowerCAmelCase__     :    Union[str, Any]					  =      hidden_act
			lowerCAmelCase__     :    List[str]					  =      layer_norm_eps
			lowerCAmelCase__     :    Any					  =      attention_dropout
			lowerCAmelCase__     :    int					  =      initializer_range
			lowerCAmelCase__     :    List[Any]					  =      qkv_bias
			lowerCAmelCase__     :    List[Any]					  =      mlp_ratio
			lowerCAmelCase__     :    List[Any]					  =      use_abs_pos
			lowerCAmelCase__     :    Union[str, Any]					  =      use_rel_pos
			lowerCAmelCase__     :    Optional[Any]					  =      window_size
			lowerCAmelCase__     :    Union[str, Any]					  =      global_attn_indexes
			lowerCAmelCase__     :    Union[str, Any]					  =      num_pos_feats
			lowerCAmelCase__     :    str					  =      int(hidden_size * mlp_ratio	) if mlp_dim is None else mlp_dim





class       _a							(       _lowercase):
	_a						:						Tuple   					=							'''sam'''
	_a						:						Optional[Any]   					=							True




	def __init__(      self		:	Optional[Any]	,      _SCREAMING_SNAKE_CASE		:	Any=None	,      _SCREAMING_SNAKE_CASE		:	Optional[Any]=None	,      _SCREAMING_SNAKE_CASE		:	int=None	,      _SCREAMING_SNAKE_CASE		:	Tuple=0.02	,      **_SCREAMING_SNAKE_CASE		:	int	,      )-> Optional[int]:
			super().__init__(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Tuple					  =      vision_config if vision_config is not None else {}
			lowerCAmelCase__     :    List[str]					  =      prompt_encoder_config if prompt_encoder_config is not None else {}
			lowerCAmelCase__     :    List[str]					  =      mask_decoder_config if mask_decoder_config is not None else {}

			if isinstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	):
					lowerCAmelCase__     :    Any					  =      vision_config.to_dict()
			if isinstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	):
					lowerCAmelCase__     :    Any					  =      prompt_encoder_config.to_dict()
			if isinstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	):
					lowerCAmelCase__     :    List[Any]					  =      mask_decoder_config.to_dict()

			lowerCAmelCase__     :    Any					  =      SamVisionConfig(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    List[Any]					  =      SamPromptEncoderConfig(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    Dict					  =      SamMaskDecoderConfig(**_SCREAMING_SNAKE_CASE	)
			lowerCAmelCase__     :    List[Any]					  =      initializer_range




	def 							UpperCAmelCase__(      self		:	Any	)-> int:
			lowerCAmelCase__     :    Optional[int]					  =      copy.deepcopy(self.__dict__	)
			lowerCAmelCase__     :    Any					  =      self.vision_config.to_dict()
			lowerCAmelCase__     :    Optional[Any]					  =      self.prompt_encoder_config.to_dict()
			lowerCAmelCase__     :    str					  =      self.mask_decoder_config.to_dict()
			lowerCAmelCase__     :    Any					  =      self.__class__.model_type
			return output




