Package: sagemaker-shim
SageMaker Shim for Grand ChallengeThis repo contains a library that adapts algorithms that implement the Grand Challenge inference API for running in SageMaker.The application contains:Aclickcli client with options to launch a web serverAfastapiweb server that implements the SageMaker endpointsandpydanticmodels that interface between S3, and run the original inference jobs.The application is compiled on Python 3.11 usingpyinstaller, and then distributed as a statically linked binary usingstaticx.
It is able to adapt any container, including ones based onscratchoralpineimages.UsageThe binary is designed to be added to an existing container image that implements the Grand Challenge API.
On Grand Challenge this happens automatically by usingcraneto add the binary, directories and environment variables to each container image.
The binary itself will:Download the input files from the provided locations on S3 to/input, optionally decompressing the inputs.Execute the original container program in a subprocess.
This is found by inspecting the following environment variables:GRAND_CHALLENGE_COMPONENT_CMD_B64J: the originalcmdof the container, json encoded as a base64 string.GRAND_CHALLENGE_COMPONENT_ENTRYPOINT_B64J: the originalentrypointof the container, json encoded as a base64 string.Upload the contents of/outputto the given output S3 bucket and prefix.LoggingCloudWatch does not offer separation ofstdoutandstderrby default.sagemaker-shimincludes a logging filter and formatter that creates structured logs from the application and subprocess.
This allows grand challenge to separate out internal, external, stdout and stderr streams.
These structured logs are JSON objects with the format:{"log":"",// The original log message"level":"CRITICAL"|"ERROR"|"WARNING"|"INFO"|"DEBUG"|"NOTSET",// The severity level of the log"source":"stdout"|"stderr",// The source stream"internal":true|false,// Whether the source of the log is from sagemaker shim or the subprocess"task":""|null,// The ID of the task}sagemaker-shim serveThis starts the webserver onhttp://0.0.0.0:8080which implements theSageMaker API.
There are three endpoints:/ping(GET): returns an empty 200 response if the container is healthy/execution-parameters(GET): returns the preferred execution parameters for AWS SageMaker Batch Inference/invocations(POST): SageMaker can make POST requests to this endpoint.
The body contains the json encoded data required to run a single inference task:{"pk":"unique-test-id","inputs":[{"relative_path":"interface/path","bucket_name":"name-of-input-bucket","bucket_key":"/path/to/input/file/in/bucket","decompress":false,},...],"output_bucket_name":"name-of-output-bucket","output_prefix":"/prefix/of/output/files",}The endpoint will return an object containing the return code of the subprocess inresponse["return_code"],
and any outputs will be placed in the output bucket at the output prefix. A file with the inference outputs
will also be located ats3://<output_bucket_name>/<output_prefix>/.sagemaker_shim/inference_result.jsonsagemaker-shim invokeThis will invoke the model directly given the arguments. You can specify either:-f / --file: S3 URI of a JSON file containing a list of task definitions, e.g.s3://my-bucket/invocations.json-t / --tasks: A JSON string of task definitionsIn both cases the contents of the file or string will be an array of task objects:[{"pk":"unique-test-id-1","inputs":[...],"output_bucket_name":"name-of-output-bucket","output_prefix":"/prefix/of/output/files-1",},{"pk":"unique-test-id-2","inputs":[...],"output_bucket_name":"name-of-output-bucket","output_prefix":"/prefix/of/output/files-2",}]A file with the inference outputs will be located ats3://<output_bucket_name>/<output_prefix>/.sagemaker_shim/inference_result.json.Patching an Existing ContainerTo patch an existing container image in a registry see the example intests/utils.py.
First you will need to get the originalcmdandentrypointusingget_new_env_varsandget_image_config.
Then you can add the binary, set the newcmd,entrypoint, and environment variables withmutate_image.