




'''simple docstring'''



from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       bool		)->				torch.Tensor:




     '''simple docstring'''





     if not inplace:
          __snake_case         =					ma + ma
     else:
          ma += ma

     return ma







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       List[int]		)->				torch.Tensor:




     '''simple docstring'''





     __snake_case         =					-1 * len(_lowerCamelCase		)
     __snake_case         =					list(range(len(tensor.shape[:zero_index]		)		)		)
     return tensor.permute(first_inds + [zero_index + i for i in inds]		)







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       int		)->				torch.Tensor:




     '''simple docstring'''





     return t.reshape(t.shape[:-no_dims] + (-1,)		)







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       int				,		_lowerCamelCase							:       float = 1E-4		)->				torch.Tensor:




     '''simple docstring'''





     __snake_case         =					mask.expand(*value.shape		)
     return torch.sum(mask * value				,		dim=_lowerCamelCase		) / (eps + torch.sum(_lowerCamelCase				,		dim=_lowerCamelCase		))







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       torch.types.Number = 2.3125				,		_lowerCamelCase							:       torch.types.Number = 21.6875				,		_lowerCamelCase							:       int = 64		)->				torch.Tensor:




     '''simple docstring'''





     __snake_case         =					torch.linspace(_lowerCamelCase				,		_lowerCamelCase				,		no_bins - 1				,		device=pts.device		)
     __snake_case         =					torch.sqrt(torch.sum((pts.unsqueeze(-2		) - pts.unsqueeze(-3		)) ** 2				,		dim=-1		)		)
     return torch.bucketize(_lowerCamelCase				,		_lowerCamelCase		)







def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[list], Any]				,		_lowerCamelCase							:       List[dict]		)->				dict:




     '''simple docstring'''





     __snake_case         =					dicts[0]
     __snake_case         =					{}
     for k, v in first.items():
          __snake_case         =					[d[k] for d in dicts]
          if isinstance(_lowerCamelCase				,		_lowerCamelCase		):
               __snake_case         =					dict_multimap(_lowerCamelCase				,		_lowerCamelCase		)
          else:
               __snake_case         =					fn(_lowerCamelCase		)

     return new_dict







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       torch.Tensor		)->				torch.Tensor:




     '''simple docstring'''





     __snake_case         =					v_bins.view(((1,) * len(x.shape		)) + (len(_lowerCamelCase		),)		)
     __snake_case         =					x[..., None] - reshaped_bins
     __snake_case         =					torch.argmin(torch.abs(_lowerCamelCase		)				,		dim=-1		)
     return nn.functional.one_hot(_lowerCamelCase				,		num_classes=len(_lowerCamelCase		)		).float()







def      _UpperCamelCase	(_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       torch.Tensor				,		_lowerCamelCase							:       int = 0				,		_lowerCamelCase							:       int = 0		)->				torch.Tensor:




     '''simple docstring'''





     __snake_case         =					[]
     for i, s in enumerate(data.shape[:no_batch_dims]		):
          __snake_case         =					torch.arange(_lowerCamelCase		)
          __snake_case         =					r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape		) - i - 1)))		)
          ranges.append(_lowerCamelCase		)

     __snake_case         =					[slice(_lowerCamelCase		) for _ in range(len(data.shape		) - no_batch_dims		)]
     __snake_case         =					inds
     ranges.extend(_lowerCamelCase		)
     # Matt note: Editing this to get around the behaviour of using a list as an array index changing
     # in recent Numpy versions
     return data[tuple(_lowerCamelCase		)]


UpperCAmelCase_    :					int					      =						TypeVar('''T''')







def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[T], Any]				,		_lowerCamelCase							:       Dict[Any, Union[dict, list, tuple, T]]				,		_lowerCamelCase							:       Type[T]		)->				Dict[Any, Union[dict, list, tuple, Any]]:




     '''simple docstring'''





     __snake_case         =					{}
     for k, v in dic.items():
          if isinstance(_lowerCamelCase				,		_lowerCamelCase		):
               __snake_case         =					dict_map(_lowerCamelCase				,		_lowerCamelCase				,		_lowerCamelCase		)
          else:
               __snake_case         =					tree_map(_lowerCamelCase				,		_lowerCamelCase				,		_lowerCamelCase		)

     return new_dict







