




import gc
import tempfile
import unittest

import numpy as np
import torch

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


UpperCAmelCase_    =False


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





				'''simple docstring'''



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



				def    __snake_case		(			self    ):
											lowerCAmelCase =		VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''    )
											# remove text_unet
											pipe.remove_unused_weights()
											pipe.to(__UpperCAmelCase    )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase    )

											lowerCAmelCase =		load_image(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''    )

											lowerCAmelCase =		torch.manual_seed(0    )
											lowerCAmelCase =		pipe(
											    prompt='''first prompt'''							,			image=__UpperCAmelCase							,			text_to_image_strength=0.75							,			generator=__UpperCAmelCase							,			guidance_scale=7.5							,			num_inference_steps=2							,			output_type='''numpy'''							,			).images

											with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(__UpperCAmelCase    )
																		lowerCAmelCase =		VersatileDiffusionDualGuidedPipeline.from_pretrained(__UpperCAmelCase    )

											pipe.to(__UpperCAmelCase    )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase    )

											lowerCAmelCase =		generator.manual_seed(0    )
											lowerCAmelCase =		pipe(
											    prompt='''first prompt'''							,			image=__UpperCAmelCase							,			text_to_image_strength=0.75							,			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    __snake_case		(			self    ):
											lowerCAmelCase =		VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''    )
											pipe.remove_unused_weights()
											pipe.to(__UpperCAmelCase    )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase    )

											lowerCAmelCase =		'''cyberpunk 2077'''
											lowerCAmelCase =		load_image(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''    )
											lowerCAmelCase =		torch.manual_seed(0    )
											lowerCAmelCase =		pipe(
											    prompt=__UpperCAmelCase							,			image=__UpperCAmelCase							,			text_to_image_strength=0.75							,			generator=__UpperCAmelCase							,			guidance_scale=7.5							,			num_inference_steps=50							,			output_type='''numpy'''							,			).images

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

											assert image.shape == (1, 5_12, 5_12, 3)
											lowerCAmelCase =		np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]    )

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







"""simple docstring"""






def    A_ ( _lowerCAmelCase  :       list[list[int]]							):






     """simple docstring"""

     _a     						=					[list(_lowerCAmelCase							) for row in matrix]
     if matrix and isinstance(_lowerCAmelCase,  _lowerCAmelCase							):
          if isinstance(matrix[0],  _lowerCAmelCase							):
               _a     						=					0
               for row in matrix:
                    if prev_len == 0:
                         _a     						=					len(_lowerCAmelCase							)
                         _a     						=					True
                    else:
                         _a     						=					prev_len == len(_lowerCAmelCase							)
          else:
               _a     						=					True
     else:
          _a     						=					False

     return result
def    A_ ( _lowerCAmelCase  :       list[list[int]]							):






     """simple docstring"""

     if check_matrix(_lowerCAmelCase							) and len(_lowerCAmelCase							) > 0:
          _a     						=					[list(_lowerCAmelCase							) for row in a]
          _a     						=					len(_lowerCAmelCase							)
          if isinstance(a[0],  _lowerCAmelCase							):
               _a     						=					len(a[0]							)
          else:
               for dat in a:
                    print(_lowerCAmelCase							)
               return

          # horizotal printing increasing
          for i in range(0,  _lowerCAmelCase							):
               print(a[0][i]							)
          # vertical printing down
          for i in range(1,  _lowerCAmelCase							):
               print(a[i][mat_col - 1]							)
          # horizotal printing decreasing
          if mat_row > 1:
               for i in range(mat_col - 2,  -1,  -1							):
                    print(a[mat_row - 1][i]							)
        # vertical printing up
          for i in range(mat_row - 2,  0,  -1							):
               print(a[i][0]							)
          _a     						=					[row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
          if len(_lowerCAmelCase							) > 0:
               spiral_print_clockwise(_lowerCAmelCase							)
          else:
               return
     else:
          print('''Not a valid matrix'''							)
          return
def    A_ ( _lowerCAmelCase  :       list[list]							):






     """simple docstring"""

     if matrix:
          return list(matrix.pop(0							)							) + spiral_traversal(list(zip(*_lowerCAmelCase							)							)[::-1]							)
     else:
          return []


# driver code
if __name__ == "__main__":
   import doctest

   doctest.testmod()

   __snake_case												=							[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
   spiral_print_clockwise(a)