


'''simple docstring'''





import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()




class 				UpperCAmelCase			(			unittest.TestCase						):


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any] ):
    torch.manual_seed(0 )
    UpperCAmelCase__	:List[Any]      =      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''')			,       )
    return model





 def        __SCREAMING_SNAKE_CASE					(      self							:	Any ):
    UpperCAmelCase__	:int      =      self.dummy_uncond_unet
    UpperCAmelCase__	:Union[str, Any]      =      ScoreSdeVeScheduler()

    UpperCAmelCase__	:Tuple      =      ScoreSdeVePipeline(unet=__lowerCamelCase			,       scheduler=__lowerCamelCase )
    sde_ve.to(__lowerCamelCase )
    sde_ve.set_progress_bar_config(disable=__lowerCamelCase )

    UpperCAmelCase__	:Tuple      =      torch.manual_seed(0 )
    UpperCAmelCase__	:List[Any]      =      sde_ve(num_inference_steps=2			,       output_type='''numpy'''			,       generator=__lowerCamelCase ).images

    UpperCAmelCase__	:Any      =      torch.manual_seed(0 )
    UpperCAmelCase__	:List[Any]      =      sde_ve(num_inference_steps=2			,       output_type='''numpy'''			,       generator=__lowerCamelCase			,       return_dict=__lowerCamelCase )[
        0
    ]

    UpperCAmelCase__	:int      =      image[0, -3:, -3:, -1]
    UpperCAmelCase__	:List[str]      =      image_from_tuple[0, -3:, -3:, -1]

    assert image.shape == (1, 3_2, 3_2, 3)
    UpperCAmelCase__	:List[Any]      =      np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] )

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




@slow
@require_torch
class 				UpperCAmelCase			(			unittest.TestCase						):


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:int      =      '''google/ncsnpp-church-256'''
    UpperCAmelCase__	:Tuple      =      UNetaDModel.from_pretrained(__lowerCamelCase )

    UpperCAmelCase__	:int      =      ScoreSdeVeScheduler.from_pretrained(__lowerCamelCase )

    UpperCAmelCase__	:Optional[int]      =      ScoreSdeVePipeline(unet=__lowerCamelCase			,       scheduler=__lowerCamelCase )
    sde_ve.to(__lowerCamelCase )
    sde_ve.set_progress_bar_config(disable=__lowerCamelCase )

    UpperCAmelCase__	:Tuple      =      torch.manual_seed(0 )
    UpperCAmelCase__	:Dict      =      sde_ve(num_inference_steps=1_0			,       output_type='''numpy'''			,       generator=__lowerCamelCase ).images

    UpperCAmelCase__	:Any      =      image[0, -3:, -3:, -1]

    assert image.shape == (1, 2_5_6, 2_5_6, 3)

    UpperCAmelCase__	:List[Any]      =      np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] )

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


 @slow
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    UpperCAmelCase__	:List[str]      =      TFCamembertModel.from_pretrained('''jplu/tf-camembert-base''' )

    UpperCAmelCase__	:Optional[Any]      =      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 !"

    UpperCAmelCase__	:Any      =      model(__lowerCamelCase )['''last_hidden_state''']
    UpperCAmelCase__	:Dict      =      tf.TensorShape((1, 1_0, 7_6_8) )
    self.assertEqual(output.shape			,       __lowerCamelCase )
    # compare the actual values for a slice.
    UpperCAmelCase__	:Any      =      tf.convert_to_tensor(
        [[[-0.02_54, 0.02_35, 0.10_27], [0.06_06, -0.18_11, -0.04_18], [-0.15_61, -0.11_27, 0.26_87]]]			,       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 ) )



