






"""simple docstring"""






import tensorflow as tf

from ...tf_utils import shape_list




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






      '''simple docstring'''

      def __init__(						self     :  Optional[int]						,	lowercase_     :  Tuple						,	lowercase_     :  int						,	lowercase_     :  List[Any]						,	lowercase_     :  int						,	lowercase_     :  Optional[int]=1						,	lowercase_     :  List[Any]=False						,	**lowercase_     :  List[Any]):




          '''simple docstring'''


          super().__init__(**lowercase_)

          SCREAMING_SNAKE_CASE_      :  List[str]    						=		vocab_size
          SCREAMING_SNAKE_CASE_      :  Any    						=		d_embed
          SCREAMING_SNAKE_CASE_      :  int    						=		d_proj

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

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

          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		[]
          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		[]
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]						,	lowercase_     :  List[str]):




          '''simple docstring'''


          if self.n_clusters > 0:
              SCREAMING_SNAKE_CASE_      :  Any    						=		self.add_weight(
                  shape=(self.n_clusters, self.d_embed)						,	initializer='''zeros'''						,	trainable=lowercase_						,	name='''cluster_weight''')
              SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		self.add_weight(
                  shape=(self.n_clusters,)						,	initializer='''zeros'''						,	trainable=lowercase_						,	name='''cluster_bias''')

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

                  SCREAMING_SNAKE_CASE_      :  List[Any]    						=		self.add_weight(
                      shape=(d_emb_i, self.d_proj)						,	initializer='''zeros'''						,	trainable=lowercase_						,	name=F'out_projs_._{i}')
                  self.out_projs.append(lowercase_)
                  SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		self.add_weight(
                      shape=(r_idx - l_idx, d_emb_i)						,	initializer='''zeros'''						,	trainable=lowercase_						,	name=F'out_layers_._{i}_._weight'						,	)
                  SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		self.add_weight(
                      shape=(r_idx - l_idx,)						,	initializer='''zeros'''						,	trainable=lowercase_						,	name=F'out_layers_._{i}_._bias'						,	)
                  self.out_layers.append((weight, bias))
          super().build(lowercase_)
      @staticmethod
      def _SCREAMING_SNAKE_CASE (						lowercase_     :  Tuple						,	lowercase_     :  Dict						,	lowercase_     :  Optional[int]						,	lowercase_     :  List[str]=None):




          '''simple docstring'''


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




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		shape_list(lowercase_)
          SCREAMING_SNAKE_CASE_      :  int    						=		tf.range(lp_size[0]						,	dtype=target.dtype)
          SCREAMING_SNAKE_CASE_      :  Dict    						=		tf.stack([r, target]						,	1)
          return tf.gather_nd(lowercase_						,	lowercase_)



      def _SCREAMING_SNAKE_CASE (						self     :  Tuple						,	lowercase_     :  Optional[Any]						,	lowercase_     :  int						,	lowercase_     :  Dict=True						,	lowercase_     :  str=False):




          '''simple docstring'''


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

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

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

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

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

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

          return out










"""simple docstring"""






import warnings

from ...utils import logging
from .image_processing_segformer import SegformerImageProcessor


UpperCAmelCase_   : Any   			= logging.get_logger(__name__)




class 							lowerCAmelCase__				(			UpperCAmelCase__ ):






      '''simple docstring'''

      def __init__(						self     :  Union[str, Any]						,	*lowercase_     :  List[str]						,	**lowercase_     :  List[str]):




          '''simple docstring'''


          warnings.warn(
              '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
              ''' Please use SegformerImageProcessor instead.'''						,	lowercase_						,	)
          super().__init__(*lowercase_						,	**lowercase_)


