모델 버전 등록 - 아마존 SageMaker

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

모델 버전 등록

Amazon SageMaker 모델이 속한 모델 그룹을 지정하는 모델 버전을 생성하여 Amazon 모델을 등록할 수 있습니다. 모델 버전에는 모델 아티팩트(모델의 훈련된 가중치)와 모델의 추론 코드가 모두 포함되어야 합니다.

추론 파이프라인은 추론 요청을 처리하는 2~15개 컨테이너의 선형 시퀀스로 구성된 SageMaker 모델입니다. 컨테이너와 관련 환경 변수를 지정하여 추론 파이프라인을 등록합니다. 추론 형식에 대한 자세한 정보는 하나의 엔드포인트 이면의 직렬 추론 파이프라인으로서 전처리 로직과 더불어 모델 호스트.섹션을 참조하세요.

컨테이너와 관련 환경 변수를 지정하여 추론 파이프라인이 있는 모델을 등록합니다. Amazon SageMaker Studio 콘솔을 사용하거나 모델 구축 파이프라인의 단계를 생성하여 추론 파이프라인이 포함된 SageMaker 모델 버전을 생성하려면 다음 단계를 사용하십시오. AWS SDK for Python (Boto3)

모델 버전 등록 (SageMaker파이프라인)

모델 구축 파이프라인을 사용하여 SageMaker 모델 버전을 등록하려면 파이프라인에서 RegisterModel 단계를 생성하세요. 파이프라인의 일부로 RegisterModel단계 생성에 대한 자세한 내용은 8단계: 모델 패키지 생성 RegisterModel 단계 정의섹션을 참조하세요.

모델 버전 등록(Boto3)

Boto3를 사용하여 모델 버전을 등록하려면 create_model_package API 작업을 호출하십시오.

먼저 create_model_package API 작업에 전달할 매개변수 사전을 설정합니다.

# Specify the model source model_url = "s3://your-bucket-name/model.tar.gz" modelpackage_inference_specification = { "InferenceSpecification": { "Containers": [ { "Image": image_uri, "ModelDataUrl": model_url } ], "SupportedContentTypes": [ "text/csv" ], "SupportedResponseMIMETypes": [ "text/csv" ], } } # Alternatively, you can specify the model source like this: # modelpackage_inference_specification["InferenceSpecification"]["Containers"][0]["ModelDataUrl"]=model_url create_model_package_input_dict = { "ModelPackageGroupName" : model_package_group_name, "ModelPackageDescription" : "Model to detect 3 different types of irises (Setosa, Versicolour, and Virginica)", "ModelApprovalStatus" : "PendingManualApproval" } create_model_package_input_dict.update(modelpackage_inference_specification)

그런 다음 방금 설정한 파라미터 사전을 전달하여 create_model_package API 작업을 호출합니다.

create_model_package_response = sm_client.create_model_package(**create_model_package_input_dict) model_package_arn = create_model_package_response["ModelPackageArn"] print('ModelPackage Version ARN : {}'.format(model_package_arn))

모델 버전 등록 (콘솔)

Amazon SageMaker Studio 콘솔에서 모델 버전을 등록하려면 Studio 또는 Studio Classic을 사용하는지 여부에 따라 다음 단계를 완료하십시오.

