


"""simple docstring"""



from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
  from transformers import (
      TFAutoModel,
      TFAutoModelForCausalLM,
      TFAutoModelForMaskedLM,
      TFAutoModelForPreTraining,
      TFAutoModelForQuestionAnswering,
      TFAutoModelForSeqaSeqLM,
      TFAutoModelForSequenceClassification,
      TFAutoModelForTableQuestionAnswering,
      TFAutoModelForTokenClassification,
      TFAutoModelWithLMHead,
      TFBertForMaskedLM,
      TFBertForPreTraining,
      TFBertForQuestionAnswering,
      TFBertForSequenceClassification,
      TFBertModel,
      TFFunnelBaseModel,
      TFFunnelModel,
      TFGPTaLMHeadModel,
      TFRobertaForMaskedLM,
      TFTaForConditionalGeneration,
      TFTapasForQuestionAnswering,
  )
  from transformers.models.auto.modeling_tf_auto import (
      TF_MODEL_FOR_CAUSAL_LM_MAPPING,
      TF_MODEL_FOR_MASKED_LM_MAPPING,
      TF_MODEL_FOR_PRETRAINING_MAPPING,
      TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
      TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
      TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
      TF_MODEL_MAPPING,
  )
  from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
  from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
  from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
  from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST







class _lowerCAmelCase    (				snake_case__     ):
   __UpperCAmelCase			:	Optional[int]							  =  """new-model"""


if is_tf_available():







  class _lowerCAmelCase    (				snake_case__     ):
     __UpperCAmelCase			:	Dict							  =  NewModelConfig







