

"""simple docstring"""





import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class  _lowerCAmelCase  (     lowercase					,unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : List[str]       		=     DDIMPipeline
 __UpperCAmelCase     : Any       		=     UNCONDITIONAL_IMAGE_GENERATION_PARAMS
 __UpperCAmelCase     : str       		=     PipelineTesterMixin.required_optional_params - {
     "num_images_per_prompt",
     "latents",
     "callback",
     "callback_steps",
 }
 __UpperCAmelCase     : Dict       		=     UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
 __UpperCAmelCase     : Dict       		=     False


 def  _lowercase			(      self							:      Optional[int]     ):
    torch.manual_seed(0     )
    __lowercase								=						UNetaDModel(
        block_out_channels=(3_2, 6_4), layers_per_block=2, sample_size=3_2, in_channels=3, out_channels=3, down_block_types=("DownBlock2D", "AttnDownBlock2D"), up_block_types=("AttnUpBlock2D", "UpBlock2D"), )
    __lowercase								=						DDIMScheduler()
    __lowercase								=						{"unet": unet, "scheduler": scheduler}
    return components


 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      List[str]=0     ):
    if str(UpperCAmelCase__     ).startswith("mps"     ):
       __lowercase								=						torch.manual_seed(UpperCAmelCase__     )
    else:
       __lowercase								=						torch.Generator(device=UpperCAmelCase__     ).manual_seed(UpperCAmelCase__     )
    __lowercase								=						{
        "batch_size": 1,
        "generator": generator,
        "num_inference_steps": 2,
        "output_type": "numpy",
    }
    return inputs


 def  _lowercase			(      self							:      Any     ):
    __lowercase								=						"cpu"

    __lowercase								=						self.get_dummy_components()
    __lowercase								=						self.pipeline_class(**UpperCAmelCase__     )
    pipe.to(UpperCAmelCase__     )
    pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_dummy_inputs(UpperCAmelCase__     )
    __lowercase								=						pipe(**UpperCAmelCase__     ).images
    __lowercase								=						image[0, -3:, -3:, -1]

    self.assertEqual(image.shape, (1, 3_2, 3_2, 3)     )
    __lowercase								=						np.array(
        [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04]     )
    __lowercase								=						np.abs(image_slice.flatten() - expected_slice     ).max()
    self.assertLessEqual(UpperCAmelCase__, 1E-3     )


 def  _lowercase			(      self							:      int     ):
    super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3     )


 def  _lowercase			(      self							:      List[str]     ):
    super().test_save_load_local(expected_max_difference=3E-3     )


 def  _lowercase			(      self							:      List[str]     ):
    super().test_save_load_optional_components(expected_max_difference=3E-3     )



 def  _lowercase			(      self							:      List[str]     ):
    super().test_inference_batch_single_identical(expected_max_diff=3E-3     )







@slow
@require_torch_gpu
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 def  _lowercase			(      self							:      str     ):
    __lowercase								=						"google/ddpm-cifar10-32"

    __lowercase								=						UNetaDModel.from_pretrained(UpperCAmelCase__     )
    __lowercase								=						DDIMScheduler()

    __lowercase								=						DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__     )
    ddim.to(UpperCAmelCase__     )
    ddim.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						torch.manual_seed(0     )
    __lowercase								=						ddim(generator=UpperCAmelCase__, eta=0.0, output_type="numpy"     ).images

    __lowercase								=						image[0, -3:, -3:, -1]

    assert image.shape == (1, 3_2, 3_2, 3)
    __lowercase								=						np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453]     )

    assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2



 def  _lowercase			(      self							:      int     ):
    __lowercase								=						"google/ddpm-ema-bedroom-256"

    __lowercase								=						UNetaDModel.from_pretrained(UpperCAmelCase__     )
    __lowercase								=						DDIMScheduler.from_pretrained(UpperCAmelCase__     )

    __lowercase								=						DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__     )
    ddpm.to(UpperCAmelCase__     )
    ddpm.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						torch.manual_seed(0     )
    __lowercase								=						ddpm(generator=UpperCAmelCase__, output_type="numpy"     ).images

    __lowercase								=						image[0, -3:, -3:, -1]

    assert image.shape == (1, 2_5_6, 2_5_6, 3)
    __lowercase								=						np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069]     )

    assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2







"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
   import numpy as np
   import tensorflow as tf

   from transformers import TFCamembertModel




@require_tf
@require_sentencepiece
@require_tokenizers
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 @slow
 def  _lowercase			(      self							:      Optional[Any]     ):
    __lowercase								=						TFCamembertModel.from_pretrained("jplu/tf-camembert-base"     )

    __lowercase								=						tf.convert_to_tensor(
        [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]], dtype=tf.intaa, )  # J'aime le camembert !"

    __lowercase								=						model(UpperCAmelCase__     )["last_hidden_state"]
    __lowercase								=						tf.TensorShape((1, 1_0, 7_6_8)     )
    self.assertEqual(output.shape, UpperCAmelCase__     )
    # compare the actual values for a slice.
    __lowercase								=						tf.convert_to_tensor(
        [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]], dtype=tf.floataa, )
    # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
    # camembert.eval()
    # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

    self.assertTrue(np.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1E-4     )     )




