






'''simple docstring'''


import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
					import transformer_engine.pytorch as te







def 						__magic_name__(			_A    ,   _A=True    ,   _A=True    ,   _A=True       ):
						'''simple docstring'''





						if not is_fpa_available():
												raise ImportError("""Using `convert_model` requires transformer_engine to be installed."""       )
						for name, module in model.named_children():
												if isinstance(_A    ,   nn.Linear       ) and to_transformer_engine and _convert_linear:
																		# Return early if the linear layer weights are not multiples of 16
																		if any(p % 16 != 0 for p in module.weight.shape       ):
																								return
																		UpperCamelCase__       =			module.bias is not None
																		UpperCamelCase__       =			te.Linear(
																		    module.in_features    ,   module.out_features    ,   bias=_A    ,   params_dtype=module.weight.dtype       )
																		UpperCamelCase__       =			module.weight.data.clone()
																		if has_bias:
																								UpperCamelCase__       =			module.bias.data.clone()

																		setattr(_A    ,   _A    ,   _A       )
												elif isinstance(_A    ,   nn.LayerNorm       ) and to_transformer_engine and _convert_ln:
																		UpperCamelCase__       =			te.LayerNorm(module.normalized_shape[0]    ,   eps=module.eps    ,   params_dtype=module.weight.dtype       )
																		UpperCamelCase__       =			module.weight.data.clone()
																		UpperCamelCase__       =			module.bias.data.clone()

																		setattr(_A    ,   _A    ,   _A       )
												elif isinstance(_A    ,   te.Linear       ) and not to_transformer_engine and _convert_linear:
																		UpperCamelCase__       =			module.bias is not None
																		UpperCamelCase__       =			nn.Linear(
																		    module.in_features    ,   module.out_features    ,   bias=_A    ,   params_dtype=module.weight.dtype       )
																		UpperCamelCase__       =			module.weight.data.clone()
																		if has_bias:
																								UpperCamelCase__       =			module.bias.data.clone()

																		setattr(_A    ,   _A    ,   _A       )
												elif isinstance(_A    ,   te.LayerNorm       ) and not to_transformer_engine and _convert_ln:
																		UpperCamelCase__       =			nn.LayerNorm(module.normalized_shape[0]    ,   eps=module.eps    ,   params_dtype=module.weight.dtype       )
																		UpperCamelCase__       =			module.weight.data.clone()
																		UpperCamelCase__       =			module.bias.data.clone()

																		setattr(_A    ,   _A    ,   _A       )
												else:
																		convert_model(
																		    _A    ,   to_transformer_engine=_A    ,   _convert_linear=_convert_linear    ,   _convert_ln=_convert_ln    ,   )







def 						__magic_name__(			_A       ):
						'''simple docstring'''





						if not is_fpa_available():
												raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed."""       )
						for m in model.modules():
												if isinstance(_A    ,   (te.LayerNorm, te.Linear)       ):
																		return True
						return False








'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


lowerCamelCase_					:			Any     						=				{
    '''configuration_sam''': [
        '''SAM_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''SamConfig''',
        '''SamMaskDecoderConfig''',
        '''SamPromptEncoderConfig''',
        '''SamVisionConfig''',
    ],
    '''processing_sam''': ['''SamProcessor'''],
}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase_					:			Union[str, Any]     						=				[
					    '''SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''SamModel''',
					    '''SamPreTrainedModel''',
					]
try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase_					:			Optional[Any]     						=				[
					    '''TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFSamModel''',
					    '''TFSamPreTrainedModel''',
					]
try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase_					:			Optional[Any]     						=				['''SamImageProcessor''']


if TYPE_CHECKING:
					from .configuration_sam import (
					    SAM_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    SamConfig,
					    SamMaskDecoderConfig,
					    SamPromptEncoderConfig,
					    SamVisionConfig,
					)
					from .processing_sam import SamProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_sam import SAM_PRETRAINED_MODEL_ARCHIVE_LIST, SamModel, SamPreTrainedModel

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_sam import TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST, TFSamModel, TFSamPreTrainedModel

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_sam import SamImageProcessor

else:
					import sys

					lowerCamelCase_					:			Optional[Any]     						=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
