




import tensorflow as tf

from ...tf_utils import shape_list







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

     def __init__(  self		,     _a		,     _a		,     _a		,     _a		,     _a=1		,     _a=False		,     **_a				)						->							Optional[int]:
           super().__init__(**_a				)

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

           _A							:     List[str]			=						cutoffs + [vocab_size]
           _A							:     Optional[int]			=						[0] + self.cutoffs
           _A							:     Optional[Any]			=						div_val

           _A							:     List[str]			=						self.cutoffs[0]
           _A							:     List[str]			=						len(self.cutoffs				) - 1
           _A							:     Tuple			=						self.shortlist_size + self.n_clusters
           _A							:     List[str]			=						keep_order

           _A							:     Tuple			=						[]
           _A							:     int			=						[]

     def 					a__	(  self		,     _a				)						->							int:
           if self.n_clusters > 0:
                 _A							:     int			=						self.add_weight(
                     shape=(self.n_clusters, self.d_embed)		,     initializer="""zeros"""		,     trainable=_a		,     name="""cluster_weight"""				)
                 _A							:     List[str]			=						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:
                             _A							:     int			=						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				)
                       _A							:     str			=						self.add_weight(
                           shape=(self.vocab_size, self.d_embed)		,     initializer="""zeros"""		,     trainable=_a		,     name=F'''out_layers_._{i}_._weight'''		,     )
                       _A							:     Union[str, Any]			=						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				)				):
                       _A			,   _A							:     List[Any]			=						self.cutoff_ends[i], self.cutoff_ends[i + 1]
                       _A							:     Optional[int]			=						self.d_embed // (self.div_val**i)

                       _A							:     Dict			=						self.add_weight(
                           shape=(d_emb_i, self.d_proj)		,     initializer="""zeros"""		,     trainable=_a		,     name=F'''out_projs_._{i}'''				)
                       self.out_projs.append(_a				)
                       _A							:     Optional[int]			=						self.add_weight(
                           shape=(r_idx - l_idx, d_emb_i)		,     initializer="""zeros"""		,     trainable=_a		,     name=F'''out_layers_._{i}_._weight'''		,     )
                       _A							:     int			=						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 					a__	(  _a		,     _a		,     _a		,     _a=None				)						->							Dict:
           _A							:     Tuple			=						x
           if proj is not None:
                 _A							:     Any			=						tf.einsum("""ibd,ed->ibe"""		,     _a		,     _a				)
           return tf.einsum("""ibd,nd->ibn"""		,     _a		,     _a				) + b

     @staticmethod
     def 					a__	(  _a		,     _a				)						->							Optional[Any]:
           _A							:     Any			=						shape_list(_a				)
           _A							:     List[str]			=						tf.range(lp_size[0]		,     dtype=target.dtype				)
           _A							:     Optional[Any]			=						tf.stack([r, target]		,     1				)
           return tf.gather_nd(_a		,     _a				)



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

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

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

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

           if target is not None:
                 if return_mean:
                       _A							:     Tuple			=						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







import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
  import torch







@is_pipeline_test
class 						lowercase							(   unittest.TestCase		):
     _a							 =						MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
     _a							 =						TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							int:
           _A							:     str			=						TextaTextGenerationPipeline(model=_a		,     tokenizer=_a				)
           return generator, ["Something to write", "Something else"]

     def 					a__	(  self		,     _a		,     _a				)						->							Dict:
           _A							:     Any			=						generator("""Something there"""				)
           self.assertEqual(_a		,     [{"""generated_text""": ANY(_a				)}]				)
           # These are encoder decoder, they don't just append to incoming string
           self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there"""				)				)

           _A							:     List[Any]			=						generator(["""This is great !""", """Something else"""]		,     num_return_sequences=2		,     do_sample=_a				)
           self.assertEqual(
               _a		,     [
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
               ]		,     )

           _A							:     Optional[int]			=						generator(
               ["""This is great !""", """Something else"""]		,     num_return_sequences=2		,     batch_size=2		,     do_sample=_a				)
           self.assertEqual(
               _a		,     [
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
               ]		,     )

           with self.assertRaises(_a				):
                 generator(4				)

     @require_torch
     def 					a__	(  self				)						->							List[str]:
           _A							:     Any			=						pipeline("""text2text-generation"""		,     model="""patrickvonplaten/t5-tiny-random"""		,     framework="""pt"""				)
           # do_sample=False necessary for reproducibility
           _A							:     Dict			=						generator("""Something there"""		,     do_sample=_a				)
           self.assertEqual(_a		,     [{"""generated_text""": """"""}]				)

           _A							:     Any			=						3
           _A							:     Any			=						generator(
               """Something there"""		,     num_return_sequences=_a		,     num_beams=_a		,     )
           _A							:     Optional[int]			=						[
               {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""},
               {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""},
               {"""generated_text""": """"""},
           ]
           self.assertEqual(_a		,     _a				)

           _A							:     Dict			=						generator("""This is a test"""		,     do_sample=_a		,     num_return_sequences=2		,     return_tensors=_a				)
           self.assertEqual(
               _a		,     [
                   {"""generated_token_ids""": ANY(torch.Tensor				)},
                   {"""generated_token_ids""": ANY(torch.Tensor				)},
               ]		,     )
           _A							:     Dict			=						generator.model.config.eos_token_id
           _A							:     List[str]			=						"""<pad>"""
           _A							:     Dict			=						generator(
               ["""This is a test""", """This is a second test"""]		,     do_sample=_a		,     num_return_sequences=2		,     batch_size=2		,     return_tensors=_a		,     )
           self.assertEqual(
               _a		,     [
                   [
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                   ],
                   [
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                   ],
               ]		,     )



     @require_tf
     def 					a__	(  self				)						->							int:
           _A							:     Optional[Any]			=						pipeline("""text2text-generation"""		,     model="""patrickvonplaten/t5-tiny-random"""		,     framework="""tf"""				)
           # do_sample=False necessary for reproducibility
           _A							:     str			=						generator("""Something there"""		,     do_sample=_a				)
           self.assertEqual(_a		,     [{"""generated_text""": """"""}]				)