Studio
  1. Amazon SageMaker Studio 시작의 지침에 따라 SageMaker 스튜디오 콘솔을 엽니다.

  2. 왼쪽 탐색 창의 메뉴에서 모델을 선택합니다.

  3. 등록된 모델 탭을 선택합니다 (아직 선택하지 않은 경우).

  4. 등록된 모델 탭 레이블 바로 아래에서 모델 그룹을 선택합니다 (아직 선택하지 않은 경우).

  5. [등록] 을 선택한 다음 [모델 버전] 을 선택합니다.

  6. 모델 버전 등록 양식에 다음 정보를 입력합니다.

    • 모델 그룹 이름 드롭다운에서 버전이 속한 모델 그룹의 이름을 선택합니다.

    • (선택 사항) 모델 버전에 대한 설명을 입력합니다.

    • 모델 승인 상태 드롭다운에서 버전 승인 상태를 선택합니다.

    • (선택 사항) 사용자 지정 메타데이터 필드에서 + 새로 추가를 선택하고 사용자 지정 태그를 키-값 쌍으로 추가합니다.

  7. 다음을 선택합니다.

  8. 추론 사양 양식에 다음 정보를 입력합니다.

    • 추론 이미지 위치 (ECR) 에 ECR 추론 이미지 위치를 입력합니다.

    • 모델 아티팩트 위치 (S3) 에 모델 데이터 아티팩트의 Amazon S3 버킷 위치를 입력합니다.

    • 데이터 구성 또는 환경 변수를 지정하고 입력하려면 추가 구성을 선택하고 이 정보를 입력합니다.

    • 컨테이너를 더 추가하려면 + 컨테이너 추가를 선택합니다.

    • 실시간 추론 인스턴스 유형에서 실시간 추론에 사용할 인스턴스 유형을 입력합니다.

    • Transform 추론 인스턴스 유형에 일괄 변환에 사용할 인스턴스 유형을 입력합니다.

    • 지원되는 콘텐츠 유형에 입력 MIME 유형을 입력합니다.

    • 지원되는 응답 콘텐츠 유형에 출력 MIME 유형을 입력합니다.

  9. 다음을 선택합니다.

  10. 선택적 추론 권장 사항 양식에 다음 정보를 입력합니다.

    • 비즈니스 문제의 경우 모델에 적용할 애플리케이션을 선택하십시오.

    • 태스크에서 모델에 적용되는 문제 유형을 선택합니다.

    • S3 버킷 주소에는 샘플 페이로드의 Amazon S3 버킷 위치를 입력합니다.

    • 첫 번째 컨테이너의 경우 다음 정보를 입력합니다.

      • 모델 이름에는 모델 동물원에서 사용된 모델 이름을 입력합니다.

      • 프레임워크의 경우 프레임워크를 선택합니다.

      • 프레임워크 버전의 경우 프레임워크 버전을 입력합니다.

    • 모든 컨테이너에 대해 이전 단계를 반복합니다.

  11. 다음을 선택합니다.

  12. 표시된 모델 지표 중 하나 이상의 옆에 있는 확인란을 선택합니다.

  13. 다음을 선택합니다.

  14. 표시된 설정이 올바른지 확인하고 모델 버전 등록을 선택합니다. 이후에 오류 메시지가 있는 모드 창이 표시되면 보기(메시지 옆)를 선택하여 오류의 원인을 확인합니다.

  15. 상위 모델 그룹 페이지에 새 모델 버전이 나타나는지 확인합니다.

Studio Classic
  1. Amazon SageMaker 스튜디오 클래식에 로그인합니다. 자세한 내용은 Amazon SageMaker 스튜디오 클래식 시작을 참조하십시오.

  2. 왼쪽의 탐색 창에서 아이콘( )을 선택합니다.

  3. 모델을 선택한 다음 모델 레지스트리를 선택합니다.

  4. 버전 등록 양식을 엽니다. 이 작업을 두 가지 방법으로 수행할 수 있습니다.

    • 작업을 선택한 후 모델 버전 생성을 선택합니다.

    • 모델 버전을 생성하려는 모델 그룹의 이름을 선택한 다음 모델 버전 생성을 선택합니다.

  5. 모델 버전 등록 양식에 다음 정보를 입력합니다.

    • 모델 패키지 그룹 이름 드롭다운에서 모델 그룹 이름을 선택합니다.

    • (선택 사항) 모델 버전에 대한 설명을 입력합니다.

    • 모델 승인 상태 드롭다운에서 버전 승인 상태를 선택합니다.

    • (선택 사항) 사용자 지정 메타데이터 필드에서 사용자 지정 태그를 키-값 쌍으로 추가합니다.

  6. 다음을 선택합니다.

  7. 추론 사양 양식에 다음 정보를 입력합니다.

    • 추론 이미지 위치를 입력합니다.

    • 모델 데이터 아티팩트 위치를 입력합니다.

    • (선택 사항) 변환 및 실시간 추론 작업에 사용할 이미지, 지원되는 입력 및 출력 MIME 유형에 대한 정보를 입력합니다.

  8. 다음을 선택합니다.

  9. (선택 사항) 엔드포인트 권장 사항에 도움이 되는 세부 정보를 제공합니다.

  10. 다음을 선택합니다.

  11. (선택 사항) 포함하려는 모델 지표를 선택합니다.

  12. 다음을 선택합니다.

  13. 표시된 설정이 올바른지 확인하고 모델 버전 등록을 선택합니다. 이후에 오류 메시지가 있는 모드 창이 표시되면 보기(메시지 옆)를 선택하여 오류의 원인을 확인합니다.

  14. 상위 모델 그룹 페이지에 새 모델 버전이 나타나는지 확인합니다.

