We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
SageMaker config TypeError
### System Info

```Shell
- `Accelerate` version: 0.22.0
- Platform: Linux-5.10.184-175.731.amzn2.x86_64-x86_64-with-glibc2.26
- Python version: 3.10.8
- Numpy version: 1.25.2
- PyTorch version (GPU?): 2.0.1+cu117 (False)
- PyTorch XPU available: False
- PyTorch NPU available: False
```


### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] One of the scripts in the examples/ folder of Accelerate or an officially supported `no_trainer` script in the `examples` folder of the `transformers` repo (such as `run_no_trainer_glue.py`)
- [ ] My own task or dataset (give details below)

### Reproduction

1. Install accelerate[sagemaker]
2. run `accelerate config`
3. Go through the configuration process

The result will be:
```
Traceback (most recent call last):                                                                                                                                                                                                                                            
  File "/home/ec2-user/anaconda3/envs/JupyterSystemEnv/bin/accelerate", line 8, in <module>                                                                                                                                                                                   
    sys.exit(main())                                                                                                                                                                                                                                                          
  File "/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/accelerate_cli.py", line 45, in main                                                                                                                                  
    args.func(args)
  File "/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/config.py", line 67, in config_command
    config = get_user_input()
  File "/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/config.py", line 38, in get_user_input
    config = get_sagemaker_input()
  File "/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/sagemaker.py", line 243, in get_sagemaker_input
    return SageMakerConfig(
TypeError: SageMakerConfig.__init__() missing 1 required positional argument: 'debug'
```

### Expected behavior