@overload
def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[T], Any]				,		_lowerCamelCase							:       T				,		_lowerCamelCase							:       Type[T]		)->				Any:




     '''simple docstring'''





     ...







@overload
def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[T], Any]				,		_lowerCamelCase							:       dict				,		_lowerCamelCase							:       Type[T]		)->				dict:




     '''simple docstring'''





     ...







@overload
def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[T], Any]				,		_lowerCamelCase							:       list				,		_lowerCamelCase							:       Type[T]		)->				list:




     '''simple docstring'''





     ...







@overload
def      _UpperCamelCase	(_lowerCamelCase							:       Callable[[T], Any]				,		_lowerCamelCase							:       tuple				,		_lowerCamelCase							:       Type[T]		)->				tuple:




     '''simple docstring'''





     ...







def      _UpperCamelCase	(_lowerCamelCase							:       List[str]				,		_lowerCamelCase							:       str				,		_lowerCamelCase							:       Any		)->				str:




     '''simple docstring'''





     if isinstance(_lowerCamelCase				,		_lowerCamelCase		):
          return dict_map(_lowerCamelCase				,		_lowerCamelCase				,		_lowerCamelCase		)
     elif isinstance(_lowerCamelCase				,		_lowerCamelCase		):
          return [tree_map(_lowerCamelCase				,		_lowerCamelCase				,		_lowerCamelCase		) for x in tree]
     elif isinstance(_lowerCamelCase				,		_lowerCamelCase		):
          return tuple(tree_map(_lowerCamelCase				,		_lowerCamelCase				,		_lowerCamelCase		) for x in tree		)
     elif isinstance(_lowerCamelCase				,		_lowerCamelCase		):
          return fn(_lowerCamelCase		)
     else:
          print(type(_lowerCamelCase		)		)
          raise ValueError('''Not supported'''		)







UpperCAmelCase_    :					Union[str, Any]					      =						partial(tree_map, leaf_type=torch.Tensor)












'''simple docstring'''



import inspect

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
     import jax





@require_flax
class       lowerCAmelCase				:


       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.prepare_init_args_and_inputs_for_common()

            __snake_case         =					self.model_class(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model.init(inputs_dict['''prng_key''']      ,					inputs_dict['''sample''']					)
            jax.lax.stop_gradient(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					model.apply(__SCREAMING_SNAKE_CASE      ,					inputs_dict['''sample''']					)

            if isinstance(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					):
                 __snake_case         =					output.sample

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					inputs_dict['''sample'''].shape
            self.assertEqual(output.shape      ,					__SCREAMING_SNAKE_CASE      ,					'''Input and output shapes do not match'''					)


       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.prepare_init_args_and_inputs_for_common()

            __snake_case         =					16
            __snake_case         =					(16, 32)

            __snake_case         =					self.model_class(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model.init(inputs_dict['''prng_key''']      ,					inputs_dict['''sample''']					)
            jax.lax.stop_gradient(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					model.apply(__SCREAMING_SNAKE_CASE      ,					inputs_dict['''sample''']					)

            if isinstance(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					):
                 __snake_case         =					output.sample

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					inputs_dict['''sample'''].shape
            self.assertEqual(output.shape      ,					__SCREAMING_SNAKE_CASE      ,					'''Input and output shapes do not match'''					)




       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					'''kwargs''' in inspect.signature(self.model_class.__init__					).parameters
            __snake_case         =					len(self.model_class._deprecated_kwargs					) > 0

            if has_kwarg_in_model_class and not has_deprecated_kwarg:
                 raise ValueError(
                     F'''{self.model_class} has `**kwargs` in its __init__ method but has not defined any deprecated kwargs'''
                     ''' under the `_deprecated_kwargs` class attribute. Make sure to either remove `**kwargs` if there are'''
                     ''' no deprecated arguments or add the deprecated argument with `_deprecated_kwargs ='''
                     ''' [<deprecated_argument>]`'''					)

            if not has_kwarg_in_model_class and has_deprecated_kwarg:
                 raise ValueError(
                     F'''{self.model_class} doesn\'t have `**kwargs` in its __init__ method but has defined deprecated kwargs'''
                     ''' under the `_deprecated_kwargs` class attribute. Make sure to either add the `**kwargs` argument to'''
                     F''' {self.model_class}.__init__ if there are deprecated arguments or remove the deprecated argument'''
                     ''' from `_deprecated_kwargs = [<deprecated_argument>]`'''					)






