

'''simple docstring'''
import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY







@is_pipeline_test
class 							A__    (       unittest.TestCase  ):
  A__		 =				MODEL_FOR_MASKED_LM_MAPPING
  A__		 =				TF_MODEL_FOR_MASKED_LM_MAPPING





  def 						A  (		self      :	Dict )	->    List[Any]:





       '''simple docstring'''






       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       if is_torch_available():
            import torch

            torch.cuda.empty_cache()





  @require_tf
  def 						A  (		self      :	Union[str, Any] )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='sshleifer/tiny-distilroberta-base'							,					top_k=2							,					framework='tf' )
       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>' )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {'sequence': 'My name is grouped', 'score': 2.1e-05, 'token': 3_8015, 'token_str': ' grouped'},
               {'sequence': 'My name is accuser', 'score': 2.1e-05, 'token': 2_5506, 'token_str': ' accuser'},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('The largest city in France is <mask>' )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {
                   'sequence': 'The largest city in France is grouped',
                   'score': 2.1e-05,
                   'token': 3_8015,
                   'token_str': ' grouped',
               },
               {
                   'sequence': 'The largest city in France is accuser',
                   'score': 2.1e-05,
                   'token': 2_5506,
                   'token_str': ' accuser',
               },
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>'							,					targets=[' Patrick', ' Clara', ' Teven']							,					top_k=3 )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {'sequence': 'My name is Clara', 'score': 2e-05, 'token': 1_3606, 'token_str': ' Clara'},
               {'sequence': 'My name is Patrick', 'score': 2e-05, 'token': 3499, 'token_str': ' Patrick'},
               {'sequence': 'My name is Te', 'score': 1.9e-05, 'token': 2941, 'token_str': ' Te'},
           ]							,					)





  @require_torch
  def 						A  (		self      :	Tuple )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='sshleifer/tiny-distilroberta-base'							,					top_k=2							,					framework='pt' )

       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>' )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {'sequence': 'My name is Maul', 'score': 2.2e-05, 'token': 3_5676, 'token_str': ' Maul'},
               {'sequence': 'My name isELS', 'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS'},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('The largest city in France is <mask>' )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {
                   'sequence': 'The largest city in France is Maul',
                   'score': 2.2e-05,
                   'token': 3_5676,
                   'token_str': ' Maul',
               },
               {'sequence': 'The largest city in France isELS', 'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS'},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>'							,					targets=[' Patrick', ' Clara', ' Teven']							,					top_k=3 )
       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               {'sequence': 'My name is Patrick', 'score': 2.1e-05, 'token': 3499, 'token_str': ' Patrick'},
               {'sequence': 'My name is Te', 'score': 2e-05, 'token': 2941, 'token_str': ' Te'},
               {'sequence': 'My name is Clara', 'score': 2e-05, 'token': 1_3606, 'token_str': ' Clara'},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask> <mask>'							,					top_k=2 )

       self.assertEqual(
           nested_simplify(_a							,					decimals=6 )							,					[
               [
                   {
                       'score': 2.2e-05,
                       'token': 3_5676,
                       'token_str': ' Maul',
                       'sequence': '<s>My name is Maul<mask></s>',
                   },
                   {'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS', 'sequence': '<s>My name isELS<mask></s>'},
               ],
               [
                   {
                       'score': 2.2e-05,
                       'token': 3_5676,
                       'token_str': ' Maul',
                       'sequence': '<s>My name is<mask> Maul</s>',
                   },
                   {'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS', 'sequence': '<s>My name is<mask>ELS</s>'},
               ],
           ]							,					)





  @require_torch_gpu
  def 						A  (		self      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline('fill-mask'							,					model='hf-internal-testing/tiny-random-distilbert'							,					device=0							,					framework='pt' )

       # convert model to fp16
       pipe.model.half()

       _SCREAMING_SNAKE_CASE							    =pipe('Paris is the [MASK] of France.' )
       # We actually don't care about the result, we just want to make sure
       # it works, meaning the float16 tensor got casted back to float32
       # for postprocessing.
       self.assertIsInstance(_a							,					_a )





  @slow
  @require_torch
  def 						A  (		self      :	Dict )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='distilroberta-base'							,					top_k=2							,					framework='pt' )
       self.run_large_test(_a )





  @slow
  @require_tf
  def 						A  (		self      :	Optional[Any] )	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='distilroberta-base'							,					top_k=2							,					framework='tf' )
       self.run_large_test(_a )





  def 						A  (		self      :	Union[str, Any]							,					_a      :	str )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>' )
       self.assertEqual(
           nested_simplify(_a )							,					[
               {'sequence': 'My name is John', 'score': 0.0_08, 'token': 610, 'token_str': ' John'},
               {'sequence': 'My name is Chris', 'score': 0.0_07, 'token': 1573, 'token_str': ' Chris'},
           ]							,					)
       _SCREAMING_SNAKE_CASE							    =unmasker('The largest city in France is <mask>' )
       self.assertEqual(
           nested_simplify(_a )							,					[
               {
                   'sequence': 'The largest city in France is Paris',
                   'score': 0.2_51,
                   'token': 2201,
                   'token_str': ' Paris',
               },
               {
                   'sequence': 'The largest city in France is Lyon',
                   'score': 0.2_14,
                   'token': 1_2790,
                   'token_str': ' Lyon',
               },
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =unmasker('My name is <mask>'							,					targets=[' Patrick', ' Clara', ' Teven']							,					top_k=3 )
       self.assertEqual(
           nested_simplify(_a )							,					[
               {'sequence': 'My name is Patrick', 'score': 0.0_05, 'token': 3499, 'token_str': ' Patrick'},
               {'sequence': 'My name is Clara', 'score': 0.0_00, 'token': 1_3606, 'token_str': ' Clara'},
               {'sequence': 'My name is Te', 'score': 0.0_00, 'token': 2941, 'token_str': ' Te'},
           ]							,					)





  @require_torch
  def 						A  (		self      :	Tuple )	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='sshleifer/tiny-distilroberta-base'							,					framework='pt' )
       _SCREAMING_SNAKE_CASE							    =None
       _SCREAMING_SNAKE_CASE							    =None
       self.run_pipeline_test(_a							,					[] )





  @require_tf
  def 						A  (		self      :	List[str] )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =pipeline(task='fill-mask'							,					model='sshleifer/tiny-distilroberta-base'							,					framework='tf' )
       _SCREAMING_SNAKE_CASE							    =None
       _SCREAMING_SNAKE_CASE							    =None
       self.run_pipeline_test(_a							,					[] )





  def 						A  (		self      :	Dict							,					_a      :	Any							,					_a      :	Union[str, Any]							,					_a      :	int )	->    List[str]:





       '''simple docstring'''






       if tokenizer is None or tokenizer.mask_token_id is None:
            self.skipTest('The provided tokenizer has no mask token, (probably reformer or wav2vec2)' )

       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )
       _SCREAMING_SNAKE_CASE							    =[
           f"This is another {tokenizer.mask_token} test",
       ]
       return fill_masker, examples





  def 						A  (		self      :	Dict							,					_a      :	List[Any]							,					_a      :	int )	->    Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =fill_masker.tokenizer
       _SCREAMING_SNAKE_CASE							    =fill_masker.model

       _SCREAMING_SNAKE_CASE							    =fill_masker(
           f"This is a {tokenizer.mask_token}"							,					)
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =fill_masker([f"This is a {tokenizer.mask_token}"] )
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =fill_masker([f"This is a {tokenizer.mask_token}", f"Another {tokenizer.mask_token} great test."] )
       self.assertEqual(
           _a							,					[
               [
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               ],
               [
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               ],
           ]							,					)

       with self.assertRaises(_a ):
            fill_masker([None] )
       # No mask_token is not supported
       with self.assertRaises(_a ):
            fill_masker('This is' )

       self.run_test_top_k(_a							,					_a )
       self.run_test_targets(_a							,					_a )
       self.run_test_top_k_targets(_a							,					_a )
       self.fill_mask_with_duplicate_targets_and_top_k(_a							,					_a )
       self.fill_mask_with_multiple_masks(_a							,					_a )





  def 						A  (		self      :	int							,					_a      :	List[str]							,					_a      :	str )	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =tokenizer.get_vocab()
       _SCREAMING_SNAKE_CASE							    =sorted(vocab.keys() )[:2]
       # Pipeline argument
       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a							,					targets=_a )
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}" )
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)
       _SCREAMING_SNAKE_CASE							    ={vocab[el] for el in targets}
       self.assertEqual({el['token'] for el in outputs}							,					_a )
       _SCREAMING_SNAKE_CASE							    =[tokenizer.decode([x] ) for x in target_ids]
       self.assertEqual({el['token_str'] for el in outputs}							,					set(_a ) )

       # Call argument
       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets=_a )
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)
       _SCREAMING_SNAKE_CASE							    ={vocab[el] for el in targets}
       self.assertEqual({el['token'] for el in outputs}							,					_a )
       _SCREAMING_SNAKE_CASE							    =[tokenizer.decode([x] ) for x in target_ids]
       self.assertEqual({el['token_str'] for el in outputs}							,					set(_a ) )

       # Score equivalence
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets=_a )
       _SCREAMING_SNAKE_CASE							    =[top_mask['token_str'] for top_mask in outputs]
       _SCREAMING_SNAKE_CASE							    =[top_mask['score'] for top_mask in outputs]

       # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
       if set(_a ) == set(_a ):
            _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets=_a )
            _SCREAMING_SNAKE_CASE							    =[top_mask['score'] for top_mask in unmasked_targets]
            self.assertEqual(nested_simplify(_a )							,					nested_simplify(_a ) )

       # Raises with invalid
       with self.assertRaises(_a ):
            _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets=[] )
       # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
       if "" not in tokenizer.get_vocab():
            with self.assertRaises(_a ):
                 _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets=[''] )
            with self.assertRaises(_a ):
                 _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					targets='' )





  def 						A  (		self      :	Any							,					_a      :	int							,					_a      :	List[str] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a							,					top_k=2 )
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}" )
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)

       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					top_k=2 )
       self.assertEqual(
           _a							,					[
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
           ]							,					)
       self.assertEqual(nested_simplify(_a )							,					nested_simplify(_a ) )





  def 						A  (		self      :	Dict							,					_a      :	Any							,					_a      :	int )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =tokenizer.get_vocab()
       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )

       # top_k=2, ntargets=3
       _SCREAMING_SNAKE_CASE							    =sorted(vocab.keys() )[:3]
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					top_k=2							,					targets=_a )

       # If we use the most probably targets, and filter differently, we should still
       # have the same results
       _SCREAMING_SNAKE_CASE							    =[el['token_str'] for el in sorted(_a							,					key=lambda _a : x["score"]							,					reverse=_a )]
       # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
       if set(_a ).issubset(_a ):
            _SCREAMING_SNAKE_CASE							    =fill_masker(f"This is a {tokenizer.mask_token}"							,					top_k=3							,					targets=_a )
            # They should yield exactly the same result
            self.assertEqual(nested_simplify(_a )							,					nested_simplify(_a ) )





  def 						A  (		self      :	Dict							,					_a      :	Dict							,					_a      :	str )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )
       _SCREAMING_SNAKE_CASE							    =tokenizer.get_vocab()
       # String duplicates + id duplicates
       _SCREAMING_SNAKE_CASE							    =sorted(vocab.keys() )[:3]
       _SCREAMING_SNAKE_CASE							    =[targets[0], targets[1], targets[0], targets[2], targets[1]]
       _SCREAMING_SNAKE_CASE							    =fill_masker(f"My name is {tokenizer.mask_token}"							,					targets=_a							,					top_k=10 )

       # The target list contains duplicates, so we can't output more
       # than them
       self.assertEqual(len(_a )							,					3 )





  def 						A  (		self      :	Dict							,					_a      :	List[Any]							,					_a      :	Dict )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =FillMaskPipeline(model=_a							,					tokenizer=_a )

       _SCREAMING_SNAKE_CASE							    =fill_masker(
           f"This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"							,					top_k=2 )
       self.assertEqual(
           _a							,					[
               [
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               ],
               [
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               ],
               [
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
                   {'sequence': ANY(_a ), 'score': ANY(_a ), 'token': ANY(_a ), 'token_str': ANY(_a )},
               ],
           ]							,					)



