





'''simple docstring'''




import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow




class      UpperCamelCase__					:




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			List[str]      ,						lowerCamelCase_				:			List[str]      ,						lowerCamelCase_				:			Any      ,						lowerCamelCase_				:			Union[str, Any]						):

							'''simple docstring'''



							return None







class      UpperCamelCase__					:




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			Dict      ,						lowerCamelCase_				:			List[str]      ,						lowerCamelCase_				:			Union[str, Any]      ,						lowerCamelCase_				:			Dict      ,						lowerCamelCase_				:			List[str]						):

							'''simple docstring'''



							return None







class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	[
					    # (model_name, model_kwargs)
					    ('''bert-base-cased''', {}),
					    ('''gpt2''', {'''use_cache''': False}),  # We don't support exporting GPT2 past keys anymore
					]


					@require_tf
					@slow
					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
									self._test_export(lowerCamelCase_      ,						"""tf"""      ,						12      ,						**lowerCamelCase_						)


					@require_torch
					@slow
					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
									self._test_export(lowerCamelCase_      ,						"""pt"""      ,						12      ,						**lowerCamelCase_						)


					@require_torch
					@slow
					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							from transformers import BertModel

							SCREAMING_SNAKE_CASE :				str		=			["""[UNK]""", """[SEP]""", """[CLS]""", """[PAD]""", """[MASK]""", """some""", """other""", """words"""]
							with NamedTemporaryFile(mode="""w+t"""						) as vocab_file:
									vocab_file.write("""\n""".join(lowerCamelCase_						)						)
									vocab_file.flush()
									SCREAMING_SNAKE_CASE :				int		=			BertTokenizerFast(vocab_file.name						)

							with TemporaryDirectory() as bert_save_dir:
									SCREAMING_SNAKE_CASE :				Tuple		=			BertModel(BertConfig(vocab_size=len(lowerCamelCase_						)						)						)
									model.save_pretrained(lowerCamelCase_						)
									self._test_export(lowerCamelCase_      ,						"""pt"""      ,						12      ,						lowerCamelCase_						)


					@require_tf
					@slow
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
									SCREAMING_SNAKE_CASE :				Optional[Any]		=			self._test_export(lowerCamelCase_      ,						"""tf"""      ,						12      ,						**lowerCamelCase_						)
									SCREAMING_SNAKE_CASE :				List[str]		=			quantize(Path(lowerCamelCase_						)						)

									# Ensure the actual quantized model is not bigger than the original one
									if quantized_path.stat().st_size >= Path(lowerCamelCase_						).stat().st_size:
											self.fail("""Quantized model is bigger than initial ONNX model"""						)


					@require_torch
					@slow
					def lowerCamelCase_			(						self				:			Dict						):

							'''simple docstring'''



							for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
									SCREAMING_SNAKE_CASE :				Dict		=			self._test_export(lowerCamelCase_      ,						"""pt"""      ,						12      ,						**lowerCamelCase_						)
									SCREAMING_SNAKE_CASE :				Optional[int]		=			quantize(lowerCamelCase_						)

									# Ensure the actual quantized model is not bigger than the original one
									if quantized_path.stat().st_size >= Path(lowerCamelCase_						).stat().st_size:
											self.fail("""Quantized model is bigger than initial ONNX model"""						)


					def lowerCamelCase_			(						self				:			List[str]      ,						lowerCamelCase_				:			Any      ,						lowerCamelCase_				:			List[str]      ,						lowerCamelCase_				:			Union[str, Any]      ,						lowerCamelCase_				:			Optional[int]=None      ,						**lowerCamelCase_				:			Optional[Any]						):

							'''simple docstring'''



							try:
									# Compute path
									with TemporaryDirectory() as tempdir:
											SCREAMING_SNAKE_CASE :				str		=			Path(lowerCamelCase_						).joinpath("""model.onnx"""						)

									# Remove folder if exists
									if path.parent.exists():
											path.parent.rmdir()

									# Export
									convert(lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						lowerCamelCase_      ,						**lowerCamelCase_						)

									return path
							except Exception as e:
									self.fail(lowerCamelCase_						)


					@require_torch
					@require_tokenizers
					@slow
					def lowerCamelCase_			(						self				:			Any						):

							'''simple docstring'''



							from transformers import BertModel

							SCREAMING_SNAKE_CASE :				int		=			BertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""						)						)
							SCREAMING_SNAKE_CASE :				Optional[int]		=			BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""						)
							self._test_infer_dynamic_axis(lowerCamelCase_      ,						lowerCamelCase_      ,						"""pt"""						)


					@require_tf
					@require_tokenizers
					@slow
					def lowerCamelCase_			(						self				:			List[Any]						):

							'''simple docstring'''



							from transformers import TFBertModel

							SCREAMING_SNAKE_CASE :				Optional[int]		=			TFBertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""						)						)
							SCREAMING_SNAKE_CASE :				Optional[int]		=			BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""						)
							self._test_infer_dynamic_axis(lowerCamelCase_      ,						lowerCamelCase_      ,						"""tf"""						)


					def lowerCamelCase_			(						self				:			Any      ,						lowerCamelCase_				:			List[Any]      ,						lowerCamelCase_				:			Optional[int]      ,						lowerCamelCase_				:			List[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			FeatureExtractionPipeline(lowerCamelCase_      ,						lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				Optional[int]		=			["""input_ids""", """token_type_ids""", """attention_mask""", """output_0""", """output_1"""]
							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				List[Any]		=			infer_shapes(lowerCamelCase_      ,						lowerCamelCase_						)

							# Assert all variables are present
							self.assertEqual(len(lowerCamelCase_						)      ,						len(lowerCamelCase_						)						)
							self.assertTrue(all(var_name in shapes for var_name in variable_names						)						)
							self.assertSequenceEqual(variable_names[:3]      ,						lowerCamelCase_						)
							self.assertSequenceEqual(variable_names[3:]      ,						lowerCamelCase_						)

							# Assert inputs are {0: batch, 1: sequence}
							for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
									self.assertDictEqual(shapes[var_name]      ,						{0: """batch""", 1: """sequence"""}						)

							# Assert outputs are {0: batch, 1: sequence} and {0: batch}
							self.assertDictEqual(shapes["""output_0"""]      ,						{0: """batch""", 1: """sequence"""}						)
							self.assertDictEqual(shapes["""output_1"""]      ,						{0: """batch"""}						)


					def lowerCamelCase_			(						self				:			List[str]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[str]		=			["""input_ids""", """attention_mask""", """token_type_ids"""]
							SCREAMING_SNAKE_CASE :				Dict		=			{"""input_ids""": [1, 2, 3, 4], """attention_mask""": [0, 0, 0, 0], """token_type_ids""": [1, 1, 1, 1]}
							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				List[Any]		=			ensure_valid_input(FuncContiguousArgs()      ,						lowerCamelCase_      ,						lowerCamelCase_						)

							# Should have exactly the same number of args (all are valid)
							self.assertEqual(len(lowerCamelCase_						)      ,						3						)

							# Should have exactly the same input names
							self.assertEqual(set(lowerCamelCase_						)      ,						set(lowerCamelCase_						)						)

							# Parameter should be reordered according to their respective place in the function:
							# (input_ids, token_type_ids, attention_mask)
							self.assertEqual(lowerCamelCase_      ,						(tokens["""input_ids"""], tokens["""token_type_ids"""], tokens["""attention_mask"""])						)

							# Generated args are interleaved with another args (for instance parameter "past" in GPT2)
							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Tuple		=			ensure_valid_input(FuncNonContiguousArgs()      ,						lowerCamelCase_      ,						lowerCamelCase_						)

							# Should have exactly the one arg (all before the one not provided "some_other_args")
							self.assertEqual(len(lowerCamelCase_						)      ,						1						)
							self.assertEqual(len(lowerCamelCase_						)      ,						1						)

							# Should have only "input_ids"
							self.assertEqual(inputs_args[0]      ,						tokens["""input_ids"""]						)
							self.assertEqual(ordered_input_names[0]      ,						"""input_ids"""						)



					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[Any]		=			generate_identified_filename(Path("""/home/something/my_fake_model.onnx"""						)      ,						"""-test"""						)
							self.assertEqual("""/home/something/my_fake_model-test.onnx"""      ,						generated.as_posix()						)










'''simple docstring'''




__UpperCAmelCase            =       """0.18.2"""

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
							if not is_onnx_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_onnx_objects import *  # noqa F403
else:
							from .pipelines import OnnxRuntimeModel

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_pt_objects import *  # noqa F403
else:
							from .models import (
							    AutoencoderKL,
							    ControlNetModel,
							    ModelMixin,
							    PriorTransformer,
							    TaFilmDecoder,
							    TransformeraDModel,
							    UNetaDModel,
							    UNetaDConditionModel,
							    UNetaDModel,
							    UNetaDConditionModel,
							    VQModel,
							)
							from .optimization import (
							    get_constant_schedule,
							    get_constant_schedule_with_warmup,
							    get_cosine_schedule_with_warmup,
							    get_cosine_with_hard_restarts_schedule_with_warmup,
							    get_linear_schedule_with_warmup,
							    get_polynomial_decay_schedule_with_warmup,
							    get_scheduler,
							)
							from .pipelines import (
							    AudioPipelineOutput,
							    ConsistencyModelPipeline,
							    DanceDiffusionPipeline,
							    DDIMPipeline,
							    DDPMPipeline,
							    DiffusionPipeline,
							    DiTPipeline,
							    ImagePipelineOutput,
							    KarrasVePipeline,
							    LDMPipeline,
							    LDMSuperResolutionPipeline,
							    PNDMPipeline,
							    RePaintPipeline,
							    ScoreSdeVePipeline,
							)
							from .schedulers import (
							    CMStochasticIterativeScheduler,
							    DDIMInverseScheduler,
							    DDIMParallelScheduler,
							    DDIMScheduler,
							    DDPMParallelScheduler,
							    DDPMScheduler,
							    DEISMultistepScheduler,
							    DPMSolverMultistepInverseScheduler,
							    DPMSolverMultistepScheduler,
							    DPMSolverSinglestepScheduler,
							    EulerAncestralDiscreteScheduler,
							    EulerDiscreteScheduler,
							    HeunDiscreteScheduler,
							    IPNDMScheduler,
							    KarrasVeScheduler,
							    KDPMaAncestralDiscreteScheduler,
							    KDPMaDiscreteScheduler,
							    PNDMScheduler,
							    RePaintScheduler,
							    SchedulerMixin,
							    ScoreSdeVeScheduler,
							    UnCLIPScheduler,
							    UniPCMultistepScheduler,
							    VQDiffusionScheduler,
							)
							from .training_utils import EMAModel

try:
							if not (is_torch_available() and is_scipy_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
							from .schedulers import LMSDiscreteScheduler

try:
							if not (is_torch_available() and is_torchsde_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
							from .schedulers import DPMSolverSDEScheduler

try:
							if not (is_torch_available() and is_transformers_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
							from .pipelines import (
							    AltDiffusionImgaImgPipeline,
							    AltDiffusionPipeline,
							    AudioLDMPipeline,
							    CycleDiffusionPipeline,
							    IFImgaImgPipeline,
							    IFImgaImgSuperResolutionPipeline,
							    IFInpaintingPipeline,
							    IFInpaintingSuperResolutionPipeline,
							    IFPipeline,
							    IFSuperResolutionPipeline,
							    ImageTextPipelineOutput,
							    KandinskyImgaImgPipeline,
							    KandinskyInpaintPipeline,
							    KandinskyPipeline,
							    KandinskyPriorPipeline,
							    KandinskyVaaControlnetImgaImgPipeline,
							    KandinskyVaaControlnetPipeline,
							    KandinskyVaaImgaImgPipeline,
							    KandinskyVaaInpaintPipeline,
							    KandinskyVaaPipeline,
							    KandinskyVaaPriorEmbaEmbPipeline,
							    KandinskyVaaPriorPipeline,
							    LDMTextToImagePipeline,
							    PaintByExamplePipeline,
							    SemanticStableDiffusionPipeline,
							    ShapEImgaImgPipeline,
							    ShapEPipeline,
							    StableDiffusionAttendAndExcitePipeline,
							    StableDiffusionControlNetImgaImgPipeline,
							    StableDiffusionControlNetInpaintPipeline,
							    StableDiffusionControlNetPipeline,
							    StableDiffusionDepthaImgPipeline,
							    StableDiffusionDiffEditPipeline,
							    StableDiffusionImageVariationPipeline,
							    StableDiffusionImgaImgPipeline,
							    StableDiffusionInpaintPipeline,
							    StableDiffusionInpaintPipelineLegacy,
							    StableDiffusionInstructPixaPixPipeline,
							    StableDiffusionLatentUpscalePipeline,
							    StableDiffusionLDMaDPipeline,
							    StableDiffusionModelEditingPipeline,
							    StableDiffusionPanoramaPipeline,
							    StableDiffusionParadigmsPipeline,
							    StableDiffusionPipeline,
							    StableDiffusionPipelineSafe,
							    StableDiffusionPixaPixZeroPipeline,
							    StableDiffusionSAGPipeline,
							    StableDiffusionUpscalePipeline,
							    StableUnCLIPImgaImgPipeline,
							    StableUnCLIPPipeline,
							    TextToVideoSDPipeline,
							    TextToVideoZeroPipeline,
							    UnCLIPImageVariationPipeline,
							    UnCLIPPipeline,
							    UniDiffuserModel,
							    UniDiffuserPipeline,
							    UniDiffuserTextDecoder,
							    VersatileDiffusionDualGuidedPipeline,
							    VersatileDiffusionImageVariationPipeline,
							    VersatileDiffusionPipeline,
							    VersatileDiffusionTextToImagePipeline,
							    VideoToVideoSDPipeline,
							    VQDiffusionPipeline,
							)

try:
							if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
							from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
							if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
							from .pipelines import StableDiffusionKDiffusionPipeline

try:
							if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
							from .pipelines import (
							    OnnxStableDiffusionImgaImgPipeline,
							    OnnxStableDiffusionInpaintPipeline,
							    OnnxStableDiffusionInpaintPipelineLegacy,
							    OnnxStableDiffusionPipeline,
							    OnnxStableDiffusionUpscalePipeline,
							    StableDiffusionOnnxPipeline,
							)

try:
							if not (is_torch_available() and is_librosa_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
							from .pipelines import AudioDiffusionPipeline, Mel

try:
							if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
							from .pipelines import SpectrogramDiffusionPipeline

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_flax_objects import *  # noqa F403
else:
							from .models.controlnet_flax import FlaxControlNetModel
							from .models.modeling_flax_utils import FlaxModelMixin
							from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
							from .models.vae_flax import FlaxAutoencoderKL
							from .pipelines import FlaxDiffusionPipeline
							from .schedulers import (
							    FlaxDDIMScheduler,
							    FlaxDDPMScheduler,
							    FlaxDPMSolverMultistepScheduler,
							    FlaxKarrasVeScheduler,
							    FlaxLMSDiscreteScheduler,
							    FlaxPNDMScheduler,
							    FlaxSchedulerMixin,
							    FlaxScoreSdeVeScheduler,
							)


try:
							if not (is_flax_available() and is_transformers_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
							from .pipelines import (
							    FlaxStableDiffusionControlNetPipeline,
							    FlaxStableDiffusionImgaImgPipeline,
							    FlaxStableDiffusionInpaintPipeline,
							    FlaxStableDiffusionPipeline,
							)

try:
							if not (is_note_seq_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from .utils.dummy_note_seq_objects import *  # noqa F403
else:
							from .pipelines import MidiProcessor



