




"""simple docstring"""

from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''

 def      lowerCAmelCase_				(					self			:			Dict						):
       _A     =  SMALL_MODEL_IDENTIFIER
       _A     =  'pt'
       _A     =  'tf'

 def      lowerCAmelCase_				(					self			:			Dict       ,				_UpperCAmelCase			:			Tuple						):
       _A     =  AutoModel.from_pretrained(self.test_model						)
       model_pt.save_pretrained(_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[Any]       ,				_UpperCAmelCase			:			int						):
       _A     =  TFAutoModel.from_pretrained(self.test_model       ,				from_pt=_UpperCAmelCase						)
       model_tf.save_pretrained(_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  'mock_framework'

       # Framework provided - return whatever the user provides
       _A     =  FeaturesManager.determine_framework(self.test_model       ,				_UpperCAmelCase						)
       self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase						)

       # Local checkpoint and framework provided - return provided framework
       # PyTorch checkpoint
       with TemporaryDirectory() as local_pt_ckpt:
             self._setup_pt_ckpt(_UpperCAmelCase						)
             _A     =  FeaturesManager.determine_framework(_UpperCAmelCase       ,				_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase						)

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
             self._setup_tf_ckpt(_UpperCAmelCase						)
             _A     =  FeaturesManager.determine_framework(_UpperCAmelCase       ,				_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Union[str, Any]						):
       # PyTorch checkpoint
       with TemporaryDirectory() as local_pt_ckpt:
             self._setup_pt_ckpt(_UpperCAmelCase						)
             _A     =  FeaturesManager.determine_framework(_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase       ,				self.framework_pt						)

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
             self._setup_tf_ckpt(_UpperCAmelCase						)
             _A     =  FeaturesManager.determine_framework(_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase       ,				self.framework_tf						)

       # Invalid local checkpoint
       with TemporaryDirectory() as local_invalid_ckpt:
             with self.assertRaises(_UpperCAmelCase						):
                   _A     =  FeaturesManager.determine_framework(_UpperCAmelCase						)



 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       with patch('transformers.onnx.features.is_tf_available'       ,				_UpperCAmelCase						):
             _A     =  FeaturesManager.determine_framework(self.test_model						)
             self.assertEqual(_UpperCAmelCase       ,				self.framework_pt						)

       # PyTorch not in environment -> use TensorFlow
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       with patch('transformers.onnx.features.is_torch_available'       ,				_UpperCAmelCase						):
             _A     =  FeaturesManager.determine_framework(self.test_model						)
             self.assertEqual(_UpperCAmelCase       ,				self.framework_tf						)

       # Both in environment -> use PyTorch
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       with patch('transformers.onnx.features.is_tf_available'       ,				_UpperCAmelCase						), patch(
           'transformers.onnx.features.is_torch_available'       ,				_UpperCAmelCase						):
             _A     =  FeaturesManager.determine_framework(self.test_model						)
             self.assertEqual(_UpperCAmelCase       ,				self.framework_pt						)

       # Both not in environment -> raise error
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       _A     =  MagicMock(return_value=_UpperCAmelCase						)
       with patch('transformers.onnx.features.is_tf_available'       ,				_UpperCAmelCase						), patch(
           'transformers.onnx.features.is_torch_available'       ,				_UpperCAmelCase						):
             with self.assertRaises(_UpperCAmelCase						):
                   _A     =  FeaturesManager.determine_framework(self.test_model						)







"""simple docstring"""

import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Optional[int]     =      (KDPMaDiscreteScheduler,)
 UpperCAmelCase       :	Any     =      10

 def      lowerCAmelCase_				(					self			:			Dict       ,				**_UpperCAmelCase			:			Optional[Any]						):
       _A     =  {
           'num_train_timesteps': 1_100,
           'beta_start': 0.0001,
           'beta_end': 0.02,
           'beta_schedule': 'linear',
       }

       config.update(**_UpperCAmelCase						)
       return config

 def      lowerCAmelCase_				(					self			:			Any						):
       for timesteps in [10, 50, 100, 1_000]:
             self.check_over_configs(num_train_timesteps=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Dict						):
       for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001]       ,				[0.0002, 0.002, 0.02]						):
             self.check_over_configs(beta_start=_UpperCAmelCase       ,				beta_end=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Tuple						):
       for schedule in ["linear", "scaled_linear"]:
             self.check_over_configs(beta_schedule=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       for prediction_type in ["epsilon", "v_prediction"]:
             self.check_over_configs(prediction_type=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config(prediction_type='v_prediction'						)
       _A     =  scheduler_class(**_UpperCAmelCase						)

       scheduler.set_timesteps(self.num_inference_steps						)

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter * scheduler.init_noise_sigma
       _A     =  sample.to(_UpperCAmelCase						)

       for i, t in enumerate(scheduler.timesteps						):
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if torch_device in ["cpu", "mps"]:
             assert abs(result_sum.item() - 4.6_9_3_4E-0_7						) < 1E-2
             assert abs(result_mean.item() - 6.1_1_1_2E-1_0						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2E-0_7						) < 1E-2
             assert abs(result_mean.item() - 0.0002						) < 1E-3

 def      lowerCAmelCase_				(					self			:			Optional[Any]						):
       if torch_device == "mps":
             return
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config()
       _A     =  scheduler_class(**_UpperCAmelCase						)

       scheduler.set_timesteps(self.num_inference_steps						)

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter * scheduler.init_noise_sigma
       _A     =  sample.to(_UpperCAmelCase						)

       for i, t in enumerate(scheduler.timesteps						):
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if torch_device in ["cpu", "mps"]:
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3



 def      lowerCAmelCase_				(					self			:			Any						):
       if torch_device == "mps":
             return
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config()
       _A     =  scheduler_class(**_UpperCAmelCase						)

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

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter.to(_UpperCAmelCase						) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if str(_UpperCAmelCase						).startswith('cpu'						):
             # The following sum varies between 148 and 156 on mps. Why?
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3

