


# Copyright 2021 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.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


_snake_case					:		Optional[Any]      =    "Create a default config file for Accelerate with only a few flags set."







def     lowerCAmelCase_					( __lowerCamelCase="no"							,     __lowerCamelCase = default_json_config_file							,     __lowerCamelCase = False     ):
							__snake_case		:      int										=       Path(__lowerCamelCase     )
							path.parent.mkdir(parents=__lowerCamelCase							,     exist_ok=__lowerCamelCase     )
							if path.exists():
														print(
														    F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'     )
														return False
							__snake_case		:      Any										=       mixed_precision.lower()
							if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
														raise ValueError(
														    F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'     )
							__snake_case		:      Optional[int]										=       {
							    "compute_environment": "LOCAL_MACHINE",
							    "mixed_precision": mixed_precision,
							}
							if torch.cuda.is_available():
														__snake_case		:      Dict										=       torch.cuda.device_count()
														__snake_case		:      Tuple										=       num_gpus
														__snake_case		:      List[str]										=       False
														if num_gpus > 1:
																					__snake_case		:      Optional[int]										=       "MULTI_GPU"
														else:
																					__snake_case		:      Dict										=       "NO"
							elif is_xpu_available() and use_xpu:
														__snake_case		:      List[str]										=       torch.xpu.device_count()
														__snake_case		:      str										=       num_xpus
														__snake_case		:      int										=       False
														if num_xpus > 1:
																					__snake_case		:      Optional[int]										=       "MULTI_XPU"
														else:
																					__snake_case		:      str										=       "NO"
							elif is_npu_available():
														__snake_case		:      Any										=       torch.npu.device_count()
														__snake_case		:      str										=       num_npus
														__snake_case		:      str										=       False
														if num_npus > 1:
																					__snake_case		:      Optional[int]										=       "MULTI_NPU"
														else:
																					__snake_case		:      int										=       "NO"
							else:
														__snake_case		:      List[Any]										=       0
														__snake_case		:      Dict										=       True
														__snake_case		:      Tuple										=       1
														__snake_case		:      Tuple										=       "NO"
							__snake_case		:      str										=       ClusterConfig(**__lowerCamelCase     )
							config.to_json_file(__lowerCamelCase     )
							return path







def     lowerCAmelCase_					( __lowerCamelCase							,     __lowerCamelCase     ):
							__snake_case		:      Optional[Any]										=       parser.add_parser("default"							,     parents=__lowerCamelCase							,     help=__lowerCamelCase							,     formatter_class=__lowerCamelCase     )
							parser.add_argument(
							    "--config_file"							,     default=__lowerCamelCase							,     help=(
							        "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
							        "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
							        "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
							        "with 'huggingface'."
							    )							,     dest="save_location"							,     )

							parser.add_argument(
							    "--mixed_precision"							,     choices=["no", "fp16", "bf16"]							,     type=__lowerCamelCase							,     help="Whether or not to use mixed precision training. "
							    "Choose between FP16 and BF16 (bfloat16) training. "
							    "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later."							,     default="no"							,     )
							parser.set_defaults(func=__lowerCamelCase     )
							return parser







def     lowerCAmelCase_					( __lowerCamelCase     ):
							__snake_case		:      List[Any]										=       write_basic_config(args.mixed_precision							,     args.save_location     )
							if config_file:
														print(F'accelerate configuration saved at {config_file}'     )





import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_snake_case					:		Union[str, Any]      =    logging.get_logger(__name__)

_snake_case					:		List[str]      =    {
    "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json",
    "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json",
}

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Tuple					  =  "encodec"

							def __init__(							self	:  Any   ,     lowerCamelCase	:  Optional[int]=[1.5, 3.0, 6.0, 12.0, 24.0]   ,     lowerCamelCase	:  List[str]=24000   ,     lowerCamelCase	:  int=1   ,     lowerCamelCase	:  Optional[int]=False   ,     lowerCamelCase	:  Dict=None   ,     lowerCamelCase	:  Tuple=None   ,     lowerCamelCase	:  Optional[int]=128   ,     lowerCamelCase	:  Optional[int]=32   ,     lowerCamelCase	:  List[str]=1   ,     lowerCamelCase	:  str=[8, 5, 4, 2]   ,     lowerCamelCase	:  List[str]="weight_norm"   ,     lowerCamelCase	:  Any=7   ,     lowerCamelCase	:  Tuple=7   ,     lowerCamelCase	:  int=3   ,     lowerCamelCase	:  int=2   ,     lowerCamelCase	:  Union[str, Any]=True   ,     lowerCamelCase	:  List[Any]="reflect"   ,     lowerCamelCase	:  Union[str, Any]=2   ,     lowerCamelCase	:  Optional[int]=2   ,     lowerCamelCase	:  int=1.0   ,     lowerCamelCase	:  Optional[Any]=1024   ,     lowerCamelCase	:  Optional[Any]=None   ,     lowerCamelCase	:  str=True   ,     **lowerCamelCase	:  Dict   ,     )     ->   Any:
														__snake_case		:      Tuple										=       target_bandwidths
														__snake_case		:      Union[str, Any]										=       sampling_rate
														__snake_case		:      Union[str, Any]										=       audio_channels
														__snake_case		:      Dict										=       normalize
														__snake_case		:      List[Any]										=       chunk_length_s
														__snake_case		:      Tuple										=       overlap
														__snake_case		:      Optional[int]										=       hidden_size
														__snake_case		:      List[Any]										=       num_filters
														__snake_case		:      Union[str, Any]										=       num_residual_layers
														__snake_case		:      Optional[int]										=       upsampling_ratios
														__snake_case		:      List[str]										=       norm_type
														__snake_case		:      Optional[int]										=       kernel_size
														__snake_case		:      Dict										=       last_kernel_size
														__snake_case		:      Tuple										=       residual_kernel_size
														__snake_case		:      List[Any]										=       dilation_growth_rate
														__snake_case		:      Optional[int]										=       use_causal_conv
														__snake_case		:      Tuple										=       pad_mode
														__snake_case		:      Union[str, Any]										=       compress
														__snake_case		:      Union[str, Any]										=       num_lstm_layers
														__snake_case		:      int										=       trim_right_ratio
														__snake_case		:      Tuple										=       codebook_size
														__snake_case		:      Optional[Any]										=       codebook_dim if codebook_dim is not None else hidden_size
														__snake_case		:      int										=       use_conv_shortcut

														if self.norm_type not in ["weight_norm", "time_group_norm"]:
																					raise ValueError(
																					    F'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'    )

														super().__init__(**lowerCamelCase    )

							@property
							def    __snake_case (							self	:  int    )     ->   Optional[int]:
														if self.chunk_length_s is None:
																					return None
														else:
																					return int(self.chunk_length_s * self.sampling_rate    )

							@property
							def    __snake_case (							self	:  Union[str, Any]    )     ->   Optional[int]:
														if self.chunk_length_s is None or self.overlap is None:
																					return None
														else:
																					return max(1   ,     int((1.0 - self.overlap) * self.chunk_length    )    )

							@property
							def    __snake_case (							self	:  Optional[Any]    )     ->   int:
														__snake_case		:      Union[str, Any]										=       np.prod(self.upsampling_ratios    )
														return math.ceil(self.sampling_rate / hop_length    )






							@property
							def    __snake_case (							self	:  Optional[Any]    )     ->   int:
														return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10)    )

