





"""simple docstring"""



# 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


a_					=     [
    """EAGER""",
    """AOT_EAGER""",
    """INDUCTOR""",
    """NVFUSER""",
    """AOT_NVFUSER""",
    """AOT_CUDAGRAPHS""",
    """OFI""",
    """FX2TRT""",
    """ONNXRT""",
    """IPEX""",
]







def 						__lowercase	(  snake_case_    :    int							,snake_case_    :    List[str]=None							,snake_case_    :    Optional[int]=None							,snake_case_    :    Dict=None		)  ->str:



    '''simple docstring'''







    __A       :		Dict							=	True
    while ask_again:
        __A       :		Dict							=	input(snake_case_		)
        try:
            if default is not None and len(snake_case_		) == 0:
                return default
            return convert_value(snake_case_		) if convert_value is not None else result
        except Exception:
            if error_message is not None:
                print(snake_case_		)







def 						__lowercase	(  snake_case_    :    Dict							,snake_case_    :    Optional[int]=[]							,snake_case_    :    Any=None							,snake_case_    :    str=0		)  ->int:



    '''simple docstring'''







    __A       :		List[str]							=	BulletMenu(snake_case_							,snake_case_		)
    __A       :		Optional[Any]							=	menu.run(default_choice=snake_case_		)
    return convert_value(snake_case_		) if convert_value is not None else result







def 						__lowercase	(  snake_case_    :    Union[str, Any]		)  ->List[Any]:



    '''simple docstring'''







    __A       :		int							=	int(snake_case_		)
    return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value]		)







def 						__lowercase	(  snake_case_    :    Any		)  ->Dict:



    '''simple docstring'''







    __A       :		Optional[Any]							=	int(snake_case_		)
    return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value]		)







def 						__lowercase	(  snake_case_    :    Union[str, Any]		)  ->int:



    '''simple docstring'''







    __A       :		Tuple							=	int(snake_case_		)
    return DynamoBackend(DYNAMO_BACKENDS[value]		).value







def 						__lowercase	(  snake_case_    :    int		)  ->Union[str, Any]:



    '''simple docstring'''







    __A       :		str							=	int(snake_case_		)
    return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value]		)







def 						__lowercase	(  snake_case_    :    Any		)  ->Union[str, Any]:



    '''simple docstring'''







    __A       :		Optional[Any]							=	int(snake_case_		)
    return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value]		)







def 						__lowercase	(  snake_case_    :    Optional[int]		)  ->Union[str, Any]:



    '''simple docstring'''







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






class 						__snake_case   (						argparse.RawDescriptionHelpFormatter	):





 """simple docstring"""


 def    UpperCamelCase__(       self   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase    ):



     '''simple docstring'''




     __A       :		Optional[Any]							=	super()._format_usage(__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase    )
     __A       :		Dict							=	usage.replace('''<command> [<args>] '''   ,		''''''    )
     return usage











"""simple docstring"""



import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
       from transformers import AutoModelForSeqaSeqLM, AutoTokenizer






@require_torch
@require_sentencepiece
@require_tokenizers
class 						__snake_case   (						unittest.TestCase	):





 """simple docstring"""


 @slow
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Any							=	AutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small'''   ,		return_dict=__lowerCamelCase    ).to(__lowerCamelCase    )
     __A       :		Optional[Any]							=	AutoTokenizer.from_pretrained('''google/mt5-small'''    )

     __A       :		Optional[int]							=	tokenizer('''Hello there'''   ,		return_tensors='''pt'''    ).input_ids
     __A       :		str							=	tokenizer('''Hi I am'''   ,		return_tensors='''pt'''    ).input_ids

     __A       :		Any							=	model(input_ids.to(__lowerCamelCase    )   ,		labels=labels.to(__lowerCamelCase    )    ).loss
     __A       :		Tuple							=	-(labels.shape[-1] * loss.item())

     __A       :		int							=	-8_4.9_1_2_7
     self.assertTrue(abs(mtf_score - EXPECTED_SCORE    ) < 1e-4    )




