mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
feat(sagemaker): add SageMaker emulation with Docker-backed training (#1949)
Implement the SageMaker control plane over the AWS JSON 1.1 protocol
(X-Amz-Target: SageMaker.*) plus the runtime invocation path:
- Models, endpoint configs, endpoints, training jobs, and tags with
AWS-shaped validation errors (ValidationException / ResourceInUse)
- CreateTrainingJob runs the training image as a real Docker container
using the /opt/ml contract: hyperparameters, resourceconfig and
inputdataconfig under /opt/ml/input/config, S3 channel data under
/opt/ml/input/data/<channel>, and /opt/ml/model uploaded back to S3
as model.tar.gz under the configured S3OutputPath
- CreateEndpoint hosts the model image as a long-lived container with
the serve command, /ping health checks, and ModelDataUrl artifacts
extracted into /opt/ml/model
- POST /endpoints/{name}/invocations proxies to the container and maps
model 5xx responses to ModelError (HTTP 424) like SageMaker Runtime
- Training container logs stream to CloudWatch Logs under
/aws/sagemaker/TrainingJobs
- Storage wired through StorageFactory with memory/persistent modes,
config in EmulatorConfig and both application.yml files
- Unit, integration, and Docker-gated integration tests plus a boto3
compatibility test; service and docs registered in the catalog
This commit is contained in:
1 parent
e5f9baa12d
commit
e2aefa654b
20 files changed
+2110
-1
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
from botocore.config import Config
|
||||
|
||||
|
||||
def client(service):
|
||||
return boto3.client(
|
||||
service,
|
||||
# Every other test file gets this from the conftest.py fixtures, which read
|
||||
# FLOCI_ENDPOINT (the test runner container's compat-net hostname for floci, e.g.
|
||||
# http://floci:4566). This file has no fixture of its own, so it must read the same
|
||||
# variable directly instead of hardcoding localhost, which only resolves to floci
|
||||
# when running the suite straight on the host rather than in its own CI container.
|
||||
endpoint_url=os.environ.get("FLOCI_ENDPOINT", "http://localhost:4566"),
|
||||
region_name="us-east-1",
|
||||
aws_access_key_id="test",
|
||||
aws_secret_access_key="test",
|
||||
config=Config(retries={"max_attempts": 1}),
|
||||
)
|
||||
|
||||
|
||||
def test_sagemaker_control_plane_and_training():
|
||||
sm = client("sagemaker")
|
||||
s3 = client("s3")
|
||||
suffix = uuid.uuid4().hex[:8]
|
||||
model = f"compat-sm-{suffix}"
|
||||
cfg = f"compat-sm-cfg-{suffix}"
|
||||
bucket = f"compat-sm-{suffix}"
|
||||
|
||||
sm.create_model(ModelName=model, PrimaryContainer={"Image": "public.ecr.aws/docker/library/busybox:stable"}, ExecutionRoleArn="arn:aws:iam::000000000000:role/r")
|
||||
assert sm.describe_model(ModelName=model)["ModelName"] == model
|
||||
sm.create_endpoint_config(EndpointConfigName=cfg, ProductionVariants=[{"VariantName": "AllTraffic", "ModelName": model, "InitialInstanceCount": 1, "InstanceType": "ml.t2.medium"}])
|
||||
assert any(c["EndpointConfigName"] == cfg for c in sm.list_endpoint_configs()["EndpointConfigs"])
|
||||
|
||||
s3.create_bucket(Bucket=bucket)
|
||||
s3.put_object(Bucket=bucket, Key="input/data.txt", Body=b"hello")
|
||||
job = f"compat-train-{suffix}"
|
||||
sm.create_training_job(
|
||||
TrainingJobName=job,
|
||||
RoleArn="arn:aws:iam::000000000000:role/r",
|
||||
AlgorithmSpecification={
|
||||
"TrainingImage": "public.ecr.aws/docker/library/busybox:stable",
|
||||
"TrainingInputMode": "File",
|
||||
"ContainerEntrypoint": ["/bin/sh", "-c"],
|
||||
"ContainerArguments": ["mkdir -p /opt/ml/model && echo ok > /opt/ml/model/model.txt"],
|
||||
},
|
||||
InputDataConfig=[{
|
||||
"ChannelName": "train",
|
||||
"DataSource": {"S3DataSource": {"S3DataType": "S3Prefix", "S3Uri": f"s3://{bucket}/input"}},
|
||||
}],
|
||||
OutputDataConfig={"S3OutputPath": f"s3://{bucket}/output"},
|
||||
ResourceConfig={"InstanceType": "ml.m5.large", "InstanceCount": 1, "VolumeSizeInGB": 1},
|
||||
StoppingCondition={"MaxRuntimeInSeconds": 60},
|
||||
)
|
||||
deadline = time.time() + 120
|
||||
while time.time() < deadline:
|
||||
desc = sm.describe_training_job(TrainingJobName=job)
|
||||
if desc["TrainingJobStatus"] == "Completed":
|
||||
artifact = desc["ModelArtifacts"]["S3ModelArtifacts"]
|
||||
key = artifact.split(f"s3://{bucket}/", 1)[1]
|
||||
assert s3.get_object(Bucket=bucket, Key=key)["Body"].read()
|
||||
break
|
||||
if desc["TrainingJobStatus"] == "Failed":
|
||||
pytest.fail(desc.get("FailureReason", "training failed"))
|
||||
time.sleep(1)
|
||||
else:
|
||||
pytest.fail("Timed out waiting for SageMaker training job")
|
||||
|
||||
sm.delete_endpoint_config(EndpointConfigName=cfg)
|
||||
sm.delete_model(ModelName=model)
|
||||
Reference in new issue
Block a user