




"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


A   =       logging.get_logger(__name__)



class      A				(	__magic_name__   ):


			def __init__(      self      :    str      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    float      ,  **UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					feature_size
						__UpperCAmelCase	: str		=					sampling_rate
						__UpperCAmelCase	: List[Any]		=					padding_value

						__UpperCAmelCase	: Union[str, Any]		=					kwargs.pop("padding_side"      ,  "right")
						__UpperCAmelCase	: Tuple		=					kwargs.pop("return_attention_mask"      ,  UpperCamelCase_)

						super().__init__(**UpperCamelCase_)


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    Union[
			        BatchFeature,
			        List[BatchFeature],
			        Dict[str, BatchFeature],
			        Dict[str, List[BatchFeature]],
			        List[Dict[str, BatchFeature]],
			    ]      ,  UpperCamelCase_      :    Union[bool, str, PaddingStrategy] = True      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    bool = False      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[bool] = None      ,  UpperCamelCase_      :    Optional[Union[str, TensorType]] = None      ,  ):


						"""simple docstring"""




						if isinstance(UpperCamelCase_      ,  (list, tuple)) and isinstance(processed_features[0]      ,  (dict, BatchFeature)):
									__UpperCAmelCase	: Union[str, Any]		=					{
									    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
									}

						# The model's main input name, usually `input_values`, has be passed for padding
						if self.model_input_names[0] not in processed_features:
									raise ValueError(
									    "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"
									    F" to this method that includes {self.model_input_names[0]}, but you provided"
									    F" {list(processed_features.keys())}")

						__UpperCAmelCase	: Dict		=					processed_features[self.model_input_names[0]]
						__UpperCAmelCase	: int		=					(
						    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
						)

						if len(UpperCamelCase_) == 0:
									if return_attention_mask:
												__UpperCAmelCase	: Dict		=					[]
									return processed_features

						# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
						# and rebuild them afterwards if no return_tensors is specified
						# Note that we lose the specific device the tensor may be on for PyTorch

						__UpperCAmelCase	: Tuple		=					required_input[0]
						if isinstance(UpperCamelCase_      ,  (list, tuple)):
									# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
									__UpperCAmelCase	: int		=					0
									while len(required_input[index]) == 0:
												index += 1
									if index < len(UpperCamelCase_):
												__UpperCAmelCase	: str		=					required_input[index][0]

						if return_tensors is None:
									if is_tf_tensor(UpperCamelCase_):
												__UpperCAmelCase	: str		=					"tf"
									elif is_torch_tensor(UpperCamelCase_):
												__UpperCAmelCase	: Dict		=					"pt"
									elif isinstance(UpperCamelCase_      ,  (int, float, list, tuple, np.ndarray)):
												__UpperCAmelCase	: int		=					"np"
									else:
												raise ValueError(
												    F"type of {first_element} unknown: {type(UpperCamelCase_)}. "
												    "Should be one of a python, numpy, pytorch or tensorflow object.")

						for key, value in processed_features.items():
									if isinstance(value[0]      ,  (int, float)):
												__UpperCAmelCase	: Union[str, Any]		=					to_numpy(UpperCamelCase_)
									else:
												__UpperCAmelCase	: Any		=					[to_numpy(UpperCamelCase_) for v in value]

        # Convert padding_strategy in PaddingStrategy
						__UpperCAmelCase	: List[str]		=					self._get_padding_strategies(padding=UpperCamelCase_      ,  max_length=UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					processed_features[self.model_input_names[0]]

						__UpperCAmelCase	: Optional[Any]		=					len(UpperCamelCase_)
						if not all(len(UpperCamelCase_) == batch_size for v in processed_features.values()):
									raise ValueError("Some items in the output dictionary have a different batch size than others.")

						__UpperCAmelCase	: Optional[int]		=					[]
						for i in range(UpperCamelCase_):
									__UpperCAmelCase	: List[str]		=					{k: v[i] for k, v in processed_features.items()}
									# truncation
									__UpperCAmelCase	: Any		=					self._truncate(
									    UpperCamelCase_      ,  max_length=UpperCamelCase_      ,  pad_to_multiple_of=UpperCamelCase_      ,  truncation=UpperCamelCase_      ,  )
									truncated_inputs.append(UpperCamelCase_)

						if padding_strategy == PaddingStrategy.LONGEST:
									# make sure that `max_length` cannot be longer than the longest truncated length
									__UpperCAmelCase	: Union[str, Any]		=					max(len(input_slice[self.model_input_names[0]]) for input_slice in truncated_inputs)
									__UpperCAmelCase	: str		=					PaddingStrategy.MAX_LENGTH

						__UpperCAmelCase	: List[Any]		=					{}
						for i in range(UpperCamelCase_):
									# padding
									__UpperCAmelCase	: str		=					self._pad(
									    truncated_inputs[i]      ,  max_length=UpperCamelCase_      ,  padding_strategy=UpperCamelCase_      ,  pad_to_multiple_of=UpperCamelCase_      ,  return_attention_mask=UpperCamelCase_      ,  )

									for key, value in outputs.items():
												if key not in batch_outputs:
															__UpperCAmelCase	: Any		=					[]
												if value.dtype is np.dtype(np.floataa):
															__UpperCAmelCase	: Tuple		=					value.astype(np.floataa)
												batch_outputs[key].append(UpperCamelCase_)

						return BatchFeature(UpperCamelCase_      ,  tensor_type=UpperCamelCase_)


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    Union[Dict[str, np.ndarray], BatchFeature]      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    PaddingStrategy = PaddingStrategy.DO_NOT_PAD      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[bool] = None      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					processed_features[self.model_input_names[0]]

						if padding_strategy == PaddingStrategy.LONGEST:
									__UpperCAmelCase	: List[Any]		=					len(UpperCamelCase_)

						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
									__UpperCAmelCase	: Optional[int]		=					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						__UpperCAmelCase	: str		=					padding_strategy != PaddingStrategy.DO_NOT_PAD and len(UpperCamelCase_) < max_length

						if return_attention_mask and "attention_mask" not in processed_features:
									__UpperCAmelCase	: int		=					np.ones(len(UpperCamelCase_)      ,  dtype=np.intaa)

						if needs_to_be_padded:
									__UpperCAmelCase	: Optional[Any]		=					max_length - len(UpperCamelCase_)
									if self.padding_side == "right":
												if return_attention_mask:
															__UpperCAmelCase	: List[Any]		=					np.pad(
															    processed_features["attention_mask"]      ,  (0, difference))
												__UpperCAmelCase	: str		=					((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
												__UpperCAmelCase	: List[Any]		=					np.pad(
												    UpperCamelCase_      ,  UpperCamelCase_      ,  "constant"      ,  constant_values=self.padding_value)
									elif self.padding_side == "left":
												if return_attention_mask:
															__UpperCAmelCase	: Dict		=					np.pad(
															    processed_features["attention_mask"]      ,  (difference, 0))
												__UpperCAmelCase	: Union[str, Any]		=					((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
												__UpperCAmelCase	: Union[str, Any]		=					np.pad(
												    UpperCamelCase_      ,  UpperCamelCase_      ,  "constant"      ,  constant_values=self.padding_value)
									else:
												raise ValueError("Invalid padding strategy:" + str(self.padding_side))

						return processed_features


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    Union[Dict[str, np.ndarray], BatchFeature]      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[bool] = None      ,  ):


						"""simple docstring"""




						if not truncation:
									return processed_features
						elif truncation and max_length is None:
									raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined.")

						__UpperCAmelCase	: Dict		=					processed_features[self.model_input_names[0]]

						# find `max_length` that fits `pad_to_multiple_of`
						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
									__UpperCAmelCase	: Optional[int]		=					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						__UpperCAmelCase	: str		=					len(UpperCamelCase_) > max_length

						if needs_to_be_truncated:
									__UpperCAmelCase	: List[Any]		=					processed_features[self.model_input_names[0]][:max_length]
									if "attention_mask" in processed_features:
												__UpperCAmelCase	: str		=					processed_features["attention_mask"][:max_length]

						return processed_features


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    List[str]=False      ,  UpperCamelCase_      :    Optional[int]=None):


						"""simple docstring"""




						if padding is not False:
									if padding is True:
												__UpperCAmelCase	: Union[str, Any]		=					PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
									elif not isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												__UpperCAmelCase	: Dict		=					PaddingStrategy(UpperCamelCase_)
									elif isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												__UpperCAmelCase	: Tuple		=					padding
						else:
									__UpperCAmelCase	: Dict		=					PaddingStrategy.DO_NOT_PAD

						# Set max length if needed
						if max_length is None:
									if padding_strategy == PaddingStrategy.MAX_LENGTH:
												raise ValueError(
												    F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined")

        # Test if we have a padding value
						if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
									raise ValueError(
									    "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"
									    " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.")

						return padding_strategy








"""simple docstring"""





import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format="""%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s""",
    datefmt="""%m/%d/%Y %H:%M:%S""",
    level=logging.INFO,
)
A   =       logging.getLogger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			Any:


			"""simple docstring"""
			__UpperCAmelCase	: Optional[Any]		=					git.Repo(search_parent_directories=UpperCamelCase							)
			__UpperCAmelCase	: Any		=					{
			    "repo_id": str(UpperCamelCase							),
			    "repo_sha": str(repo.head.object.hexsha							),
			    "repo_branch": str(repo.active_branch							),
			}

			with open(os.path.join(UpperCamelCase				,       "git_log.json"							)				,       "w"							) as f:
						json.dump(UpperCamelCase				,       UpperCamelCase				,       indent=4							)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			str:


			"""simple docstring"""
			if params.n_gpu <= 0:
						__UpperCAmelCase	: str		=					0
						__UpperCAmelCase	: Dict		=					-1
						__UpperCAmelCase	: Tuple		=					True
						__UpperCAmelCase	: List[str]		=					False
						return

			assert torch.cuda.is_available()

			logger.info("Initializing GPUs"							)
			if params.n_gpu > 1:
						assert params.local_rank != -1

						__UpperCAmelCase	: Optional[int]		=					int(os.environ["WORLD_SIZE"]							)
						__UpperCAmelCase	: Union[str, Any]		=					int(os.environ["N_GPU_NODE"]							)
						__UpperCAmelCase	: Optional[int]		=					int(os.environ["RANK"]							)

						# number of nodes / node ID
						__UpperCAmelCase	: int		=					params.world_size // params.n_gpu_per_node
						__UpperCAmelCase	: Optional[int]		=					params.global_rank // params.n_gpu_per_node
						__UpperCAmelCase	: Tuple		=					True

						assert params.n_nodes == int(os.environ["N_NODES"]							)
						assert params.node_id == int(os.environ["NODE_RANK"]							)

			# local job (single GPU)
			else:
						assert params.local_rank == -1

						__UpperCAmelCase	: Union[str, Any]		=					1
						__UpperCAmelCase	: Dict		=					0
						__UpperCAmelCase	: List[Any]		=					0
						__UpperCAmelCase	: Tuple		=					0
						__UpperCAmelCase	: Any		=					1
						__UpperCAmelCase	: str		=					1
						__UpperCAmelCase	: Dict		=					False

			# sanity checks
			assert params.n_nodes >= 1
			assert 0 <= params.node_id < params.n_nodes
			assert 0 <= params.local_rank <= params.global_rank < params.world_size
			assert params.world_size == params.n_nodes * params.n_gpu_per_node

			# define whether this is the master process / if we are in multi-node distributed mode
			__UpperCAmelCase	: Union[str, Any]		=					params.node_id == 0 and params.local_rank == 0
			__UpperCAmelCase	: Dict		=					params.n_nodes > 1

			# summary
			__UpperCAmelCase	: Any		=					f"--- Global rank: {params.global_rank} - "
			logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes							)
			logger.info(PREFIX + "Node ID        : %i" % params.node_id							)
			logger.info(PREFIX + "Local rank     : %i" % params.local_rank							)
			logger.info(PREFIX + "World size     : %i" % params.world_size							)
			logger.info(PREFIX + "GPUs per node  : %i" % params.n_gpu_per_node							)
			logger.info(PREFIX + "Master         : %s" % str(params.is_master							)							)
			logger.info(PREFIX + "Multi-node     : %s" % str(params.multi_node							)							)
			logger.info(PREFIX + "Multi-GPU      : %s" % str(params.multi_gpu							)							)
			logger.info(PREFIX + "Hostname       : %s" % socket.gethostname()							)

			# set GPU device
			torch.cuda.set_device(params.local_rank							)

			# initialize multi-GPU
			if params.multi_gpu:
						logger.info("Initializing PyTorch distributed"							)
						torch.distributed.init_process_group(
						    init_method="env://"				,       backend="nccl"				,       )





def 					_UpperCamelCase    (			UpperCamelCase							)			->			Tuple:


			"""simple docstring"""
			np.random.seed(args.seed							)
			torch.manual_seed(args.seed							)
			if args.n_gpu > 0:
						torch.cuda.manual_seed_all(args.seed							)


