ModelBuilder.build() ignores the configured session when it reloads a model
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- aws, python
調査の方向性
バグは sagemaker/serve/model_builder.py の ModelBuilder.build() の最後の return にあり、設定済みのセッションを渡さずに Model.get() が呼び出されています。このクラスで self.sagemaker_session がどのように設定されているかを読み、次に Model.get() がセッション引数を受け取るかを確認してください。デフォルトと対象の boto セッションが異なる単体テストを追加し、CreateModel の後に Model.get() が対象のセッションを受け取ることを検証してください。そのテストが通り、既存の ModelBuilder のテストも引き続き通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
PySDK Version
- PySDK V2 (2.x)
- PySDK V3 (3.x)
Describe the bug
ModelBuilder.build() creates a SageMaker model with its configured
sagemaker_session. It then reloads the model with Model.get() without
passing that session.
If the configured session and default boto session use different AWS accounts,
the create request succeeds in the configured account. The subsequent describe
request uses the default account and fails because the model does not exist
there.
The relevant call in sagemaker/serve/model_builder.py is:
return Model.get(model_name=self.model_name, region=self.region)
To reproduce
Configure two AWS CLI profiles that use different AWS accounts. The target
profile must have SageMaker permissions and permission to pass the execution
role.
Set these environment variables:
export TARGET_PROFILE="<profile-for-target-account>"
export DEFAULT_PROFILE="<profile-for-different-account>"
export TARGET_ROLE_ARN="arn:aws:iam::<target-account-id>:role/<sagemaker-execution-role>"
export TARGET_BUCKET="<existing-bucket-in-target-account>"
export IMAGE_URI="<inference-image-uri-accessible-from-target-account>"
export AWS_REGION="us-east-2"
Run this script:
import os
import uuid
import boto3
from botocore.exceptions import ClientError
from sagemaker.core.helper.session_helper import Session
from sagemaker.core.resources import Model
from sagemaker.core.shapes import ContainerDefinition
from sagemaker.serve import ModelBuilder
region = os.environ["AWS_REGION"]
target_profile = os.environ["TARGET_PROFILE"]
default_profile = os.environ["DEFAULT_PROFILE"]
role_arn = os.environ["TARGET_ROLE_ARN"]
bucket = os.environ["TARGET_BUCKET"]
image_uri = os.environ["IMAGE_URI"]
model_name = f"modelbuilder-session-repro-{uuid.uuid4().hex[:8]}"
# Make the process default session use a different account.
boto3.setup_default_session(profile_name=default_profile, region_name=region)
target_boto_session = boto3.Session(
profile_name=target_profile,
region_name=region,
)
target_sagemaker_session = Session(
boto_session=target_boto_session,
default_bucket=bucket,
)
target_account = target_boto_session.client("sts").get_caller_identity()["Account"]
default_account = boto3.client("sts", region_name=region).get_caller_identity()["Account"]
print(f"Target account: {target_account}")
print(f"Default account: {default_account}")
source_model = Model(
model_name=f"{model_name}-source",
primary_container=ContainerDefinition(image=image_uri),
execution_role_arn=role_arn,
)
builder = ModelBuilder(
model=[source_model],
role_arn=role_arn,
sagemaker_session=target_sagemaker_session,
instance_type="ml.m5.large",
)
try:
builder.build(model_name=model_name)
finally:
# The create request succeeds before build() raises.
try:
target_sagemaker_session.sagemaker_client.delete_model(
ModelName=model_name,
)
except ClientError as error:
if error.response["Error"]["Code"] != "ValidationException":
raise
The script raises an error similar to:
botocore.exceptions.ClientError: An error occurred (ValidationException)
when calling the DescribeModel operation:
Could not find model "modelbuilder-session-repro-1234abcd".
Before cleanup, DescribeModel succeeds through target_sagemaker_session.
The model is present in the target account.
Expected behavior
ModelBuilder.build() must use the configured session for all operations.
The method must return the created Model resource from the target account.
The reload call can pass the boto session:
return Model.get(
model_name=self.model_name,
session=self.sagemaker_session.boto_session,
region=self.region,
)
Suggested patch:
diff --git a/src/sagemaker/serve/model_builder.py b/src/sagemaker/serve/model_builder.py
--- a/src/sagemaker/serve/model_builder.py
+++ b/src/sagemaker/serve/model_builder.py
@@
- return Model.get(model_name=self.model_name, region=self.region)
+ return Model.get(
+ model_name=self.model_name,
+ session=self.sagemaker_session.boto_session,
+ region=self.region,
+ )
The regression test must configure different default and target boto sessions.
It must make sure that Model.get() receives the target boto session after
CreateModel.
Screenshots or logs
Target account: <target-account-id>
Default account: <different-account-id>
Creating model with name: modelbuilder-session-repro-1234abcd
Model has been created: modelbuilder-session-repro-1234abcd
ValidationException: Could not find model
"modelbuilder-session-repro-1234abcd".
CloudTrail shows CreateModel in the target account. The failing
DescribeModel request uses the default account.
System information
- SageMaker Python SDK version: 3.24.0
- Framework name (eg. PyTorch) or algorithm (eg. KMeans): Not framework-specific
- Framework version: N/A
- Python version: 3.12.0
- CPU or GPU: CPU, Apple Silicon (arm64)
- Custom Docker image (Y/N): N
Additional context
The failure is hidden when the default boto session uses the same account as
the configured SageMaker session.
This also affects multi-container inference pipelines. CreateModel completes
in the target account, but endpoint creation never starts because the model
reload fails first.
- 主要言語
- Python
- スター
- 2.3k
- フォーク
- 1.3k
- 平均マージ
- 3日 4時間
- マージ済み PR(30日)
- 90
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
aws/sagemaker-python-sdk のほかの issue
-
[Bug] ModelTrainer with no input channels emits InputDataConfig: [], which CreatePipeline rejects (min=1) — v2 omitted the key対応中かも @sagemaker-bot が 14 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
aws/sagemaker-python-sdk#6156 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
-
sagemaker-train should depend on mlflow-skinny, following sagemaker-mlflow 0.5.0対応中かも @mohamedzeidan2021 が 15 日前に担当しました。 オープン
難易度 2/5 半日 初心者へのやさしさ 72/100
aws/sagemaker-python-sdk#6152 ·
メンテナーはふだん 2 日以内に返信
-
[Bug] ModelTrainer drops TrainingJobName for PipelineSession, breaking use_custom_job_prefix on TrainingStep対応中かも @mohamedzeidan2021 が 19 日前に担当しました。 オープンcomponent: pipelines type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
aws/sagemaker-python-sdk#5776 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 2 日以内に返信
-
`ModelTrainer` bug with method to load hyperparameters from file for Amazon Nova Recipe対応中かも @sagemaker-bot が 14 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
aws/sagemaker-python-sdk#5770 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
aws/sagemaker-python-sdk#5588 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
aws/sagemaker-python-sdk の issue をすべて見る
似ている issue
-
vis.image() reports "Unsupported number of image channels: 224" for an HxWxC image対応中かも @piyush182004 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
fossasia/visdom#1997 · コメント 3 件 ·
メンテナーはふだん 6 日以内に返信
-
Byse new domainオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
Gujal00/ResolveURL#1525 ·
メンテナーはふだん 1 日以内に返信
-
Serialisation fails with pydantic 2.14: wrap serialisers pass SerializationInfo as index_key対応中かも @hugohills-regnosys が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
finos/rune-python-runtime#45 ·
-
[Bug]: LangGraph Store sync put bypasses namespace validation対応中かも @ktz03 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
volcengine/OpenViking#6050 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
hummingbot/condor#283 ·
メンテナーはふだん 1 日以内に返信