



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
 import transformer_engine.pytorch as te



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase=True				,		_lowerCAmelCase=True				,		_lowerCAmelCase=True):
     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(_lowerCAmelCase				,		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=_lowerCAmelCase				,		params_dtype=module.weight.dtype)
               UpperCamelCase_											=				module.weight.data.clone()
               if has_bias:
                    UpperCamelCase_											=				module.bias.data.clone()

               setattr(_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase)
          elif isinstance(_lowerCAmelCase				,		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(_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase)
          elif isinstance(_lowerCAmelCase				,		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=_lowerCAmelCase				,		params_dtype=module.weight.dtype)
               UpperCamelCase_											=				module.weight.data.clone()
               if has_bias:
                    UpperCamelCase_											=				module.bias.data.clone()

               setattr(_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase)
          elif isinstance(_lowerCAmelCase				,		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(_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase)
          else:
               convert_model(
                   _lowerCAmelCase				,		to_transformer_engine=_lowerCAmelCase				,		_convert_linear=_convert_linear				,		_convert_ln=_convert_ln				,		)






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





from typing import TYPE_CHECKING

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


UpperCAmelCase				:							Optional[int]								={
    """configuration_realm""": ["""REALM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RealmConfig"""],
    """tokenization_realm""": ["""RealmTokenizer"""],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase				:							Optional[int]								=["""RealmTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase				:							Union[str, Any]								=[
	    """REALM_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """RealmEmbedder""",
	    """RealmForOpenQA""",
	    """RealmKnowledgeAugEncoder""",
	    """RealmPreTrainedModel""",
	    """RealmReader""",
	    """RealmScorer""",
	    """load_tf_weights_in_realm""",
	]
	UpperCAmelCase				:							List[str]								=["""RealmRetriever"""]


if TYPE_CHECKING:
	from .configuration_realm import REALM_PRETRAINED_CONFIG_ARCHIVE_MAP, RealmConfig
	from .tokenization_realm import RealmTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_realm import RealmTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_realm import (
		    REALM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    RealmEmbedder,
		    RealmForOpenQA,
		    RealmKnowledgeAugEncoder,
		    RealmPreTrainedModel,
		    RealmReader,
		    RealmScorer,
		    load_tf_weights_in_realm,
		)
		from .retrieval_realm import RealmRetriever


else:
	import sys

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