






"""simple docstring"""



import json
import os
import tempfile
import unittest

from transformers.modelcard import ModelCard




class 							__lowercase(							unittest.TestCase	):






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					={
											    'model_details': {
											        'Organization': 'testing',
											        'Model date': 'today',
											        'Model version': 'v2.1, Developed by Test Corp in 2019.',
											        'Architecture': 'Convolutional Neural Network.',
											    },
											    'metrics': 'BLEU and ROUGE-1',
											    'evaluation_data': {
											        'Datasets': {'BLEU': 'My-great-dataset-v1', 'ROUGE-1': 'My-short-dataset-v2.1'},
											        'Preprocessing': 'See details on https://arxiv.org/pdf/1810.03993.pdf',
											    },
											    'training_data': {
											        'Dataset': 'English Wikipedia dump dated 2018-12-01',
											        'Preprocessing': (
											            'Using SentencePiece vocabulary of size 52k tokens. See details on'
											            ' https://arxiv.org/pdf/1810.03993.pdf'
											        ),
											    },
											    'quantitative_analyses': {'BLEU': 55.1, 'ROUGE-1': 76},
											}






						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											lowerCamelCase__					=ModelCard.from_dict(self.inputs_dict)
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'model_details'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'intended_use'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'factors'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'metrics'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'evaluation_data'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'training_data'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'quantitative_analyses'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'ethical_considerations'))
											self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,    'caveats_and_recommendations'))






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					=ModelCard.from_dict(self.inputs_dict)
											lowerCamelCase__					=json.loads(modelcard.to_json_string())
											for key, value in self.inputs_dict.items():
																self.assertEqual(obj[key]     ,    _SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					=ModelCard.from_dict(self.inputs_dict)

											with tempfile.TemporaryDirectory() as tmpdirname:
																lowerCamelCase__					=os.path.join(_SCREAMING_SNAKE_CASE     ,    'modelcard.json')
																model_card_first.to_json_file(_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=ModelCard.from_json_file(_SCREAMING_SNAKE_CASE)

											self.assertEqual(model_card_second.to_dict()     ,    model_card_first.to_dict())






						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					=ModelCard.from_dict(self.inputs_dict)

											with tempfile.TemporaryDirectory() as tmpdirname:
																model_card_first.save_pretrained(_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=ModelCard.from_pretrained(_SCREAMING_SNAKE_CASE)

											self.assertEqual(model_card_second.to_dict()     ,    model_card_first.to_dict())







"""simple docstring"""



import torch

from diffusers import EulerAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				Dict=							(EulerAncestralDiscreteScheduler,)
						lowerCAmelCase_		:				int=							10






						def 		lowerCAmelCase (       self     ,    **_SCREAMING_SNAKE_CASE)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					={
											    'num_train_timesteps': 11_00,
											    'beta_start': 0.0001,
											    'beta_end': 0.02,
											    'beta_schedule': 'linear',
											}

											config.update(**_SCREAMING_SNAKE_CASE)
											return config






						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											for timesteps in [10, 50, 1_00, 10_00]:
																self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]     ,    [0.0002, 0.002, 0.02]):
																self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE     ,    beta_end=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											for schedule in ["linear", "scaled_linear"]:
																self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											for prediction_type in ["epsilon", "v_prediction"]:
																self.check_over_configs(prediction_type=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config()
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps)

											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for i, t in enumerate(scheduler.timesteps):
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 152.3192) < 1E-2
											assert abs(result_mean.item() - 0.1983) < 1E-3






						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config(prediction_type='v_prediction')
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps)

											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for i, t in enumerate(scheduler.timesteps):
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 108.4439) < 1E-2
											assert abs(result_mean.item() - 0.1412) < 1E-3






						def 		lowerCAmelCase (       self)      ->	Any:




											"""simple docstring"""





											lowerCamelCase__					=self.scheduler_classes[0]
											lowerCamelCase__					=self.get_scheduler_config()
											lowerCamelCase__					=scheduler_class(**_SCREAMING_SNAKE_CASE)

											scheduler.set_timesteps(self.num_inference_steps     ,    device=_SCREAMING_SNAKE_CASE)
											lowerCamelCase__					=torch.manual_seed(0)

											lowerCamelCase__					=self.dummy_model()
											lowerCamelCase__					=self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
											lowerCamelCase__					=sample.to(_SCREAMING_SNAKE_CASE)

											for t in scheduler.timesteps:
																lowerCamelCase__					=scheduler.scale_model_input(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					=scheduler.step(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    generator=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=output.prev_sample

											lowerCamelCase__					=torch.sum(torch.abs(_SCREAMING_SNAKE_CASE))
											lowerCamelCase__					=torch.mean(torch.abs(_SCREAMING_SNAKE_CASE))

											assert abs(result_sum.item() - 152.3192) < 1E-2
											assert abs(result_mean.item() - 0.1983) < 1E-3