






import tensorflow as tf

from ...tf_utils import shape_list







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




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase=1    ,	_lowerCAmelCase=False    ,	**_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__(**_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[int]      							=				vocab_size
          lowerCAmelCase__   :int      							=				d_embed
          lowerCAmelCase__   :Dict      							=				d_proj

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

          lowerCAmelCase__   :Union[str, Any]      							=				self.cutoffs[0]
          lowerCAmelCase__   :int      							=				len(self.cutoffs		) - 1
          lowerCAmelCase__   :Tuple      							=				self.shortlist_size + self.n_clusters
          lowerCAmelCase__   :List[str]      							=				keep_order

          lowerCAmelCase__   :Tuple      							=				[]
          lowerCAmelCase__   :Tuple      							=				[]
      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          if self.n_clusters > 0:
              lowerCAmelCase__   :str      							=				self.add_weight(
                  shape=(self.n_clusters, self.d_embed)    ,	initializer="zeros"    ,	trainable=_lowerCAmelCase    ,	name="cluster_weight"		)
              lowerCAmelCase__   :Optional[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:
                      lowerCAmelCase__   :Optional[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		)
                  lowerCAmelCase__   :int      							=				self.add_weight(
                      shape=(self.vocab_size, self.d_embed)    ,	initializer="zeros"    ,	trainable=_lowerCAmelCase    ,	name=f'''out_layers_._{i}_._weight'''    ,	)
                  lowerCAmelCase__   :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		)		):
                  lowerCAmelCase__   :int      							=				self.cutoff_ends[i], self.cutoff_ends[i + 1]
                  lowerCAmelCase__   :Optional[Any]      							=				self.d_embed // (self.div_val**i)

                  lowerCAmelCase__   :List[str]      							=				self.add_weight(
                      shape=(d_emb_i, self.d_proj)    ,	initializer="zeros"    ,	trainable=_lowerCAmelCase    ,	name=f'''out_projs_._{i}'''		)
                  self.out_projs.append(_lowerCAmelCase		)
                  lowerCAmelCase__   :Any      							=				self.add_weight(
                      shape=(r_idx - l_idx, d_emb_i)    ,	initializer="zeros"    ,	trainable=_lowerCAmelCase    ,	name=f'''out_layers_._{i}_._weight'''    ,	)
                  lowerCAmelCase__   :str      							=				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 			snake_case_      (   _lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase=None		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				x
          if proj is not None:
              lowerCAmelCase__   :Any      							=				tf.einsum("ibd,ed->ibe"    ,	_lowerCAmelCase    ,	_lowerCAmelCase		)
          return tf.einsum("ibd,nd->ibn"    ,	_lowerCAmelCase    ,	_lowerCAmelCase		) + b
      @staticmethod
      def 			snake_case_      (   _lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				shape_list(_lowerCAmelCase		)
          lowerCAmelCase__   :Any      							=				tf.range(lp_size[0]    ,	dtype=target.dtype		)
          lowerCAmelCase__   :List[str]      							=				tf.stack([r, target]    ,	1		)
          return tf.gather_nd(_lowerCAmelCase    ,	_lowerCAmelCase		)



      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=False		):



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

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

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

                      lowerCAmelCase__   :List[Any]      							=				self._logit(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	self.out_projs[0]		)
                      lowerCAmelCase__   :str      							=				tf.nn.log_softmax(_lowerCAmelCase		)
                      out.append(head_logprob[..., : self.cutoffs[0]]		)
                      if target is not None:
                          lowerCAmelCase__   :Dict      							=				tf.boolean_mask(_lowerCAmelCase    ,	_lowerCAmelCase		)
                          lowerCAmelCase__   :int      							=				self._gather_logprob(_lowerCAmelCase    ,	_lowerCAmelCase		)
                  else:
                      lowerCAmelCase__   :List[Any]      							=				self._logit(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	self.out_projs[i]		)
                      lowerCAmelCase__   :Dict      							=				tf.nn.log_softmax(_lowerCAmelCase		)
                      lowerCAmelCase__   :Union[str, Any]      							=				self.cutoffs[0] + i - 1  # No probability for the head cluster
                      lowerCAmelCase__   :List[str]      							=				head_logprob[..., cluster_prob_idx, None] + tail_logprob
                      out.append(_lowerCAmelCase		)
                      if target is not None:
                          lowerCAmelCase__   :Tuple      							=				tf.boolean_mask(_lowerCAmelCase    ,	_lowerCAmelCase		)
                          lowerCAmelCase__   :int      							=				tf.boolean_mask(_lowerCAmelCase    ,	_lowerCAmelCase		)
                          lowerCAmelCase__   :List[Any]      							=				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		)		)
              lowerCAmelCase__   :Any      							=				tf.concat(_lowerCAmelCase    ,	axis=-1		)

          if target is not None:
              if return_mean:
                  lowerCAmelCase__   :List[str]      							=				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











from argparse import ArgumentParser

from datasets.commands.convert import ConvertCommand
from datasets.commands.dummy_data import DummyDataCommand
from datasets.commands.env import EnvironmentCommand
from datasets.commands.run_beam import RunBeamCommand
from datasets.commands.test import TestCommand
from datasets.utils.logging import set_verbosity_info


def 				snake_case__							(							UpperCAmelCase							:  Dict       ):
    return {key.lstrip("-"       ): value for key, value in zip(unknown_args[::2]    ,			unknown_args[1::2]       )}


def 				snake_case__							(							):
    lowerCAmelCase__   :str      							=				ArgumentParser(
        "HuggingFace Datasets CLI tool"    ,			usage="datasets-cli <command> [<args>]"    ,			allow_abbrev=UpperCAmelCase       )
    lowerCAmelCase__   :Any      							=				parser.add_subparsers(help="datasets-cli command helpers"       )
    set_verbosity_info()

    # Register commands
    ConvertCommand.register_subcommand(UpperCAmelCase       )
    EnvironmentCommand.register_subcommand(UpperCAmelCase       )
    TestCommand.register_subcommand(UpperCAmelCase       )
    RunBeamCommand.register_subcommand(UpperCAmelCase       )
    DummyDataCommand.register_subcommand(UpperCAmelCase       )

    # Parse args
    lowerCAmelCase__							,lowerCAmelCase__   :int      							=				parser.parse_known_args()
    if not hasattr(UpperCAmelCase    ,			"func"       ):
        parser.print_help()
        exit(1       )
    lowerCAmelCase__   :Optional[int]      							=				parse_unknown_args(UpperCAmelCase       )

    # Run
    lowerCAmelCase__   :str      							=				args.func(UpperCAmelCase    ,			**UpperCAmelCase       )
    service.run()


if __name__ == "__main__":
 main()



