
"""simple docstring"""





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_a		       =							False


class A_	(unittest.TestCase      ):




  '''simple docstring'''
  pass




@nightly
@require_torch_gpu
class A_	(unittest.TestCase      ):




  '''simple docstring'''







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[Any]			     =    VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"				,   torch_dtype=torch.floataa  )
     pipe.to(lowercase_  )
     pipe.set_progress_bar_config(disable=lowercase_  )

     UpperCAmelCase_					:					List[str]			     =    load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"  )

     UpperCAmelCase_					:					List[str]			     =    torch.manual_seed(0  )
     UpperCAmelCase_					:					int			     =    pipe.dual_guided(
         prompt="first prompt"				,   image=lowercase_				,   text_to_image_strength=0.75				,   generator=lowercase_				,   guidance_scale=7.5				,   num_inference_steps=2				,   output_type="numpy"				,   ).images

     with tempfile.TemporaryDirectory() as tmpdirname:
        pipe.save_pretrained(lowercase_  )
        UpperCAmelCase_					:					Tuple			     =    VersatileDiffusionPipeline.from_pretrained(lowercase_				,   torch_dtype=torch.floataa  )
     pipe.to(lowercase_  )
     pipe.set_progress_bar_config(disable=lowercase_  )

     UpperCAmelCase_					:					Optional[int]			     =    generator.manual_seed(0  )
     UpperCAmelCase_					:					List[Any]			     =    pipe.dual_guided(
         prompt="first prompt"				,   image=lowercase_				,   text_to_image_strength=0.75				,   generator=lowercase_				,   guidance_scale=7.5				,   num_inference_steps=2				,   output_type="numpy"				,   ).images

     assert np.abs(image - new_image  ).sum() < 1E-5, "Models don't have the same forward pass"







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[str]			     =    VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"				,   torch_dtype=torch.floataa  )
     pipe.to(lowercase_  )
     pipe.set_progress_bar_config(disable=lowercase_  )

     UpperCAmelCase_					:					Union[str, Any]			     =    "cyberpunk 2077"
     UpperCAmelCase_					:					Optional[Any]			     =    load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"  )
     UpperCAmelCase_					:					List[Any]			     =    torch.manual_seed(0  )
     UpperCAmelCase_					:					Tuple			     =    pipe.dual_guided(
         prompt=lowercase_				,   image=lowercase_				,   text_to_image_strength=0.75				,   generator=lowercase_				,   guidance_scale=7.5				,   num_inference_steps=50				,   output_type="numpy"				,   ).images

     UpperCAmelCase_					:					Union[str, Any]			     =    image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     UpperCAmelCase_					:					Optional[Any]			     =    np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01]  )

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

     UpperCAmelCase_					:					Any			     =    "A painting of a squirrel eating a burger "
     UpperCAmelCase_					:					Dict			     =    torch.manual_seed(0  )
     UpperCAmelCase_					:					Tuple			     =    pipe.text_to_image(
         prompt=lowercase_				,   generator=lowercase_				,   guidance_scale=7.5				,   num_inference_steps=50				,   output_type="numpy"  ).images

     UpperCAmelCase_					:					List[str]			     =    image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     UpperCAmelCase_					:					int			     =    np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]  )

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

     UpperCAmelCase_					:					Optional[Any]			     =    pipe.image_variation(lowercase_				,   generator=lowercase_				,   output_type="numpy"  ).images

     UpperCAmelCase_					:					Tuple			     =    image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     UpperCAmelCase_					:					Union[str, Any]			     =    np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56]  )

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






'''simple docstring'''




import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
       import torch

       from transformers import XLMRobertaModel

@require_sentencepiece
@require_tokenizers
@require_torch
class 		UpperCAmelCase_      (  unittest.TestCase		):





     @slow
     def 							__UpperCAmelCase      ( self       :      str    )				->  List[str]:
        lowerCAmelCase     =			XLMRobertaModel.from_pretrained('xlm-roberta-base'    )
        lowerCAmelCase     =			torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]    )
        # The dog is cute and lives in the garden house

        lowerCAmelCase     =			torch.Size((1, 1_2, 7_6_8)    )  # batch_size, sequence_length, embedding_vector_dim
        lowerCAmelCase     =			torch.tensor(
            [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]]    )
        #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
        #  xlmr.eval()
        #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
        with torch.no_grad():
           lowerCAmelCase     =			model(UpperCAmelCase__    )['last_hidden_state'].detach()
        self.assertEqual(output.shape    ,      UpperCAmelCase__    )
        # compare the actual values for a slice of last dim
        self.assertTrue(torch.allclose(output[:, :, -1]    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





     @slow
     def 							__UpperCAmelCase      ( self       :      List[Any]    )				->  Tuple:
        lowerCAmelCase     =			XLMRobertaModel.from_pretrained('xlm-roberta-large'    )
        lowerCAmelCase     =			torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]    )
        # The dog is cute and lives in the garden house

        lowerCAmelCase     =			torch.Size((1, 1_2, 1_0_2_4)    )  # batch_size, sequence_length, embedding_vector_dim
        lowerCAmelCase     =			torch.tensor(
            [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]]    )
        #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
        #  xlmr.eval()
        #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
        with torch.no_grad():
           lowerCAmelCase     =			model(UpperCAmelCase__    )['last_hidden_state'].detach()
        self.assertEqual(output.shape    ,      UpperCAmelCase__    )
        # compare the actual values for a slice of last dim
        self.assertTrue(torch.allclose(output[:, :, -1]    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