'''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 json
import os

from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
from ...utils.imports import is_botoa_available
from .config_args import SageMakerConfig
from .config_utils import (
    DYNAMO_BACKENDS,
    _ask_field,
    _ask_options,
    _convert_dynamo_backend,
    _convert_mixed_precision,
    _convert_sagemaker_distributed_mode,
    _convert_yes_no_to_bool,
)


if is_botoa_available():
     import botoa  # noqa: F401


def        _lowerCAmelCase					(						_UpperCamelCase						:     Optional[int]				)      ->			List[str]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =botoa.client('iam'				)

     _SCREAMING_SNAKE_CASE							    ={
         'Version': '2012-10-17',
         'Statement': [
             {'Effect': 'Allow', 'Principal': {'Service': 'sagemaker.amazonaws.com'}, 'Action': 'sts:AssumeRole'}
         ],
     }
     try:
          # create the role, associated with the chosen trust policy
          iam_client.create_role(
              RoleName=_UpperCamelCase							, AssumeRolePolicyDocument=json.dumps(_UpperCamelCase							, indent=2				)				)
          _SCREAMING_SNAKE_CASE							    ={
              'Version': '2012-10-17',
              'Statement': [
                  {
                      'Effect': 'Allow',
                      'Action': [
                          'sagemaker:*',
                          'ecr:GetDownloadUrlForLayer',
                          'ecr:BatchGetImage',
                          'ecr:BatchCheckLayerAvailability',
                          'ecr:GetAuthorizationToken',
                          'cloudwatch:PutMetricData',
                          'cloudwatch:GetMetricData',
                          'cloudwatch:GetMetricStatistics',
                          'cloudwatch:ListMetrics',
                          'logs:CreateLogGroup',
                          'logs:CreateLogStream',
                          'logs:DescribeLogStreams',
                          'logs:PutLogEvents',
                          'logs:GetLogEvents',
                          's3:CreateBucket',
                          's3:ListBucket',
                          's3:GetBucketLocation',
                          's3:GetObject',
                          's3:PutObject',
                      ],
                      'Resource': '*',
                  }
              ],
          }
          # attach policy to role
          iam_client.put_role_policy(
              RoleName=_UpperCamelCase							, PolicyName=f"{role_name}_policy_permission"							, PolicyDocument=json.dumps(_UpperCamelCase							, indent=2				)							, )
     except iam_client.exceptions.EntityAlreadyExistsException:
          print(f"role {role_name} already exists. Using existing one"				)


def        _lowerCAmelCase					(						_UpperCamelCase						:     List[str]				)      ->			Optional[int]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =botoa.client('iam'				)
     return iam_client.get_role(RoleName=_UpperCamelCase				)["Role"]["Arn"]







def        _lowerCAmelCase					(						)      ->			Optional[int]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =_ask_options(
         'How do you want to authorize?'							, ['AWS Profile', 'Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) ']							, _UpperCamelCase							, )
     _SCREAMING_SNAKE_CASE							    =None
     if credentials_configuration == 0:
          _SCREAMING_SNAKE_CASE							    =_ask_field('Enter your AWS Profile name: [default] '							, default='default'				)
          _SCREAMING_SNAKE_CASE							    =aws_profile
     else:
          print(
              'Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,'
              '`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`'				)
          _SCREAMING_SNAKE_CASE							    =_ask_field('AWS Access Key ID: '				)
          _SCREAMING_SNAKE_CASE							    =aws_access_key_id

          _SCREAMING_SNAKE_CASE							    =_ask_field('AWS Secret Access Key: '				)
          _SCREAMING_SNAKE_CASE							    =aws_secret_access_key

     _SCREAMING_SNAKE_CASE							    =_ask_field('Enter your AWS Region: [us-east-1]'							, default='us-east-1'				)
     _SCREAMING_SNAKE_CASE							    =aws_region

     _SCREAMING_SNAKE_CASE							    =_ask_options(
         'Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?'							, ['Provide IAM Role name', 'Create new IAM role using credentials']							, _UpperCamelCase							, )
     if role_management == 0:
          _SCREAMING_SNAKE_CASE							    =_ask_field('Enter your IAM role name: '				)
     else:
          _SCREAMING_SNAKE_CASE							    ='accelerate_sagemaker_execution_role'
          print(f"Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials"				)
          _create_iam_role_for_sagemaker(_UpperCamelCase				)

     _SCREAMING_SNAKE_CASE							    =_ask_field(
         'Do you want to use custom Docker image? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
     _SCREAMING_SNAKE_CASE							    =None
     if is_custom_docker_image:
          _SCREAMING_SNAKE_CASE							    =_ask_field('Enter your Docker image: '							, lambda _UpperCamelCase				: str(_UpperCamelCase				).lower()				)

     _SCREAMING_SNAKE_CASE							    =_ask_field(
         'Do you want to provide SageMaker input channels with data locations? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
     _SCREAMING_SNAKE_CASE							    =None
     if is_sagemaker_inputs_enabled:
          _SCREAMING_SNAKE_CASE							    =_ask_field(
              'Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): '							, lambda _UpperCamelCase				: str(_UpperCamelCase				).lower()							, )

     _SCREAMING_SNAKE_CASE							    =_ask_field(
         'Do you want to enable SageMaker metrics? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
     _SCREAMING_SNAKE_CASE							    =None
     if is_sagemaker_metrics_enabled:
          _SCREAMING_SNAKE_CASE							    =_ask_field(
              'Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): '							, lambda _UpperCamelCase				: str(_UpperCamelCase				).lower()							, )

     _SCREAMING_SNAKE_CASE							    =_ask_options(
         'What is the distributed mode?'							, ['No distributed training', 'Data parallelism']							, _convert_sagemaker_distributed_mode							, )
     _SCREAMING_SNAKE_CASE							    ={}
     _SCREAMING_SNAKE_CASE							    =_ask_field(
         'Do you wish to optimize your script with torch dynamo?[yes/NO]:'							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
     if use_dynamo:
          _SCREAMING_SNAKE_CASE							    ='dynamo_'
          _SCREAMING_SNAKE_CASE							    =_ask_options(
              'Which dynamo backend would you like to use?'							, [x.lower() for x in DYNAMO_BACKENDS]							, _convert_dynamo_backend							, default=2							, )
          _SCREAMING_SNAKE_CASE							    =_ask_field(
              'Do you want to customize the defaults sent to torch.compile? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )

          if use_custom_options:
               _SCREAMING_SNAKE_CASE							    =_ask_options(
                   'Which mode do you want to use?'							, _UpperCamelCase							, lambda _UpperCamelCase				: TORCH_DYNAMO_MODES[int(_UpperCamelCase				)]							, default='default'							, )
               _SCREAMING_SNAKE_CASE							    =_ask_field(
                   'Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
               _SCREAMING_SNAKE_CASE							    =_ask_field(
                   'Do you want to enable dynamic shape tracing? [yes/NO]: '							, _convert_yes_no_to_bool							, default=_UpperCamelCase							, error_message='Please enter yes or no.'							, )
     _SCREAMING_SNAKE_CASE							    ='Which EC2 instance type you want to use for your training?'
     if distributed_type != SageMakerDistributedType.NO:
          _SCREAMING_SNAKE_CASE							    =_ask_options(
              _UpperCamelCase							, _UpperCamelCase							, lambda _UpperCamelCase				: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(_UpperCamelCase				)]				)
     else:
          eca_instance_query += "? [ml.p3.2xlarge]:"
          _SCREAMING_SNAKE_CASE							    =_ask_field(_UpperCamelCase							, lambda _UpperCamelCase				: str(_UpperCamelCase				).lower()							, default='ml.p3.2xlarge'				)

     _SCREAMING_SNAKE_CASE							    =1
     if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
          _SCREAMING_SNAKE_CASE							    =_ask_field(
              'How many machines do you want use? [1]: '							, _UpperCamelCase							, default=1							, )

     _SCREAMING_SNAKE_CASE							    =_ask_options(
         'Do you wish to use FP16 or BF16 (mixed precision)?'							, ['no', 'fp16', 'bf16', 'fp8']							, _convert_mixed_precision							, )

     if use_dynamo and mixed_precision == "no":
          print(
              'Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.'				)

     return SageMakerConfig(
         image_uri=_UpperCamelCase							, compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER							, distributed_type=_UpperCamelCase							, use_cpu=_UpperCamelCase							, dynamo_config=_UpperCamelCase							, eca_instance_type=_UpperCamelCase							, profile=_UpperCamelCase							, region=_UpperCamelCase							, iam_role_name=_UpperCamelCase							, mixed_precision=_UpperCamelCase							, num_machines=_UpperCamelCase							, sagemaker_inputs_file=_UpperCamelCase							, sagemaker_metrics_file=_UpperCamelCase							, )
