


from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
							from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


a					  =     logging.get_logger(__name__)

a					  =     {
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}







class        UpperCamelCase__   (	__magic_name__   ):
					__SCREAMING_SNAKE_CASE       :  Union[str, Any]						=   'bloom'
					__SCREAMING_SNAKE_CASE       :  Any						=   ['past_key_values']
					__SCREAMING_SNAKE_CASE       :  List[Any]						=   {
					    'num_hidden_layers': 'n_layer',
					    'num_attention_heads': 'n_head',
					}

					def __init__(     self       :   Any							,					UpperCamelCase__       :   Dict=250_880							,					UpperCamelCase__       :   str=64							,					UpperCamelCase__       :   str=2							,					UpperCamelCase__       :   Union[str, Any]=8							,					UpperCamelCase__       :   Optional[int]=1e-5							,					UpperCamelCase__       :   str=0.02							,					UpperCamelCase__       :   Any=True							,					UpperCamelCase__       :   Dict=1							,					UpperCamelCase__       :   str=2							,					UpperCamelCase__       :   List[Any]=False							,					UpperCamelCase__       :   List[str]=0.0							,					UpperCamelCase__       :   List[Any]=0.0							,					UpperCamelCase__       :   List[str]=1							,					UpperCamelCase__       :   str=False							,					**UpperCamelCase__       :   Tuple							,					):






										'''simple docstring'''






										lowercase_  							=			vocab_size
										# Backward compatibility with n_embed kwarg
										lowercase_  							=			kwargs.pop("""n_embed"""							,					UpperCamelCase__       )
										lowercase_  							=			hidden_size if n_embed is None else n_embed
										lowercase_  							=			n_layer
										lowercase_  							=			n_head
										lowercase_  							=			layer_norm_epsilon
										lowercase_  							=			initializer_range
										lowercase_  							=			use_cache
										lowercase_  							=			pretraining_tp
										lowercase_  							=			apply_residual_connection_post_layernorm
										lowercase_  							=			hidden_dropout
										lowercase_  							=			attention_dropout

										lowercase_  							=			bos_token_id
										lowercase_  							=			eos_token_id
										lowercase_  							=			slow_but_exact

										super().__init__(bos_token_id=UpperCamelCase__							,					eos_token_id=UpperCamelCase__							,					**UpperCamelCase__       )







class        UpperCamelCase__   (	__magic_name__   ):
					__SCREAMING_SNAKE_CASE       :  Optional[int]						=   version.parse('1.12'   )

					def __init__(     self       :   List[Any]							,					UpperCamelCase__       :   PretrainedConfig							,					UpperCamelCase__       :   str = "default"							,					UpperCamelCase__       :   List[PatchingSpec] = None							,					UpperCamelCase__       :   bool = False							,					):






										'''simple docstring'''






										super().__init__(UpperCamelCase__							,					task=UpperCamelCase__							,					patching_specs=UpperCamelCase__							,					use_past=UpperCamelCase__       )
										if not getattr(self._config							,					"""pad_token_id"""							,					UpperCamelCase__       ):
															# TODO: how to do that better?
															lowercase_  							=			0

					@property
					def   UpperCAmelCase__       (     self       :   Union[str, Any]       ):






										'''simple docstring'''






										lowercase_  							=			OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}       )
										if self.use_past:
															# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
															self.fill_with_past_key_values_(UpperCamelCase__							,					direction="""inputs"""							,					inverted_values_shape=UpperCamelCase__       )
															lowercase_  							=			{0: """batch""", 1: """past_sequence + sequence"""}
										else:
															lowercase_  							=			{0: """batch""", 1: """sequence"""}

										return common_inputs

					@property
					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										return self._config.n_layer

					@property
					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										return self._config.n_head

					@property
					def   UpperCAmelCase__       (     self       :   Tuple       ):






										'''simple docstring'''






										return 1e-3

					def   UpperCAmelCase__       (     self       :   int							,					UpperCamelCase__       :   "PreTrainedTokenizer"							,					UpperCamelCase__       :   int = -1							,					UpperCamelCase__       :   int = -1							,					UpperCamelCase__       :   bool = False							,					UpperCamelCase__       :   Optional["TensorType"] = None							,					):






										'''simple docstring'''






										lowercase_  							=			super(UpperCamelCase__							,					self       ).generate_dummy_inputs(
										    UpperCamelCase__							,					batch_size=UpperCamelCase__							,					seq_length=UpperCamelCase__							,					is_pair=UpperCamelCase__							,					framework=UpperCamelCase__       )

										# We need to order the input in the way they appears in the forward()
										lowercase_  							=			OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}       )

										# Need to add the past_keys
										if self.use_past:
															if not is_torch_available():
																				raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""       )
															else:
																				import torch

																				lowercase_							,					lowercase_  							=			common_inputs["""input_ids"""].shape
																				# Not using the same length for past_key_values
																				lowercase_  							=			seqlen + 2
																				lowercase_  							=			self._config.hidden_size // self.num_attention_heads
																				lowercase_  							=			(
																				    batch * self.num_attention_heads,
																				    head_dim,
																				    past_key_values_length,
																				)
																				lowercase_  							=			(
																				    batch * self.num_attention_heads,
																				    past_key_values_length,
																				    head_dim,
																				)
																				lowercase_  							=			[
																				    (torch.zeros(UpperCamelCase__       ), torch.zeros(UpperCamelCase__       )) for _ in range(self.num_layers       )
																				]

										lowercase_  							=			common_inputs["""attention_mask"""]
										if self.use_past:
															lowercase_  							=			ordered_inputs["""attention_mask"""].dtype
															lowercase_  							=			torch.cat(
															    [ordered_inputs["""attention_mask"""], torch.ones(UpperCamelCase__							,					UpperCamelCase__							,					dtype=UpperCamelCase__       )]							,					dim=1       )

										return ordered_inputs







					@property
					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										return 13






# 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_				(      UpperCAmelCase__=None  ):
					if subparsers is not None:
										lowercase_  							=			subparsers.add_parser("""env"""  )
					else:
										lowercase_  							=			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 			UpperCAmelCase_				(      UpperCAmelCase__  ):
					lowercase_  							=			torch.__version__
					lowercase_  							=			torch.cuda.is_available()
					lowercase_  							=			is_xpu_available()
					lowercase_  							=			is_npu_available()

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

					lowercase_  							=			{
					    """`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 / 1_0_2_4 ** 3:.2f} GB''',
					}
					if pt_cuda_available:
										lowercase_  							=			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:"""  )
					lowercase_  							=			(
					    """\n""".join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()]  )
					    if isinstance(UpperCAmelCase__						,							UpperCAmelCase__  )
					    else F'''\t{accelerate_config}'''
					)
					print(UpperCAmelCase__  )

					lowercase_  							=			accelerate_config

					return info


def 			UpperCAmelCase_				(      ):
					lowercase_  							=			env_command_parser()
					lowercase_  							=			parser.parse_args()
					env_command(UpperCAmelCase__  )
					return 0


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


