





"""simple docstring"""



import time
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
 import torch

 from transformers.generation import (
     MaxLengthCriteria,
     MaxNewTokensCriteria,
     MaxTimeCriteria,
     StoppingCriteriaList,
     validate_stopping_criteria,
 )



@require_torch
class 					a						(       unittest.TestCase				):



    """simple docstring"""



    def 				UpperCamelCase	(     self: List[Any]    ,						UpperCamelCase: Dict       ):






         """simple docstring"""


         A__										=  3
         A__										=  2_50

         A__										=  ids_tensor((batch_size, length)    ,						UpperCamelCase       )
         A__										=  torch.ones((batch_size, length)    ,						device=UpperCamelCase    ,						dtype=torch.float       ) / length
         return input_ids, scores

    def 				UpperCamelCase	(     self: List[str]       ):






         """simple docstring"""


         A__										=  self._get_tensors(5       )

         A__										=  StoppingCriteriaList(
             [
                 MaxLengthCriteria(max_length=10       ),
                 MaxTimeCriteria(max_time=0.1       ),
             ]       )

         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(9       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(10       )
         self.assertTrue(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

    def 				UpperCamelCase	(     self: Union[str, Any]       ):






         """simple docstring"""


         A__										=  MaxLengthCriteria(max_length=10       )

         A__										=  self._get_tensors(5       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(9       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(10       )
         self.assertTrue(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

    def 				UpperCamelCase	(     self: int       ):






         """simple docstring"""


         A__										=  MaxNewTokensCriteria(start_length=5    ,						max_new_tokens=5       )

         A__										=  self._get_tensors(5       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(9       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  self._get_tensors(10       )
         self.assertTrue(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  StoppingCriteriaList([criteria]       )
         self.assertEqual(criteria_list.max_length    ,						10       )

    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


         A__										=  self._get_tensors(5       )

         A__										=  MaxTimeCriteria(max_time=0.1       )
         self.assertFalse(criteria(UpperCamelCase    ,						UpperCamelCase       )       )

         A__										=  MaxTimeCriteria(max_time=0.1    ,						initial_timestamp=time.time() - 0.2       )
         self.assertTrue(criteria(UpperCamelCase    ,						UpperCamelCase       )       )





    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


         validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10       )]       )    ,						10       )

         with self.assertWarns(UpperCamelCase       ):
              validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10       )]       )    ,						11       )

         A__										=  validate_stopping_criteria(StoppingCriteriaList()    ,						11       )

         self.assertEqual(len(UpperCamelCase       )    ,						1       )









"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()



class 					a						(       unittest.TestCase				):



    """simple docstring"""



    @property
    def 				UpperCamelCase	(     self: Any       ):






         """simple docstring"""


         torch.manual_seed(0       )
         A__										=  UNetaDModel(
             block_out_channels=(32, 64)    ,						layers_per_block=2    ,						sample_size=32    ,						in_channels=3    ,						out_channels=3    ,						down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")    ,						up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")    ,						)
         return model





    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


         A__										=  self.dummy_uncond_unet
         A__										=  PNDMScheduler()

         A__										=  PNDMPipeline(unet=UpperCamelCase    ,						scheduler=UpperCamelCase       )
         pndm.to(UpperCamelCase       )
         pndm.set_progress_bar_config(disable=UpperCamelCase       )

         A__										=  torch.manual_seed(0       )
         A__										=  pndm(generator=UpperCamelCase    ,						num_inference_steps=20    ,						output_type="""numpy"""       ).images

         A__										=  torch.manual_seed(0       )
         A__										=  pndm(generator=UpperCamelCase    ,						num_inference_steps=20    ,						output_type="""numpy"""    ,						return_dict=UpperCamelCase       )[0]

         A__										=  image[0, -3:, -3:, -1]
         A__										=  image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 32, 32, 3)
         A__										=  np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]       )

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



@slow
@require_torch
class 					a						(       unittest.TestCase				):



    """simple docstring"""



    def 				UpperCamelCase	(     self: Dict       ):






         """simple docstring"""


         A__										=  """google/ddpm-cifar10-32"""

         A__										=  UNetaDModel.from_pretrained(UpperCamelCase       )
         A__										=  PNDMScheduler()

         A__										=  PNDMPipeline(unet=UpperCamelCase    ,						scheduler=UpperCamelCase       )
         pndm.to(UpperCamelCase       )
         pndm.set_progress_bar_config(disable=UpperCamelCase       )
         A__										=  torch.manual_seed(0       )
         A__										=  pndm(generator=UpperCamelCase    ,						output_type="""numpy"""       ).images

         A__										=  image[0, -3:, -3:, -1]

         assert image.shape == (1, 32, 32, 3)
         A__										=  np.array([0.1_564, 0.14_645, 0.1_406, 0.14_715, 0.12_425, 0.14_045, 0.13_115, 0.12_175, 0.125]       )

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






