


'''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      UpperCAmelCase      (		lowerCamelCase_  :List[str]=None ):







		'''simple docstring'''







		if subparsers is not None:
				snake_case_		:							List[Any]			       =						subparsers.add_parser("""env""" )
		else:
				snake_case_		:							Optional[int]			       =						argparse.ArgumentParser("""Accelerate env command""" )

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

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







def      UpperCAmelCase      (		lowerCamelCase_  :Tuple ):







		'''simple docstring'''
		snake_case_		:							str			       =						torch.__version__
		snake_case_		:							Optional[Any]			       =						torch.cuda.is_available()
		snake_case_		:							Tuple			       =						is_xpu_available()
		snake_case_		:							Tuple			       =						is_npu_available()

		snake_case_		:							Optional[int]			       =						"""Not found"""
		# Get the default from the config file.
		if args.config_file is not None or os.path.isfile(lowerCamelCase_ ):
				snake_case_		:							List[str]			       =						load_config_from_file(args.config_file ).to_dict()

		snake_case_		:							int			       =						{
		    """`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(lowerCamelCase_ ),
		    """PyTorch NPU available""": str(lowerCamelCase_ ),
		    """System RAM""": F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''',
		}
		if pt_cuda_available:
				snake_case_		:							Optional[Any]			       =						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:""" )
		snake_case_		:							Dict			       =						(
		    """\n""".join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] )
		    if isinstance(lowerCamelCase_				,      lowerCamelCase_ )
		    else F'''\t{accelerate_config}'''
		)
		print(lowerCamelCase_ )

		snake_case_		:							List[Any]			       =						accelerate_config

		return info







def      UpperCAmelCase      (		):







		'''simple docstring'''
		snake_case_		:							Union[str, Any]			       =						env_command_parser()
		snake_case_		:							List[Any]			       =						parser.parse_args()
		env_command(lowerCamelCase_ )
		return 0


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



'''simple docstring'''






import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin
class 						__UpperCamelCase	(						lowercase__						,  unittest.TestCase							):
							lowercase			:		Dict   		=  TransfoXLTokenizer
							lowercase			:		Optional[Any]   		=  False
							lowercase			:		Dict   		=  False





							def 		a__   (  self		:Union[str, Any]			):
									super().setUp()

									snake_case_		:							Optional[int]			       =						[
									    """<unk>""",
									    """[CLS]""",
									    """[SEP]""",
									    """want""",
									    """unwanted""",
									    """wa""",
									    """un""",
									    """running""",
									    """,""",
									    """low""",
									    """l""",
									]
									snake_case_		:							Optional[int]			       =						os.path.join(self.tmpdirname  ,VOCAB_FILES_NAMES["""vocab_file"""]			)
									with open(self.vocab_file  ,"""w"""  ,encoding="""utf-8"""			) as vocab_writer:
											vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]			)			)





							def 		a__   (  self		:List[Any]  ,**_UpperCamelCase		:Optional[Any]			):
									snake_case_		:							Tuple			       =						True
									return TransfoXLTokenizer.from_pretrained(self.tmpdirname  ,**_UpperCamelCase			)





							def 		a__   (  self		:Tuple  ,_UpperCamelCase		:Union[str, Any]			):
									snake_case_		:							Any			       =						"""<unk> UNwanted , running"""
									snake_case_		:							Optional[int]			       =						"""<unk> unwanted, running"""
									return input_text, output_text





							def 		a__   (  self		:Dict			):
									snake_case_		:							Dict			       =						TransfoXLTokenizer(vocab_file=self.vocab_file  ,lower_case=_UpperCamelCase			)

									snake_case_		:							Dict			       =						tokenizer.tokenize("""<unk> UNwanted , running"""			)
									self.assertListEqual(_UpperCamelCase  ,["""<unk>""", """unwanted""", """,""", """running"""]			)

									self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase			)  ,[0, 4, 8, 7]			)





							def 		a__   (  self		:Optional[Any]			):
									snake_case_		:							Dict			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """			)  ,["""hello""", """!""", """how""", """are""", """you""", """?"""]			)





							def 		a__   (  self		:Any			):
									snake_case_		:							List[Any]			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """			)  ,["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]			)





							def 		a__   (  self		:List[str]			):
									snake_case_		:							str			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)
									snake_case_		:							List[str]			       =						"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
									snake_case_		:							Optional[int]			       =						[
									    """Hello""",
									    """(""",
									    """bracket""",
									    """)""",
									    """and""",
									    """side""",
									    """@-@""",
									    """scrolled""",
									    """[""",
									    """and""",
									    """]""",
									    """Henry""",
									    """'s""",
									    """$""",
									    """5""",
									    """@,@""",
									    """000""",
									    """with""",
									    """3""",
									    """@.@""",
									    """34""",
									    """m""",
									    """.""",
									    """What""",
									    """'s""",
									    """up""",
									    """!""",
									    """?""",
									]

									self.assertListEqual(tokenizer.tokenize(_UpperCamelCase			)  ,_UpperCamelCase			)

									self.assertEqual(tokenizer.convert_tokens_to_string(_UpperCamelCase			)  ,_UpperCamelCase			)





							def 		a__   (  self		:Dict			):
									snake_case_		:							Union[str, Any]			       =						self.get_tokenizer()
									snake_case_		:							Dict			       =						len(_UpperCamelCase			)

									tokenizer.add_tokens(["""new1""", """new2"""]			)
									tokenizer.move_added_token("""new1"""  ,1			)

									# Check that moved token is not copied (duplicate)
									self.assertEqual(len(_UpperCamelCase			)  ,original_len + 2			)
									# Check that token is moved to specified id
									self.assertEqual(tokenizer.encode("""new1"""			)  ,[1]			)
									self.assertEqual(tokenizer.decode([1]			)  ,"""new1"""			)