




from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       AutoTokenizer,
       TFAutoModelForCausalLM,
       TFAutoModelForSeqaSeqLM,
       TFAutoModelForSpeechSeqaSeq,
       TFAutoModelForVisionaSeq,
       TFBartForConditionalGeneration,
       TFLogitsProcessorList,
       TFMinLengthLogitsProcessor,
       tf_top_k_top_p_filtering,
   )

if is_tensorflow_text_available():
   import tensorflow_text as text







@require_tf
class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							int:
           _A							:  Any    =		tf.convert_to_tensor(
               [
                   [
                       8.2220991,  # 3rd highest value; idx. 0
                       -0.5620044,
                       5.23229752,
                       4.0386393,
                       -6.8798378,
                       -0.54785802,
                       -3.2012153,
                       2.92777176,
                       1.88171953,
                       7.35341276,  # 5th highest value; idx. 9
                       8.43207833,  # 2nd highest value; idx. 10
                       -9.85711836,
                       -5.96209236,
                       -1.13039161,
                       -7.1115294,
                       -0.8369633,
                       -5.3186408,
                       7.06427407,
                       0.81369344,
                       -0.82023817,
                       -5.9179796,
                       0.58813443,
                       -6.99778438,
                       4.71551189,
                       -0.18771637,
                       7.44020759,  # 4th highest value; idx. 25
                       9.38450987,  # 1st highest value; idx. 26
                       2.12662941,
                       -9.32562038,
                       2.35652522,
                   ],  # cummulative prob of 5 highest values <= 0.6
                   [
                       0.58425518,
                       4.53139238,
                       -5.57510464,
                       -6.28030699,
                       -7.19529503,
                       -4.02122551,
                       1.39337037,
                       -6.06707057,
                       1.59480517,
                       -9.643119,
                       0.03907799,
                       0.67231762,
                       -8.88206726,
                       6.27115922,  # 4th highest value; idx. 13
                       2.28520723,
                       4.82767506,
                       4.30421368,
                       8.8275313,  # 2nd highest value; idx. 17
                       5.44029958,  # 5th highest value; idx. 18
                       -4.4735794,
                       7.38579536,  # 3rd highest value; idx. 20
                       -2.91051663,
                       2.61946077,
                       -2.5674762,
                       -9.48959302,
                       -4.02922645,
                       -1.35416918,
                       9.67702323,  # 1st highest value; idx. 27
                       -5.89478553,
                       1.85370467,
                   ],  # cummulative prob of 5 highest values <= 0.6
               ]	,  dtype=tf.floataa	,  )

           _A							:  Optional[int]    =		tf.convert_to_tensor(
               [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]]	,  dtype=tf.intaa	,  )  # expected non filtered idx as noted above

           _A							:  int    =		tf.convert_to_tensor(
               [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023]	,  dtype=tf.floataa	,  )  # expected non filtered values as noted above

           _A							:  Union[str, Any]    =		tf_top_k_top_p_filtering(_a	,  top_k=10	,  top_p=0.6	,  min_tokens_to_keep=4       )

           _A							:  Tuple    =		output[output != -float("""inf"""       )]
           _A							:  List[Any]    =		tf.cast(
               tf.where(tf.not_equal(_a	,  tf.constant(-float("""inf"""       )	,  dtype=tf.floataa       )       )       )	,  dtype=tf.intaa	,  )

           tf.debugging.assert_near(_a	,  _a	,  rtol=1e-12       )
           tf.debugging.assert_equal(_a	,  _a       )







