






import tensorflow as tf

from ...tf_utils import shape_list




class 							_lowerCAmelCase(			tf.keras.layers.Layer   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase=1    ,     _lowerCamelCase=False    ,     **_lowerCamelCase				):
         super().__init__(**_lowerCamelCase				)

         UpperCamelCase_:   Dict        =							vocab_size
         UpperCamelCase_:   Union[str, Any]        =							d_embed
         UpperCamelCase_:   List[Any]        =							d_proj

         UpperCamelCase_:   Tuple        =							cutoffs + [vocab_size]
         UpperCamelCase_:   Any        =							[0] + self.cutoffs
         UpperCamelCase_:   List[str]        =							div_val

         UpperCamelCase_:   Optional[int]        =							self.cutoffs[0]
         UpperCamelCase_:   Tuple        =							len(self.cutoffs				) - 1
         UpperCamelCase_:   Optional[Any]        =							self.shortlist_size + self.n_clusters
         UpperCamelCase_:   int        =							keep_order

         UpperCamelCase_:   Optional[Any]        =							[]
         UpperCamelCase_:   int        =							[]





       def      _a       (   self    ,     _lowerCamelCase				):
         if self.n_clusters > 0:
           UpperCamelCase_:   str        =							self.add_weight(
               shape=(self.n_clusters, self.d_embed)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name='cluster_weight'				)
           UpperCamelCase_:   Any        =							self.add_weight(
               shape=(self.n_clusters,)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name='cluster_bias'				)

         if self.div_val == 1:
           for i in range(len(self.cutoffs				)				):
             if self.d_proj != self.d_embed:
               UpperCamelCase_:   List[Any]        =							self.add_weight(
                   shape=(self.d_embed, self.d_proj)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_projs_._{i}'''    ,     )
               self.out_projs.append(_lowerCamelCase				)
             else:
               self.out_projs.append(_lowerCamelCase				)
             UpperCamelCase_:   List[Any]        =							self.add_weight(
                 shape=(self.vocab_size, self.d_embed)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_layers_._{i}_._weight'''    ,     )
             UpperCamelCase_:   Optional[Any]        =							self.add_weight(
                 shape=(self.vocab_size,)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_layers_._{i}_._bias'''    ,     )
             self.out_layers.append((weight, bias)				)
         else:
           for i in range(len(self.cutoffs				)				):
             UpperCamelCase_ ,UpperCamelCase_:   List[Any]        =							self.cutoff_ends[i], self.cutoff_ends[i + 1]
             UpperCamelCase_:   Dict        =							self.d_embed // (self.div_val**i)

             UpperCamelCase_:   Any        =							self.add_weight(
                 shape=(d_emb_i, self.d_proj)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_projs_._{i}'''				)
             self.out_projs.append(_lowerCamelCase				)
             UpperCamelCase_:   Union[str, Any]        =							self.add_weight(
                 shape=(r_idx - l_idx, d_emb_i)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_layers_._{i}_._weight'''    ,     )
             UpperCamelCase_:   int        =							self.add_weight(
                 shape=(r_idx - l_idx,)    ,     initializer='zeros'    ,     trainable=_lowerCamelCase    ,     name=f'''out_layers_._{i}_._bias'''    ,     )
             self.out_layers.append((weight, bias)				)
         super().build(_lowerCamelCase				)





       @staticmethod
       def      _a       (   _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase=None				):
         UpperCamelCase_:   List[str]        =							x
         if proj is not None:
           UpperCamelCase_:   Tuple        =							tf.einsum('ibd,ed->ibe'    ,     _lowerCamelCase    ,     _lowerCamelCase				)
         return tf.einsum('ibd,nd->ibn'    ,     _lowerCamelCase    ,     _lowerCamelCase				) + b





       @staticmethod
       def      _a       (   _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   Tuple        =							shape_list(_lowerCamelCase				)
         UpperCamelCase_:   Optional[Any]        =							tf.range(lp_size[0]    ,     dtype=target.dtype				)
         UpperCamelCase_:   Any        =							tf.stack([r, target]    ,     1				)
         return tf.gather_nd(_lowerCamelCase    ,     _lowerCamelCase				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase=True    ,     _lowerCamelCase=False				):
         UpperCamelCase_:   int        =							0
         if self.n_clusters == 0:
           UpperCamelCase_:   Union[str, Any]        =							self._logit(_lowerCamelCase    ,     self.out_layers[0][0]    ,     self.out_layers[0][1]    ,     self.out_projs[0]				)
           if target is not None:
             UpperCamelCase_:   List[Any]        =							tf.nn.sparse_softmax_cross_entropy_with_logits(labels=_lowerCamelCase    ,     logits=_lowerCamelCase				)
           UpperCamelCase_:   Optional[Any]        =							tf.nn.log_softmax(_lowerCamelCase    ,     axis=-1				)
         else:
           UpperCamelCase_:   Optional[Any]        =							shape_list(_lowerCamelCase				)
           UpperCamelCase_:   Union[str, Any]        =							[]
           UpperCamelCase_:   Optional[Any]        =							tf.zeros(hidden_sizes[:2]				)
           for i in range(len(self.cutoffs				)				):
             UpperCamelCase_ ,UpperCamelCase_:   Optional[Any]        =							self.cutoff_ends[i], self.cutoff_ends[i + 1]
             if target is not None:
               UpperCamelCase_:   List[Any]        =							(target >= l_idx) & (target < r_idx)
               UpperCamelCase_:   List[Any]        =							tf.where(_lowerCamelCase				)
               UpperCamelCase_:   str        =							tf.boolean_mask(_lowerCamelCase    ,     _lowerCamelCase				) - l_idx

             if self.div_val == 1:
               UpperCamelCase_:   Optional[Any]        =							self.out_layers[0][0][l_idx:r_idx]
               UpperCamelCase_:   List[str]        =							self.out_layers[0][1][l_idx:r_idx]
             else:
               UpperCamelCase_:   Optional[int]        =							self.out_layers[i][0]
               UpperCamelCase_:   int        =							self.out_layers[i][1]

             if i == 0:
               UpperCamelCase_:   str        =							tf.concat([cur_W, self.cluster_weight]    ,     0				)
               UpperCamelCase_:   Any        =							tf.concat([cur_b, self.cluster_bias]    ,     0				)

               UpperCamelCase_:   List[Any]        =							self._logit(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     self.out_projs[0]				)
               UpperCamelCase_:   int        =							tf.nn.log_softmax(_lowerCamelCase				)
               out.append(head_logprob[..., : self.cutoffs[0]]				)
               if target is not None:
                 UpperCamelCase_:   Any        =							tf.boolean_mask(_lowerCamelCase    ,     _lowerCamelCase				)
                 UpperCamelCase_:   List[Any]        =							self._gather_logprob(_lowerCamelCase    ,     _lowerCamelCase				)
             else:
               UpperCamelCase_:   str        =							self._logit(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     self.out_projs[i]				)
               UpperCamelCase_:   Optional[Any]        =							tf.nn.log_softmax(_lowerCamelCase				)
               UpperCamelCase_:   List[Any]        =							self.cutoffs[0] + i - 1  # No probability for the head cluster
               UpperCamelCase_:   Dict        =							head_logprob[..., cluster_prob_idx, None] + tail_logprob
               out.append(_lowerCamelCase				)
               if target is not None:
                 UpperCamelCase_:   List[Any]        =							tf.boolean_mask(_lowerCamelCase    ,     _lowerCamelCase				)
                 UpperCamelCase_:   Optional[Any]        =							tf.boolean_mask(_lowerCamelCase    ,     _lowerCamelCase				)
                 UpperCamelCase_:   int        =							self._gather_logprob(_lowerCamelCase    ,     _lowerCamelCase				)
                 cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
             if target is not None:
               loss += tf.scatter_nd(_lowerCamelCase    ,     -cur_logprob    ,     shape_list(_lowerCamelCase				)				)
           UpperCamelCase_:   Optional[Any]        =							tf.concat(_lowerCamelCase    ,     axis=-1				)

         if target is not None:
           if return_mean:
             UpperCamelCase_:   Any        =							tf.reduce_mean(_lowerCamelCase				)
           # Add the training-time loss value to the layer using `self.add_loss()`.
           self.add_loss(_lowerCamelCase				)

           # Log the loss as a metric (we could log arbitrary metrics,
           # including different metrics for training and inference.
           self.add_metric(_lowerCamelCase    ,     name=self.name    ,     aggregation='mean' if return_mean else ''				)

         return out


import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






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







			@property
			def 				__UpperCamelCase			(      self				:			List[str]						)				->							Union[str, Any]:

				"""simple docstring"""

				torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   str					  =							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				:			Union[str, Any]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.dummy_uncond_unet
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							KarrasVeScheduler()

				SCREAMING_SNAKE_CASE :   Any					  =							KarrasVePipeline(unet=a   ,   scheduler=a						)
				pipe.to(a						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							pipe(num_inference_steps=2   ,   generator=a   ,   output_type="numpy"						).images

				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							pipe(num_inference_steps=2   ,   generator=a   ,   output_type="numpy"   ,   return_dict=a						)[0]

				SCREAMING_SNAKE_CASE :   List[Any]					  =							image[0, -3:, -3:, -1]
				SCREAMING_SNAKE_CASE :   Any					  =							image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 32, 32, 3)
				SCREAMING_SNAKE_CASE :   str					  =							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 _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			int						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							"google/ncsnpp-celebahq-256"
				SCREAMING_SNAKE_CASE :   List[Any]					  =							UNetaDModel.from_pretrained(a						)
				SCREAMING_SNAKE_CASE :   Any					  =							KarrasVeScheduler()

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							KarrasVePipeline(unet=a   ,   scheduler=a						)
				pipe.to(a						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Any					  =							pipe(num_inference_steps=20   ,   generator=a   ,   output_type="numpy"						).images

				SCREAMING_SNAKE_CASE :   List[str]					  =							image[0, -3:, -3:, -1]
				assert image.shape == (1, 256, 256, 3)
				SCREAMING_SNAKE_CASE :   str					  =							np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]						)

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