


import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


UpperCAmelCase      =   False







class lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 pass







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

 '''simple docstring'''

 def 						UpperCamelCase__					(     self ):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion''' )
       # remove text_unet
       pipe.remove_unused_weights()
       pipe.to(_UpperCAmelCase )
       pipe.set_progress_bar_config(disable=_UpperCAmelCase )

       snake_case_	=				'''A painting of a squirrel eating a burger '''
       snake_case_	=				torch.manual_seed(0 )
       snake_case_	=				pipe(
           prompt=_UpperCAmelCase		,				generator=_UpperCAmelCase		,				guidance_scale=7.5		,				num_inference_steps=2		,				output_type='''numpy''' ).images

       with tempfile.TemporaryDirectory() as tmpdirname:
             pipe.save_pretrained(_UpperCAmelCase )
             snake_case_	=				VersatileDiffusionTextToImagePipeline.from_pretrained(_UpperCAmelCase )
       pipe.to(_UpperCAmelCase )
       pipe.set_progress_bar_config(disable=_UpperCAmelCase )

       snake_case_	=				generator.manual_seed(0 )
       snake_case_	=				pipe(
           prompt=_UpperCAmelCase		,				generator=_UpperCAmelCase		,				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 ):
       snake_case_	=				VersatileDiffusionTextToImagePipeline.from_pretrained(
           '''shi-labs/versatile-diffusion'''		,				torch_dtype=torch.floataa )
       pipe.to(_UpperCAmelCase )
       pipe.set_progress_bar_config(disable=_UpperCAmelCase )

       snake_case_	=				'''A painting of a squirrel eating a burger '''
       snake_case_	=				torch.manual_seed(0 )
       snake_case_	=				pipe(
           prompt=_UpperCAmelCase		,				generator=_UpperCAmelCase		,				guidance_scale=7.5		,				num_inference_steps=50		,				output_type='''numpy''' ).images

       snake_case_	=				image[0, 2_53:2_56, 2_53:2_56, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       snake_case_	=				np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778] )

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



import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)







class lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 def 						UpperCamelCase__					(     self ):
       snake_case_	=				['''a''', '''b''', '''c''']

       # Defaults to last layer if both are None
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[2] )

       # Out indices set to match out features
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(['''a''', '''c''']		,				_UpperCAmelCase		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[0, 2] )

       # Out features set to match out indices
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				[0, 2]		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[0, 2] )

       # Out features selected from negative indices
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				[-3, -1]		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[-3, -1] )



 def 						UpperCamelCase__					(     self ):
       # Stage names must be set
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 1)		,				_UpperCAmelCase )

       # Out features must be a list
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(('''a''', '''b''')		,				(0, 1)		,				['''a''', '''b'''] )

       # Out features must be a subset of stage names
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 1)		,				['''a'''] )

       # Out indices must be a list or tuple
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(_UpperCAmelCase		,				0		,				['''a''', '''b'''] )

       # Out indices must be a subset of stage names
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(_UpperCAmelCase		,				(0, 1)		,				['''a'''] )

       # Out features and out indices must be the same length
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0,)		,				['''a''', '''b''', '''c'''] )

       # Out features should match out indices
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 2)		,				['''a''', '''b''', '''c'''] )

       # Out features and out indices should be in order
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''b''', '''a''']		,				(0, 1)		,				['''a''', '''b'''] )

       # Check passes with valid inputs
       verify_out_features_out_indices(['''a''', '''b''', '''d''']		,				(0, 1, -1)		,				['''a''', '''b''', '''c''', '''d'''] )





 def 						UpperCamelCase__					(     self ):
       snake_case_	=				BackboneMixin()

       snake_case_	=				['''a''', '''b''', '''c''']
       snake_case_	=				['''a''', '''c''']
       snake_case_	=				[0, 2]

       # Check that the output features and indices are set correctly
       self.assertEqual(backbone.out_features		,				['''a''', '''c'''] )
       self.assertEqual(backbone.out_indices		,				[0, 2] )

       # Check out features and indices are updated correctly
       snake_case_	=				['''a''', '''b''']
       self.assertEqual(backbone.out_features		,				['''a''', '''b'''] )
       self.assertEqual(backbone.out_indices		,				[0, 1] )

       snake_case_	=				[-3, -1]
       self.assertEqual(backbone.out_features		,				['''a''', '''c'''] )
       self.assertEqual(backbone.out_indices		,				[-3, -1] )