






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase								=						logging.get_logger(__name__)

__UpperCAmelCase								=						{
    'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json',
    'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json',
    'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json',
    'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json',
    'funnel-transformer/intermediate': (
        'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json'
    ),
    'funnel-transformer/intermediate-base': (
        'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json'
    ),
    'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json',
    'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json',
    'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json',
    'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json',
}



class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		_snake_case    :    Union[str, Any]       						=  '''funnel'''
		_snake_case    :    int       						=  {
		    '''hidden_size''': '''d_model''',
		    '''num_attention_heads''': '''n_head''',
		}



		def __init__(							self				,		_UpperCamelCase=3_0_5_2_2				,		_UpperCamelCase=[4, 4, 4]				,		_UpperCamelCase=None				,		_UpperCamelCase=2				,		_UpperCamelCase=7_6_8				,		_UpperCamelCase=1_2				,		_UpperCamelCase=6_4				,		_UpperCamelCase=3_0_7_2				,		_UpperCamelCase="gelu_new"				,		_UpperCamelCase=0.1				,		_UpperCamelCase=0.1				,		_UpperCamelCase=0.0				,		_UpperCamelCase=0.1				,		_UpperCamelCase=None				,		_UpperCamelCase=1E-9				,		_UpperCamelCase="mean"				,		_UpperCamelCase="relative_shift"				,		_UpperCamelCase=True				,		_UpperCamelCase=True				,		_UpperCamelCase=True				,		**_UpperCamelCase				,		)	->							Optional[Any]:
							UpperCAmelCase_    :      Optional[Any]            =						vocab_size
							UpperCAmelCase_    :      Union[str, Any]            =						block_sizes
							UpperCAmelCase_    :      Tuple            =						[1] * len(_UpperCamelCase   ) if block_repeats is None else block_repeats
							assert len(_UpperCamelCase   ) == len(
							    self.block_repeats   ), "`block_sizes` and `block_repeats` should have the same length."
							UpperCAmelCase_    :      List[Any]            =						num_decoder_layers
							UpperCAmelCase_    :      Union[str, Any]            =						d_model
							UpperCAmelCase_    :      Any            =						n_head
							UpperCAmelCase_    :      List[str]            =						d_head
							UpperCAmelCase_    :      Dict            =						d_inner
							UpperCAmelCase_    :      Dict            =						hidden_act
							UpperCAmelCase_    :      List[str]            =						hidden_dropout
							UpperCAmelCase_    :      Optional[int]            =						attention_dropout
							UpperCAmelCase_    :      Any            =						activation_dropout
							UpperCAmelCase_    :      str            =						initializer_range
							UpperCAmelCase_    :      Dict            =						initializer_std
							UpperCAmelCase_    :      Optional[Any]            =						layer_norm_eps
							assert pooling_type in [
							    "mean",
							    "max",
							], f"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported."
							UpperCAmelCase_    :      Optional[int]            =						pooling_type
							assert attention_type in [
							    "relative_shift",
							    "factorized",
							], f"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."
							UpperCAmelCase_    :      List[Any]            =						attention_type
							UpperCAmelCase_    :      Optional[Any]            =						separate_cls
							UpperCAmelCase_    :      int            =						truncate_seq
							UpperCAmelCase_    :      str            =						pool_q_only

							super().__init__(**_UpperCamelCase   )



		@property
		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							return sum(self.block_sizes   )



		@num_hidden_layers.setter
		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							List[Any]:
							raise NotImplementedError(
							    'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.'   )



		@property
		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							return len(self.block_sizes   )



		@num_blocks.setter
		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							List[str]:
							raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.'   )














import os
import shutil
from pathlib import Path
from typing import Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ..utils import ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, is_onnx_available, logging


if is_onnx_available():
		import onnxruntime as ort


__UpperCAmelCase								=						logging.get_logger(__name__)

__UpperCAmelCase								=						{
    'tensor(bool)': np.bool_,
    'tensor(int8)': np.inta,
    'tensor(uint8)': np.uinta,
    'tensor(int16)': np.intaa,
    'tensor(uint16)': np.uintaa,
    'tensor(int32)': np.intaa,
    'tensor(uint32)': np.uintaa,
    'tensor(int64)': np.intaa,
    'tensor(uint64)': np.uintaa,
    'tensor(float16)': np.floataa,
    'tensor(float)': np.floataa,
    'tensor(double)': np.floataa,
}