다른 계정에서 모델 버전 등록

다른 AWS 계정에서 만든 모델 그룹에 모델 버전을 등록하려면 교차 계정 AWS Identity and Access Management 리소스 정책을 추가하여 해당 계정을 활성화해야 합니다. 예를 들어 조직의 한 AWS 계정은 모델 교육을 담당하고 다른 계정은 모델 관리, 배포 및 업데이트를 담당합니다. IAM 리소스 정책을 생성하여 이 사례에 대한 액세스 권한을 부여하려는 특정 계정 리소스에 정책을 적용합니다. 의 교차 계정 리소스 정책에 대한 자세한 내용은 사용 AWS설명서의 교차 계정 정책 평가 로직을 참조하십시오.AWS Identity and Access Management

참고

또한 계정 간 모델 배포를 위한 훈련 중에 KMS 키를 사용하여 출력 데이터 구성 작업을 암호화해야 합니다.

에서 SageMaker 교차 계정 모델 레지스트리를 활성화하려면 모델 버전이 포함된 모델 그룹에 교차 계정 리소스 정책을 제공해야 합니다. 다음은 모델 그룹을 위한 교차 계정 정책을 생성하고 이러한 정책을 해당 특정 리소스에 적용하는 예시입니다.

모델 그룹에 모델 교차 계정을 등록하는 소스 계정에서 다음 구성을 설정해야 합니다. 이 예시에서 소스 계정은 모델 훈련 계정이며, 이 계정은 모델 교차 계정을 훈련한 다음 모델 레지스트리 계정의 모델 레지스트리에 등록합니다.

이 예시에서는 이전에 다음 변수를 정의했다고 가정합니다.

  • sm_client— SageMaker Boto3 클라이언트.

  • model_package_group_name— 액세스 권한을 부여하려는 모델 그룹.

  • model_package_group_arn— 계정 간 액세스 권한을 부여하려는 모델 그룹 ARN

  • bucket— 모델 교육 아티팩트가 저장되는 Amazon S3 버킷입니다.

다른 계정에서 만든 모델을 배포하려면 사용자에게 SageMaker 작업에 액세스할 수 있는 역할 (예: AmazonSageMakerFullAccess 관리형 정책의 역할) 이 있어야 합니다. SageMaker 관리형 정책에 대한 자세한 내용은 AWS 아마존 관리형 정책 SageMaker 단원을 참조하십시오.

필수 IAM 리소스 정책

다음 다이어그램은 교차 계정 모델 등록을 허용하는 데 필요한 정책을 보여줍니다. 그림과 같이 모델을 모델 레지스트리 계정에 제대로 등록하려면 모델 훈련 중에 이러한 정책을 활성화해야 합니다.

여러 계정에 모델을 등록하는 데 필요한 정책.

Amazon ECR, Amazon S3 및 AWS KMS 정책은 다음 코드 샘플에 나와 있습니다.

샘플 Amazon ECR 정책

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::{model_registry_account}:root" }, "Action": [ "ecr:BatchGetImage", "ecr:Describe*" ] } ] }

