



# 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_		(					snake_case__=None					)	->     Any:



				"""simple docstring"""





				if subparsers is not None:
								lowerCAmelCase__		  =  subparsers.add_parser('env'					)
				else:
								lowerCAmelCase__		  =  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     UpperCAmelCase_		(					snake_case__					)	->     Dict:



				"""simple docstring"""





				lowerCAmelCase__		  =  torch.__version__
				lowerCAmelCase__		  =  torch.cuda.is_available()
				lowerCAmelCase__		  =  is_xpu_available()
				lowerCAmelCase__		  =  is_npu_available()

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

				lowerCAmelCase__		  =  {
				    '`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 / 1024 ** 3:.2f} GB',
				}
				if pt_cuda_available:
								lowerCAmelCase__		  =  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:'					)
				lowerCAmelCase__		  =  (
				    '\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__					)

				lowerCAmelCase__		  =  accelerate_config

				return info







def     UpperCAmelCase_		(					)	->     int:



				"""simple docstring"""





				lowerCAmelCase__		  =  env_command_parser()
				lowerCAmelCase__		  =  parser.parse_args()
				env_command(snake_case__					)
				return 0


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





from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin


@dataclass
class 	__snake_case   ( SCREAMING_SNAKE_CASE ):
		SCREAMING_SNAKE_CASE__								=	42



class 	__snake_case   ( SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE ):
		@register_to_config
		def __init__(							self    ,a_ = 16    ,a_ = 88    ,a_ = None    ,a_ = None    ,a_ = 1    ,a_ = 0.0    ,a_ = 32    ,a_ = None    ,a_ = False    ,a_ = None    ,a_ = "geglu"    ,a_ = True    ,a_ = True    ,):







						"""simple docstring"""
						super().__init__()
						lowerCAmelCase__		  =  num_attention_heads
						lowerCAmelCase__		  =  attention_head_dim
						lowerCAmelCase__		  =  num_attention_heads * attention_head_dim

						lowerCAmelCase__		  =  in_channels

						lowerCAmelCase__		  =  torch.nn.GroupNorm(num_groups=a_    ,num_channels=a_    ,eps=1e-6    ,affine=a_						)
						lowerCAmelCase__		  =  nn.Linear(a_    ,a_						)

						# 3. Define transformers blocks
						lowerCAmelCase__		  =  nn.ModuleList(
						    [
						        BasicTransformerBlock(
						            a_    ,a_    ,a_    ,dropout=a_    ,cross_attention_dim=a_    ,activation_fn=a_    ,attention_bias=a_    ,double_self_attention=a_    ,norm_elementwise_affine=a_    ,)
						        for d in range(a_						)
						    ]						)

						lowerCAmelCase__		  =  nn.Linear(a_    ,a_						)



		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_=None    ,a_=None    ,a_=None    ,a_=1    ,a_=None    ,a_ = True    ,):







						"""simple docstring"""
						lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__		  =  hidden_states.shape
						lowerCAmelCase__		  =  batch_frames // num_frames

						lowerCAmelCase__		  =  hidden_states

						lowerCAmelCase__		  =  hidden_states[None, :].reshape(a_    ,a_    ,a_    ,a_    ,a_						)
						lowerCAmelCase__		  =  hidden_states.permute(0    ,2    ,1    ,3    ,4						)

						lowerCAmelCase__		  =  self.norm(a_						)
						lowerCAmelCase__		  =  hidden_states.permute(0    ,3    ,4    ,2    ,1						).reshape(batch_size * height * width    ,a_    ,a_						)

						lowerCAmelCase__		  =  self.proj_in(a_						)

						# 2. Blocks
						for block in self.transformer_blocks:
										lowerCAmelCase__		  =  block(
										    a_    ,encoder_hidden_states=a_    ,timestep=a_    ,cross_attention_kwargs=a_    ,class_labels=a_    ,)

						# 3. Output
						lowerCAmelCase__		  =  self.proj_out(a_						)
						lowerCAmelCase__		  =  (
						    hidden_states[None, None, :]
						    .reshape(a_    ,a_    ,a_    ,a_    ,a_						)
						    .permute(0    ,3    ,4    ,1    ,2						)
						    .contiguous()
						)
						lowerCAmelCase__		  =  hidden_states.reshape(a_    ,a_    ,a_    ,a_						)

						lowerCAmelCase__		  =  hidden_states + residual

						if not return_dict:
										return (output,)

						return TransformerTemporalModelOutput(sample=a_						)