accelerate config works

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/accelerate/commands/config/sagemaker.py`
Content:
```
1 #!/usr/bin/env python
2 
3 # Copyright 2021 The HuggingFace Team. All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import json
17 import os
18 
19 from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
20 from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
21 from ...utils.imports import is_boto3_available
22 from .config_args import SageMakerConfig
23 from .config_utils import (
24     DYNAMO_BACKENDS,
25     _ask_field,
26     _ask_options,
27     _convert_dynamo_backend,
28     _convert_mixed_precision,
29     _convert_sagemaker_distributed_mode,
30     _convert_yes_no_to_bool,
31 )
32 
33 
34 if is_boto3_available():
35     import boto3  # noqa: F401
36 
37 
38 def _create_iam_role_for_sagemaker(role_name):
39     iam_client = boto3.client("iam")
40 
41     sagemaker_trust_policy = {
42         "Version": "2012-10-17",
43         "Statement": [
44             {"Effect": "Allow", "Principal": {"Service": "sagemaker.amazonaws.com"}, "Action": "sts:AssumeRole"}
45         ],
46     }
47     try:
48         # create the role, associated with the chosen trust policy
49         iam_client.create_role(
50             RoleName=role_name, AssumeRolePolicyDocument=json.dumps(sagemaker_trust_policy, indent=2)
51         )
52         policy_document = {
53             "Version": "2012-10-17",
54             "Statement": [
55                 {
56                     "Effect": "Allow",
57                     "Action": [
58                         "sagemaker:*",
59                         "ecr:GetDownloadUrlForLayer",
60                         "ecr:BatchGetImage",
61                         "ecr:BatchCheckLayerAvailability",
62                         "ecr:GetAuthorizationToken",
63                         "cloudwatch:PutMetricData",
64                         "cloudwatch:GetMetricData",
65                         "cloudwatch:GetMetricStatistics",
66                         "cloudwatch:ListMetrics",
67                         "logs:CreateLogGroup",
68                         "logs:CreateLogStream",
69                         "logs:DescribeLogStreams",
70                         "logs:PutLogEvents",
71                         "logs:GetLogEvents",
72                         "s3:CreateBucket",
73                         "s3:ListBucket",
74                         "s3:GetBucketLocation",
75                         "s3:GetObject",
76                         "s3:PutObject",
77                     ],
78                     "Resource": "*",
79                 }
80             ],
81         }
82         # attach policy to role
83         iam_client.put_role_policy(
84             RoleName=role_name,
85             PolicyName=f"{role_name}_policy_permission",
86             PolicyDocument=json.dumps(policy_document, indent=2),
87         )
88     except iam_client.exceptions.EntityAlreadyExistsException:
89         print(f"role {role_name} already exists. Using existing one")
90 
91 
92 def _get_iam_role_arn(role_name):
93     iam_client = boto3.client("iam")
94     return iam_client.get_role(RoleName=role_name)["Role"]["Arn"]
95 
96 
97 def get_sagemaker_input():
98     credentials_configuration = _ask_options(
99         "How do you want to authorize?",
100         ["AWS Profile", "Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) "],
101         int,
102     )
103     aws_profile = None
104     if credentials_configuration == 0:
105         aws_profile = _ask_field("Enter your AWS Profile name: [default] ", default="default")
106         os.environ["AWS_PROFILE"] = aws_profile
107     else:
108         print(
109             "Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,"
110             "`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`"
111         )
112         aws_access_key_id = _ask_field("AWS Access Key ID: ")
113         os.environ["AWS_ACCESS_KEY_ID"] = aws_access_key_id
114 
115         aws_secret_access_key = _ask_field("AWS Secret Access Key: ")
116         os.environ["AWS_SECRET_ACCESS_KEY"] = aws_secret_access_key
117 
118     aws_region = _ask_field("Enter your AWS Region: [us-east-1]", default="us-east-1")
119     os.environ["AWS_DEFAULT_REGION"] = aws_region
120 
121     role_management = _ask_options(
122         "Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?",
123         ["Provide IAM Role name", "Create new IAM role using credentials"],
124         int,
125     )
126     if role_management == 0:
127         iam_role_name = _ask_field("Enter your IAM role name: ")
128     else:
129         iam_role_name = "accelerate_sagemaker_execution_role"
130         print(f'Accelerate will create an iam role "{iam_role_name}" using the provided credentials')
131         _create_iam_role_for_sagemaker(iam_role_name)
132 
133     is_custom_docker_image = _ask_field(
134         "Do you want to use custom Docker image? [yes/NO]: ",
135         _convert_yes_no_to_bool,
136         default=False,
137         error_message="Please enter yes or no.",
138     )
139     docker_image = None
140     if is_custom_docker_image:
141         docker_image = _ask_field("Enter your Docker image: ", lambda x: str(x).lower())
142 
143     is_sagemaker_inputs_enabled = _ask_field(
144         "Do you want to provide SageMaker input channels with data locations? [yes/NO]: ",
145         _convert_yes_no_to_bool,
146         default=False,
147         error_message="Please enter yes or no.",
148     )
149     sagemaker_inputs_file = None
150     if is_sagemaker_inputs_enabled:
151         sagemaker_inputs_file = _ask_field(
152             "Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ",
153             lambda x: str(x).lower(),
154         )
155 
156     is_sagemaker_metrics_enabled = _ask_field(
157         "Do you want to enable SageMaker metrics? [yes/NO]: ",
158         _convert_yes_no_to_bool,
159         default=False,
160         error_message="Please enter yes or no.",
161     )
162     sagemaker_metrics_file = None
163     if is_sagemaker_metrics_enabled:
164         sagemaker_metrics_file = _ask_field(
165             "Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ",
166             lambda x: str(x).lower(),
167         )
168 
169     distributed_type = _ask_options(
170         "What is the distributed mode?",
171         ["No distributed training", "Data parallelism"],
172         _convert_sagemaker_distributed_mode,
173     )
174     dynamo_config = {}
175     use_dynamo = _ask_field(
176         "Do you wish to optimize your script with torch dynamo?[yes/NO]:",
177         _convert_yes_no_to_bool,
178         default=False,
179         error_message="Please enter yes or no.",
180     )
181     if use_dynamo:
182         prefix = "dynamo_"
183         dynamo_config[prefix + "backend"] = _ask_options(
184             "Which dynamo backend would you like to use?",
185             [x.lower() for x in DYNAMO_BACKENDS],
186             _convert_dynamo_backend,
187             default=2,
188         )
189         use_custom_options = _ask_field(
190             "Do you want to customize the defaults sent to torch.compile? [yes/NO]: ",
191             _convert_yes_no_to_bool,
192             default=False,
193             error_message="Please enter yes or no.",
194         )
195 
196         if use_custom_options:
197             dynamo_config[prefix + "mode"] = _ask_options(
198                 "Which mode do you want to use?",
199                 TORCH_DYNAMO_MODES,
200                 lambda x: TORCH_DYNAMO_MODES[int(x)],
201                 default="default",
202             )
203             dynamo_config[prefix + "use_fullgraph"] = _ask_field(
204                 "Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ",
205                 _convert_yes_no_to_bool,
206                 default=False,
207                 error_message="Please enter yes or no.",
208             )
209             dynamo_config[prefix + "use_dynamic"] = _ask_field(
210                 "Do you want to enable dynamic shape tracing? [yes/NO]: ",
211                 _convert_yes_no_to_bool,
212                 default=False,
213                 error_message="Please enter yes or no.",
214             )
215     ec2_instance_query = "Which EC2 instance type you want to use for your training?"
216     if distributed_type != SageMakerDistributedType.NO:
217         ec2_instance_type = _ask_options(
218             ec2_instance_query, SAGEMAKER_PARALLEL_EC2_INSTANCES, lambda x: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(x)]
219         )
220     else:
221         ec2_instance_query += "? [ml.p3.2xlarge]:"
222         ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default="ml.p3.2xlarge")
223 
224     num_machines = 1
225     if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
226         num_machines = _ask_field(
227             "How many machines do you want use? [1]: ",
228             int,
229             default=1,
230         )
231 
232     mixed_precision = _ask_options(
233         "Do you wish to use FP16 or BF16 (mixed precision)?",
234         ["no", "fp16", "bf16", "fp8"],
235         _convert_mixed_precision,
236     )
237 
238     if use_dynamo and mixed_precision == "no":
239         print(
240             "Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts."
241         )
242 
243     return SageMakerConfig(
244         image_uri=docker_image,
245         compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER,
246         distributed_type=distributed_type,
247         use_cpu=False,
248         dynamo_config=dynamo_config,
249         ec2_instance_type=ec2_instance_type,
250         profile=aws_profile,
251         region=aws_region,
252         iam_role_name=iam_role_name,
253         mixed_precision=mixed_precision,
254         num_machines=num_machines,
255         sagemaker_inputs_file=sagemaker_inputs_file,
256         sagemaker_metrics_file=sagemaker_metrics_file,
257     )
258
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/accelerate/commands/config/sagemaker.py b/src/accelerate/commands/config/sagemaker.py
--- a/src/accelerate/commands/config/sagemaker.py
+++ b/src/accelerate/commands/config/sagemaker.py
@@ -221,6 +221,15 @@
         ec2_instance_query += "? [ml.p3.2xlarge]:"
         ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default="ml.p3.2xlarge")
 
