



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
	import transformer_engine.pytorch as te

def       A_   (						__a  :					Any			,		__a  :					Tuple=True			,		__a  :					List[Any]=True			,		__a  :					int=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
												a__					   =		module.bias is not None
												a__					   =		te.Linear(
												    module.in_features			,		module.out_features			,		bias=A__			,		params_dtype=module.weight.dtype		)
												a__					   =		module.weight.data.clone()
												if has_bias:
																a__					   =		module.bias.data.clone()

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

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

												setattr(A__			,		A__			,		A__		)
								elif isinstance(A__			,		te.LayerNorm		) and not to_transformer_engine and _convert_ln:
												a__					   =		nn.LayerNorm(module.normalized_shape[0]			,		eps=module.eps			,		params_dtype=module.weight.dtype		)
												a__					   =		module.weight.data.clone()
												a__					   =		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       A_   (						__a  :					Dict		):

				"""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


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowerCAmelCase__	:  int						=  {
    '''configuration_deta''': ['''DETA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DetaConfig'''],
}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowerCAmelCase__	:  Union[str, Any]						=  ['''DetaImageProcessor''']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowerCAmelCase__	:  Optional[int]						=  [
				    '''DETA_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''DetaForObjectDetection''',
				    '''DetaModel''',
				    '''DetaPreTrainedModel''',
				]


if TYPE_CHECKING:
				from .configuration_deta import DETA_PRETRAINED_CONFIG_ARCHIVE_MAP, DetaConfig

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .image_processing_deta import DetaImageProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_deta import (
								    DETA_PRETRAINED_MODEL_ARCHIVE_LIST,
								    DetaForObjectDetection,
								    DetaModel,
								    DetaPreTrainedModel,
								)

else:
				import sys

				lowerCAmelCase__	:  int						=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


