






'''simple docstring'''

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        a_		(    unittest.TestCase       ):

  '''simple docstring'''





  lowercase  	=   MODEL_FOR_CAUSAL_LM_MAPPING
  lowercase  	=   TF_MODEL_FOR_CAUSAL_LM_MAPPING

  @require_torch
  def 						A__		(   self   )						->   Tuple:



        """simple docstring"""






        UpperCamelCase       						=      pipeline(task="""text-generation"""    ,	model="""sshleifer/tiny-ctrl"""    ,	framework="""pt"""   )
        # Using `do_sample=False` to force deterministic output
        UpperCamelCase       						=      text_generator("""This is a test"""    ,	do_sample=_SCREAMING_SNAKE_CASE   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                {
                    """generated_text""": (
                        """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope."""
                        """ oscope. FiliFili@@"""
                    )
                }
            ]    ,	)

        UpperCamelCase       						=      text_generator(["""This is a test""", """This is a second test"""]   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                [
                    {
                        """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@@"""
                        )
                    }
                ],
            ]    ,	)

        UpperCamelCase       						=      text_generator("""This is a test"""    ,	do_sample=_SCREAMING_SNAKE_CASE    ,	num_return_sequences=2    ,	return_tensors=_SCREAMING_SNAKE_CASE   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
                {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
            ]    ,	)
        UpperCamelCase       						=      text_generator.model.config.eos_token_id
        UpperCamelCase       						=      """<pad>"""
        UpperCamelCase       						=      text_generator(
            ["""This is a test""", """This is a second test"""]    ,	do_sample=_SCREAMING_SNAKE_CASE    ,	num_return_sequences=2    ,	batch_size=2    ,	return_tensors=_SCREAMING_SNAKE_CASE    ,	)
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                [
                    {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
                    {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
                ],
                [
                    {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
                    {"""generated_token_ids""": ANY(_SCREAMING_SNAKE_CASE   )},
                ],
            ]    ,	)

  @require_tf
  def 						A__		(   self   )						->   Tuple:



        """simple docstring"""






        UpperCamelCase       						=      pipeline(task="""text-generation"""    ,	model="""sshleifer/tiny-ctrl"""    ,	framework="""tf"""   )

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

        UpperCamelCase       						=      text_generator(["""This is a test""", """This is a second test"""]    ,	do_sample=_SCREAMING_SNAKE_CASE   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                [
                    {
                        """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 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   Optional[Any]:



        """simple docstring"""






        UpperCamelCase       						=      TextGenerationPipeline(model=_SCREAMING_SNAKE_CASE    ,	tokenizer=_SCREAMING_SNAKE_CASE   )
        return text_generator, ["This is a test", "Another test"]

  def 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






        UpperCamelCase       						=      """Hello I believe in"""
        UpperCamelCase       						=      pipeline("""text-generation"""    ,	model="""hf-internal-testing/tiny-random-gpt2"""   )
        UpperCamelCase       						=      text_generator(_SCREAMING_SNAKE_CASE   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[{"""generated_text""": """Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"""}]    ,	)

        UpperCamelCase       						=      text_generator(_SCREAMING_SNAKE_CASE    ,	stop_sequence=""" fe"""   )
        self.assertEqual(_SCREAMING_SNAKE_CASE    ,	[{"""generated_text""": """Hello I believe in fe"""}]   )

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   Any:



        """simple docstring"""






        UpperCamelCase       						=      text_generator.model
        UpperCamelCase       						=      text_generator.tokenizer

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

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

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

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

        UpperCamelCase       						=      text_generator(["""This is great !""", """Something else"""]    ,	num_return_sequences=2    ,	do_sample=_SCREAMING_SNAKE_CASE   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                [{"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}, {"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}],
                [{"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}, {"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}],
            ]    ,	)

        if text_generator.tokenizer.pad_token is not None:
              UpperCamelCase       						=      text_generator(
                  ["""This is great !""", """Something else"""]    ,	num_return_sequences=2    ,	batch_size=2    ,	do_sample=_SCREAMING_SNAKE_CASE   )
              self.assertEqual(
                  _SCREAMING_SNAKE_CASE    ,	[
                      [{"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}, {"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}],
                      [{"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}, {"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}],
                  ]    ,	)

        with self.assertRaises(_SCREAMING_SNAKE_CASE   ):
              UpperCamelCase       						=      text_generator("""test"""    ,	return_full_text=_SCREAMING_SNAKE_CASE    ,	return_text=_SCREAMING_SNAKE_CASE   )
        with self.assertRaises(_SCREAMING_SNAKE_CASE   ):
              UpperCamelCase       						=      text_generator("""test"""    ,	return_full_text=_SCREAMING_SNAKE_CASE    ,	return_tensors=_SCREAMING_SNAKE_CASE   )
        with self.assertRaises(_SCREAMING_SNAKE_CASE   ):
              UpperCamelCase       						=      text_generator("""test"""    ,	return_text=_SCREAMING_SNAKE_CASE    ,	return_tensors=_SCREAMING_SNAKE_CASE   )

        # 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__
        ):
              UpperCamelCase       						=      text_generator(""""""   )
              self.assertEqual(_SCREAMING_SNAKE_CASE    ,	[{"""generated_text""": ANY(_SCREAMING_SNAKE_CASE   )}]   )
        else:
              with self.assertRaises((ValueError, AssertionError)   ):
                    UpperCamelCase       						=      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.
        UpperCamelCase       						=      ["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""]
        if (
            tokenizer.model_max_length < 10000
            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   )

              UpperCamelCase       						=      text_generator("""This is a test""" * 500    ,	handle_long_generation="""hole"""    ,	max_new_tokens=20   )
              # Hole strategy cannot work
              with self.assertRaises(_SCREAMING_SNAKE_CASE   ):
                    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 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






        import torch

        # Classic `model_kwargs`
        UpperCamelCase       						=      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   )
        UpperCamelCase       						=      pipe("""This is a test"""   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                {
                    """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.)
        UpperCamelCase       						=      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   )
        UpperCamelCase       						=      pipe("""This is a test"""   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                {
                    """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
        UpperCamelCase       						=      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   )
        UpperCamelCase       						=      pipe("""This is a test"""   )
        self.assertEqual(
            _SCREAMING_SNAKE_CASE    ,	[
                {
                    """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 						A__		(   self   )						->   List[Any]:



        """simple docstring"""






        import torch

        UpperCamelCase       						=      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 						A__		(   self   )						->   List[str]:



        """simple docstring"""






        import torch

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


  def 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






        UpperCamelCase       						=      """Hello world"""
        UpperCamelCase       						=      pipeline("""text-generation"""    ,	model="""hf-internal-testing/tiny-random-gpt2"""   )
        if text_generator.model.framework == "tf":
              UpperCamelCase       						=      logging.get_logger("""transformers.generation.tf_utils"""   )
        else:
              UpperCamelCase       						=      logging.get_logger("""transformers.generation.utils"""   )
        UpperCamelCase       						=      """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(_SCREAMING_SNAKE_CASE   ) as cl:
              UpperCamelCase       						=      text_generator(_SCREAMING_SNAKE_CASE    ,	max_length=10    ,	max_new_tokens=1   )
        self.assertIn(_SCREAMING_SNAKE_CASE    ,	cl.out   )

        # The user only sets one -> no warning
        with CaptureLogger(_SCREAMING_SNAKE_CASE   ) as cl:
              UpperCamelCase       						=      text_generator(_SCREAMING_SNAKE_CASE    ,	max_new_tokens=1   )
        self.assertNotIn(_SCREAMING_SNAKE_CASE    ,	cl.out   )

        with CaptureLogger(_SCREAMING_SNAKE_CASE   ) as cl:
              UpperCamelCase       						=      text_generator(_SCREAMING_SNAKE_CASE    ,	max_length=10   )
        self.assertNotIn(_SCREAMING_SNAKE_CASE    ,	cl.out   )








'''simple docstring'''

# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import platform

import numpy as np
import psutil
import torch

from accelerate import __version__ as version
from accelerate.commands.config import default_config_file, load_config_from_file

from ..utils import is_npu_available, is_xpu_available







def 					lowercase__				(   __UpperCamelCase=None					)->				Union[str, Any]:
      if subparsers is not None:
            UpperCamelCase       						=      subparsers.add_parser("""env"""					)
      else:
            UpperCamelCase       						=      argparse.ArgumentParser("""Accelerate env command"""					)

      parser.add_argument(
          """--config_file"""	,		default=__UpperCamelCase	,		help="""The config file to use for the default values in the launching script."""					)

      if subparsers is not None:
            parser.set_defaults(func=__UpperCamelCase					)
      return parser







def 					lowercase__				(   __UpperCamelCase					)->				List[str]:
      UpperCamelCase       						=      torch.__version__
      UpperCamelCase       						=      torch.cuda.is_available()
      UpperCamelCase       						=      is_xpu_available()
      UpperCamelCase       						=      is_npu_available()

      UpperCamelCase       						=      """Not found"""
      # Get the default from the config file.
      if args.config_file is not None or os.path.isfile(__UpperCamelCase					):
            UpperCamelCase       						=      load_config_from_file(args.config_file					).to_dict()

      UpperCamelCase       						=      {
          """`Accelerate` version""": version,
          """Platform""": platform.platform(),
          """Python version""": platform.python_version(),
          """Numpy version""": np.__version__,
          """PyTorch version (GPU?)""": F"{pt_version} ({pt_cuda_available})",
          """PyTorch XPU available""": str(__UpperCamelCase					),
          """PyTorch NPU available""": str(__UpperCamelCase					),
          """System RAM""": F"{psutil.virtual_memory().total / 1024 ** 3:.2f} GB",
      }
      if pt_cuda_available:
            UpperCamelCase       						=      torch.cuda.get_device_name()

      print("""\nCopy-and-paste the text below in your GitHub issue\n"""					)
      print("""\n""".join([F"- {prop}: {val}" for prop, val in info.items()]					)					)

      print("""- `Accelerate` default config:""" if args.config_file is None else """- `Accelerate` config passed:"""					)
      UpperCamelCase       						=      (
          """\n""".join([F"\t- {prop}: {val}" for prop, val in accelerate_config.items()]					)
          if isinstance(__UpperCamelCase	,		__UpperCamelCase					)
          else F"\t{accelerate_config}"
      )
      print(__UpperCamelCase					)

      UpperCamelCase       						=      accelerate_config

      return info







def 					lowercase__				(   )->				int:
      UpperCamelCase       						=      env_command_parser()
      UpperCamelCase       						=      parser.parse_args()
      env_command(__UpperCamelCase					)
      return 0


if __name__ == "__main__":
     raise SystemExit(main())
