
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 						lowercase       (  unittest.TestCase					):
					@patch('''sys.argv''' ,	['''fakeprogrampath''', '''env''']    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								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 lowercase_			(		self    ):
								"""simple docstring"""







								import transformers.commands.transformers_cli

								shutil.rmtree('''/tmp/hf-internal-testing/tiny-random-gptj''' ,	ignore_errors=SCREAMING_SNAKE_CASE__    )  # 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 lowercase_			(		self    ):
								"""simple docstring"""







								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=SCREAMING_SNAKE_CASE__    )

								# 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 lowercase_			(		self    ):
								"""simple docstring"""







								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=SCREAMING_SNAKE_CASE__    )

								# 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'''    )    )





import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class 						lowercase       (  __UpperCamelCase					):
					__a					=    (HeunDiscreteScheduler,)
					__a					=    10
					def lowercase_			(		self ,	**SCREAMING_SNAKE_CASE__    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Any				      =			{
								    '''num_train_timesteps''': 1100,
								    '''beta_start''': 0.0_001,
								    '''beta_end''': 0.02,
								    '''beta_schedule''': '''linear''',
								}

								config.update(**SCREAMING_SNAKE_CASE__    )
								return config
					def lowercase_			(		self    ):
								"""simple docstring"""







								for timesteps in [10, 50, 100, 1000]:
											self.check_over_configs(num_train_timesteps=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001] ,	[0.0_002, 0.002, 0.02]    ):
											self.check_over_configs(beta_start=SCREAMING_SNAKE_CASE__ ,	beta_end=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for schedule in ["linear", "scaled_linear", "exp"]:
											self.check_over_configs(beta_schedule=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for clip_sample_range in [1.0, 2.0, 3.0]:
											self.check_over_configs(clip_sample_range=SCREAMING_SNAKE_CASE__ ,	clip_sample=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for prediction_type in ["epsilon", "v_prediction", "sample"]:
											self.check_over_configs(prediction_type=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    List[str]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    Optional[Any]				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps    )

								lowerCAmelCase__ :    str				      =			self.dummy_model()
								lowerCAmelCase__ :    Optional[int]				      =			self.dummy_sample_deter * scheduler.init_noise_sigma
								lowerCAmelCase__ :    Optional[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for i, t in enumerate(scheduler.timesteps    ):
											lowerCAmelCase__ :    List[Any]				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    str				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Tuple				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    Union[str, Any]				      =			output.prev_sample

								lowerCAmelCase__ :    Any				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    Union[str, Any]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if torch_device in ["cpu", "mps"]:
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
								else:
											# CUDA
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Optional[Any]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    str				      =			self.get_scheduler_config(prediction_type='''v_prediction'''    )
								lowerCAmelCase__ :    Any				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps    )

								lowerCAmelCase__ :    int				      =			self.dummy_model()
								lowerCAmelCase__ :    List[str]				      =			self.dummy_sample_deter * scheduler.init_noise_sigma
								lowerCAmelCase__ :    List[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for i, t in enumerate(scheduler.timesteps    ):
											lowerCAmelCase__ :    int				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Optional[Any]				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Optional[int]				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    Tuple				      =			output.prev_sample

								lowerCAmelCase__ :    List[Any]				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    List[str]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if torch_device in ["cpu", "mps"]:
											assert abs(result_sum.item() - 4.6934E-07    ) < 1E-2
											assert abs(result_mean.item() - 6.1112E-10    ) < 1E-3
								else:
											# CUDA
											assert abs(result_sum.item() - 4.693_4286_5017_0972E-07    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Optional[int]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    Union[str, Any]				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps ,	device=SCREAMING_SNAKE_CASE__    )

								lowerCAmelCase__ :    Optional[Any]				      =			self.dummy_model()
								lowerCAmelCase__ :    Dict				      =			self.dummy_sample_deter.to(SCREAMING_SNAKE_CASE__    ) * scheduler.init_noise_sigma

								for t in scheduler.timesteps:
											lowerCAmelCase__ :    int				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Dict				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Any				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    List[Any]				      =			output.prev_sample

								lowerCAmelCase__ :    Dict				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    List[str]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if str(SCREAMING_SNAKE_CASE__    ).startswith('''cpu'''    ):
											# The following sum varies between 148 and 156 on mps. Why?
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
								elif str(SCREAMING_SNAKE_CASE__    ).startswith('''mps'''    ):
											# Larger tolerance on mps
											assert abs(result_mean.item() - 0.0_002    ) < 1E-2
								else:
											# CUDA
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Any				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    str				      =			scheduler_class(**SCREAMING_SNAKE_CASE__ ,	use_karras_sigmas=SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps ,	device=SCREAMING_SNAKE_CASE__    )

								lowerCAmelCase__ :    str				      =			self.dummy_model()
								lowerCAmelCase__ :    Any				      =			self.dummy_sample_deter.to(SCREAMING_SNAKE_CASE__    ) * scheduler.init_noise_sigma
								lowerCAmelCase__ :    List[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for t in scheduler.timesteps:
											lowerCAmelCase__ :    Union[str, Any]				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Any				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    str				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    List[Any]				      =			output.prev_sample

								lowerCAmelCase__ :    int				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    Dict				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								assert abs(result_sum.item() - 0.00_015    ) < 1E-2
								assert abs(result_mean.item() - 1.9869_5545_3503_4695E-07    ) < 1E-2