@require_tf
class _lowerCAmelCase    (				unittest.TestCase     ):







   @slow
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				int				 =   "bert-base-cased"
       snake_case   :				str				 =   AutoConfig.from_pretrained(_A						)
       self.assertIsNotNone(_A						)
       self.assertIsInstance(_A						,     _A						)

       snake_case   :				Union[str, Any]				 =   TFAutoModel.from_pretrained(_A						)
       self.assertIsNotNone(_A						)
       self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   "bert-base-cased"
       snake_case   :				Tuple				 =   AutoConfig.from_pretrained(_A						)
       self.assertIsNotNone(_A						)
       self.assertIsInstance(_A						,     _A						)

       snake_case   :				List[Any]				 =   TFAutoModelForPreTraining.from_pretrained(_A						)
       self.assertIsNotNone(_A						)
       self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				Optional[int]				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				Optional[int]				 =   TFAutoModelForCausalLM.from_pretrained(_A						)
           snake_case ,snake_case   :				Optional[Any]				 =   TFAutoModelForCausalLM.from_pretrained(_A						,     output_loading_info=_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				Any				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				List[str]				 =   TFAutoModelWithLMHead.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				str				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				Optional[int]				 =   TFAutoModelForMaskedLM.from_pretrained(_A						)
           snake_case ,snake_case   :				Optional[int]				 =   TFAutoModelForMaskedLM.from_pretrained(_A						,     output_loading_info=_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				Optional[int]				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				Dict				 =   TFAutoModelForSeqaSeqLM.from_pretrained(_A						)
           snake_case ,snake_case   :				int				 =   TFAutoModelForSeqaSeqLM.from_pretrained(_A						,     output_loading_info=_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       for model_name in ["bert-base-uncased"]:
           snake_case   :				Tuple				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				List[str]				 =   TFAutoModelForSequenceClassification.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       for model_name in ["bert-base-uncased"]:
           snake_case   :				List[str]				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				str				 =   TFAutoModelForQuestionAnswering.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   @slow
   @require_tensorflow_probability
   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
           snake_case   :				List[Any]				 =   AutoConfig.from_pretrained(_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)

           snake_case   :				List[str]				 =   TFAutoModelForTableQuestionAnswering.from_pretrained(_A						)
           snake_case ,snake_case   :				Dict				 =   TFAutoModelForTableQuestionAnswering.from_pretrained(
               _A						,     output_loading_info=_A						)
           self.assertIsNotNone(_A						)
           self.assertIsInstance(_A						,     _A						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Any				 =   TFAutoModelWithLMHead.from_pretrained(_A						)
       self.assertIsInstance(_A						,     _A						)
       self.assertEqual(model.num_parameters()						,     1_4410						)
       self.assertEqual(model.num_parameters(only_trainable=_A						)						,     1_4410						)







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   TFAutoModelWithLMHead.from_pretrained(_A						)
       self.assertIsInstance(_A						,     _A						)
       self.assertEqual(model.num_parameters()						,     1_4410						)
       self.assertEqual(model.num_parameters(only_trainable=_A						)						,     1_4410						)







   def 			lowerCamelCase		(     self						)     ->		Union[str, Any]:

       '''simple docstring'''




       snake_case   :				List[str]				 =   TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"						)
       self.assertIsInstance(_A						,     _A						)

       snake_case   :				Optional[Any]				 =   copy.deepcopy(model.config						)
       snake_case   :				Optional[Any]				 =   ["FunnelBaseModel"]
       snake_case   :				Tuple				 =   TFAutoModel.from_config(_A						)
       self.assertIsInstance(_A						,     _A						)

       with tempfile.TemporaryDirectory() as tmp_dir:
           model.save_pretrained(_A						)
           snake_case   :				Optional[Any]				 =   TFAutoModel.from_pretrained(_A						)
           self.assertIsInstance(_A						,     _A						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       try:
           AutoConfig.register("new-model"						,     _A						)

           snake_case   :				Tuple				 =   [
               TFAutoModel,
               TFAutoModelForCausalLM,
               TFAutoModelForMaskedLM,
               TFAutoModelForPreTraining,
               TFAutoModelForQuestionAnswering,
               TFAutoModelForSequenceClassification,
               TFAutoModelForTokenClassification,
           ]

           for auto_class in auto_classes:
               with self.subTest(auto_class.__name__						):
                   # Wrong config class will raise an error
                   with self.assertRaises(_A						):
                       auto_class.register(_A						,     _A						)
                   auto_class.register(_A						,     _A						)
                   # Trying to register something existing in the Transformers library will raise an error
                   with self.assertRaises(_A						):
                       auto_class.register(_A						,     _A						)

                   # Now that the config is registered, it can be used as any other config with the auto-API
                   snake_case   :				int				 =   BertModelTester(self						).get_config()
                   snake_case   :				List[str]				 =   NewModelConfig(**tiny_config.to_dict()						)
                   snake_case   :				Union[str, Any]				 =   auto_class.from_config(_A						)
                   self.assertIsInstance(_A						,     _A						)

                   with tempfile.TemporaryDirectory() as tmp_dir:
                       model.save_pretrained(_A						)
                       snake_case   :				int				 =   auto_class.from_pretrained(_A						)
                       self.assertIsInstance(_A						,     _A						)

       finally:
           if "new-model" in CONFIG_MAPPING._extra_content:
               del CONFIG_MAPPING._extra_content["new-model"]
           for mapping in (
               TF_MODEL_MAPPING,
               TF_MODEL_FOR_PRETRAINING_MAPPING,
               TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
               TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
               TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
               TF_MODEL_FOR_CAUSAL_LM_MAPPING,
               TF_MODEL_FOR_MASKED_LM_MAPPING,
           ):
               if NewModelConfig in mapping._extra_content:
                   del mapping._extra_content[NewModelConfig]







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       with self.assertRaisesRegex(
           _A						,     "bert-base is not a local folder and is not a valid model identifier"						):
           snake_case   :				Tuple				 =   TFAutoModel.from_pretrained("bert-base"						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       with self.assertRaisesRegex(
           _A						,     r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"						):
           snake_case   :				Dict				 =   TFAutoModel.from_pretrained(_A						,     revision="aaaaaa"						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       with self.assertRaisesRegex(
           _A						,     "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"						,     ):
           snake_case   :				Union[str, Any]				 =   TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"						)







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       with self.assertRaisesRegex(_A						,     "Use `from_pt=True` to load this model"						):
           snake_case   :				Any				 =   TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Any				 =   TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"						)
       with RequestCounter() as counter:
           snake_case   :				Tuple				 =   TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"						)
           self.assertEqual(counter.get_request_count						,     0						)
           self.assertEqual(counter.head_request_count						,     1						)
           self.assertEqual(counter.other_request_count						,     0						)

       # With a sharded checkpoint
       snake_case   :				Dict				 =   TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"						)
       with RequestCounter() as counter:
           snake_case   :				Tuple				 =   TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"						)
           self.assertEqual(counter.get_request_count						,     0						)
           self.assertEqual(counter.head_request_count						,     1						)
           self.assertEqual(counter.other_request_count						,     0						)









"""simple docstring"""



from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import (
    BaseOutput,
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)







@dataclass
class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	Union[List[PIL.Image.Image], np.ndarray]
   __UpperCAmelCase			:	Optional[List[bool]]


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
  from .pipeline_cycle_diffusion import CycleDiffusionPipeline
  from .pipeline_stable_diffusion import StableDiffusionPipeline
  from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline
  from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline
  from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
  from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
  from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline
  from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline
  from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline
  from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline
  from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline
  from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline
  from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline
  from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
  from .pipeline_stable_unclip import StableUnCLIPPipeline
  from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline
  from .safety_checker import StableDiffusionSafetyChecker
  from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer

try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline
else:
  from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline


try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.26.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      StableDiffusionDepthaImgPipeline,
      StableDiffusionDiffEditPipeline,
      StableDiffusionPixaPixZeroPipeline,
  )
else:
  from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline
  from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline
  from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline


try:
  if not (
      is_torch_available()
      and is_transformers_available()
      and is_k_diffusion_available()
      and is_k_diffusion_version(""">=""", """0.0.12""")
  ):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
  from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline

try:
  if not (is_transformers_available() and is_onnx_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_onnx_objects import *  # noqa F403
else:
  from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline
  from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline
  from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline
  from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy
  from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline

if is_transformers_available() and is_flax_available():
  import flax







  @flax.struct.dataclass
  class _lowerCAmelCase    (				snake_case_     ):
     __UpperCAmelCase			:	np.ndarray
     __UpperCAmelCase			:	List[bool]



  from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState
  from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline
  from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline
  from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline
  from .safety_checker_flax import FlaxStableDiffusionSafetyChecker





