| 1 |
# Copyright 2021 The HuggingFace Team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
import unittest |
| 15 |
from dataclasses import dataclass |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from accelerate.commands.config.config_args import SageMakerConfig |
| 20 |
from accelerate.utils import ComputeEnvironment |
| 21 |
from accelerate.utils.launch import _convert_nargs_to_dict |
| 22 |
|
| 23 |
|
| 24 |
@dataclass |
| 25 |
class MockLaunchConfig(SageMakerConfig): |
| 26 |
compute_environment = ComputeEnvironment.AMAZON_SAGEMAKER |
| 27 |
fp16 = True |
| 28 |
ec2_instance_type = "ml.p3.2xlarge" |
| 29 |
iam_role_name = "accelerate_sagemaker_execution_role" |
| 30 |
profile = "hf-sm" |
| 31 |
region = "us-east-1" |
| 32 |
num_machines = 1 |
| 33 |
base_job_name = "accelerate-sagemaker-1" |
| 34 |
pytorch_version = "1.6" |
| 35 |
transformers_version = "4.4" |
| 36 |
training_script = "train.py" |
| 37 |
success_training_script_args = [ |
| 38 |
"--model_name_or_path", |
| 39 |
"bert", |
| 40 |
"--do_train", |
| 41 |
"False", |
| 42 |
"--epochs", |
| 43 |
"3", |
| 44 |
"--learning_rate", |
| 45 |
"5e-5", |
| 46 |
"--max_steps", |
| 47 |
"50.5", |
| 48 |
] |
| 49 |
fail_training_script_args = [ |
| 50 |
"--model_name_or_path", |
| 51 |
"bert", |
| 52 |
"--do_train", |
| 53 |
"--do_test", |
| 54 |
"False", |
| 55 |
"--do_predict", |
| 56 |
"--epochs", |
| 57 |
"3", |
| 58 |
"--learning_rate", |
| 59 |
"5e-5", |
| 60 |
"--max_steps", |
| 61 |
"50.5", |
| 62 |
] |
| 63 |
|
| 64 |
|
| 65 |
class SageMakerLaunch(unittest.TestCase): |
| 66 |
def test_args_convert(self): |
| 67 |
# If no defaults are changed, `to_kwargs` returns an empty dict. |
| 68 |
converted_args = _convert_nargs_to_dict(MockLaunchConfig.success_training_script_args) |
| 69 |
assert isinstance(converted_args["model_name_or_path"], str) |
| 70 |
assert isinstance(converted_args["do_train"], bool) |
| 71 |
assert isinstance(converted_args["epochs"], int) |
| 72 |
assert isinstance(converted_args["learning_rate"], float) |
| 73 |
assert isinstance(converted_args["max_steps"], float) |
| 74 |
|
| 75 |
with pytest.raises(ValueError): |
| 76 |
_convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args) |