


import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 @property
 def 						UpperCamelCase__					(     self ):
       torch.manual_seed(0 )
       snake_case_	=				UNetaDModel(
           block_out_channels=(32, 64)		,				layers_per_block=2		,				sample_size=32		,				in_channels=3		,				out_channels=3		,				down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')		,				up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')		,				)
       return model





 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.dummy_uncond_unet
       snake_case_	=				PNDMScheduler()

       snake_case_	=				PNDMPipeline(unet=_UpperCAmelCase		,				scheduler=_UpperCAmelCase )
       pndm.to(_UpperCAmelCase )
       pndm.set_progress_bar_config(disable=_UpperCAmelCase )

       snake_case_	=				torch.manual_seed(0 )
       snake_case_	=				pndm(generator=_UpperCAmelCase		,				num_inference_steps=20		,				output_type='''numpy''' ).images

       snake_case_	=				torch.manual_seed(0 )
       snake_case_	=				pndm(generator=_UpperCAmelCase		,				num_inference_steps=20		,				output_type='''numpy'''		,				return_dict=_UpperCAmelCase )[0]

       snake_case_	=				image[0, -3:, -3:, -1]
       snake_case_	=				image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case_	=				np.array([1.0, 1.0, 0.0, 1.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 lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 def 						UpperCamelCase__					(     self ):
       snake_case_	=				'''google/ddpm-cifar10-32'''

       snake_case_	=				UNetaDModel.from_pretrained(_UpperCAmelCase )
       snake_case_	=				PNDMScheduler()

       snake_case_	=				PNDMPipeline(unet=_UpperCAmelCase		,				scheduler=_UpperCAmelCase )
       pndm.to(_UpperCAmelCase )
       pndm.set_progress_bar_config(disable=_UpperCAmelCase )
       snake_case_	=				torch.manual_seed(0 )
       snake_case_	=				pndm(generator=_UpperCAmelCase		,				output_type='''numpy''' ).images

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

       assert image.shape == (1, 32, 32, 3)
       snake_case_	=				np.array([0.1_564, 0.14_645, 0.1_406, 0.14_715, 0.12_425, 0.14_045, 0.13_115, 0.12_175, 0.125] )

       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] )