

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# SDK for Python(Boto3)
<a name="debugger-built-in-rules-api.Boto3"></a>

Amazon SageMaker Debugger 기본 제공 규칙은 AWS Boto3 SageMaker AI 클라이언트의 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job) 함수를 사용하여 훈련 작업에 대해 구성할 수 있습니다. `RuleEvaluatorImage` 파라미터에 올바른 이미지 URI를 지정해야 하며, 다음 예제는 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job) 기능에 대해 요청 본문을 설정하는 방법을 안내합니다.

다음 코드는 TensorFlow를 사용하여 훈련 스크립트 `entry_point/train.py`이 준비되었다고 가정하고, `create_training_job()` 요청 본문에 대해 Debugger를 구성하고 `us-west-2`에서 훈련 작업을 시작하는 방법에 대한 전체 예제를 보여줍니다. 종합적인 예제 노트북을 찾으려면 [Amazon SageMaker Debugger(Boto3)를 사용한 TensorFlow 다중 GPU 다중 Node 훈련 작업 프로파일링](https://sagemaker-examples.readthedocs.io/en/latest/sagemaker-debugger/tensorflow_profiling/tf-resnet-profiling-multi-gpu-multi-node-boto3.html)을 참조하세요.

**참고**  
올바른 Docker 컨테이너 이미지를 사용해야 합니다. 사용 가능한 AWS 딥 러닝 컨테이너 이미지를 찾으려면 [사용 가능한 딥 러닝 컨테이너 이미지를 참조하세요](https://github.com/aws/deep-learning-containers/blob/master/available_images.md). Debugger 규칙을 사용하는 데 사용할 수 있는 도커 이미지의 전체 목록을 찾으려면 [Debugger 규칙용 도커 이미지](debugger-reference.md#debugger-docker-images-rules)을 참고하세요.

```
import sagemaker, boto3
import datetime, tarfile

# Start setting up a SageMaker session and a Boto3 SageMaker client
session = sagemaker.Session()
region = session.boto_region_name
bucket = session.default_bucket()

# Upload a training script to a default Amazon S3 bucket of the current SageMaker session
source = 'source.tar.gz'
project = '{{debugger-boto3-test}}'

tar = tarfile.open(source, 'w:gz')
tar.add ('{{entry_point/train.py}}') # Specify the directory and name of your training script
tar.close()

s3 = boto3.client('s3')
s3.upload_file(source, bucket, project+'/'+source)

# Set up a Boto3 session client for SageMaker
sm = boto3.Session(region_name=region).client("sagemaker")

# Start a training job
sm.create_training_job(
    TrainingJobName='debugger-boto3-'+datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'),
    HyperParameters={
        'sagemaker_submit_directory': 's3://'+bucket+'/'+project+'/'+source,
        'sagemaker_program': '{{/entry_point/train.py}}' # training scrip file location and name under the sagemaker_submit_directory
    },
    AlgorithmSpecification={
        # Specify a training Docker container image URI (Deep Learning Container or your own training container) to TrainingImage.
        'TrainingImage': '{{763104351884.dkr.ecr.us-west-2.amazonaws.com/tensorflow-training:2.4.1-gpu-py37-cu110-ubuntu18.04}}',
        'TrainingInputMode': '{{File}}',
        'EnableSageMakerMetricsTimeSeries': {{False}}
    },
    RoleArn='arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole-20201014T161125',
    OutputDataConfig={'S3OutputPath': 's3://'+bucket+'/'+project+'/output'},
    ResourceConfig={
        'InstanceType': '{{ml.p3.8xlarge}}',
        'InstanceCount': {{1}},
        'VolumeSizeInGB': 30
    },
    StoppingCondition={
        'MaxRuntimeInSeconds': 86400
    },
    DebugHookConfig={
        'S3OutputPath': 's3://'+bucket+'/'+project+'/debug-output',
        'CollectionConfigurations': [
            {
                'CollectionName': '{{losses}}',
                'CollectionParameters' : {
                    'train.save_interval': '{{500}}',
                    'eval.save_interval': '{{50}}'
                }
            }
        ]
    },
    DebugRuleConfigurations=[
        {
            'RuleConfigurationName': '{{LossNotDecreasing}}',
            'RuleEvaluatorImage': '{{895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest}}',
            'RuleParameters': {'rule_to_invoke': '{{LossNotDecreasing}}'}
        }
    ],
    ProfilerConfig={
        'S3OutputPath': 's3://'+bucket+'/'+project+'/profiler-output',
        'ProfilingIntervalInMilliseconds': 500,
        'ProfilingParameters': {
            'DataloaderProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "MetricsRegex": ".*", }',
            'DetailedProfilingConfig': '{"StartStep": 5, "NumSteps": 3, }',
            'PythonProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "ProfilerName": "cprofile", "cProfileTimer": "total_time"}',
            'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
        }
    },
    ProfilerRuleConfigurations=[
        {
            'RuleConfigurationName': 'ProfilerReport',
            'RuleEvaluatorImage': '{{895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest}}',
            'RuleParameters': {'rule_to_invoke': 'ProfilerReport'}
        }
    ]
)
```

## 모델 파라미터를 디버깅하기 위한 Debugger 규칙을 구성하려면,
<a name="debugger-built-in-rules-api-debug.Boto3"></a>

다음 코드 샘플은 이러한 SageMaker API를 사용하여 기본 제공되는 `VanishingGradient` 규칙을 구성하는 방법을 보여 줍니다.

**Debugger가 출력 텐서를 수집할 수 있도록 하려면**

Debugger 후크 구성을 다음과 같이 지정하세요.

```
DebugHookConfig={
    'S3OutputPath': '{{s3://<default-bucket>/<training-job-name>/debug-output}}',
    'CollectionConfigurations': [
        {
            'CollectionName': '{{gradients}}',
            'CollectionParameters' : {
                'train.save_interval': '{{500}}',
                'eval.save_interval': '{{50}}'
            }
        }
    ]
}
```

이렇게 하면 500단계마다 `save_interval` 훈련 작업에 텐서 컬렉션이 저장됩니다. `gradients` 사용 가능한 `CollectionName` 값을 찾으려면 *SMDebug 클라이언트 라이브러리 설명서*의 [Debugger 기본 제공 모음](https://github.com/awslabs/sagemaker-debugger/blob/master/docs/api.md#built-in-collections)을 참고하세요. 사용 가능한 `CollectionParameters` 파라미터 키와 값을 찾으려면 *SageMaker Python SDK 설명서*에서 [https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.CollectionConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.CollectionConfig) 클래스를 참고하세요.

**출력 텐서를 디버깅하기 위한 Debugger 규칙을 활성화하려면**

다음 `DebugRuleConfigurations` API 예제는 저장된 `gradients` 모음에서 기본 제공된 `VanishingGradient` 규칙을 실행하는 방법을 보여줍니다.

```
DebugRuleConfigurations=[
    {
        'RuleConfigurationName': '{{VanishingGradient}}',
        'RuleEvaluatorImage': '{{895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest}}',
        'RuleParameters': {
            'rule_to_invoke': '{{VanishingGradient}}',
            'threshold': '{{20.0}}'
        }
    }
]
```

이 샘플 구성과 동일한 구성을 갖춘 Debugger는 `gradients` 텐서 모음에서 `VanishingGradient` 규칙을 사용하여 훈련 작업에 대한 규칙 평가 작업을 시작합니다. Debugger 규칙을 사용하는 데 사용할 수 있는 도커 이미지의 전체 목록을 찾으려면 [Debugger 규칙용 도커 이미지](debugger-reference.md#debugger-docker-images-rules)을 참고하세요. `RuleParameters`에 대한 키-값 쌍을 찾으려면 [Debugger 기본 제공 규칙 목록](debugger-built-in-rules.md)을 참고하세요.

## 시스템 및 프레임워크 지표를 프로파일링하기 위한 디버거 내장 규칙을 구성하려면
<a name="debugger-built-in-rules-api-profile.Boto3"></a>

다음 예제 코드는 ProfilerConfig API 작업을 지정하여 시스템 및 프레임워크 지표를 수집할 수 있도록 하는 방법을 보여줍니다.

**Debugger 프로파일링을 활성화하여 시스템 및 프레임워크 지표를 수집하도록 하려면**

------
#### [ Target Step ]

```
ProfilerConfig={ 
    'S3OutputPath': '{{s3://<default-bucket>/<training-job-name>/profiler-output}}', # Optional. Path to an S3 bucket to save profiling outputs
    # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds.
    'ProfilingIntervalInMilliseconds': {{500}}, 
    'ProfilingParameters': {
        'DataloaderProfilingConfig': '{
            "StartStep": {{5}}, 
            "NumSteps": {{3}}, 
            "MetricsRegex": ".*"
        }',
        'DetailedProfilingConfig': '{
            "StartStep": {{5}}, 
            "NumSteps": {{3}} 
        }',
        'PythonProfilingConfig': '{
            "StartStep": {{5}}, 
            "NumSteps": {{3}}, 
            "ProfilerName": "{{cprofile}}",  # Available options: cprofile, pyinstrument
            "cProfileTimer": "{{total_time}}"  # Include only when using cprofile. Available options: cpu, off_cpu, total_time
        }',
        'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
    }
}
```

------
#### [ Target Time Duration ]

```
ProfilerConfig={ 
    'S3OutputPath': '{{s3://<default-bucket>/<training-job-name>/profiler-output}}', # Optional. Path to an S3 bucket to save profiling outputs
    # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds.
    'ProfilingIntervalInMilliseconds': {{500}},
    'ProfilingParameters': {
        'DataloaderProfilingConfig': '{
            "StartTimeInSecSinceEpoch": {{12345567789}}, 
            "DurationInSeconds": {{10}}, 
            "MetricsRegex": ".*"
        }',
        'DetailedProfilingConfig': '{
            "StartTimeInSecSinceEpoch": {{12345567789}}, 
            "DurationInSeconds": {{10}}
        }',
        'PythonProfilingConfig': '{
            "StartTimeInSecSinceEpoch": {{12345567789}}, 
            "DurationInSeconds": {{10}}, 
            "ProfilerName": "{{cprofile}}",  # Available options: cprofile, pyinstrument
            "cProfileTimer": "{{total_time}}"  # Include only when using cprofile. Available options: cpu, off_cpu, total_time
        }',
        'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs
    }
}
```

------

**지표를 프로파일링하기 위한 Debugger 규칙을 활성화하려면**

다음 예제 코드에서는 `ProfilerReport` 규칙을 구성하는 방법을 보여줍니다.

```
ProfilerRuleConfigurations=[ 
    {
        'RuleConfigurationName': 'ProfilerReport',
        'RuleEvaluatorImage': '{{895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest}}',
        'RuleParameters': {
            'rule_to_invoke': 'ProfilerReport',
            'CPUBottleneck_cpu_threshold': '{{90}}',
            'IOBottleneck_threshold': '{{90}}'
        }
    }
]
```

Debugger 규칙을 사용하는 데 사용할 수 있는 도커 이미지의 전체 목록을 찾으려면 [Debugger 규칙용 도커 이미지](debugger-reference.md#debugger-docker-images-rules)을 참고하세요. `RuleParameters`에 대한 키-값 쌍을 찾으려면 [Debugger 기본 제공 규칙 목록](debugger-built-in-rules.md)을 참고하세요.

## `UpdateTrainingJob` API 작업을 사용하여 Debugger 프로파일링 구성을 업데이트하세요.
<a name="debugger-updatetrainingjob-api.Boto3"></a>

훈련 작업이 실행되는 동안 AWS Boto3 SageMaker AI 클라이언트의 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.update_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.update_training_job) 함수를 사용하여 디버거 프로파일링 구성을 업데이트할 수 있습니다. 새 [ProfilerConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProfilerConfig.html) 및 [ProfilerRuleConfiguration](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProfilerRuleConfiguration.html) 객체를 구성하고 `TrainingJobName` 파라미터에 훈련 작업 이름을 지정합니다.

```
ProfilerConfig={ 
    'DisableProfiler': {{boolean}},
    'ProfilingIntervalInMilliseconds': {{number}},
    'ProfilingParameters': { 
        '{{string}}' : '{{string}}' 
    }
},
ProfilerRuleConfigurations=[ 
    { 
        'RuleConfigurationName': '{{string}}',
        'RuleEvaluatorImage': '{{string}}',
        'RuleParameters': { 
            'string' : '{{string}}' 
        }
    }
],
TrainingJobName='{{your-training-job-name-YYYY-MM-DD-HH-MM-SS-SSS}}'
```

## CreateTrainingJob API 작업에 Debugger 사용자 지정 규칙 구성 추가
<a name="debugger-custom-rules-api.Boto3"></a>

사용자 지정 규칙은 AWS Boto3 SageMaker AI 클라이언트의 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job) 함수를 사용하여 [ DebugHookConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DebugHookConfig.html) 및 [ DebugRuleConfiguration](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DebugRuleConfiguration.html) 객체를 사용하여 훈련 작업에 대해 구성할 수 있습니다. 다음 코드 샘플에서는 이 SageMaker API 작업을 사용하여 *smdebug* 라이브러리로 작성된 사용자 지정 `ImproperActivation` 규칙의 구성 방법을 보여 줍니다. 이 예제에서는 *custom\_rules.py* 파일에 사용자 지정 규칙을 작성하여 Amazon S3 버킷에 업로드했다고 가정합니다. 그리고 사용자가 사용자 지정 규칙을 실행하는 데 사용할 수 있는 사전 빌드된 도커 이미지를 보여줍니다. 이는 [사용자 지정 규칙 평가기를 위한 Amazon SageMaker Debugger 이미지 URI](debugger-reference.md#debuger-custom-rule-registry-ids)에 나열되어 있습니다. `RuleEvaluatorImage` 파라미터에 사전 구축된 도커 이미지의 URL 레지스트리 주소를 지정합니다.

```
DebugHookConfig={
    'S3OutputPath': '{{s3://<default-bucket>/<training-job-name>/debug-output}}',
    'CollectionConfigurations': [
        {
            'CollectionName': '{{relu_activations}}',
            'CollectionParameters': {
                'include_regex': '{{relu}}',
                'save_interval': '{{500}}',
                'end_step': '{{5000}}'
            }
        }
    ]
},
DebugRulesConfigurations=[
    {
        'RuleConfigurationName': '{{improper_activation_job}}',
        'RuleEvaluatorImage': '{{552407032007.dkr.ecr.ap-south-1.amazonaws.com/sagemaker-debugger-rule-evaluator:latest}}',
        'InstanceType': '{{ml.c4.xlarge}}',
        'VolumeSizeInGB': {{400}},
        'RuleParameters': {
           'source_s3_uri': '{{s3://bucket/custom_rules.py}}',
           'rule_to_invoke': '{{ImproperActivation}}',
           'collection_names': '{{relu_activations}}'
        }
    }
]
```

Debugger 규칙을 사용하는 데 사용할 수 있는 도커 이미지의 전체 목록을 찾으려면 [Debugger 규칙용 도커 이미지](debugger-reference.md#debugger-docker-images-rules)을 참고하세요. `RuleParameters`에 대한 키-값 쌍을 찾으려면 [Debugger 기본 제공 규칙 목록](debugger-built-in-rules.md)을 참고하세요.