Offline SageMaker local mode — XGBoost training¶
Trains against a moto-backed S3/STS endpoint through the sagemaker-local library (no AWS). The job runs in a local Docker container.
Dataset: diabetes (regression), loaded inside the container from scikit-learn. The built sagemaker-local:latest image already includes xgboost 2.1.4.
import os
from dataclasses import replace
from sagemaker.deserializers import JSONDeserializer
from sagemaker.estimator import Estimator
from sagemaker.serializers import JSONSerializer
from sagemaker_local.config import config_from_env
from sagemaker_local.session import make_local_session
cfg = replace(
config_from_env(),
bucket="sagemaker-xgboost",
image_tag="sagemaker-xgboost:train",
)
boto_session, sm_session = make_local_session(cfg)
Train¶
est.fit() runs synchronously in a local container; train.py loads diabetes and writes model.joblib to /opt/ml/model.
The generic Estimator (bring-your-own-container) points at the local image through image_uri, so no AWS image is pulled.
est = Estimator(
entry_point="train.py",
source_dir=os.path.join(
os.environ.get("SAGEMAKER_LOCAL_REPO_PATH", "/workspace"),
"projects",
"sagemaker_xgboost",
"src",
"sagemaker_xgboost",
),
image_uri=cfg.image_tag,
role=cfg.role_arn,
instance_type="local",
instance_count=1,
sagemaker_session=sm_session,
output_path=f"s3://{cfg.bucket}/models",
hyperparameters={"dataset": "diabetes"},
)
est.fit()
Deploy & predict¶
Inputs are 2-D JSON arrays so the default serving input handler parses them correctly.
predictor = est.deploy(
initial_instance_count=1,
instance_type="local",
serializer=JSONSerializer(),
deserializer=JSONDeserializer(),
)
sample = [0.05, 0.08, 0.0, 0.02, 0.04, 0.06, 0.0, 0.0, 0.03, 0.05]
result = predictor.predict([sample])
print("predicted disease progression:", result)
Cleanup¶
Delete the endpoint so the serving container is released and port 8080 is freed.
predictor.delete_endpoint()