






# 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_ (    __snake_case     :							Optional[Any]=None      )     -> str:





				'''simple docstring'''


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

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

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





def 					lowercase_ (    __snake_case     :							int      )     -> int:





				'''simple docstring'''


				snake_case__      :Dict         =   torch.__version__
				snake_case__      :int         =   torch.cuda.is_available()
				snake_case__      :str         =   is_xpu_available()
				snake_case__      :List[str]         =   is_npu_available()

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

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

				snake_case__      :str         =   accelerate_config

				return info





def 					lowercase_ (    )     -> int:





				'''simple docstring'''


				snake_case__      :Union[str, Any]         =   env_command_parser()
				snake_case__      :Union[str, Any]         =   parser.parse_args()
				env_command(__snake_case      )
				return 0


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







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin


class 					_snake_case						(			_A       ,    _A       ,    _A ):






	@register_to_config
	def __init__(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = False					,)						->    int:
					super().__init__()

					snake_case__      :Union[str, Any]         =   nn.Embedding(UpperCamelCase					,UpperCamelCase       )

					snake_case__      :int         =   nn.Embedding(UpperCamelCase					,UpperCamelCase       )
					snake_case__      :Any         =   False

					snake_case__      :List[Any]         =   nn.Dropout(p=UpperCamelCase       )

					snake_case__      :Tuple         =   TaConfig(
					    vocab_size=UpperCamelCase					,d_model=UpperCamelCase					,num_heads=UpperCamelCase					,d_kv=UpperCamelCase					,d_ff=UpperCamelCase					,dropout_rate=UpperCamelCase					,feed_forward_proj=UpperCamelCase					,is_decoder=UpperCamelCase					,is_encoder_decoder=UpperCamelCase					,)

					snake_case__      :List[str]         =   nn.ModuleList()
					for lyr_num in range(UpperCamelCase       ):
									snake_case__      :List[Any]         =   TaBlock(UpperCamelCase       )
									self.encoders.append(UpperCamelCase       )

					snake_case__      :Optional[Any]         =   TaLayerNorm(UpperCamelCase       )
					snake_case__      :Any         =   nn.Dropout(p=UpperCamelCase       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase       )						->    int:
					snake_case__      :str         =   self.token_embedder(UpperCamelCase       )

					snake_case__      :int         =   encoder_input_tokens.shape[1]
					snake_case__      :List[Any]         =   torch.arange(UpperCamelCase					,device=encoder_input_tokens.device       )
					x += self.position_encoding(UpperCamelCase       )

					snake_case__      :Optional[int]         =   self.dropout_pre(UpperCamelCase       )

					# inverted the attention mask
					snake_case__      :Optional[Any]         =   encoder_input_tokens.size()
					snake_case__      :Dict         =   self.get_extended_attention_mask(UpperCamelCase					,UpperCamelCase       )

					for lyr in self.encoders:
									snake_case__      :str         =   lyr(UpperCamelCase					,UpperCamelCase       )[0]
					snake_case__      :List[Any]         =   self.layer_norm(UpperCamelCase       )

					return self.dropout_post(UpperCamelCase       ), encoder_inputs_mask