


import unittest

from transformers import (
    MODEL_FOR_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
    TextGenerationPipeline,
    logging,
    pipeline,
)
from transformers.testing_utils import (
    CaptureLogger,
    is_pipeline_test,
    require_accelerate,
    require_tf,
    require_torch,
    require_torch_gpu,
    require_torch_or_tf,
)

from .test_pipelines_common import ANY





@is_pipeline_test
@require_torch_or_tf
class   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       _a   							=							MODEL_FOR_CAUSAL_LM_MAPPING
       _a   							=							TF_MODEL_FOR_CAUSAL_LM_MAPPING



       @require_torch
       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Union[str, Any]:
          lowerCamelCase_						:   Union[str, Any]		= pipeline(task="""text-generation"""		,				model="""sshleifer/tiny-ctrl"""		,				framework="""pt"""							)
          # Using `do_sample=False` to force deterministic output
          lowerCamelCase_						:   List[Any]		= text_generator("""This is a test"""		,				do_sample=__a							)
          self.assertEqual(
              __a		,				[
                  {
                      """generated_text""": (
                          """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope."""
                          """ oscope. FiliFili@@"""
                      )
                  }
              ]		,				)

          lowerCamelCase_						:   Tuple		= text_generator(["""This is a test""", """This is a second test"""]							)
          self.assertEqual(
              __a		,				[
                  [
                      {
                          """generated_text""": (
                              """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope."""
                              """ oscope. FiliFili@@"""
                          )
                      }
                  ],
                  [
                      {
                          """generated_text""": (
                              """This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy"""
                              """ oscope. oscope. FiliFili@@"""
                          )
                      }
                  ],
              ]		,				)

          lowerCamelCase_						:   Dict		= text_generator("""This is a test"""		,				do_sample=__a		,				num_return_sequences=2		,				return_tensors=__a							)
          self.assertEqual(
              __a		,				[
                  {"""generated_token_ids""": ANY(__a							)},
                  {"""generated_token_ids""": ANY(__a							)},
              ]		,				)
          lowerCamelCase_						:   Union[str, Any]		= text_generator.model.config.eos_token_id
          lowerCamelCase_						:   Tuple		= """<pad>"""
          lowerCamelCase_						:   str		= text_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(__a							)},
                      {"""generated_token_ids""": ANY(__a							)},
                  ],
                  [
                      {"""generated_token_ids""": ANY(__a							)},
                      {"""generated_token_ids""": ANY(__a							)},
                  ],
              ]		,				)



       @require_tf
       def 				_lowerCAmelCase							(						self				:				Dict							)					->Union[str, Any]:
          lowerCamelCase_						:   int		= pipeline(task="""text-generation"""		,				model="""sshleifer/tiny-ctrl"""		,				framework="""tf"""							)

          # Using `do_sample=False` to force deterministic output
          lowerCamelCase_						:   Any		= text_generator("""This is a test"""		,				do_sample=__a							)
          self.assertEqual(
              __a		,				[
                  {
                      """generated_text""": (
                          """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵"""
                          """ please,"""
                      )
                  }
              ]		,				)

          lowerCamelCase_						:   int		= text_generator(["""This is a test""", """This is a second test"""]		,				do_sample=__a							)
          self.assertEqual(
              __a		,				[
                  [
                      {
                          """generated_text""": (
                              """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵"""
                              """ please,"""
                          )
                      }
                  ],
                  [
                      {
                          """generated_text""": (
                              """This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes"""
                              """ Cannes 閲閲Cannes Cannes Cannes 攵 please,"""
                          )
                      }
                  ],
              ]		,				)



       def 				_lowerCAmelCase							(						self				:				Any		,				__a				:				int		,				__a				:				Any		,				__a				:				Optional[int]							)					->Dict:
          lowerCamelCase_						:   Any		= TextGenerationPipeline(model=__a		,				tokenizer=__a							)
          return text_generator, ["This is a test", "Another test"]



       def 				_lowerCAmelCase							(						self				:				str							)					->Tuple:
          lowerCamelCase_						:   Optional[Any]		= """Hello I believe in"""
          lowerCamelCase_						:   str		= pipeline("""text-generation"""		,				model="""hf-internal-testing/tiny-random-gpt2"""							)
          lowerCamelCase_						:   int		= text_generator(__a							)
          self.assertEqual(
              __a		,				[{"""generated_text""": """Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"""}]		,				)

          lowerCamelCase_						:   Optional[Any]		= text_generator(__a		,				stop_sequence=""" fe"""							)
          self.assertEqual(__a		,				[{"""generated_text""": """Hello I believe in fe"""}]							)



       def 				_lowerCAmelCase							(						self				:				Optional[Any]		,				__a				:				Any		,				__a				:				Union[str, Any]							)					->Optional[int]:
          lowerCamelCase_						:   Union[str, Any]		= text_generator.model
          lowerCamelCase_						:   Any		= text_generator.tokenizer

          lowerCamelCase_						:   Any		= text_generator("""This is a test"""							)
          self.assertEqual(__a		,				[{"""generated_text""": ANY(__a							)}]							)
          self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test"""							)							)

          lowerCamelCase_						:   int		= text_generator("""This is a test"""		,				return_full_text=__a							)
          self.assertEqual(__a		,				[{"""generated_text""": ANY(__a							)}]							)
          self.assertNotIn("""This is a test"""		,				outputs[0]["""generated_text"""]							)

          lowerCamelCase_						:   Any		= pipeline(task="""text-generation"""		,				model=__a		,				tokenizer=__a		,				return_full_text=__a							)
          lowerCamelCase_						:   List[Any]		= text_generator("""This is a test"""							)
          self.assertEqual(__a		,				[{"""generated_text""": ANY(__a							)}]							)
          self.assertNotIn("""This is a test"""		,				outputs[0]["""generated_text"""]							)

          lowerCamelCase_						:   Dict		= text_generator("""This is a test"""		,				return_full_text=__a							)
          self.assertEqual(__a		,				[{"""generated_text""": ANY(__a							)}]							)
          self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test"""							)							)

          lowerCamelCase_						:   Any		= text_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							)}],
              ]		,				)

          if text_generator.tokenizer.pad_token is not None:
             lowerCamelCase_						:   Optional[Any]		= text_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							):
             lowerCamelCase_						:   int		= text_generator("""test"""		,				return_full_text=__a		,				return_text=__a							)
          with self.assertRaises(__a							):
             lowerCamelCase_						:   Optional[int]		= text_generator("""test"""		,				return_full_text=__a		,				return_tensors=__a							)
          with self.assertRaises(__a							):
             lowerCamelCase_						:   Union[str, Any]		= text_generator("""test"""		,				return_text=__a		,				return_tensors=__a							)

          # Empty prompt is slighly special
          # it requires BOS token to exist.
          # Special case for Pegasus which will always append EOS so will
          # work even without BOS.
          if (
              text_generator.tokenizer.bos_token_id is not None
              or "Pegasus" in tokenizer.__class__.__name__
              or "Git" in model.__class__.__name__
          ):
             lowerCamelCase_						:   List[str]		= text_generator(""""""							)
             self.assertEqual(__a		,				[{"""generated_text""": ANY(__a							)}]							)
          else:
             with self.assertRaises((ValueError, AssertionError)							):
                lowerCamelCase_						:   Optional[Any]		= text_generator(""""""							)

          if text_generator.framework == "tf":
             # TF generation does not support max_new_tokens, and it's impossible
             # to control long generation with only max_length without
             # fancy calculation, dismissing tests for now.
             return
          # We don't care about infinite range models.
          # They already work.
          # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly.
          lowerCamelCase_						:   Optional[Any]		= ["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""]
          if (
              tokenizer.model_max_length < 10_000
              and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS
          ):
             # Handling of large generations
             with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError)							):
                text_generator("""This is a test""" * 500		,				max_new_tokens=20							)

             lowerCamelCase_						:   Optional[Any]		= text_generator("""This is a test""" * 500		,				handle_long_generation="""hole"""		,				max_new_tokens=20							)
             # Hole strategy cannot work
             with self.assertRaises(__a							):
                text_generator(
                    """This is a test""" * 500		,				handle_long_generation="""hole"""		,				max_new_tokens=tokenizer.model_max_length + 10		,				)



       @require_torch
       @require_accelerate
       @require_torch_gpu
       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Optional[int]:
          import torch

          # Classic `model_kwargs`
          lowerCamelCase_						:   List[Any]		= pipeline(
              model="""hf-internal-testing/tiny-random-bloom"""		,				model_kwargs={"""device_map""": """auto""", """torch_dtype""": torch.bfloataa}		,				)
          self.assertEqual(pipe.model.device		,				torch.device(0							)							)
          self.assertEqual(pipe.model.lm_head.weight.dtype		,				torch.bfloataa							)
          lowerCamelCase_						:   str		= pipe("""This is a test"""							)
          self.assertEqual(
              __a		,				[
                  {
                      """generated_text""": (
                          """This is a test test test test test test test test test test test test test test test test"""
                          """ test"""
                      )
                  }
              ]		,				)

          # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.)
          lowerCamelCase_						:   str		= pipeline(model="""hf-internal-testing/tiny-random-bloom"""		,				device_map="""auto"""		,				torch_dtype=torch.bfloataa							)
          self.assertEqual(pipe.model.device		,				torch.device(0							)							)
          self.assertEqual(pipe.model.lm_head.weight.dtype		,				torch.bfloataa							)
          lowerCamelCase_						:   int		= pipe("""This is a test"""							)
          self.assertEqual(
              __a		,				[
                  {
                      """generated_text""": (
                          """This is a test test test test test test test test test test test test test test test test"""
                          """ test"""
                      )
                  }
              ]		,				)

          # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602
          lowerCamelCase_						:   List[str]		= pipeline(model="""hf-internal-testing/tiny-random-bloom"""		,				device_map="""auto"""							)
          self.assertEqual(pipe.model.device		,				torch.device(0							)							)
          self.assertEqual(pipe.model.lm_head.weight.dtype		,				torch.floataa							)
          lowerCamelCase_						:   Optional[int]		= pipe("""This is a test"""							)
          self.assertEqual(
              __a		,				[
                  {
                      """generated_text""": (
                          """This is a test test test test test test test test test test test test test test test test"""
                          """ test"""
                      )
                  }
              ]		,				)



       @require_torch
       @require_torch_gpu
       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Dict:
          import torch

          lowerCamelCase_						:   Optional[int]		= pipeline(model="""hf-internal-testing/tiny-random-bloom"""		,				device=0		,				torch_dtype=torch.floataa							)
          pipe("""This is a test"""							)



       @require_torch
       @require_accelerate
       @require_torch_gpu
       def 				_lowerCAmelCase							(						self				:				str							)					->List[Any]:
          import torch

          lowerCamelCase_						:   Any		= pipeline(model="""hf-internal-testing/tiny-random-bloom"""		,				device_map="""auto"""		,				torch_dtype=torch.floataa							)
          pipe("""This is a test"""		,				do_sample=__a		,				top_p=0.5							)




       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Dict:
          lowerCamelCase_						:   Optional[int]		= """Hello world"""
          lowerCamelCase_						:   int		= pipeline("""text-generation"""		,				model="""hf-internal-testing/tiny-random-gpt2"""							)
          if text_generator.model.framework == "tf":
             lowerCamelCase_						:   Optional[Any]		= logging.get_logger("""transformers.generation.tf_utils"""							)
          else:
             lowerCamelCase_						:   List[str]		= logging.get_logger("""transformers.generation.utils"""							)
          lowerCamelCase_						:   Any		= """Both `max_new_tokens`"""  # The beggining of the message to be checked in this test

          # Both are set by the user -> log warning
          with CaptureLogger(__a							) as cl:
             lowerCamelCase_						:   Tuple		= text_generator(__a		,				max_length=10		,				max_new_tokens=1							)
          self.assertIn(__a		,				cl.out							)

          # The user only sets one -> no warning
          with CaptureLogger(__a							) as cl:
             lowerCamelCase_						:   List[Any]		= text_generator(__a		,				max_new_tokens=1							)
          self.assertNotIn(__a		,				cl.out							)

          with CaptureLogger(__a							) as cl:
             lowerCamelCase_						:   Any		= text_generator(__a		,				max_length=10							)
          self.assertNotIn(__a		,				cl.out							)









