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

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


lowerCAmelCase__     						=			[
    'EAGER',
    'AOT_EAGER',
    'INDUCTOR',
    'NVFUSER',
    'AOT_NVFUSER',
    'AOT_CUDAGRAPHS',
    'OFI',
    'FX2TRT',
    'ONNXRT',
    'IPEX',
]






def 				__lowerCamelCase		(						lowerCAmelCase__     ,  lowerCAmelCase__=None     ,  lowerCAmelCase__=None     ,  lowerCAmelCase__=None     ):
       lowerCAmelCase__					     = True
       while ask_again:
              lowerCAmelCase__					     = input(lowerCAmelCase__     )
              try:
                     if default is not None and len(lowerCAmelCase__     ) == 0:
                            return default
                     return convert_value(lowerCAmelCase__     ) if convert_value is not None else result
              except Exception:
                     if error_message is not None:
                            print(lowerCAmelCase__     )






def 				__lowerCamelCase		(						lowerCAmelCase__     ,  lowerCAmelCase__=[]     ,  lowerCAmelCase__=None     ,  lowerCAmelCase__=0     ):
       lowerCAmelCase__					     = BulletMenu(lowerCAmelCase__     ,  lowerCAmelCase__     )
       lowerCAmelCase__					     = menu.run(default_choice=lowerCAmelCase__     )
       return convert_value(lowerCAmelCase__     ) if convert_value is not None else result






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       lowerCAmelCase__					     = int(lowerCAmelCase__     )
       return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value]     )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       lowerCAmelCase__					     = int(lowerCAmelCase__     )
       return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value]     )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       lowerCAmelCase__					     = int(lowerCAmelCase__     )
       return DynamoBackend(DYNAMO_BACKENDS[value]     ).value






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       lowerCAmelCase__					     = int(lowerCAmelCase__     )
       return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value]     )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       lowerCAmelCase__					     = int(lowerCAmelCase__     )
       return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value]     )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       return {"yes": True, "no": False}[value.lower()]



class     a_			(						argparse.RawDescriptionHelpFormatter						):






 '''simple docstring'''






 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  List[Any]      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = super()._format_usage(lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__)
        lowerCAmelCase__					     = usage.replace('<command> [<args>] '      ,   '')
        return usage
lowerCAmelCase__     						=			'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       # Make sure the supplied data is a bytes-like object
       if not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              lowerCAmelCase__					     = F"""a bytes-like object is required, not '{data.__class__.__name__}'"""
              raise TypeError(lowerCAmelCase__     )

       lowerCAmelCase__					     = ''.join(bin(lowerCAmelCase__     )[2:].zfill(8     ) for byte in data     )

       lowerCAmelCase__					     = len(lowerCAmelCase__     ) % 6 != 0

       if padding_needed:
              # The padding that will be added later
              lowerCAmelCase__					     = b'=' * ((6 - len(lowerCAmelCase__     ) % 6) // 2)

              # Append binary_stream with arbitrary binary digits (0's by default) to make its
              # length a multiple of 6.
              binary_stream += "0" * (6 - len(lowerCAmelCase__     ) % 6)
       else:
              lowerCAmelCase__					     = b''

       # Encode every 6 binary digits to their corresponding Base64 character
       return (
           "".join(
               B64_CHARSET[int(binary_stream[index : index + 6]     ,  2     )]
               for index in range(0     ,  len(lowerCAmelCase__     )     ,  6     )     ).encode()
           + padding
       )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       # Make sure encoded_data is either a string or a bytes-like object
       if not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ) and not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              lowerCAmelCase__					     = (
                  'argument should be a bytes-like object or ASCII string, '
                  F"""not '{encoded_data.__class__.__name__}'"""
              )
              raise TypeError(lowerCAmelCase__     )

       # In case encoded_data is a bytes-like object, make sure it contains only
       # ASCII characters so we convert it to a string object
       if isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              try:
                     lowerCAmelCase__					     = encoded_data.decode('utf-8'     )
              except UnicodeDecodeError:
                     raise ValueError('base64 encoded data should only contain ASCII characters'     )

       lowerCAmelCase__					     = encoded_data.count('='     )

       # Check if the encoded string contains non base64 characters
       if padding:
              assert all(
                  char in B64_CHARSET for char in encoded_data[:-padding]     ), "Invalid base64 character(s) found."
       else:
              assert all(
                  char in B64_CHARSET for char in encoded_data     ), "Invalid base64 character(s) found."

       # Check the padding
       assert len(lowerCAmelCase__     ) % 4 == 0 and padding < 3, "Incorrect padding"

       if padding:
              # Remove padding if there is one
              lowerCAmelCase__					     = encoded_data[:-padding]

              lowerCAmelCase__					     = ''.join(
                  bin(B64_CHARSET.index(lowerCAmelCase__     )     )[2:].zfill(6     ) for char in encoded_data     )[: -padding * 2]
       else:
              lowerCAmelCase__					     = ''.join(
                  bin(B64_CHARSET.index(lowerCAmelCase__     )     )[2:].zfill(6     ) for char in encoded_data     )

       lowerCAmelCase__					     = [
           int(binary_stream[index : index + 8]     ,  2     )
           for index in range(0     ,  len(lowerCAmelCase__     )     ,  8     )
       ]

       return bytes(lowerCAmelCase__     )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
