


'''simple docstring'''



import importlib
import inspect
import json
import os
import re
import shutil
import sys
from pathlib import Path
from typing import Dict, Optional, Union
from urllib import request

from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info
from packaging import version

from .. import __version__
from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging


__lowercase      :		Union[str, Any]			=      (
    'https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py'
)


__lowercase      :		Union[str, Any]			=      logging.get_logger(__name__)  # pylint: disable=invalid-name






def     lowerCamelCase				():
					__a     :			List[Any]          =    '''https://pypi.org/pypi/diffusers/json'''
					__a     :			Any          =    json.loads(request.urlopen(UpperCamelCase__  ).read()  )['''releases'''].keys()
					return sorted(UpperCamelCase__		, key=lambda _SCREAMING_SNAKE_CASE  : version.Version(UpperCamelCase__  )  )






def     lowerCamelCase				():
					# This function has already been executed if HF_MODULES_CACHE already is in the Python path.
					if HF_MODULES_CACHE in sys.path:
										return

					sys.path.append(UpperCamelCase__  )
					os.makedirs(UpperCamelCase__		, exist_ok=UpperCamelCase__  )
					__a     :			Union[str, Any]          =    Path(UpperCamelCase__  ) / '''__init__.py'''
					if not init_path.exists():
										init_path.touch()






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  List[str]  ):
					init_hf_modules()
					__a     :			Union[str, Any]          =    Path(UpperCamelCase__  ) / name
					# If the parent module does not exist yet, recursively create it.
					if not dynamic_module_path.parent.exists():
										create_dynamic_module(dynamic_module_path.parent  )
					os.makedirs(UpperCamelCase__		, exist_ok=UpperCamelCase__  )
					__a     :			Optional[Any]          =    dynamic_module_path / '''__init__.py'''
					if not init_path.exists():
										init_path.touch()






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  List[Any]  ):
					with open(UpperCamelCase__		, 'r'		, encoding='utf-8'  ) as f:
										__a     :			List[str]          =    f.read()

					# Imports of the form `import .xxx`
					__a     :			Optional[int]          =    re.findall('^\s*import\s+\.(\S+)\s*$'		, UpperCamelCase__		, flags=re.MULTILINE  )
					# Imports of the form `from .xxx import yyy`
					relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import'		, UpperCamelCase__		, flags=re.MULTILINE  )
					# Unique-ify
					return list(set(UpperCamelCase__  )  )






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  str  ):
					__a     :			List[Any]          =    False
					__a     :			Optional[int]          =    [module_file]
					__a     :			Tuple          =    []

					# Let's recurse through all relative imports
					while not no_change:
										__a     :			List[str]          =    []
										for f in files_to_check:
															new_imports.extend(get_relative_imports(UpperCamelCase__  )  )

										__a     :			Tuple          =    Path(UpperCamelCase__  ).parent
										__a     :			Tuple          =    [str(module_path / m  ) for m in new_imports]
										__a     :			Union[str, Any]          =    [f for f in new_import_files if f not in all_relative_imports]
										__a     :			Tuple          =    [F"""{f}.py""" for f in new_import_files]

										__a     :			List[str]          =    len(UpperCamelCase__  ) == 0
										all_relative_imports.extend(UpperCamelCase__  )

					return all_relative_imports






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  List[Any]  ):
					with open(UpperCamelCase__		, 'r'		, encoding='utf-8'  ) as f:
										__a     :			List[str]          =    f.read()

					# Imports of the form `import xxx`
					__a     :			Union[str, Any]          =    re.findall('^\s*import\s+(\S+)\s*$'		, UpperCamelCase__		, flags=re.MULTILINE  )
					# Imports of the form `from xxx import yyy`
					imports += re.findall('^\s*from\s+(\S+)\s+import'		, UpperCamelCase__		, flags=re.MULTILINE  )
					# Only keep the top-level module
					__a     :			List[str]          =    [imp.split('.'  )[0] for imp in imports if not imp.startswith('.'  )]

					# Unique-ify and test we got them all
					__a     :			List[Any]          =    list(set(UpperCamelCase__  )  )
					__a     :			List[Any]          =    []
					for imp in imports:
										try:
															importlib.import_module(UpperCamelCase__  )
										except ImportError:
															missing_packages.append(UpperCamelCase__  )

					if len(UpperCamelCase__  ) > 0:
										raise ImportError(
										    'This modeling file requires the following packages that were not found in your environment: '
										    F"""{", ".join(UpperCamelCase__  )}. Run `pip install {" ".join(UpperCamelCase__  )}`"""  )

					return get_relative_imports(UpperCamelCase__  )






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  int  ):
					__a     :			Dict          =    module_path.replace(os.path.sep		, '.'  )
					__a     :			List[Any]          =    importlib.import_module(UpperCamelCase__  )

					if class_name is None:
										return find_pipeline_class(UpperCamelCase__  )
					return getattr(UpperCamelCase__		, UpperCamelCase__  )






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Union[str, Any]  ):
					from ..pipelines import DiffusionPipeline

					__a     :			Optional[int]          =    dict(inspect.getmembers(UpperCamelCase__		, inspect.isclass  )  )

					__a     :			Dict          =    None
					for cls_name, cls in cls_members.items():
										if (
										    cls_name != DiffusionPipeline.__name__
										    and issubclass(cls		, UpperCamelCase__  )
										    and cls.__module__.split('.'  )[0] != "diffusers"
										):
															if pipeline_class is not None:
																				raise ValueError(
																				    F"""Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:"""
																				    F""" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in"""
																				    F""" {loaded_module}."""  )
															__a     :			Tuple          =    cls

					return pipeline_class






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  Optional[Any]		, _SCREAMING_SNAKE_CASE :  Optional[int] = None		, _SCREAMING_SNAKE_CASE :  int = False		, _SCREAMING_SNAKE_CASE :  Optional[Any] = False		, _SCREAMING_SNAKE_CASE :  List[Any] = None		, _SCREAMING_SNAKE_CASE :  Union[str, Any] = None		, _SCREAMING_SNAKE_CASE :  Tuple = None		, _SCREAMING_SNAKE_CASE :  int = False		, ):
					__a     :			int          =    str(UpperCamelCase__  )

					__a     :			str          =    os.path.join(UpperCamelCase__		, UpperCamelCase__  )

					if os.path.isfile(UpperCamelCase__  ):
										__a     :			Union[str, Any]          =    module_file_or_url
										__a     :			List[str]          =    '''local'''
					elif pretrained_model_name_or_path.count('/'  ) == 0:
										__a     :			Union[str, Any]          =    get_diffusers_versions()
										# cut ".dev0"
										__a     :			Tuple          =    '''v''' + '''.'''.join(__version__.split('.'  )[:3]  )

										# retrieve github version that matches
										if revision is None:
															__a     :			Optional[int]          =    latest_version if latest_version[1:] in available_versions else '''main'''
															logger.info(F"""Defaulting to latest_version: {revision}."""  )
										elif revision in available_versions:
															__a     :			List[Any]          =    F"""v{revision}"""
										elif revision == "main":
															__a     :			str          =    revision
										else:
															raise ValueError(
															    F"""`custom_revision`: {revision} does not exist. Please make sure to choose one of"""
															    F""" {", ".join(available_versions + ["main"]  )}."""  )

										# community pipeline on GitHub
										__a     :			List[str]          =    COMMUNITY_PIPELINES_URL.format(revision=UpperCamelCase__		, pipeline=UpperCamelCase__  )
										try:
															__a     :			List[str]          =    cached_download(
															    UpperCamelCase__		, cache_dir=UpperCamelCase__		, force_download=UpperCamelCase__		, proxies=UpperCamelCase__		, resume_download=UpperCamelCase__		, local_files_only=UpperCamelCase__		, use_auth_token=UpperCamelCase__		, )
															__a     :			Optional[int]          =    '''git'''
															__a     :			Union[str, Any]          =    pretrained_model_name_or_path + '''.py'''
										except EnvironmentError:
															logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}."""  )
															raise
					else:
										try:
															# Load from URL or cache if already cached
															__a     :			str          =    hf_hub_download(
															    UpperCamelCase__		, UpperCamelCase__		, cache_dir=UpperCamelCase__		, force_download=UpperCamelCase__		, proxies=UpperCamelCase__		, resume_download=UpperCamelCase__		, local_files_only=UpperCamelCase__		, use_auth_token=UpperCamelCase__		, )
															__a     :			Tuple          =    os.path.join('local'		, '--'.join(pretrained_model_name_or_path.split('/'  )  )  )
										except EnvironmentError:
															logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}."""  )
															raise

    # Check we have all the requirements in our environment
					__a     :			Dict          =    check_imports(UpperCamelCase__  )

					# Now we move the module inside our cached dynamic modules.
					__a     :			List[str]          =    DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule
					create_dynamic_module(UpperCamelCase__  )
					__a     :			str          =    Path(UpperCamelCase__  ) / full_submodule
					if submodule == "local" or submodule == "git":
										# We always copy local files (we could hash the file to see if there was a change, and give them the name of
										# that hash, to only copy when there is a modification but it seems overkill for now).
										# The only reason we do the copy is to avoid putting too many folders in sys.path.
										shutil.copy(UpperCamelCase__		, submodule_path / module_file  )
										for module_needed in modules_needed:
															__a     :			List[str]          =    F"""{module_needed}.py"""
															shutil.copy(os.path.join(UpperCamelCase__		, UpperCamelCase__  )		, submodule_path / module_needed  )
					else:
										# Get the commit hash
										# TODO: we will get this info in the etag soon, so retrieve it from there and not here.
										if isinstance(UpperCamelCase__		, UpperCamelCase__  ):
															__a     :			str          =    use_auth_token
										elif use_auth_token is True:
															__a     :			List[str]          =    HfFolder.get_token()
										else:
															__a     :			Optional[int]          =    None

										__a     :			List[str]          =    model_info(UpperCamelCase__		, revision=UpperCamelCase__		, token=UpperCamelCase__  ).sha

										# The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the
										# benefit of versioning.
										__a     :			str          =    submodule_path / commit_hash
										__a     :			Optional[Any]          =    full_submodule + os.path.sep + commit_hash
										create_dynamic_module(UpperCamelCase__  )

										if not (submodule_path / module_file).exists():
															shutil.copy(UpperCamelCase__		, submodule_path / module_file  )
										# Make sure we also have every file with relative
										for module_needed in modules_needed:
															if not (submodule_path / module_needed).exists():
																				get_cached_module_file(
																				    UpperCamelCase__		, F"""{module_needed}.py"""		, cache_dir=UpperCamelCase__		, force_download=UpperCamelCase__		, resume_download=UpperCamelCase__		, proxies=UpperCamelCase__		, use_auth_token=UpperCamelCase__		, revision=UpperCamelCase__		, local_files_only=UpperCamelCase__		, )
					return os.path.join(UpperCamelCase__		, UpperCamelCase__  )






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  str = None		, _SCREAMING_SNAKE_CASE :  Any = None		, _SCREAMING_SNAKE_CASE :  Dict = False		, _SCREAMING_SNAKE_CASE :  Any = False		, _SCREAMING_SNAKE_CASE :  Optional[Any] = None		, _SCREAMING_SNAKE_CASE :  str = None		, _SCREAMING_SNAKE_CASE :  int = None		, _SCREAMING_SNAKE_CASE :  str = False		, **_SCREAMING_SNAKE_CASE :  Dict		, ):
					__a     :			List[Any]          =    get_cached_module_file(
					    UpperCamelCase__		, UpperCamelCase__		, cache_dir=UpperCamelCase__		, force_download=UpperCamelCase__		, resume_download=UpperCamelCase__		, proxies=UpperCamelCase__		, use_auth_token=UpperCamelCase__		, revision=UpperCamelCase__		, local_files_only=UpperCamelCase__		, )
					return get_class_in_module(UpperCamelCase__		, final_module.replace('.py'		, ''  )  )