@require_tf
class 		lowercase							(     unittest.TestCase,UpperCamelCase__	):
     # setting framework_dependent_parameters needs to be gated, just like its contents' imports
     if is_tf_available():
          _a											=		{
              "AutoModelForCausalLM": TFAutoModelForCausalLM,
              "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq,
              "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM,
              "AutoModelForVision2Seq": TFAutoModelForVisionaSeq,
              "LogitsProcessorList": TFLogitsProcessorList,
              "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor,
              "create_tensor_fn": tf.convert_to_tensor,
              "floats_tensor": floats_tensor,
              "return_tensors": "tf",
          }

     @slow
     def       a__			(						self       )		->							List[Any]:
           # TF-only test: tf.saved_model export
           _A							:  Optional[Any]    =		TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""       )
           _A							:  int    =		2
           _A							:  int    =		2







           class 		lowercase							(     tf.Module	):

                def __init__(						self	,  _a       )		->							List[str]:
                      super(_a	,  self       ).__init__()
                      _A							:  int    =		model



                @tf.function(
                    input_signature=(
                        tf.TensorSpec((None, input_length)	,  tf.intaa	,  name="""input_ids"""       ),
                        tf.TensorSpec((None, input_length)	,  tf.intaa	,  name="""attention_mask"""       ),
                    )	,  jit_compile=_a	,  )
                def       a__			(						self	,  _a	,  _a       )		->							Any:
                      _A							:  Any    =		self.model.generate(
                          input_ids=_a	,  attention_mask=_a	,  max_new_tokens=_a	,  return_dict_in_generate=_a	,  )
                      return {"sequences": outputs["sequences"]}


           _A							:  int    =		[[2, 0], [102, 103]]
           _A							:  Any    =		[[1, 0], [1, 1]]
           _A							:  Optional[int]    =		DummyModel(model=_a       )
           with tempfile.TemporaryDirectory() as tmp_dir:
                 tf.saved_model.save(_a	,  _a	,  signatures={"""serving_default""": dummy_model.serving}       )
                 _A							:  Union[str, Any]    =		tf.saved_model.load(_a       ).signatures["""serving_default"""]
                 for batch_size in range(1	,  len(_a       ) + 1       ):
                       _A							:  List[Any]    =		{
                           """input_ids""": tf.constant(dummy_input_ids[:batch_size]       ),
                           """attention_mask""": tf.constant(dummy_attention_masks[:batch_size]       ),
                       }
                       _A							:  List[Any]    =		serving_func(**_a       )["""sequences"""]
                       _A							:  List[Any]    =		test_model.generate(**_a	,  max_new_tokens=_a       )
                       tf.debugging.assert_equal(_a	,  _a       )

     @slow
     def       a__			(						self       )		->							str:
           # TF-only test: tf.saved_model export
           _A							:  Tuple    =		TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""       )
           _A							:  Union[str, Any]    =		1
           _A							:  str    =		2







           class 		lowercase							(     tf.Module	):

                def __init__(						self	,  _a       )		->							str:
                      super(_a	,  self       ).__init__()
                      _A							:  Optional[Any]    =		model



                @tf.function(
                    input_signature=(
                        tf.TensorSpec((batch_size, None)	,  tf.intaa	,  name="""input_ids"""       ),
                        tf.TensorSpec((batch_size, None)	,  tf.intaa	,  name="""attention_mask"""       ),
                    )	,  jit_compile=_a	,  )
                def       a__			(						self	,  _a	,  _a       )		->							Any:
                      _A							:  Tuple    =		self.model.generate(
                          input_ids=_a	,  attention_mask=_a	,  max_new_tokens=_a	,  return_dict_in_generate=_a	,  )
                      return {"sequences": outputs["sequences"]}


           _A							:  Optional[Any]    =		[[2], [102, 103]]
           _A							:  List[Any]    =		[[1], [1, 1]]
           _A							:  List[Any]    =		DummyModel(model=_a       )
           with tempfile.TemporaryDirectory() as tmp_dir:
                 tf.saved_model.save(_a	,  _a	,  signatures={"""serving_default""": dummy_model.serving}       )
                 _A							:  Optional[int]    =		tf.saved_model.load(_a       ).signatures["""serving_default"""]
                 for input_row in range(len(_a       )       ):
                       _A							:  List[str]    =		{
                           """input_ids""": tf.constant([dummy_input_ids[input_row]]       ),
                           """attention_mask""": tf.constant([dummy_attention_masks[input_row]]       ),
                       }
                       _A							:  int    =		serving_func(**_a       )["""sequences"""]
                       _A							:  str    =		test_model.generate(**_a	,  max_new_tokens=_a       )
                       tf.debugging.assert_equal(_a	,  _a       )

     @slow
     @require_tensorflow_text
     def       a__			(						self       )		->							Tuple:
           # TF-only test: tf.saved_model export
           with tempfile.TemporaryDirectory() as tmp_dir:
                 # file needed to load the TF tokenizer
                 hf_hub_download(repo_id="""google/flan-t5-small"""	,  filename="""spiece.model"""	,  local_dir=_a       )







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

                      def __init__(						self       )		->							Tuple:
                            super().__init__()
                            _A							:  str    =		text.SentencepieceTokenizer(
                                model=tf.io.gfile.GFile(os.path.join(_a	,  """spiece.model"""       )	,  """rb"""       ).read()       )
                            _A							:  str    =		TFAutoModelForSeqaSeqLM.from_pretrained("""hf-internal-testing/tiny-random-t5"""       )



                      def       a__			(						self	,  _a	,  *_a	,  **_a       )		->							str:
                            _A							:  Any    =		self.tokenizer.tokenize(_a       )
                            _A							:  Any    =		text.pad_model_inputs(
                                _a	,  max_seq_length=64	,  pad_value=self.model.config.pad_token_id       )
                            _A							:  Any    =		self.model.generate(input_ids=_a	,  attention_mask=_a       )
                            return self.tokenizer.detokenize(_a       )


                 _A							:  Optional[int]    =		CompleteSentenceTransformer()
                 _A							:  Dict    =		tf.keras.layers.Input(shape=(1,)	,  dtype=tf.string	,  name="""inputs"""       )
                 _A							:  List[str]    =		complete_model(_a       )
                 _A							:  List[Any]    =		tf.keras.Model(_a	,  _a       )
                 keras_model.save(_a       )

     def       a__			(						self       )		->							Optional[Any]:
           # Has PT equivalent: this test relies on random sampling
           _A							:  Optional[int]    =		{
               """do_sample""": True,
               """num_beams""": 1,
               """top_p""": 0.7,
               """top_k""": 10,
               """temperature""": 0.7,
           }
           _A							:  Any    =		14

           _A							:  Optional[int]    =		AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""       )
           _A							:  str    =		"""Hello, my dog is cute and"""
           _A							:  Optional[Any]    =		tokenizer(_a	,  return_tensors="""tf"""       )
           _A							:  List[str]    =		TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""       )

           _A							:  Optional[Any]    =		638
           # forces the generation to happen on CPU, to avoid GPU-related quirks
           with tf.device(""":/CPU:0"""       ):
                 tf.random.set_seed(0       )
                 _A							:  Dict    =		model.generate(**_a	,  eos_token_id=_a	,  **_a       )
           self.assertTrue(expectation == len(generated_tokens[0]       )       )

           _A							:  int    =		[638, 198]
           with tf.device(""":/CPU:0"""       ):
                 tf.random.set_seed(0       )
                 _A							:  int    =		model.generate(**_a	,  eos_token_id=_a	,  **_a       )
           self.assertTrue(expectation == len(generated_tokens[0]       )       )



     def       a__			(						self       )		->							Optional[Any]:
           # Has PT equivalent: ample use of framework-specific code
           _A							:  Any    =		AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-bart"""       )
           _A							:  Tuple    =		"""Hugging Face is a technology company based in New York and Paris."""
           _A							:  Union[str, Any]    =		bart_tokenizer(_a	,  return_tensors="""tf"""       ).input_ids
           _A							:  Dict    =		TFBartForConditionalGeneration.from_pretrained("""hf-internal-testing/tiny-random-bart"""       )
           _A							:  Optional[Any]    =		bart_model.generate(_a       ).numpy()







           class 		lowercase							(     UpperCamelCase__	):

                def       a__			(						self	,  _a	,  _a=None	,  **_a       )		->							Dict:
                      return super().call(_a	,  **_a       )


           _A							:  Union[str, Any]    =		FakeBart.from_pretrained("""hf-internal-testing/tiny-random-bart"""       )
           _A							:  List[str]    =		bart_model.generate(_a	,  foo="""bar"""       ).numpy()
           self.assertTrue(np.array_equal(_a	,  _a       )       )







           class 		lowercase							(     bart_model.model.encoder.__class__	):

                def       a__			(						self	,  _a	,  **_a       )		->							Union[str, Any]:
                      return super().call(_a	,  **_a       )


           _A							:  Optional[int]    =		FakeEncoder(bart_model.config	,  bart_model.model.shared       )
           _A							:  Optional[int]    =		fake_encoder

           # Normal generation still works (the output will be different because the encoder weights are different)
           _A							:  Optional[int]    =		bart_model.generate(_a       ).numpy()
           with self.assertRaises(_a       ):
                 # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
                 bart_model.generate(_a	,  foo="""bar"""       )







def        lowerCAmelCase_			(  snake_case_,snake_case_  ):
      _A							:  Union[str, Any]    =		""""""
      for i in table:
            res += inp[i - 1]
      return res







def        lowerCAmelCase_			(  snake_case_  ):
      return data[1:] + data[0]







def        lowerCAmelCase_			(  snake_case_,snake_case_  ):
      _A							:  Dict    =		""""""
      for i in range(len(snake_case_  )  ):
            if a[i] == b[i]:
                  res += "0"
            else:
                  res += "1"
      return res







def        lowerCAmelCase_			(  snake_case_,snake_case_  ):
      _A							:  int    =		int("""0b""" + data[0] + data[-1],2  )
      _A							:  Any    =		int("""0b""" + data[1:3],2  )
      return bin(s[row][col]  )[2:]







def        lowerCAmelCase_			(  snake_case_,snake_case_,snake_case_,snake_case_,snake_case_  ):
      _A							:  List[str]    =		message[:4]
      _A							:  List[Any]    =		message[4:]
      _A							:  Union[str, Any]    =		apply_table(snake_case_,snake_case_  )
      _A							:  List[Any]    =		xor(snake_case_,snake_case_  )
      _A							:  Optional[Any]    =		apply_sbox(snake_case_,temp[:4]  )  # noqa: E741
      _A							:  List[Any]    =		apply_sbox(snake_case_,temp[4:]  )
      _A							:  int    =		"""0""" * (2 - len(snake_case_  )) + l  # noqa: E741
      _A							:  Union[str, Any]    =		"""0""" * (2 - len(snake_case_  )) + r
      _A							:  List[Any]    =		apply_table(l + r,snake_case_  )
      _A							:  Any    =		xor(snake_case_,snake_case_  )
      return temp + right


if __name__ == "__main__":
  _snake_case            = input("Enter 10 bit key: ")
  _snake_case            = input("Enter 8 bit message: ")

  _snake_case            = [6, 3, 7, 4, 8, 5, 10, 9]
  _snake_case            = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
  _snake_case            = [2, 4, 3, 1]
  _snake_case            = [2, 6, 3, 1, 4, 8, 5, 7]
  _snake_case            = [4, 1, 3, 5, 7, 2, 8, 6]
  _snake_case            = [4, 1, 2, 3, 2, 3, 4, 1]
  _snake_case            = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
  _snake_case            = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

  # key generation
  _snake_case            = apply_table(key, paa_table)
  _snake_case            = temp[:5]
  _snake_case            = temp[5:]
  _snake_case            = left_shift(left)
  _snake_case            = left_shift(right)
  _snake_case            = apply_table(left + right, pa_table)
  _snake_case            = left_shift(left)
  _snake_case            = left_shift(right)
  _snake_case            = left_shift(left)
  _snake_case            = left_shift(right)
  _snake_case            = apply_table(left + right, pa_table)

  # encryption
  _snake_case            = apply_table(message, IP)
  _snake_case            = function(expansion, sa, sa, keya, temp)
  _snake_case            = temp[4:] + temp[:4]
  _snake_case            = function(expansion, sa, sa, keya, temp)
  _snake_case            = apply_table(temp, IP_inv)
  print("Cipher text is:", CT)

  # decryption
  _snake_case            = apply_table(CT, IP)
  _snake_case            = function(expansion, sa, sa, keya, temp)
  _snake_case            = temp[4:] + temp[:4]
  _snake_case            = function(expansion, sa, sa, keya, temp)
  _snake_case            = apply_table(temp, IP_inv)
  print("Plain text after decypting is:", PT)