class   lowerCamelCase      :







		'''simple docstring'''



		def __init__(							self				,		_UpperCamelCase=None				,		**_UpperCamelCase   )	->							Dict:
							logger.info('`diffusers.OnnxRuntimeModel` is experimental and might change in the future.'   )
							UpperCAmelCase_    :      Any            =						model
							UpperCAmelCase_    :      int            =						kwargs.get('model_save_dir'				,		_UpperCamelCase   )
							UpperCAmelCase_    :      List[Any]            =						kwargs.get('latest_model_name'				,		_UpperCamelCase   )



		def __call__(							self				,		**_UpperCamelCase   )	->							str:
							UpperCAmelCase_    :      Optional[int]            =						{k: np.array(_UpperCamelCase   ) for k, v in kwargs.items()}
							return self.model.run(_UpperCamelCase				,		_UpperCamelCase   )



		@staticmethod
		def 					__UpperCAmelCase			(							_UpperCamelCase				,		_UpperCamelCase=None				,		_UpperCamelCase=None   )	->							List[Any]:
							if provider is None:
												logger.info('No onnxruntime provider specified, using CPUExecutionProvider'   )
												UpperCAmelCase_    :      List[str]            =						'CPUExecutionProvider'

							return ort.InferenceSession(_UpperCamelCase				,		providers=[provider]				,		sess_options=_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = None				,		**_UpperCamelCase   )	->							Dict:
							UpperCAmelCase_    :      Any            =						file_name if file_name is not None else ONNX_WEIGHTS_NAME

							UpperCAmelCase_    :      Optional[Any]            =						self.model_save_dir.joinpath(self.latest_model_name   )
							UpperCAmelCase_    :      str            =						Path(_UpperCamelCase   ).joinpath(_UpperCamelCase   )
							try:
												shutil.copyfile(_UpperCamelCase				,		_UpperCamelCase   )
							except shutil.SameFileError:
												pass

							# copy external weights (for models >2GB)
							UpperCAmelCase_    :      Optional[Any]            =						self.model_save_dir.joinpath(_UpperCamelCase   )
							if src_path.exists():
												UpperCAmelCase_    :      List[Any]            =						Path(_UpperCamelCase   ).joinpath(_UpperCamelCase   )
												try:
																	shutil.copyfile(_UpperCamelCase				,		_UpperCamelCase   )
												except shutil.SameFileError:
																	pass



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		**_UpperCamelCase				,		)	->							List[str]:
							if os.path.isfile(_UpperCamelCase   ):
												logger.error(f"Provided path ({save_directory}) should be a directory, not a file"   )
												return

							os.makedirs(_UpperCamelCase				,		exist_ok=_UpperCamelCase   )

							# saving model weights/files
							self._save_pretrained(_UpperCamelCase				,		**_UpperCamelCase   )



		@classmethod
		def 					__UpperCAmelCase			(							cls				,		_UpperCamelCase				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		_UpperCamelCase = False				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		**_UpperCamelCase				,		)	->							List[str]:
							UpperCAmelCase_    :      List[str]            =						file_name if file_name is not None else ONNX_WEIGHTS_NAME
							# load model from local directory
							if os.path.isdir(_UpperCamelCase   ):
												UpperCAmelCase_    :      Union[str, Any]            =						OnnxRuntimeModel.load_model(
												    os.path.join(_UpperCamelCase				,		_UpperCamelCase   )				,		provider=_UpperCamelCase				,		sess_options=_UpperCamelCase   )
												UpperCAmelCase_    :      Tuple            =						Path(_UpperCamelCase   )
							# load model from hub
							else:
												# download model
												UpperCAmelCase_    :      List[str]            =						hf_hub_download(
												    repo_id=_UpperCamelCase				,		filename=_UpperCamelCase				,		use_auth_token=_UpperCamelCase				,		revision=_UpperCamelCase				,		cache_dir=_UpperCamelCase				,		force_download=_UpperCamelCase				,		)
												UpperCAmelCase_    :      Union[str, Any]            =						Path(_UpperCamelCase   ).parent
												UpperCAmelCase_    :      List[str]            =						Path(_UpperCamelCase   ).name
												UpperCAmelCase_    :      Union[str, Any]            =						OnnxRuntimeModel.load_model(_UpperCamelCase				,		provider=_UpperCamelCase				,		sess_options=_UpperCamelCase   )
							return cls(model=_UpperCamelCase				,		**_UpperCamelCase   )



		@classmethod
		def 					__UpperCAmelCase			(							cls				,		_UpperCamelCase				,		_UpperCamelCase = True				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		**_UpperCamelCase				,		)	->							Optional[int]:
							UpperCAmelCase_    :      List[str]            =						None
							if len(str(_UpperCamelCase   ).split('@'   )   ) == 2:
												UpperCAmelCase_       ,  UpperCAmelCase_    :      Tuple            =						model_id.split('@'   )

							return cls._from_pretrained(
							    model_id=_UpperCamelCase				,		revision=_UpperCamelCase				,		cache_dir=_UpperCamelCase				,		force_download=_UpperCamelCase				,		use_auth_token=_UpperCamelCase				,		**_UpperCamelCase				,		)






