






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase										=		{
    'configuration_bart': ['BART_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BartConfig', 'BartOnnxConfig'],
    'tokenization_bart': ['BartTokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase										=		['BartTokenizerFast']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase										=		[
		    'BART_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'BartForCausalLM',
		    'BartForConditionalGeneration',
		    'BartForQuestionAnswering',
		    'BartForSequenceClassification',
		    'BartModel',
		    'BartPretrainedModel',
		    'PretrainedBartModel',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase										=		[
		    'TFBartForConditionalGeneration',
		    'TFBartForSequenceClassification',
		    'TFBartModel',
		    'TFBartPretrainedModel',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase										=		[
		    'FlaxBartDecoderPreTrainedModel',
		    'FlaxBartForCausalLM',
		    'FlaxBartForConditionalGeneration',
		    'FlaxBartForQuestionAnswering',
		    'FlaxBartForSequenceClassification',
		    'FlaxBartModel',
		    'FlaxBartPreTrainedModel',
		]

if TYPE_CHECKING:
		from .configuration_bart import BART_PRETRAINED_CONFIG_ARCHIVE_MAP, BartConfig, BartOnnxConfig
		from .tokenization_bart import BartTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_bart_fast import BartTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_bart import (
				    BART_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BartForCausalLM,
				    BartForConditionalGeneration,
				    BartForQuestionAnswering,
				    BartForSequenceClassification,
				    BartModel,
				    BartPretrainedModel,
				    PretrainedBartModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_bart import (
				    TFBartForConditionalGeneration,
				    TFBartForSequenceClassification,
				    TFBartModel,
				    TFBartPretrainedModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_bart import (
				    FlaxBartDecoderPreTrainedModel,
				    FlaxBartForCausalLM,
				    FlaxBartForConditionalGeneration,
				    FlaxBartForQuestionAnswering,
				    FlaxBartForSequenceClassification,
				    FlaxBartModel,
				    FlaxBartPreTrainedModel,
				)

else:
		import sys

		__UpperCAmelCase										=		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)














import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
		import transformer_engine.pytorch as te




def   lowercase__   (  __snake_case					:							List[str]  , __snake_case					:							List[Any]=True  , __snake_case					:							str=True  , __snake_case					:							Tuple=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(__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
															UpperCAmelCase_					:   Optional[int]   =		module.bias is not None
															UpperCAmelCase_					:   Optional[int]   =		te.Linear(
															    module.in_features  , module.out_features  , bias=__snake_case  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   List[str]   =		module.weight.data.clone()
															if has_bias:
																				UpperCAmelCase_					:   List[Any]   =		module.bias.data.clone()

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

															setattr(__snake_case  , __snake_case  , __snake_case						)
										elif isinstance(__snake_case  , te.Linear						) and not to_transformer_engine and _convert_linear:
															UpperCAmelCase_					:   int   =		module.bias is not None
															UpperCAmelCase_					:   Tuple   =		nn.Linear(
															    module.in_features  , module.out_features  , bias=__snake_case  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   str   =		module.weight.data.clone()
															if has_bias:
																				UpperCAmelCase_					:   Dict   =		module.bias.data.clone()

															setattr(__snake_case  , __snake_case  , __snake_case						)
										elif isinstance(__snake_case  , te.LayerNorm						) and not to_transformer_engine and _convert_ln:
															UpperCAmelCase_					:   List[Any]   =		nn.LayerNorm(module.normalized_shape[0]  , eps=module.eps  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   List[str]   =		module.weight.data.clone()
															UpperCAmelCase_					:   Tuple   =		module.bias.data.clone()

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






def   lowercase__   (  __snake_case					:							List[Any]						):


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






