
import tensorflow as tf

from ...tf_utils import shape_list







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





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

         snake_case				  =      vocab_size
         snake_case				  =      d_embed
         snake_case				  =      d_proj

         snake_case				  =      cutoffs + [vocab_size]
         snake_case				  =      [0] + self.cutoffs
         snake_case				  =      div_val

         snake_case				  =      self.cutoffs[0]
         snake_case				  =      len(self.cutoffs     ) - 1
         snake_case				  =      self.shortlist_size + self.n_clusters
         snake_case				  =      keep_order

         snake_case				  =      []
         snake_case				  =      []





     def 	UpperCAmelCase(self   :					str       ,					_A   :					Any     )		->		Any:
         if self.n_clusters > 0:
             snake_case				  =      self.add_weight(
                 shape=(self.n_clusters, self.d_embed)       ,					initializer="zeros"       ,					trainable=_A       ,					name="cluster_weight"     )
             snake_case				  =      self.add_weight(
                 shape=(self.n_clusters,)       ,					initializer="zeros"       ,					trainable=_A       ,					name="cluster_bias"     )

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

                 snake_case				  =      self.add_weight(
                     shape=(d_emb_i, self.d_proj)       ,					initializer="zeros"       ,					trainable=_A       ,					name=f'out_projs_._{i}'     )
                 self.out_projs.append(_A     )
                 snake_case				  =      self.add_weight(
                     shape=(r_idx - l_idx, d_emb_i)       ,					initializer="zeros"       ,					trainable=_A       ,					name=f'out_layers_._{i}_._weight'       ,					)
                 snake_case				  =      self.add_weight(
                     shape=(r_idx - l_idx,)       ,					initializer="zeros"       ,					trainable=_A       ,					name=f'out_layers_._{i}_._bias'       ,					)
                 self.out_layers.append((weight, bias)     )
         super().build(_A     )





     @staticmethod
     def 	UpperCAmelCase(_A   :					List[Any]       ,					_A   :					str       ,					_A   :					List[Any]       ,					_A   :					Tuple=None     )		->		int:
         snake_case				  =      x
         if proj is not None:
             snake_case				  =      tf.einsum("ibd,ed->ibe"       ,					_A       ,					_A     )
         return tf.einsum("ibd,nd->ibn"       ,					_A       ,					_A     ) + b





     @staticmethod
     def 	UpperCAmelCase(_A   :					int       ,					_A   :					Optional[int]     )		->		int:
         snake_case				  =      shape_list(_A     )
         snake_case				  =      tf.range(lp_size[0]       ,					dtype=target.dtype     )
         snake_case				  =      tf.stack([r, target]       ,					1     )
         return tf.gather_nd(_A       ,					_A     )





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

                 if self.div_val == 1:
                     snake_case				  =      self.out_layers[0][0][l_idx:r_idx]
                     snake_case				  =      self.out_layers[0][1][l_idx:r_idx]
                 else:
                     snake_case				  =      self.out_layers[i][0]
                     snake_case				  =      self.out_layers[i][1]

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

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

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

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

         return out



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_A						      =				logging.get_logger(__name__)

_A						      =				{
    "roberta-base": "https://huggingface.co/roberta-base/resolve/main/config.json",
    "roberta-large": "https://huggingface.co/roberta-large/resolve/main/config.json",
    "roberta-large-mnli": "https://huggingface.co/roberta-large-mnli/resolve/main/config.json",
    "distilroberta-base": "https://huggingface.co/distilroberta-base/resolve/main/config.json",
    "roberta-base-openai-detector": "https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json",
    "roberta-large-openai-detector": "https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json",
}







class   lowerCamelCase	(						A_						):
     UpperCAmelCase__       :							Optional[int]     =	"roberta"





     def __init__(self   :					Union[str, Any]       ,					_A   :					List[Any]=5_0_2_6_5       ,					_A   :					Dict=7_6_8       ,					_A   :					Tuple=1_2       ,					_A   :					Optional[Any]=1_2       ,					_A   :					int=3_0_7_2       ,					_A   :					List[str]="gelu"       ,					_A   :					Tuple=0.1       ,					_A   :					Dict=0.1       ,					_A   :					Optional[int]=5_1_2       ,					_A   :					Dict=2       ,					_A   :					Optional[Any]=0.02       ,					_A   :					Optional[Any]=1E-12       ,					_A   :					str=1       ,					_A   :					Dict=0       ,					_A   :					Optional[int]=2       ,					_A   :					int="absolute"       ,					_A   :					Any=True       ,					_A   :					Union[str, Any]=None       ,					**_A   :					Optional[int]       ,					)		->		Tuple:
         super().__init__(pad_token_id=_A       ,					bos_token_id=_A       ,					eos_token_id=_A       ,					**_A     )

         snake_case				  =      vocab_size
         snake_case				  =      hidden_size
         snake_case				  =      num_hidden_layers
         snake_case				  =      num_attention_heads
         snake_case				  =      hidden_act
         snake_case				  =      intermediate_size
         snake_case				  =      hidden_dropout_prob
         snake_case				  =      attention_probs_dropout_prob
         snake_case				  =      max_position_embeddings
         snake_case				  =      type_vocab_size
         snake_case				  =      initializer_range
         snake_case				  =      layer_norm_eps
         snake_case				  =      position_embedding_type
         snake_case				  =      use_cache
         snake_case				  =      classifier_dropout







class   lowerCamelCase	(						A_						):





     @property
     def 	UpperCAmelCase(self   :					int     )		->		Mapping[str, Mapping[int, str]]:
         if self.task == "multiple-choice":
             snake_case				  =      {0: "batch", 1: "choice", 2: "sequence"}
         else:
             snake_case				  =      {0: "batch", 1: "sequence"}
         return OrderedDict(
             [
                 ("input_ids", dynamic_axis),
                 ("attention_mask", dynamic_axis),
             ]     )

