





import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
      import transformer_engine.pytorch as te






def 				_lowerCAmelCase			(	_a :       Union[str, Any]       , _a :       Tuple=True       , _a :       Optional[Any]=True       , _a :       Tuple=True      )						->      str:
     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
               lowerCAmelCase_       :							Tuple			 =		module.bias is not None
               lowerCAmelCase_       :							Optional[Any]			 =		te.Linear(
                   module.in_features       , module.out_features       , bias=_a       , params_dtype=module.weight.dtype      )
               lowerCAmelCase_       :							Optional[Any]			 =		module.weight.data.clone()
               if has_bias:
                    lowerCAmelCase_       :							Optional[Any]			 =		module.bias.data.clone()

               setattr(_a       , _a       , _a      )
          elif isinstance(_a       , nn.LayerNorm      ) and to_transformer_engine and _convert_ln:
               lowerCAmelCase_       :							Optional[int]			 =		te.LayerNorm(module.normalized_shape[0]       , eps=module.eps       , params_dtype=module.weight.dtype      )
               lowerCAmelCase_       :							List[Any]			 =		module.weight.data.clone()
               lowerCAmelCase_       :							List[str]			 =		module.bias.data.clone()

               setattr(_a       , _a       , _a      )
          elif isinstance(_a       , te.Linear      ) and not to_transformer_engine and _convert_linear:
               lowerCAmelCase_       :							List[Any]			 =		module.bias is not None
               lowerCAmelCase_       :							int			 =		nn.Linear(
                   module.in_features       , module.out_features       , bias=_a       , params_dtype=module.weight.dtype      )
               lowerCAmelCase_       :							Tuple			 =		module.weight.data.clone()
               if has_bias:
                    lowerCAmelCase_       :							int			 =		module.bias.data.clone()

               setattr(_a       , _a       , _a      )
          elif isinstance(_a       , te.LayerNorm      ) and not to_transformer_engine and _convert_ln:
               lowerCAmelCase_       :							Tuple			 =		nn.LayerNorm(module.normalized_shape[0]       , eps=module.eps       , params_dtype=module.weight.dtype      )
               lowerCAmelCase_       :							Union[str, Any]			 =		module.weight.data.clone()
               lowerCAmelCase_       :							Any			 =		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 				_lowerCAmelCase			(	_a :       str      )						->      Optional[Any]:
     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_torch_available, is_vision_available


UpperCamelCase_						:					Tuple      				=			{"""configuration_detr""": ["""DETR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DetrConfig""", """DetrOnnxConfig"""]}

try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Tuple      				=			["""DetrFeatureExtractor"""]
    UpperCamelCase_						:					int      				=			["""DetrImageProcessor"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Dict      				=			[
        """DETR_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """DetrForObjectDetection""",
        """DetrForSegmentation""",
        """DetrModel""",
        """DetrPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_detr import DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, DetrConfig, DetrOnnxConfig

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_detr import DetrFeatureExtractor
        from .image_processing_detr import DetrImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_detr import (
            DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
            DetrForObjectDetection,
            DetrForSegmentation,
            DetrModel,
            DetrPreTrainedModel,
        )

else:
    import sys

    UpperCamelCase_						:					List[str]      				=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