+    debug = False
+    if distributed_type != SageMakerDistributedType.NO:
+        debug = _ask_field(
+            "Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: ",
+            _convert_yes_no_to_bool,
+            default=False,
+            error_message="Please enter yes or no.",
+        )
+
     num_machines = 1
     if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
         num_machines = _ask_field(
@@ -254,4 +263,5 @@
         num_machines=num_machines,
         sagemaker_inputs_file=sagemaker_inputs_file,
         sagemaker_metrics_file=sagemaker_metrics_file,
+        debug=debug,
     )

{"golden_diff": "diff --git a/src/accelerate/commands/config/sagemaker.py b/src/accelerate/commands/config/sagemaker.py\n--- a/src/accelerate/commands/config/sagemaker.py\n+++ b/src/accelerate/commands/config/sagemaker.py\n@@ -221,6 +221,15 @@\n         ec2_instance_query += \"? [ml.p3.2xlarge]:\"\n         ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default=\"ml.p3.2xlarge\")\n \n+    debug = False\n+    if distributed_type != SageMakerDistributedType.NO:\n+        debug = _ask_field(\n+            \"Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: \",\n+            _convert_yes_no_to_bool,\n+            default=False,\n+            error_message=\"Please enter yes or no.\",\n+        )\n+\n     num_machines = 1\n     if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):\n         num_machines = _ask_field(\n@@ -254,4 +263,5 @@\n         num_machines=num_machines,\n         sagemaker_inputs_file=sagemaker_inputs_file,\n         sagemaker_metrics_file=sagemaker_metrics_file,\n+        debug=debug,\n     )\n", "issue": "SageMaker config TypeError\n### System Info\n\n```Shell\n- `Accelerate` version: 0.22.0\r\n- Platform: Linux-5.10.184-175.731.amzn2.x86_64-x86_64-with-glibc2.26\r\n- Python version: 3.10.8\r\n- Numpy version: 1.25.2\r\n- PyTorch version (GPU?): 2.0.1+cu117 (False)\r\n- PyTorch XPU available: False\r\n- PyTorch NPU available: False\n```\n\n\n### Information\n\n- [x] The official example scripts\n- [ ] My own modified scripts\n\n### Tasks\n\n- [ ] One of the scripts in the examples/ folder of Accelerate or an officially supported `no_trainer` script in the `examples` folder of the `transformers` repo (such as `run_no_trainer_glue.py`)\n- [ ] My own task or dataset (give details below)\n\n### Reproduction\n\n1. Install accelerate[sagemaker]\r\n2. run `accelerate config`\r\n3. Go through the configuration process\r\n\r\nThe result will be:\r\n```\r\nTraceback (most recent call last):                                                                                                                                                                                                                                            \r\n  File \"/home/ec2-user/anaconda3/envs/JupyterSystemEnv/bin/accelerate\", line 8, in <module>                                                                                                                                                                                   \r\n    sys.exit(main())                                                                                                                                                                                                                                                          \r\n  File \"/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/accelerate_cli.py\", line 45, in main                                                                                                                                  \r\n    args.func(args)\r\n  File \"/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/config.py\", line 67, in config_command\r\n    config = get_user_input()\r\n  File \"/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/config.py\", line 38, in get_user_input\r\n    config = get_sagemaker_input()\r\n  File \"/home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/accelerate/commands/config/sagemaker.py\", line 243, in get_sagemaker_input\r\n    return SageMakerConfig(\r\nTypeError: SageMakerConfig.__init__() missing 1 required positional argument: 'debug'\r\n```\n\n### Expected behavior\n\naccelerate config works\n", "before_files": [{"content": "#!/usr/bin/env python\n\n# Copyright 2021 The HuggingFace Team. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport json\nimport os\n\nfrom ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES\nfrom ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType\nfrom ...utils.imports import is_boto3_available\nfrom .config_args import SageMakerConfig\nfrom .config_utils import (\n    DYNAMO_BACKENDS,\n    _ask_field,\n    _ask_options,\n    _convert_dynamo_backend,\n    _convert_mixed_precision,\n    _convert_sagemaker_distributed_mode,\n    _convert_yes_no_to_bool,\n)\n\n\nif is_boto3_available():\n    import boto3  # noqa: F401\n\n\ndef _create_iam_role_for_sagemaker(role_name):\n    iam_client = boto3.client(\"iam\")\n\n    sagemaker_trust_policy = {\n        \"Version\": \"2012-10-17\",\n        \"Statement\": [\n            {\"Effect\": \"Allow\", \"Principal\": {\"Service\": \"sagemaker.amazonaws.com\"}, \"Action\": \"sts:AssumeRole\"}\n        ],\n    }\n    try:\n        # create the role, associated with the chosen trust policy\n        iam_client.create_role(\n            RoleName=role_name, AssumeRolePolicyDocument=json.dumps(sagemaker_trust_policy, indent=2)\n        )\n        policy_document = {\n            \"Version\": \"2012-10-17\",\n            \"Statement\": [\n                {\n                    \"Effect\": \"Allow\",\n                    \"Action\": [\n                        \"sagemaker:*\",\n                        \"ecr:GetDownloadUrlForLayer\",\n                        \"ecr:BatchGetImage\",\n                        \"ecr:BatchCheckLayerAvailability\",\n                        \"ecr:GetAuthorizationToken\",\n                        \"cloudwatch:PutMetricData\",\n                        \"cloudwatch:GetMetricData\",\n                        \"cloudwatch:GetMetricStatistics\",\n                        \"cloudwatch:ListMetrics\",\n                        \"logs:CreateLogGroup\",\n                        \"logs:CreateLogStream\",\n                        \"logs:DescribeLogStreams\",\n                        \"logs:PutLogEvents\",\n                        \"logs:GetLogEvents\",\n                        \"s3:CreateBucket\",\n                        \"s3:ListBucket\",\n                        \"s3:GetBucketLocation\",\n                        \"s3:GetObject\",\n                        \"s3:PutObject\",\n                    ],\n                    \"Resource\": \"*\",\n                }\n            ],\n        }\n        # attach policy to role\n        iam_client.put_role_policy(\n            RoleName=role_name,\n            PolicyName=f\"{role_name}_policy_permission\",\n            PolicyDocument=json.dumps(policy_document, indent=2),\n        )\n    except iam_client.exceptions.EntityAlreadyExistsException:\n        print(f\"role {role_name} already exists. Using existing one\")\n\n\ndef _get_iam_role_arn(role_name):\n    iam_client = boto3.client(\"iam\")\n    return iam_client.get_role(RoleName=role_name)[\"Role\"][\"Arn\"]\n\n\ndef get_sagemaker_input():\n    credentials_configuration = _ask_options(\n        \"How do you want to authorize?\",\n        [\"AWS Profile\", \"Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) \"],\n        int,\n    )\n    aws_profile = None\n    if credentials_configuration == 0:\n        aws_profile = _ask_field(\"Enter your AWS Profile name: [default] \", default=\"default\")\n        os.environ[\"AWS_PROFILE\"] = aws_profile\n    else:\n        print(\n            \"Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,\"\n            \"`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`\"\n        )\n        aws_access_key_id = _ask_field(\"AWS Access Key ID: \")\n        os.environ[\"AWS_ACCESS_KEY_ID\"] = aws_access_key_id\n\n        aws_secret_access_key = _ask_field(\"AWS Secret Access Key: \")\n        os.environ[\"AWS_SECRET_ACCESS_KEY\"] = aws_secret_access_key\n\n    aws_region = _ask_field(\"Enter your AWS Region: [us-east-1]\", default=\"us-east-1\")\n    os.environ[\"AWS_DEFAULT_REGION\"] = aws_region\n\n    role_management = _ask_options(\n        \"Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?\",\n        [\"Provide IAM Role name\", \"Create new IAM role using credentials\"],\n        int,\n    )\n    if role_management == 0:\n        iam_role_name = _ask_field(\"Enter your IAM role name: \")\n    else:\n        iam_role_name = \"accelerate_sagemaker_execution_role\"\n        print(f'Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials')\n        _create_iam_role_for_sagemaker(iam_role_name)\n\n    is_custom_docker_image = _ask_field(\n        \"Do you want to use custom Docker image? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    docker_image = None\n    if is_custom_docker_image:\n        docker_image = _ask_field(\"Enter your Docker image: \", lambda x: str(x).lower())\n\n    is_sagemaker_inputs_enabled = _ask_field(\n        \"Do you want to provide SageMaker input channels with data locations? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    sagemaker_inputs_file = None\n    if is_sagemaker_inputs_enabled:\n        sagemaker_inputs_file = _ask_field(\n            \"Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): \",\n            lambda x: str(x).lower(),\n        )\n\n    is_sagemaker_metrics_enabled = _ask_field(\n        \"Do you want to enable SageMaker metrics? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    sagemaker_metrics_file = None\n    if is_sagemaker_metrics_enabled:\n        sagemaker_metrics_file = _ask_field(\n            \"Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): \",\n            lambda x: str(x).lower(),\n        )\n\n    distributed_type = _ask_options(\n        \"What is the distributed mode?\",\n        [\"No distributed training\", \"Data parallelism\"],\n        _convert_sagemaker_distributed_mode,\n    )\n    dynamo_config = {}\n    use_dynamo = _ask_field(\n        \"Do you wish to optimize your script with torch dynamo?[yes/NO]:\",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    if use_dynamo:\n        prefix = \"dynamo_\"\n        dynamo_config[prefix + \"backend\"] = _ask_options(\n            \"Which dynamo backend would you like to use?\",\n            [x.lower() for x in DYNAMO_BACKENDS],\n            _convert_dynamo_backend,\n            default=2,\n        )\n        use_custom_options = _ask_field(\n            \"Do you want to customize the defaults sent to torch.compile? [yes/NO]: \",\n            _convert_yes_no_to_bool,\n            default=False,\n            error_message=\"Please enter yes or no.\",\n        )\n\n        if use_custom_options:\n            dynamo_config[prefix + \"mode\"] = _ask_options(\n                \"Which mode do you want to use?\",\n                TORCH_DYNAMO_MODES,\n                lambda x: TORCH_DYNAMO_MODES[int(x)],\n                default=\"default\",\n            )\n            dynamo_config[prefix + \"use_fullgraph\"] = _ask_field(\n                \"Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: \",\n                _convert_yes_no_to_bool,\n                default=False,\n                error_message=\"Please enter yes or no.\",\n            )\n            dynamo_config[prefix + \"use_dynamic\"] = _ask_field(\n                \"Do you want to enable dynamic shape tracing? [yes/NO]: \",\n                _convert_yes_no_to_bool,\n                default=False,\n                error_message=\"Please enter yes or no.\",\n            )\n    ec2_instance_query = \"Which EC2 instance type you want to use for your training?\"\n    if distributed_type != SageMakerDistributedType.NO:\n        ec2_instance_type = _ask_options(\n            ec2_instance_query, SAGEMAKER_PARALLEL_EC2_INSTANCES, lambda x: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(x)]\n        )\n    else:\n        ec2_instance_query += \"? [ml.p3.2xlarge]:\"\n        ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default=\"ml.p3.2xlarge\")\n\n    num_machines = 1\n    if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):\n        num_machines = _ask_field(\n            \"How many machines do you want use? [1]: \",\n            int,\n            default=1,\n        )\n\n    mixed_precision = _ask_options(\n        \"Do you wish to use FP16 or BF16 (mixed precision)?\",\n        [\"no\", \"fp16\", \"bf16\", \"fp8\"],\n        _convert_mixed_precision,\n    )\n\n    if use_dynamo and mixed_precision == \"no\":\n        print(\n            \"Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.\"\n        )\n\n    return SageMakerConfig(\n        image_uri=docker_image,\n        compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER,\n        distributed_type=distributed_type,\n        use_cpu=False,\n        dynamo_config=dynamo_config,\n        ec2_instance_type=ec2_instance_type,\n        profile=aws_profile,\n        region=aws_region,\n        iam_role_name=iam_role_name,\n        mixed_precision=mixed_precision,\n        num_machines=num_machines,\n        sagemaker_inputs_file=sagemaker_inputs_file,\n        sagemaker_metrics_file=sagemaker_metrics_file,\n    )\n", "path": "src/accelerate/commands/config/sagemaker.py"}], "after_files": [{"content": "#!/usr/bin/env python\n\n# Copyright 2021 The HuggingFace Team. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport json\nimport os\n\nfrom ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES\nfrom ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType\nfrom ...utils.imports import is_boto3_available\nfrom .config_args import SageMakerConfig\nfrom .config_utils import (\n    DYNAMO_BACKENDS,\n    _ask_field,\n    _ask_options,\n    _convert_dynamo_backend,\n    _convert_mixed_precision,\n    _convert_sagemaker_distributed_mode,\n    _convert_yes_no_to_bool,\n)\n\n\nif is_boto3_available():\n    import boto3  # noqa: F401\n\n\ndef _create_iam_role_for_sagemaker(role_name):\n    iam_client = boto3.client(\"iam\")\n\n    sagemaker_trust_policy = {\n        \"Version\": \"2012-10-17\",\n        \"Statement\": [\n            {\"Effect\": \"Allow\", \"Principal\": {\"Service\": \"sagemaker.amazonaws.com\"}, \"Action\": \"sts:AssumeRole\"}\n        ],\n    }\n    try:\n        # create the role, associated with the chosen trust policy\n        iam_client.create_role(\n            RoleName=role_name, AssumeRolePolicyDocument=json.dumps(sagemaker_trust_policy, indent=2)\n        )\n        policy_document = {\n            \"Version\": \"2012-10-17\",\n            \"Statement\": [\n                {\n                    \"Effect\": \"Allow\",\n                    \"Action\": [\n                        \"sagemaker:*\",\n                        \"ecr:GetDownloadUrlForLayer\",\n                        \"ecr:BatchGetImage\",\n                        \"ecr:BatchCheckLayerAvailability\",\n                        \"ecr:GetAuthorizationToken\",\n                        \"cloudwatch:PutMetricData\",\n                        \"cloudwatch:GetMetricData\",\n                        \"cloudwatch:GetMetricStatistics\",\n                        \"cloudwatch:ListMetrics\",\n                        \"logs:CreateLogGroup\",\n                        \"logs:CreateLogStream\",\n                        \"logs:DescribeLogStreams\",\n                        \"logs:PutLogEvents\",\n                        \"logs:GetLogEvents\",\n                        \"s3:CreateBucket\",\n                        \"s3:ListBucket\",\n                        \"s3:GetBucketLocation\",\n                        \"s3:GetObject\",\n                        \"s3:PutObject\",\n                    ],\n                    \"Resource\": \"*\",\n                }\n            ],\n        }\n        # attach policy to role\n        iam_client.put_role_policy(\n            RoleName=role_name,\n            PolicyName=f\"{role_name}_policy_permission\",\n            PolicyDocument=json.dumps(policy_document, indent=2),\n        )\n    except iam_client.exceptions.EntityAlreadyExistsException:\n        print(f\"role {role_name} already exists. Using existing one\")\n\n\ndef _get_iam_role_arn(role_name):\n    iam_client = boto3.client(\"iam\")\n    return iam_client.get_role(RoleName=role_name)[\"Role\"][\"Arn\"]\n\n\ndef get_sagemaker_input():\n    credentials_configuration = _ask_options(\n        \"How do you want to authorize?\",\n        [\"AWS Profile\", \"Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) \"],\n        int,\n    )\n    aws_profile = None\n    if credentials_configuration == 0:\n        aws_profile = _ask_field(\"Enter your AWS Profile name: [default] \", default=\"default\")\n        os.environ[\"AWS_PROFILE\"] = aws_profile\n    else:\n        print(\n            \"Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,\"\n            \"`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`\"\n        )\n        aws_access_key_id = _ask_field(\"AWS Access Key ID: \")\n        os.environ[\"AWS_ACCESS_KEY_ID\"] = aws_access_key_id\n\n        aws_secret_access_key = _ask_field(\"AWS Secret Access Key: \")\n        os.environ[\"AWS_SECRET_ACCESS_KEY\"] = aws_secret_access_key\n\n    aws_region = _ask_field(\"Enter your AWS Region: [us-east-1]\", default=\"us-east-1\")\n    os.environ[\"AWS_DEFAULT_REGION\"] = aws_region\n\n    role_management = _ask_options(\n        \"Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?\",\n        [\"Provide IAM Role name\", \"Create new IAM role using credentials\"],\n        int,\n    )\n    if role_management == 0:\n        iam_role_name = _ask_field(\"Enter your IAM role name: \")\n    else:\n        iam_role_name = \"accelerate_sagemaker_execution_role\"\n        print(f'Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials')\n        _create_iam_role_for_sagemaker(iam_role_name)\n\n    is_custom_docker_image = _ask_field(\n        \"Do you want to use custom Docker image? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    docker_image = None\n    if is_custom_docker_image:\n        docker_image = _ask_field(\"Enter your Docker image: \", lambda x: str(x).lower())\n\n    is_sagemaker_inputs_enabled = _ask_field(\n        \"Do you want to provide SageMaker input channels with data locations? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    sagemaker_inputs_file = None\n    if is_sagemaker_inputs_enabled:\n        sagemaker_inputs_file = _ask_field(\n            \"Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): \",\n            lambda x: str(x).lower(),\n        )\n\n    is_sagemaker_metrics_enabled = _ask_field(\n        \"Do you want to enable SageMaker metrics? [yes/NO]: \",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    sagemaker_metrics_file = None\n    if is_sagemaker_metrics_enabled:\n        sagemaker_metrics_file = _ask_field(\n            \"Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): \",\n            lambda x: str(x).lower(),\n        )\n\n    distributed_type = _ask_options(\n        \"What is the distributed mode?\",\n        [\"No distributed training\", \"Data parallelism\"],\n        _convert_sagemaker_distributed_mode,\n    )\n    dynamo_config = {}\n    use_dynamo = _ask_field(\n        \"Do you wish to optimize your script with torch dynamo?[yes/NO]:\",\n        _convert_yes_no_to_bool,\n        default=False,\n        error_message=\"Please enter yes or no.\",\n    )\n    if use_dynamo:\n        prefix = \"dynamo_\"\n        dynamo_config[prefix + \"backend\"] = _ask_options(\n            \"Which dynamo backend would you like to use?\",\n            [x.lower() for x in DYNAMO_BACKENDS],\n            _convert_dynamo_backend,\n            default=2,\n        )\n        use_custom_options = _ask_field(\n            \"Do you want to customize the defaults sent to torch.compile? [yes/NO]: \",\n            _convert_yes_no_to_bool,\n            default=False,\n            error_message=\"Please enter yes or no.\",\n        )\n\n        if use_custom_options:\n            dynamo_config[prefix + \"mode\"] = _ask_options(\n                \"Which mode do you want to use?\",\n                TORCH_DYNAMO_MODES,\n                lambda x: TORCH_DYNAMO_MODES[int(x)],\n                default=\"default\",\n            )\n            dynamo_config[prefix + \"use_fullgraph\"] = _ask_field(\n                \"Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: \",\n                _convert_yes_no_to_bool,\n                default=False,\n                error_message=\"Please enter yes or no.\",\n            )\n            dynamo_config[prefix + \"use_dynamic\"] = _ask_field(\n                \"Do you want to enable dynamic shape tracing? [yes/NO]: \",\n                _convert_yes_no_to_bool,\n                default=False,\n                error_message=\"Please enter yes or no.\",\n            )\n    ec2_instance_query = \"Which EC2 instance type you want to use for your training?\"\n    if distributed_type != SageMakerDistributedType.NO:\n        ec2_instance_type = _ask_options(\n            ec2_instance_query, SAGEMAKER_PARALLEL_EC2_INSTANCES, lambda x: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(x)]\n        )\n    else:\n        ec2_instance_query += \"? [ml.p3.2xlarge]:\"\n        ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default=\"ml.p3.2xlarge\")\n\n    debug = False\n    if distributed_type != SageMakerDistributedType.NO:\n        debug = _ask_field(\n            \"Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: \",\n            _convert_yes_no_to_bool,\n            default=False,\n            error_message=\"Please enter yes or no.\",\n        )\n\n    num_machines = 1\n    if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):\n        num_machines = _ask_field(\n            \"How many machines do you want use? [1]: \",\n            int,\n            default=1,\n        )\n\n    mixed_precision = _ask_options(\n        \"Do you wish to use FP16 or BF16 (mixed precision)?\",\n        [\"no\", \"fp16\", \"bf16\", \"fp8\"],\n        _convert_mixed_precision,\n    )\n\n    if use_dynamo and mixed_precision == \"no\":\n        print(\n            \"Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.\"\n        )\n\n    return SageMakerConfig(\n        image_uri=docker_image,\n        compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER,\n        distributed_type=distributed_type,\n        use_cpu=False,\n        dynamo_config=dynamo_config,\n        ec2_instance_type=ec2_instance_type,\n        profile=aws_profile,\n        region=aws_region,\n        iam_role_name=iam_role_name,\n        mixed_precision=mixed_precision,\n        num_machines=num_machines,\n        sagemaker_inputs_file=sagemaker_inputs_file,\n        sagemaker_metrics_file=sagemaker_metrics_file,\n        debug=debug,\n    )\n", "path": "src/accelerate/commands/config/sagemaker.py"}]}