


import tensorflow as tf

from ...tf_utils import shape_list



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


     """simple docstring"""






     def __init__(    self					:		List[str]							,			snake_case					:		Optional[Any]							,			snake_case					:		int							,			snake_case					:		Optional[Any]							,			snake_case					:		Optional[Any]							,			snake_case					:		str=1							,			snake_case					:		str=False							,			**snake_case					:		Dict      )						->							Optional[int]:
          super().__init__(**snake_case_      )

          __UpperCAmelCase     :			Tuple  							=      vocab_size
          __UpperCAmelCase     :			Optional[Any]  							=      d_embed
          __UpperCAmelCase     :			Union[str, Any]  							=      d_proj

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

          __UpperCAmelCase     :			str  							=      self.cutoffs[0]
          __UpperCAmelCase     :			Dict  							=      len(self.cutoffs      ) - 1
          __UpperCAmelCase     :			Dict  							=      self.shortlist_size + self.n_clusters
          __UpperCAmelCase     :			Optional[Any]  							=      keep_order

          __UpperCAmelCase     :			List[Any]  							=      []
          __UpperCAmelCase     :			Any  							=      []

     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		Optional[Any]      )						->							str:
          if self.n_clusters > 0:
               __UpperCAmelCase     :			Optional[Any]  							=      self.add_weight(
                   shape=(self.n_clusters, self.d_embed)							,			initializer='''zeros'''							,			trainable=snake_case_							,			name='''cluster_weight'''      )
               __UpperCAmelCase     :			Tuple  							=      self.add_weight(
                   shape=(self.n_clusters,)							,			initializer='''zeros'''							,			trainable=snake_case_							,			name='''cluster_bias'''      )

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

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

     @staticmethod
     def 			lowerCamelCase__			(    snake_case					:		Any							,			snake_case					:		Dict							,			snake_case					:		Union[str, Any]							,			snake_case					:		int=None      )						->							Optional[Any]:
          __UpperCAmelCase     :			Dict  							=      x
          if proj is not None:
               __UpperCAmelCase     :			Dict  							=      tf.einsum('''ibd,ed->ibe'''							,			snake_case_							,			snake_case_      )
          return tf.einsum('''ibd,nd->ibn'''							,			snake_case_							,			snake_case_      ) + b

     @staticmethod
     def 			lowerCamelCase__			(    snake_case					:		Any							,			snake_case					:		Any      )						->							int:
          __UpperCAmelCase     :			Union[str, Any]  							=      shape_list(snake_case_      )
          __UpperCAmelCase     :			List[str]  							=      tf.range(lp_size[0]							,			dtype=target.dtype      )
          __UpperCAmelCase     :			Optional[Any]  							=      tf.stack([r, target]							,			1      )
          return tf.gather_nd(snake_case_							,			snake_case_      )






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

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

                    if i == 0:
                         __UpperCAmelCase     :			Dict  							=      tf.concat([cur_W, self.cluster_weight]							,			0      )
                         __UpperCAmelCase     :			int  							=      tf.concat([cur_b, self.cluster_bias]							,			0      )

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

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

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

          return out



'''simple docstring'''



import os
import sys
import unittest


__UpperCAmelCase      :Optional[int]			    =       os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__UpperCAmelCase      :Dict			    =       os.path.join(git_repo_path, "src", "diffusers")



class      a    (		unittest.TestCase	):


     """simple docstring"""






     def 			lowerCamelCase__			(    self					:		Any      )						->							int:
          __UpperCAmelCase     :			Optional[Any]  							=      find_backend('''    if not is_torch_available():'''      )
          self.assertEqual(snake_case							,			'''torch'''      )

          # backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
          # self.assertEqual(backend_with_underscore, "tensorflow_text")

          __UpperCAmelCase     :			Union[str, Any]  							=      find_backend('''    if not (is_torch_available() and is_transformers_available()):'''      )
          self.assertEqual(snake_case							,			'''torch_and_transformers'''      )

          # double_backend_with_underscore = find_backend(
          #    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
          # )
          # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

          __UpperCAmelCase     :			List[str]  							=      find_backend(
              '''    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):'''      )
          self.assertEqual(snake_case							,			'''torch_and_transformers_and_onnx'''      )

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							int:
          __UpperCAmelCase     :			Tuple  							=      read_init()
          # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
          self.assertIn('''torch'''							,			snake_case      )
          self.assertIn('''torch_and_transformers'''							,			snake_case      )
          self.assertIn('''flax_and_transformers'''							,			snake_case      )
          self.assertIn('''torch_and_transformers_and_onnx'''							,			snake_case      )

          # Likewise, we can't assert on the exact content of a key
          self.assertIn('''UNet2DModel'''							,			objects['''torch''']      )
          self.assertIn('''FlaxUNet2DConditionModel'''							,			objects['''flax''']      )
          self.assertIn('''StableDiffusionPipeline'''							,			objects['''torch_and_transformers''']      )
          self.assertIn('''FlaxStableDiffusionPipeline'''							,			objects['''flax_and_transformers''']      )
          self.assertIn('''LMSDiscreteScheduler'''							,			objects['''torch_and_scipy''']      )
          self.assertIn('''OnnxStableDiffusionPipeline'''							,			objects['''torch_and_transformers_and_onnx''']      )

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							List[Any]:
          __UpperCAmelCase     :			str  							=      create_dummy_object('''CONSTANT'''							,			'''\'torch\''''      )
          self.assertEqual(snake_case							,			'''\nCONSTANT = None\n'''      )

          __UpperCAmelCase     :			Union[str, Any]  							=      create_dummy_object('''function'''							,			'''\'torch\''''      )
          self.assertEqual(
              snake_case							,			'''\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'''      )

          __UpperCAmelCase     :			Optional[int]  							=      '''
class FakeClass(metaclass=DummyObject):
    _backends = \'torch\'

    def __init__(self, *args, **kwargs):
        requires_backends(self, \'torch\')

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, \'torch\')

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, \'torch\')
'''
          __UpperCAmelCase     :			Optional[Any]  							=      create_dummy_object('''FakeClass'''							,			'''\'torch\''''      )
          self.assertEqual(snake_case							,			snake_case      )






     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			List[str]  							=      '''# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, ["torch"])


class FakeClass(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, ["torch"])

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, ["torch"])
'''
          __UpperCAmelCase     :			Optional[int]  							=      create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']}      )
          self.assertEqual(dummy_files['''torch''']							,			snake_case      )