from collections import OrderedDict
from typing import Mapping

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


lowerCamelCase  =logging.get_logger(__name__)

lowerCamelCase  ={
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	'''distilbert'''
     SCREAMING_SNAKE_CASE_		 =	{
         '''hidden_size''': '''dim''',
         '''num_attention_heads''': '''n_heads''',
         '''num_hidden_layers''': '''n_layers''',
     }






     def __init__(				self    ,    __SCREAMING_SNAKE_CASE=3_0_5_2_2    ,    __SCREAMING_SNAKE_CASE=5_1_2    ,    __SCREAMING_SNAKE_CASE=False    ,    __SCREAMING_SNAKE_CASE=6    ,    __SCREAMING_SNAKE_CASE=1_2    ,    __SCREAMING_SNAKE_CASE=7_6_8    ,    __SCREAMING_SNAKE_CASE=4 * 7_6_8    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE="gelu"    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.2    ,    __SCREAMING_SNAKE_CASE=0    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					vocab_size
           UpperCamelCase__     : Dict											=					max_position_embeddings
           UpperCamelCase__     : Any											=					sinusoidal_pos_embds
           UpperCamelCase__     : Dict											=					n_layers
           UpperCamelCase__     : List[Any]											=					n_heads
           UpperCamelCase__     : Dict											=					dim
           UpperCamelCase__     : Dict											=					hidden_dim
           UpperCamelCase__     : Optional[int]											=					dropout
           UpperCamelCase__     : Optional[Any]											=					attention_dropout
           UpperCamelCase__     : Tuple											=					activation
           UpperCamelCase__     : Optional[int]											=					initializer_range
           UpperCamelCase__     : Optional[int]											=					qa_dropout
           UpperCamelCase__     : str											=					seq_classif_dropout
           super().__init__(**__SCREAMING_SNAKE_CASE    ,    pad_token_id=__SCREAMING_SNAKE_CASE	)




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







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




           """simple docstring"""







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