class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       def __init__(						self				:				List[Any]							)					->Tuple:
          lowerCamelCase_						:   Optional[Any]		= """"""
          lowerCamelCase_						:   Dict		= """"""
          lowerCamelCase_						:   Optional[Any]		= []



       def 				_lowerCAmelCase							(						self				:				Optional[Any]		,				__a				:				int		,				__a				:				int							)					->int:
          if m == -1:
             return n + 1
          elif n == -1:
             return m + 1
          elif self.dp[m][n] > -1:
             return self.dp[m][n]
          else:
             if self.worda[m] == self.worda[n]:
                lowerCamelCase_						:   Optional[Any]		= self.__min_dist_top_down_dp(m - 1		,				n - 1							)
             else:
                lowerCamelCase_						:   List[Any]		= self.__min_dist_top_down_dp(__a		,				n - 1							)
                lowerCamelCase_						:   Union[str, Any]		= self.__min_dist_top_down_dp(m - 1		,				__a							)
                lowerCamelCase_						:   Any		= self.__min_dist_top_down_dp(m - 1		,				n - 1							)
                lowerCamelCase_						:   Tuple		= 1 + min(__a		,				__a		,				__a							)

             return self.dp[m][n]



       def 				_lowerCAmelCase							(						self				:				Any		,				__a				:				str		,				__a				:				str							)					->int:
          lowerCamelCase_						:   Dict		= worda
          lowerCamelCase_						:   str		= worda
          lowerCamelCase_						:   Union[str, Any]		= [[-1 for _ in range(len(__a							)							)] for _ in range(len(__a							)							)]

          return self.__min_dist_top_down_dp(len(__a							) - 1		,				len(__a							) - 1							)




       def 				_lowerCAmelCase							(						self				:				int		,				__a				:				str		,				__a				:				str							)					->int:
          lowerCamelCase_						:   Tuple		= worda
          lowerCamelCase_						:   Optional[Any]		= worda
          lowerCamelCase_						:   Tuple		= len(__a							)
          lowerCamelCase_						:   Optional[Any]		= len(__a							)
          lowerCamelCase_						:   List[Any]		= [[0 for _ in range(n + 1							)] for _ in range(m + 1							)]

          for i in range(m + 1							):
             for j in range(n + 1							):
                if i == 0:  # first string is empty
                   lowerCamelCase_						:   Optional[Any]		= j
                elif j == 0:  # second string is empty
                   lowerCamelCase_						:   Any		= i
                elif worda[i - 1] == worda[j - 1]:  # last characters are equal
                   lowerCamelCase_						:   Tuple		= self.dp[i - 1][j - 1]
                else:
                   lowerCamelCase_						:   Dict		= self.dp[i][j - 1]
                   lowerCamelCase_						:   Union[str, Any]		= self.dp[i - 1][j]
                   lowerCamelCase_						:   Any		= self.dp[i - 1][j - 1]
                   lowerCamelCase_						:   Optional[int]		= 1 + min(__a		,				__a		,				__a							)
          return self.dp[m][n]


if __name__ == "__main__":
      snake_case__				:	Optional[Any]					       =					EditDistance()

      print('****************** Testing Edit Distance DP Algorithm ******************')
      print()

      snake_case__				:	Union[str, Any]					       =					input('Enter the first string: ').strip()
      snake_case__				:	List[Any]					       =					input('Enter the second string: ').strip()

      print()
      print(F'The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}')
      print(F'The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}')
      print()
      print('*************** End of Testing Edit Distance DP Algorithm ***************')