샘플 Amazon S3 정책

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::{model_registry_account}:root" }, "Action": [ "s3:GetObject", "s3:GetBucketAcl", "s3:GetObjectAcl" ], "Resource": "arn:aws:s3:::{bucket}/*" } ] }

샘플 정책 AWS KMS

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::{model_registry_account}:root" }, "Action": [ "kms:Decrypt", "kms:GenerateDataKey*" ], "Resource": "*" } ] }

계정에 리소스 정책 적용

다음 정책 구성은 이전 섹션에서 설명한 정책을 적용하며 모델 훈련 계정에 입력해야 합니다.

import json # The Model Registry account id of the Model Group model_registry_account = "111111111111" # The model training account id where training happens model_training_account = "222222222222" # 1. Create a policy for access to the ECR repository # in the model training account for the Model Registry account Model Group ecr_repository_policy = {"Version": "2012-10-17", "Statement": [{"Sid": "AddPerm", "Effect": "Allow", "Principal": { "AWS": f"arn:aws:iam::{model_registry_account}:root" }, "Action": [ "ecr:BatchGetImage", "ecr:Describe*" ] }] } # Convert the ECR policy from JSON dict to string ecr_repository_policy = json.dumps(ecr_repository_policy) # Set the new ECR policy ecr = boto3.client('ecr') response = ecr.set_repository_policy( registryId = model_training_account, repositoryName = "decision-trees-sample", policyText = ecr_repository_policy ) # 2. Create a policy in the model training account for access to the S3 bucket # where the model is present in the Model Registry account Model Group bucket_policy = {"Version": "2012-10-17", "Statement": [{"Sid": "AddPerm", "Effect": "Allow", "Principal": {"AWS": f"arn:aws:iam::{model_registry_account}:root" }, "Action": [ "s3:GetObject", "s3:GetBucketAcl", "s3:GetObjectAcl" ], "Resource": "arn:aws:s3:::{bucket}/*" }] } # Convert the S3 policy from JSON dict to string bucket_policy = json.dumps(bucket_policy) # Set the new bucket policy s3 = boto3.client("s3") response = s3.put_bucket_policy( Bucket = bucket, Policy = bucket_policy) # 3. Create the KMS grant for the key used during training for encryption # in the model training account to the Model Registry account Model Group client = boto3.client("kms") response = client.create_grant( GranteePrincipal=model_registry_account, KeyId=kms_key_id Operations=[ "Decrypt", "GenerateDataKey", ], )

모델 그룹이 있는 모델 레지스트리 계정에 다음 구성을 넣어야 합니다.

# The Model Registry account id of the Model Group model_registry_account = "111111111111" # 1. Create policy to allow the model training account to access the ModelPackageGroup model_package_group_policy = {"Version": "2012-10-17", "Statement": [ { "Sid": "AddPermModelPackageVersion", "Effect": "Allow", "Principal": {"AWS": f"arn:aws:iam::{model_training_account}:root"}, "Action": ["sagemaker:CreateModelPackage"], "Resource": f"arn:aws:sagemaker:{region}:{model_registry_account}:model-package/{model_package_group_name}/*" } ] } # Convert the policy from JSON dict to string model_package_group_policy = json.dumps(model_package_group_policy) # Set the new policy response = sm_client.put_model_package_group_policy( ModelPackageGroupName = model_package_group_name, ResourcePolicy = model_package_group_policy)

마지막으로, 모델 훈련 계정의 create_model_package작업을 사용하여 교차 계정에 모델 패키지를 등록합니다.

# Specify the model source model_url = "s3://{bucket}/model.tar.gz" #Set up the parameter dictionary to pass to the create_model_package API operation modelpackage_inference_specification = { "InferenceSpecification": { "Containers": [ { "Image": f"{model_training_account}.dkr.ecr.us-east-2.amazonaws.com/decision-trees-sample:latest", "ModelDataUrl": model_url } ], "SupportedContentTypes": [ "text/csv" ], "SupportedResponseMIMETypes": [ "text/csv" ], } } # Alternatively, you can specify the model source like this: # modelpackage_inference_specification["InferenceSpecification"]["Containers"][0]["ModelDataUrl"]=model_url create_model_package_input_dict = { "ModelPackageGroupName" : model_package_group_arn, "ModelPackageDescription" : "Model to detect 3 different types of irises (Setosa, Versicolour, and Virginica)", "ModelApprovalStatus" : "PendingManualApproval" } create_model_package_input_dict.update(modelpackage_inference_specification) # Create the model package in the Model Registry account create_model_package_response = sm_client.create_model_package(**create_model_package_input_dict) model_package_arn = create_model_package_response["ModelPackageArn"] print('ModelPackage Version ARN : {}'.format(model_package_arn))