

"""simple docstring"""




import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
						import jax
						import jax.numpy as jnp





@slow
@require_flax
class    lowerCamelCase						(unittest.TestCase			):

			def 					SCREAMING_SNAKE_CASE				(					self				:						Dict    ,				__UpperCAmelCase				:						Optional[int]    ,				__UpperCAmelCase				:						str       )   ->    List[str]:
						return F"""gaussian_noise_s={seed}_shape={"_".join([str(_A       ) for s in shape]       )}.npy"""

			def 					SCREAMING_SNAKE_CASE				(					self				:						str       )   ->    Tuple:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()

			def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]    ,				__UpperCAmelCase				:						Union[str, Any]=0    ,				__UpperCAmelCase				:						List[str]=(4, 4, 6_4, 6_4)    ,				__UpperCAmelCase				:						str=False       )   ->    Optional[Any]:
						SCREAMING_SNAKE_CASE__								=      jnp.bfloataa if fpaa else jnp.floataa
						SCREAMING_SNAKE_CASE__								=      jnp.array(load_hf_numpy(self.get_file_format(_A    ,				_A       )       )    ,				dtype=_A       )
						return image

			def 					SCREAMING_SNAKE_CASE				(					self				:						Any    ,				__UpperCAmelCase				:						Tuple=False    ,				__UpperCAmelCase				:						str="CompVis/stable-diffusion-v1-4"       )   ->    Optional[Any]:
						SCREAMING_SNAKE_CASE__								=      jnp.bfloataa if fpaa else jnp.floataa
						SCREAMING_SNAKE_CASE__								=      """bf16""" if fpaa else None

						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      FlaxUNetaDConditionModel.from_pretrained(
						    _A    ,				subfolder="""unet"""    ,				dtype=_A    ,				revision=_A       )
						return model, params

			def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple    ,				__UpperCAmelCase				:						Optional[Any]=0    ,				__UpperCAmelCase				:						List[str]=(4, 7_7, 7_6_8)    ,				__UpperCAmelCase				:						List[str]=False       )   ->    int:
						SCREAMING_SNAKE_CASE__								=      jnp.bfloataa if fpaa else jnp.floataa
						SCREAMING_SNAKE_CASE__								=      jnp.array(load_hf_numpy(self.get_file_format(_A    ,				_A       )       )    ,				dtype=_A       )
						return hidden_states

			@parameterized.expand(
			    [
			        # fmt: off
			        [8_3, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]],
			        [1_7, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]],
			        [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]],
			        [3, 1_0_0_0, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]],
			        # fmt: on
			    ]       )
			def 					SCREAMING_SNAKE_CASE				(					self				:						List[str]    ,				__UpperCAmelCase				:						Optional[int]    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Any       )   ->    Any:
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""    ,				fpaa=_A       )
						SCREAMING_SNAKE_CASE__								=      self.get_latents(_A    ,				fpaa=_A       )
						SCREAMING_SNAKE_CASE__								=      self.get_encoder_hidden_states(_A    ,				fpaa=_A       )

						SCREAMING_SNAKE_CASE__								=      model.apply(
						    {"""params""": params}    ,				_A    ,				jnp.array(_A    ,				dtype=jnp.intaa       )    ,				encoder_hidden_states=_A    ,				).sample

						assert sample.shape == latents.shape

						SCREAMING_SNAKE_CASE__								=      jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())       )    ,				dtype=jnp.floataa       )
						SCREAMING_SNAKE_CASE__								=      jnp.array(_A    ,				dtype=jnp.floataa       )

						# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
						assert jnp.allclose(_A    ,				_A    ,				atol=1e-2       )





			@parameterized.expand(
			    [
			        # fmt: off
			        [8_3, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]],
			        [1_7, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]],
			        [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]],
			        [3, 1_0_0_0, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]],
			        # fmt: on
			    ]       )
			def 					SCREAMING_SNAKE_CASE				(					self				:						int    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Dict    ,				__UpperCAmelCase				:						Union[str, Any]       )   ->    Optional[int]:
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""    ,				fpaa=_A       )
						SCREAMING_SNAKE_CASE__								=      self.get_latents(_A    ,				shape=(4, 4, 9_6, 9_6)    ,				fpaa=_A       )
						SCREAMING_SNAKE_CASE__								=      self.get_encoder_hidden_states(_A    ,				shape=(4, 7_7, 1_0_2_4)    ,				fpaa=_A       )

						SCREAMING_SNAKE_CASE__								=      model.apply(
						    {"""params""": params}    ,				_A    ,				jnp.array(_A    ,				dtype=jnp.intaa       )    ,				encoder_hidden_states=_A    ,				).sample

						assert sample.shape == latents.shape

						SCREAMING_SNAKE_CASE__								=      jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())       )    ,				dtype=jnp.floataa       )
						SCREAMING_SNAKE_CASE__								=      jnp.array(_A    ,				dtype=jnp.floataa       )

						# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
						assert jnp.allclose(_A    ,				_A    ,				atol=1e-2       )








"""simple docstring"""




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


A_							:			Dict											= {
    "configuration_efficientnet": [
        "EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "EfficientNetConfig",
        "EfficientNetOnnxConfig",
    ]
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A_							:			List[Any]											= ["EfficientNetImageProcessor"]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A_							:			Dict											= [
						    "EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "EfficientNetForImageClassification",
						    "EfficientNetModel",
						    "EfficientNetPreTrainedModel",
						]

if TYPE_CHECKING:
						from .configuration_efficientnet import (
						    EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    EfficientNetConfig,
						    EfficientNetOnnxConfig,
						)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_efficientnet import EfficientNetImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_efficientnet import (
												    EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST,
												    EfficientNetForImageClassification,
												    EfficientNetModel,
												    EfficientNetPreTrainedModel,
												)

else:
						import sys

						A_							:			List[Any]											= _LazyModule(__name__, globals()["__file__"], _import_structure)


