
"""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 		a_    (			unittest.TestCase						):







    def   _snake_case       (      self      :							Union[str, Any]  ,       __UpperCamelCase      :							List[Any]  ,       __UpperCamelCase      :							str  )		->int:






        '''simple docstring'''






        return f"""gaussian_noise_s={seed}_shape={"_".join([str(__UpperCamelCase  ) for s in shape]  )}.npy"""







    def   _snake_case       (      self      :							Dict  )		->Any:






        '''simple docstring'''






        super().tearDown()
        gc.collect()







    def   _snake_case       (      self      :							List[Any]  ,       __UpperCamelCase      :							Tuple=0  ,       __UpperCamelCase      :							Optional[int]=(4, 4, 64, 64)  ,       __UpperCamelCase      :							Any=False  )		->Optional[Any]:






        '''simple docstring'''






        _UpperCAmelCase        = jnp.bfloataa if fpaa else jnp.floataa
        _UpperCAmelCase        = jnp.array(load_hf_numpy(self.get_file_format(__UpperCamelCase  ,       __UpperCamelCase  )  )  ,       dtype=__UpperCamelCase  )
        return image







    def   _snake_case       (      self      :							List[Any]  ,       __UpperCamelCase      :							Optional[int]=False  ,       __UpperCamelCase      :							int="CompVis/stable-diffusion-v1-4"  )		->List[Any]:






        '''simple docstring'''






        _UpperCAmelCase        = jnp.bfloataa if fpaa else jnp.floataa
        _UpperCAmelCase        = """bf16""" if fpaa else None

        _UpperCAmelCase     ,_UpperCAmelCase        = FlaxUNetaDConditionModel.from_pretrained(
            __UpperCamelCase  ,       subfolder="""unet"""  ,       dtype=__UpperCamelCase  ,       revision=__UpperCamelCase  )
        return model, params







    def   _snake_case       (      self      :							List[str]  ,       __UpperCamelCase      :							Optional[int]=0  ,       __UpperCamelCase      :							Optional[int]=(4, 77, 7_68)  ,       __UpperCamelCase      :							str=False  )		->List[Any]:






        '''simple docstring'''






        _UpperCAmelCase        = jnp.bfloataa if fpaa else jnp.floataa
        _UpperCAmelCase        = jnp.array(load_hf_numpy(self.get_file_format(__UpperCamelCase  ,       __UpperCamelCase  )  )  ,       dtype=__UpperCamelCase  )
        return hidden_states







    @parameterized.expand(
        [
            # fmt: off
            [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
            [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
            [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
            [3, 10_00, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
            # fmt: on
        ]  )
    def   _snake_case       (      self      :							str  ,       __UpperCamelCase      :							str  ,       __UpperCamelCase      :							Optional[int]  ,       __UpperCamelCase      :							Any  )		->List[str]:






        '''simple docstring'''






        _UpperCAmelCase     ,_UpperCAmelCase        = self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""  ,       fpaa=__UpperCamelCase  )
        _UpperCAmelCase        = self.get_latents(__UpperCamelCase  ,       fpaa=__UpperCamelCase  )
        _UpperCAmelCase        = self.get_encoder_hidden_states(__UpperCamelCase  ,       fpaa=__UpperCamelCase  )

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

        assert sample.shape == latents.shape

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

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







    @parameterized.expand(
        [
            # fmt: off
            [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
            [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
            [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
            [3, 10_00, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
            # fmt: on
        ]  )
    def   _snake_case       (      self      :							Optional[int]  ,       __UpperCamelCase      :							Union[str, Any]  ,       __UpperCamelCase      :							Union[str, Any]  ,       __UpperCamelCase      :							Any  )		->List[Any]:






        '''simple docstring'''






        _UpperCAmelCase     ,_UpperCAmelCase        = self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""  ,       fpaa=__UpperCamelCase  )
        _UpperCAmelCase        = self.get_latents(__UpperCamelCase  ,       shape=(4, 4, 96, 96)  ,       fpaa=__UpperCamelCase  )
        _UpperCAmelCase        = self.get_encoder_hidden_states(__UpperCamelCase  ,       shape=(4, 77, 10_24)  ,       fpaa=__UpperCamelCase  )

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

        assert sample.shape == latents.shape

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

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

"""simple docstring"""



import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


a       :						int       		=		5_0_0_0_0_0

a		,			a       :						Union[str, Any]       		=		os.path.split(__file__)
a       :						Dict       		=		os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))


@get_duration
def 			_UpperCamelCase						(    _A       ,   **_A   )     ->	Any:





    """simple docstring"""

    _UpperCAmelCase        = dataset.map(**_A   )


@get_duration
def 			_UpperCamelCase						(    _A       ,   **_A   )     ->	Dict:





    """simple docstring"""

    _UpperCAmelCase        = dataset.filter(**_A   )


def 			_UpperCamelCase						(    )     ->	Tuple:





    """simple docstring"""

    _UpperCAmelCase        = {"""num examples""": SPEED_TEST_N_EXAMPLES}
    with tempfile.TemporaryDirectory() as tmp_dir:
        _UpperCAmelCase        = datasets.Features({"""text""": datasets.Value("""string"""   ), """numbers""": datasets.Value("""float32"""   )}   )
        _UpperCAmelCase        = generate_example_dataset(
            os.path.join(_A       ,   """dataset.arrow"""   )       ,   _A       ,   num_examples=_A   )

        _UpperCAmelCase        = transformers.AutoTokenizer.from_pretrained("""bert-base-cased"""       ,   use_fast=_A   )

        def tokenize(_A   ):
            return tokenizer(examples["""text"""]   )

        _UpperCAmelCase        = map(_A   )

        _UpperCAmelCase        = map(_A       ,   batched=_A   )

        _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""numpy"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""pandas"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""torch"""       ,   columns="""numbers"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""tensorflow"""       ,   columns="""numbers"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        _UpperCAmelCase        = map(_A       ,   function=_A       ,   batched=_A   )

        _UpperCAmelCase        = filter(_A   )

        # Activate later when tokenizer support batched inputs
        # with dataset.formatted_as(type='numpy'):
        #     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

    with open(_A       ,   """wb"""   ) as f:
        f.write(json.dumps(_A   ).encode("""utf-8"""   )   )


if __name__ == "__main__":  # useful to run the profiler
 benchmark_map_filter()