




import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				@property
				def     _A							(							self:   Optional[Any]		):
											torch.manual_seed(0		)
											SCREAMING_SNAKE_CASE_   	=    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     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    self.dummy_uncond_unet
											SCREAMING_SNAKE_CASE_   	=    ScoreSdeVeScheduler()

											SCREAMING_SNAKE_CASE_   	=    ScoreSdeVePipeline(unet=_lowerCamelCase		,					scheduler=_lowerCamelCase		)
											sde_ve.to(_lowerCamelCase		)
											sde_ve.set_progress_bar_config(disable=_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    torch.manual_seed(0		)
											SCREAMING_SNAKE_CASE_   	=    sde_ve(num_inference_steps=2		,					output_type='''numpy'''		,					generator=_lowerCamelCase		).images

											SCREAMING_SNAKE_CASE_   	=    torch.manual_seed(0		)
											SCREAMING_SNAKE_CASE_   	=    sde_ve(num_inference_steps=2		,					output_type='''numpy'''		,					generator=_lowerCamelCase		,					return_dict=_lowerCamelCase		)[
											    0
											]

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

											assert image.shape == (1, 32, 32, 3)
											SCREAMING_SNAKE_CASE_   	=    np.array([0.0, 1.0, 0.0, 0.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  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    '''google/ncsnpp-church-256'''
											SCREAMING_SNAKE_CASE_   	=    UNetaDModel.from_pretrained(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    ScoreSdeVeScheduler.from_pretrained(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    ScoreSdeVePipeline(unet=_lowerCamelCase		,					scheduler=_lowerCamelCase		)
											sde_ve.to(_lowerCamelCase		)
											sde_ve.set_progress_bar_config(disable=_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    torch.manual_seed(0		)
											SCREAMING_SNAKE_CASE_   	=    sde_ve(num_inference_steps=10		,					output_type='''numpy'''		,					generator=_lowerCamelCase		).images

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

											assert image.shape == (1, 2_56, 2_56, 3)

											SCREAMING_SNAKE_CASE_   	=    np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]		)

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








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
			import torch

			from transformers.generation import DisjunctiveConstraint






@require_torch
class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				def     _A							(							self:   str		):
											# For consistency across different places the DisjunctiveConstraint is called,
											# dc.token_ids is a list of integers. It is also initialized only by integers.

											SCREAMING_SNAKE_CASE_   	=    [[1, 2, 4], [1, 2, 3, 4]]
											SCREAMING_SNAKE_CASE_   	=    DisjunctiveConstraint(_lowerCamelCase		)
											self.assertTrue(isinstance(dc.token_ids		,					_lowerCamelCase		)		)

											with self.assertRaises(_lowerCamelCase		):
																		DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]		)		)

											with self.assertRaises(_lowerCamelCase		):
																		DisjunctiveConstraint([torch.LongTensor([1, 2, 4]		), torch.LongTensor([1, 2, 3, 4, 5]		)]		)


				def     _A							(							self:   Union[str, Any]		):
											# We can't have constraints that are complete subsets of another. This leads to a preverse
											# interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
											# It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
											# fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
											# will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
											SCREAMING_SNAKE_CASE_   	=    [[1, 2], [1, 2, 3, 4]]

											with self.assertRaises(_lowerCamelCase		):
																		DisjunctiveConstraint(_lowerCamelCase		)  # fails here


				def     _A							(							self:   Dict		):
											SCREAMING_SNAKE_CASE_   	=    [[1, 2, 3], [1, 2, 4]]

											SCREAMING_SNAKE_CASE_   	=    DisjunctiveConstraint(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(1		)
											SCREAMING_SNAKE_CASE_   	=    stepped is True and completed is False and reset is False
											self.assertTrue(_lowerCamelCase		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.current_seq == [1]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(2		)
											SCREAMING_SNAKE_CASE_   	=    stepped is True and completed is False and reset is False
											self.assertTrue(_lowerCamelCase		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.current_seq == [1, 2]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(3		)
											SCREAMING_SNAKE_CASE_   	=    stepped is True and completed is True and reset is False
											self.assertTrue(_lowerCamelCase		)
											self.assertTrue(dc.completed		)  # Completed!
											self.assertTrue(dc.current_seq == [1, 2, 3]		)





				def     _A							(							self:   Optional[int]		):
											SCREAMING_SNAKE_CASE_   	=    [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

											SCREAMING_SNAKE_CASE_   	=    DisjunctiveConstraint(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(1		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.current_seq == [1]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(2		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.current_seq == [1, 2]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(4		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.current_seq == [1, 2, 4]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(5		)
											self.assertTrue(dc.completed		)  # Completed!
											self.assertTrue(dc.current_seq == [1, 2, 4, 5]		)

											dc.reset()

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(1		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.remaining() == 3		)
											self.assertTrue(dc.current_seq == [1]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(2		)
											self.assertTrue(not dc.completed		)
											self.assertTrue(dc.remaining() == 2		)
											self.assertTrue(dc.current_seq == [1, 2]		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    dc.update(5		)
											self.assertTrue(dc.completed		)  # Completed!
											self.assertTrue(dc.remaining() == 0		)
											self.assertTrue(dc.current_seq == [1, 2, 5]		)


