





import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
      import transformer_engine.pytorch as te



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE=True					,		_SCREAMING_SNAKE_CASE=True					,		_SCREAMING_SNAKE_CASE=True      )					->  List[str]:


      '''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(_SCREAMING_SNAKE_CASE					,		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
                  SCREAMING_SNAKE_CASE						=		module.bias is not None
                  SCREAMING_SNAKE_CASE						=		te.Linear(
                      module.in_features					,		module.out_features					,		bias=_SCREAMING_SNAKE_CASE					,		params_dtype=module.weight.dtype      )
                  SCREAMING_SNAKE_CASE						=		module.weight.data.clone()
                  if has_bias:
                        SCREAMING_SNAKE_CASE						=		module.bias.data.clone()

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

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

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

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



def   __lowercase  ( _SCREAMING_SNAKE_CASE      )					->  int:


      '''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(_SCREAMING_SNAKE_CASE					,		(te.LayerNorm, te.Linear)      ):
                  return True
      return False









from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE_		  =			{
    """configuration_markuplm""": ["""MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MarkupLMConfig"""],
    """feature_extraction_markuplm""": ["""MarkupLMFeatureExtractor"""],
    """processing_markuplm""": ["""MarkupLMProcessor"""],
    """tokenization_markuplm""": ["""MarkupLMTokenizer"""],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE_		  =			["""MarkupLMTokenizerFast"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE_		  =			[
          """MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """MarkupLMForQuestionAnswering""",
          """MarkupLMForSequenceClassification""",
          """MarkupLMForTokenClassification""",
          """MarkupLMModel""",
          """MarkupLMPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
      from .feature_extraction_markuplm import MarkupLMFeatureExtractor
      from .processing_markuplm import MarkupLMProcessor
      from .tokenization_markuplm import MarkupLMTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_markuplm_fast import MarkupLMTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_markuplm import (
                MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                MarkupLMForQuestionAnswering,
                MarkupLMForSequenceClassification,
                MarkupLMForTokenClassification,
                MarkupLMModel,
                MarkupLMPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE_		  =			_LazyModule(__name__, globals()["""__file__"""], _import_structure)


