


'''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'''



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD






class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			str				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[Any]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			int				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet, attn in zip(self.resnets				,   self.attentions				):
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)
										UpperCAmelCase					    =     resnets

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[str]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet in self.resnets:
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Optional[int]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			Union[str, Any]=True				)->					Optional[int]:

										"""simple docstring"""




										for resnet, attn in zip(self.resnets				,   self.attentions				):
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




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

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict=True				)->					Tuple:

										"""simple docstring"""




										for resnet in self.resnets:
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




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

										"""simple docstring"""




										UpperCAmelCase					    =     [
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
										]

										UpperCAmelCase					    =     []

										for _ in range(self.num_layers				):
																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.in_channels				,   n_heads=self.num_attention_heads				,   d_head=self.in_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions





				def __call__(		self      :			Dict				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Any=True				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     self.resnets[0](lowerCAmelCase				,   lowerCAmelCase				)
										for attn, resnet in zip(self.attentions				,   self.resnets[1:]				):
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										return hidden_states





