


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a : Union[str, Any]		=				logging.get_logger(__name__)

a : Optional[int]		=				{
    '''xlm-mlm-en-2048''': '''https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json''',
    '''xlm-mlm-ende-1024''': '''https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-enfr-1024''': '''https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json''',
    '''xlm-mlm-enro-1024''': '''https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json''',
    '''xlm-mlm-tlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-mlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-clm-enfr-1024''': '''https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json''',
    '''xlm-clm-ende-1024''': '''https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-17-1280''': '''https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json''',
    '''xlm-mlm-100-1280''': '''https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json''',
}
class 	lowerCamelCase_       (				lowerCAmelCase__				):



					'''simple docstring'''







					__UpperCAmelCase            =				"xlm"
					__UpperCAmelCase            =				{
					    "hidden_size": "emb_dim",
					    "num_attention_heads": "n_heads",
					    "num_hidden_layers": "n_layers",
					    "n_words": "vocab_size",  # For backward compatibility
					}






					def __init__(				self ,       snake_case_=3_0_1_4_5 ,       snake_case_=2_0_4_8 ,       snake_case_=1_2 ,       snake_case_=1_6 ,       snake_case_=0.1 ,       snake_case_=0.1 ,       snake_case_=True ,       snake_case_=False ,       snake_case_=False ,       snake_case_=False ,       snake_case_=1 ,       snake_case_=True ,       snake_case_=5_1_2 ,       snake_case_=2_0_4_8**-0.5 ,       snake_case_=1e-1_2 ,       snake_case_=0.0_2 ,       snake_case_=0 ,       snake_case_=1 ,       snake_case_=2 ,       snake_case_=3 ,       snake_case_=5 ,       snake_case_=True ,       snake_case_="first" ,       snake_case_=True ,       snake_case_=None ,       snake_case_=True ,       snake_case_=0.1 ,       snake_case_=5 ,       snake_case_=5 ,       snake_case_=0 ,       snake_case_=0 ,       snake_case_=2 ,       snake_case_=0 ,       **snake_case_ ,       )   ->			List[str]:







								'''simple docstring'''

								__lowercase 					=   vocab_size
								__lowercase 					=   emb_dim
								__lowercase 					=   n_layers
								__lowercase 					=   n_heads
								__lowercase 					=   dropout
								__lowercase 					=   attention_dropout
								__lowercase 					=   gelu_activation
								__lowercase 					=   sinusoidal_embeddings
								__lowercase 					=   causal
								__lowercase 					=   asm
								__lowercase 					=   n_langs
								__lowercase 					=   use_lang_emb
								__lowercase 					=   layer_norm_eps
								__lowercase 					=   bos_index
								__lowercase 					=   eos_index
								__lowercase 					=   pad_index
								__lowercase 					=   unk_index
								__lowercase 					=   mask_index
								__lowercase 					=   is_encoder
								__lowercase 					=   max_position_embeddings
								__lowercase 					=   embed_init_std
								__lowercase 					=   init_std
								__lowercase 					=   summary_type
								__lowercase 					=   summary_use_proj
								__lowercase 					=   summary_activation
								__lowercase 					=   summary_proj_to_labels
								__lowercase 					=   summary_first_dropout
								__lowercase 					=   start_n_top
								__lowercase 					=   end_n_top
								__lowercase 					=   mask_token_id
								__lowercase 					=   lang_id

								if "n_words" in kwargs:
											__lowercase 					=   kwargs['''n_words''']

								super().__init__(pad_token_id=_lowerCamelCase ,       bos_token_id=_lowerCamelCase ,       **_lowerCamelCase						)




class 	lowerCamelCase_       (				lowerCAmelCase__				):



					'''simple docstring'''







					@property
					def A      (				self						)   ->			Mapping[str, Mapping[int, str]]:







								'''simple docstring'''

								if self.task == "multiple-choice":
											__lowercase 					=   {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
								else:
											__lowercase 					=   {0: '''batch''', 1: '''sequence'''}
								return OrderedDict(
								    [
								        ('''input_ids''', dynamic_axis),
								        ('''attention_mask''', dynamic_axis),
								        ('''token_type_ids''', dynamic_axis),
								    ]						)






import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available


def 		lowercase_  (     *_UpperCamelCase  ):


			'''simple docstring'''







			if not isinstance(_UpperCamelCase       ,			_UpperCamelCase  ):
						__lowercase 					=   list(_UpperCamelCase  )
			for i in range(len(_UpperCamelCase  )  ):
						__lowercase 					=   None
			gc.collect()
			if is_xpu_available():
						torch.xpu.empty_cache()
			elif is_npu_available():
						torch.npu.empty_cache()
			else:
						torch.cuda.empty_cache()
			return objects


def 		lowercase_  (     _UpperCamelCase  ):


			'''simple docstring'''







			__lowercase 					=   [
			    '''CUDA out of memory.''',  # CUDA OOM
			    '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''',  # CUDNN SNAFU
			    '''DefaultCPUAllocator: can\'t allocate memory''',  # CPU OOM
			]
			if isinstance(_UpperCamelCase       ,			_UpperCamelCase  ) and len(exception.args  ) == 1:
						return any(err in exception.args[0] for err in _statements  )
			return False





def 		lowercase_  (     _UpperCamelCase = None       ,			_UpperCamelCase = 1_28  ):


			'''simple docstring'''







			if function is None:
						return functools.partial(_UpperCamelCase       ,			starting_batch_size=_UpperCamelCase  )

			__lowercase 					=   starting_batch_size

			def decorator(*_UpperCamelCase       ,			**_UpperCamelCase  ):
						nonlocal batch_size
						gc.collect()
						if is_xpu_available():
									torch.xpu.empty_cache()
						elif is_npu_available():
									torch.npu.empty_cache()
						else:
									torch.cuda.empty_cache()
						__lowercase 					=   list(inspect.signature(_UpperCamelCase  ).parameters.keys()  )
						# Guard against user error
						if len(_UpperCamelCase  ) < (len(_UpperCamelCase  ) + 1):
									__lowercase 					=   ''', '''.join([F'{arg}={value}' for arg, value in zip(params[1:]       ,			args[1:]  )]  )
									raise TypeError(
									    F'Batch size was passed into `{function.__name__}` as the first argument when called.'
									    F'Remove this as the decorator already does so: `{function.__name__}({arg_str})`'  )
						while True:
									if batch_size == 0:
												raise RuntimeError('''No executable batch size found, reached zero.'''  )
									try:
												return function(_UpperCamelCase       ,			*_UpperCamelCase       ,			**_UpperCamelCase  )
									except Exception as e:
												if should_reduce_batch_size(_UpperCamelCase  ):
															gc.collect()
															if is_xpu_available():
																		torch.xpu.empty_cache()
															elif is_npu_available():
																		torch.npu.empty_cache()
															else:
																		torch.cuda.empty_cache()
															batch_size //= 2
												else:
															raise

			return decorator


