


'''simple docstring'''



import tempfile

import torch

from diffusers import IPNDMScheduler

from .test_schedulers import SchedulerCommonTest






class 	UpperCamelCase__(       lowerCAmelCase	):
				__magic_name__    :				List[Any]  		=	(IPNDMScheduler,)
				__magic_name__    :				Optional[Any]  		=	(("num_inference_steps", 50),)




				def 					a__(		self      :			Any				,   **lowerCAmelCase      :			Tuple				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     {'''num_train_timesteps''': 1000}
										config.update(**lowerCAmelCase				)
										return config




				def 					a__(		self      :			List[Any]				,   lowerCAmelCase      :			int=0				,   **lowerCAmelCase      :			Tuple				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     dict(self.forward_default_kwargs				)
										UpperCAmelCase					    =     kwargs.pop('''num_inference_steps'''				,   lowerCAmelCase				)
										UpperCAmelCase					    =     self.dummy_sample
										UpperCAmelCase					    =     0.1 * sample
										UpperCAmelCase					    =     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

										for scheduler_class in self.scheduler_classes:
																UpperCAmelCase					    =     self.get_scheduler_config(**lowerCAmelCase				)
																UpperCAmelCase					    =     scheduler_class(**lowerCAmelCase				)
																scheduler.set_timesteps(lowerCAmelCase				)
																# copy over dummy past residuals
																UpperCAmelCase					    =     dummy_past_residuals[:]

																if time_step is None:
																						UpperCAmelCase					    =     scheduler.timesteps[len(scheduler.timesteps				) // 2]

																with tempfile.TemporaryDirectory() as tmpdirname:
																						scheduler.save_config(lowerCAmelCase				)
																						UpperCAmelCase					    =     scheduler_class.from_pretrained(lowerCAmelCase				)
																						new_scheduler.set_timesteps(lowerCAmelCase				)
																						# copy over dummy past residuals
																						UpperCAmelCase					    =     dummy_past_residuals[:]

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     new_scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     new_scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"




				def 					a__(		self      :			Optional[Any]				)->					Optional[int]:

										"""simple docstring"""




										pass




				def 					a__(		self      :			List[Any]				,   lowerCAmelCase      :			Dict=0				,   **lowerCAmelCase      :			Any				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     dict(self.forward_default_kwargs				)
										UpperCAmelCase					    =     kwargs.pop('''num_inference_steps'''				,   lowerCAmelCase				)
										UpperCAmelCase					    =     self.dummy_sample
										UpperCAmelCase					    =     0.1 * sample
										UpperCAmelCase					    =     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

										for scheduler_class in self.scheduler_classes:
																UpperCAmelCase					    =     self.get_scheduler_config()
																UpperCAmelCase					    =     scheduler_class(**lowerCAmelCase				)
																scheduler.set_timesteps(lowerCAmelCase				)

																# copy over dummy past residuals (must be after setting timesteps)
																UpperCAmelCase					    =     dummy_past_residuals[:]

																if time_step is None:
																						UpperCAmelCase					    =     scheduler.timesteps[len(scheduler.timesteps				) // 2]

																with tempfile.TemporaryDirectory() as tmpdirname:
																						scheduler.save_config(lowerCAmelCase				)
																						UpperCAmelCase					    =     scheduler_class.from_pretrained(lowerCAmelCase				)
																						# copy over dummy past residuals
																						new_scheduler.set_timesteps(lowerCAmelCase				)

																						# copy over dummy past residual (must be after setting timesteps)
																						UpperCAmelCase					    =     dummy_past_residuals[:]

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     new_scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     new_scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"




				def 					a__(		self      :			List[str]				,   **lowerCAmelCase      :			Union[str, Any]				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     self.scheduler_classes[0]
										UpperCAmelCase					    =     self.get_scheduler_config(**lowerCAmelCase				)
										UpperCAmelCase					    =     scheduler_class(**lowerCAmelCase				)

										UpperCAmelCase					    =     10
										UpperCAmelCase					    =     self.dummy_model()
										UpperCAmelCase					    =     self.dummy_sample_deter
										scheduler.set_timesteps(lowerCAmelCase				)

										for i, t in enumerate(scheduler.timesteps				):
																UpperCAmelCase					    =     model(lowerCAmelCase				,   lowerCAmelCase				)
																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				).prev_sample

										for i, t in enumerate(scheduler.timesteps				):
																UpperCAmelCase					    =     model(lowerCAmelCase				,   lowerCAmelCase				)
																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				).prev_sample

										return sample




				def 					a__(		self      :			Union[str, Any]				)->					int:

										"""simple docstring"""




										UpperCAmelCase					    =     dict(self.forward_default_kwargs				)

										UpperCAmelCase					    =     kwargs.pop('''num_inference_steps'''				,   lowerCAmelCase				)

										for scheduler_class in self.scheduler_classes:
																UpperCAmelCase					    =     self.get_scheduler_config()
																UpperCAmelCase					    =     scheduler_class(**lowerCAmelCase				)

																UpperCAmelCase					    =     self.dummy_sample
																UpperCAmelCase					    =     0.1 * sample

																if num_inference_steps is not None and hasattr(lowerCAmelCase				,   '''set_timesteps'''				):
																						scheduler.set_timesteps(lowerCAmelCase				)
																elif num_inference_steps is not None and not hasattr(lowerCAmelCase				,   '''set_timesteps'''				):
																						UpperCAmelCase					    =     num_inference_steps

																# copy over dummy past residuals (must be done after set_timesteps)
																UpperCAmelCase					    =     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
																UpperCAmelCase					    =     dummy_past_residuals[:]

																UpperCAmelCase					    =     scheduler.timesteps[5]
																UpperCAmelCase					    =     scheduler.timesteps[6]

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																self.assertEqual(output_a.shape				,   sample.shape				)
																self.assertEqual(output_a.shape				,   output_a.shape				)

																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample
																UpperCAmelCase					    =     scheduler.step(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				,   **lowerCAmelCase				).prev_sample

																self.assertEqual(output_a.shape				,   sample.shape				)
																self.assertEqual(output_a.shape				,   output_a.shape				)




				def 					a__(		self      :			Any				)->					Tuple:

										"""simple docstring"""




										for timesteps in [100, 1000]:
																self.check_over_configs(num_train_timesteps=lowerCAmelCase				,   time_step=lowerCAmelCase				)




				def 					a__(		self      :			int				)->					Optional[int]:

										"""simple docstring"""




										for t, num_inference_steps in zip([1, 5, 10]				,   [10, 50, 100]				):
																self.check_over_forward(num_inference_steps=lowerCAmelCase				,   time_step=lowerCAmelCase				)





				def 					a__(		self      :			Optional[Any]				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     self.full_loop()
										UpperCAmelCase					    =     torch.mean(torch.abs(lowerCAmelCase				)				)

										assert abs(result_mean.item() - 2540529				) < 10









'''simple docstring'''



def 				lowerCamelCase__				(	A       :      int				,       A       :      int					):




						'''simple docstring'''






						return int(input_a == input_a == 0					)


def 				lowerCamelCase__				(	):




						'''simple docstring'''






						print('''Truth Table of NOR Gate:'''					)
						print('''|   Input 1   |  Input 2  | Output |'''					)
						print(f"""|      0      |    0      |  {nor_gate(0				,       0					)}     |"""					)
						print(f"""|      0      |    1      |  {nor_gate(0				,       1					)}     |"""					)
						print(f"""|      1      |    0      |  {nor_gate(1				,       0					)}     |"""					)
						print(f"""|      1      |    1      |  {nor_gate(1				,       1					)}     |"""					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	main()





