

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

# SageMaker 훈련 작업 훈련 전 자습서(GPU)
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-pretrain-tutorial"></a>

이 자습서에서는 GPU 인스턴스에서 SageMaker 훈련 작업을 사용하여 사전 훈련 작업을 설정하고 실행하는 프로세스를 안내합니다.
+ 환경 설정
+ SageMaker HyperPod 레시피를 사용하여 훈련 작업 시작

시작하기 전에 다음 사전 조건을 충족하는지 확인합니다.

**사전 조건**  
환경을 설정하기 전에 다음 조건을 충족해야 합니다.  
데이터를 로드하고 훈련 아티팩트를 출력할 수 있는 Amazon FSx 파일 시스템 또는 Amazon S3 버킷이 있어야 합니다.
Amazon SageMaker AI에서 1x ml.p4d.24xlarge 및 1x ml.p5.48xlarge에 대한 서비스 할당량을 요청한 상태여야 합니다. 서비스 할당량 증가를 요청하려면 다음을 수행합니다.  
 AWS Service Quotas 콘솔에서 서비스로 이동합니다 AWS .
**Amazon SageMaker AI**를 선택합니다.
ml.p4d.24xlarge 인스턴스 하나와 ml.p5.48xlarge 인스턴스 하나를 선택합니다.
다음 관리형 정책을 사용하여 AWS Identity and Access Management(IAM) 역할을 생성하여 SageMaker AI에 예제를 실행할 수 있는 권한을 부여합니다.  
AmazonSageMakerFullAccess
AmazonEC2FullAccess
다음 형식 중 하나의 데이터가 있어야 합니다.  
JSON
JSONGZ(압축된 JSON)
ARROW
(선택 사항) 사전 훈련 또는 미세 조정에 HuggingFace의 모델 가중치를 사용하는 경우 HuggingFace 토큰을 받아야 합니다. 토큰 가져오기에 대한 자세한 내용은 [User access tokens](https://huggingface.co/docs/hub/en/security-tokens) 섹션을 참조하세요.

## GPU SageMaker 훈련 작업 환경 설정
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-environment-setup"></a>

SageMaker 훈련 작업을 실행하기 전에 `aws configure` 명령을 실행하여 AWS 자격 증명과 기본 리전을 구성합니다. 구성 명령의 대안으로 `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN.`과 같은 환경 변수를 통해 자격 증명을 제공할 수 있습니다. 자세한 내용은 [SageMaker AI Python SDK](https://github.com/aws/sagemaker-python-sdk)를 참조하세요.

SageMaker AI JupyterLab에서 SageMaker AI Jupyter Notebook을 사용하여 SageMaker 훈련 작업을 시작하는 것이 좋습니다. 자세한 내용은 [SageMaker JupyterLab](studio-updated-jl.md) 단원을 참조하십시오.
+ (선택 사항) 가상 환경 및 종속성을 설정합니다. Amazon SageMaker Studio에서 Jupyter Notebook을 사용하는 경우 이 단계를 건너뛸 수 있습니다. Python 3.9 이상을 사용하고 있는지 확인합니다.

  ```
  # set up a virtual environment
  python3 -m venv ${PWD}/venv
  source venv/bin/activate
  # install dependencies after git clone.
  
  git clone --recursive git@github.com:aws/sagemaker-hyperpod-recipes.git
  cd sagemaker-hyperpod-recipes
  pip3 install -r requirements.txt
  # Set the aws region.
  
  aws configure set <your_region>
  ```
+ SageMaker AI Python SDK를 설치합니다.

  ```
  pip3 install --upgrade sagemaker
  ```
+ `Container`: GPU 컨테이너는 SageMaker AI Python SDK에 의해 자동으로 설정됩니다. 자체 컨테이너를 제공할 수도 있습니다.
**참고**  
Llama 3.2 다중 모달 훈련 작업을 실행하는 경우 `transformers` 버전은 `4.45.2 `이상이어야 합니다.

  SageMaker AI Python SDK를 사용하는 경우에만 `source_dir`에서 `requirements.txt`에 `transformers==4.45.2`를 추가합니다. 예를 들어 SageMaker AI JupyterLab의 노트북에서 사용하는 경우 추가합니다.

  HyperPod 레시피로 클러스터 유형 `sm_jobs`을 사용하여 시작하는 경우 자동으로 추가됩니다.

## Jupyter Notebook을 사용하여 훈련 작업 시작
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-launch-training-job-notebook"></a>

다음 Python 코드를 사용하여 레시피로 SageMaker 훈련 작업을 실행할 수 있습니다. [SageMaker AI Python SDK](https://sagemaker.readthedocs.io/en/stable/)의 PyTorch 예측기를 활용하여 레시피를 제출합니다. 다음 예시에서는 SageMaker AI Training 플랫폼에서 llama3-8b 레시피를 시작합니다.

```
import os
import sagemaker,boto3
from sagemaker.debugger import TensorBoardOutputConfig

from sagemaker.pytorch import PyTorch

sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()

bucket = sagemaker_session.default_bucket() 
output = os.path.join(f"s3://{bucket}", "output")
output_path = "<s3-URI>"

overrides = {
    "run": {
        "results_dir": "/opt/ml/model",
    },
    "exp_manager": {
        "exp_dir": "",
        "explicit_log_dir": "/opt/ml/output/tensorboard",
        "checkpoint_dir": "/opt/ml/checkpoints",
    },   
    "model": {
        "data": {
            "train_dir": "/opt/ml/input/data/train",
            "val_dir": "/opt/ml/input/data/val",
        },
    },
}

tensorboard_output_config = TensorBoardOutputConfig(
    s3_output_path=os.path.join(output, 'tensorboard'),
    container_local_output_path=overrides["exp_manager"]["explicit_log_dir"]
)

estimator = PyTorch(
    output_path=output_path,
    base_job_name=f"llama-recipe",
    role=role,
    instance_type="ml.p5.48xlarge",
    training_recipe="training/llama/hf_llama3_8b_seq8k_gpu_p5x16_pretrain",
    recipe_overrides=recipe_overrides,
    sagemaker_session=sagemaker_session,
    tensorboard_output_config=tensorboard_output_config,
)

estimator.fit(inputs={"train": "s3 or fsx input", "val": "s3 or fsx input"}, wait=True)
```

앞의 코드는 훈련 레시피를 사용하여 PyTorch 예측기 객체를 생성한 다음 `fit()` 메서드를 사용하여 모델에 맞춥니다. training\$1recipe 파라미터를 사용하여 훈련에 사용할 레시피를 지정합니다.

**참고**  
Llama 3.2 다중 모달 훈련 작업을 실행하는 경우 transformers 버전은 4.45.2 이상이어야 합니다.

SageMaker AI Python SDK를 직접 사용하는 경우에만 `source_dir`에서 `requirements.txt`에 `transformers==4.45.2`를 추가합니다. 예를 들어 Jupyter Notebook을 사용할 때는 텍스트 파일에 버전을 추가해야 합니다.

SageMaker 훈련 작업에 대한 엔드포인트를 배포할 때는 사용 중인 이미지 URI를 지정해야 합니다. 이미지 URI를 제공하지 않으면 예측기는 훈련 이미지를 배포용 이미지로 사용합니다. SageMaker HyperPod가 제공하는 훈련 이미지에는 추론 및 배포에 필요한 종속성이 포함되어 있지 않습니다. 다음은 배포에 추론 이미지를 사용하는 방법의 예입니다.

```
from sagemaker import image_uris
container=image_uris.retrieve(framework='pytorch',region='us-west-2',version='2.0',py_version='py310',image_scope='inference', instance_type='ml.p4d.24xlarge')
predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.p4d.24xlarge',image_uri=container)
```

**참고**  
Sagemaker 노트북 인스턴스에서 이전 코드를 실행하려면 SageMaker AI JupyterLab에서 제공하는 기본 5GB보다 더 큰 스토리지가 필요할 수 있습니다. 스페이스를 사용할 수 없는 문제가 발생하는 경우 다른 노트북 인스턴스를 사용하는 새 노트북 인스턴스를 생성하고 노트북의 스토리지를 늘립니다.

## 레시피 런처를 사용하여 훈련 작업 시작
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-launch-training-job-recipes"></a>

`./recipes_collection/cluster/sm_jobs.yaml` 파일을 다음과 같이 업데이트합니다.

```
sm_jobs_config:
  output_path: <s3_output_path>
  tensorboard_config:
    output_path: <s3_output_path>
    container_logs_path: /opt/ml/output/tensorboard  # Path to logs on the container
  wait: True  # Whether to wait for training job to finish
  inputs:  # Inputs to call fit with. Set either s3 or file_system, not both.
    s3:  # Dictionary of channel names and s3 URIs. For GPUs, use channels for train and validation.
      train: <s3_train_data_path>
      val: null
  additional_estimator_kwargs:  # All other additional args to pass to estimator. Must be int, float or string.
    max_run: 180000
    enable_remote_debug: True
  recipe_overrides:
    exp_manager:
      explicit_log_dir: /opt/ml/output/tensorboard
    data:
      train_dir: /opt/ml/input/data/train
    model:
      model_config: /opt/ml/input/data/train/config.json
    compiler_cache_url: "<compiler_cache_url>"
```

`cluster` 및 `cluster_type`에서 `sm_jobs`을 지정하도록 `./recipes_collection/config.yaml`을 업데이트합니다.

```
defaults:
  - _self_
  - cluster: sm_jobs  # set to `slurm`, `k8s` or `sm_jobs`, depending on the desired cluster
  - recipes: training/llama/hf_llama3_8b_seq8k_trn1x4_pretrain
cluster_type: sm_jobs  # bcm, bcp, k8s or sm_jobs. If bcm, k8s or sm_jobs, it must match - cluster above.
```

다음 명령을 사용하여 작업 시작

```
python3 main.py --config-path recipes_collection --config-name config
```

SageMaker 훈련 작업 구성에 대한 자세한 내용은 SageMaker 훈련 작업에서 훈련 작업 실행을 참조하세요.