






'''simple docstring'''
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


__lowerCAmelCase							=False


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

					"""simple docstring"""





					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[int]:
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										a_									=   VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'     )
										# remove text_unet
										pipe.remove_unused_weights()
										pipe.to(UpperCAmelCase__     )
										pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

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

										a_									=   torch.manual_seed(0     )
										a_									=   pipe(
										    prompt='first prompt'   , image=UpperCAmelCase__   , text_to_image_strength=0.7_5   , generator=UpperCAmelCase__   , guidance_scale=7.5   , num_inference_steps=2   , output_type='numpy'   , ).images

										with tempfile.TemporaryDirectory() as tmpdirname:
															pipe.save_pretrained(UpperCAmelCase__     )
															a_									=   VersatileDiffusionDualGuidedPipeline.from_pretrained(UpperCAmelCase__     )

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

										a_									=   generator.manual_seed(0     )
										a_									=   pipe(
										    prompt='first prompt'   , image=UpperCAmelCase__   , text_to_image_strength=0.7_5   , 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  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										a_									=   VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'     )
										pipe.remove_unused_weights()
										pipe.to(UpperCAmelCase__     )
										pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

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

										a_									=   image[0, 253:256, 253:256, -1]

										assert image.shape == (1, 512, 512, 3)
										a_									=   np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]     )

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









'''simple docstring'''
import os
import shutil
import unittest
from unittest.mock import patch

from transformers.testing_utils import CaptureStd, is_pt_tf_cross_test, require_torch


class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					@patch('sys.argv'   , ['fakeprogrampath', 'env']     )
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										# test transformers-cli env
										import transformers.commands.transformers_cli

										with CaptureStd() as cs:
															transformers.commands.transformers_cli.main()
										self.assertIn('Python version'   , cs.out     )
										self.assertIn('Platform'   , cs.out     )
										self.assertIn('Using distributed or parallel set-up in script?'   , cs.out     )




					@is_pt_tf_cross_test
					@patch(
					    'sys.argv'   , ['fakeprogrampath', 'pt-to-tf', '--model-name', 'hf-internal-testing/tiny-random-gptj', '--no-pr']     )
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										import transformers.commands.transformers_cli

										shutil.rmtree('/tmp/hf-internal-testing/tiny-random-gptj'   , ignore_errors=UpperCAmelCase__     )  # cleans potential past runs
										transformers.commands.transformers_cli.main()

										# The original repo has no TF weights -- if they exist, they were created by the CLI
										self.assertTrue(os.path.exists('/tmp/hf-internal-testing/tiny-random-gptj/tf_model.h5'     )     )




					@require_torch
					@patch('sys.argv'   , ['fakeprogrampath', 'download', 'hf-internal-testing/tiny-random-gptj', '--cache-dir', '/tmp']     )
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[Any]:
										import transformers.commands.transformers_cli

										# # remove any previously downloaded model to start clean
										shutil.rmtree('/tmp/models--hf-internal-testing--tiny-random-gptj'   , ignore_errors=UpperCAmelCase__     )

										# run the command
										transformers.commands.transformers_cli.main()

										# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--tiny-random-gptj
										self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/blobs'     )     )
										self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/refs'     )     )
										self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/snapshots'     )     )




					@require_torch
					@patch(
					    'sys.argv'   , [
					        'fakeprogrampath',
					        'download',
					        'hf-internal-testing/test_dynamic_model_with_tokenizer',
					        '--trust-remote-code',
					        '--cache-dir',
					        '/tmp',
					    ]   , )
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										import transformers.commands.transformers_cli

										# # remove any previously downloaded model to start clean
										shutil.rmtree('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer'   , ignore_errors=UpperCAmelCase__     )

										# run the command
										transformers.commands.transformers_cli.main()

										# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer
										self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/blobs'     )     )
										self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/refs'     )     )
										self.assertTrue(
										    os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/snapshots'     )     )

