Chainer

Chainer Estimator

class sagemaker.chainer.estimator.Chainer(entry_point, use_mpi=None, num_processes=None, process_slots_per_host=None, additional_mpi_options=None, source_dir=None, hyperparameters=None, framework_version=None, py_version=None, image_uri=None, **kwargs)

Bases: sagemaker.estimator.Framework

Handle end-to-end training and deployment of custom Chainer code.

This Estimator executes an Chainer script in a managed execution environment.

The managed Chainer environment is an Amazon-built Docker container that executes functions defined in the supplied entry_point Python script within a SageMaker Training Job.

Training is started by calling fit() on this Estimator. After training is complete, calling deploy() creates a hosted SageMaker endpoint and returns an ChainerPredictor instance that can be used to perform inference against the hosted model.

Technical documentation on preparing Chainer scripts for SageMaker training and using the Chainer Estimator is available on the project home-page: https://github.com/aws/sagemaker-python-sdk

Parameters
  • entry_point (str) – Path (absolute or relative) to the Python source file which should be executed as the entry point to training. If source_dir is specified, then entry_point must point to a file located at the root of source_dir.

  • use_mpi (bool) – If true, entry point is run as an MPI script. By default, the Chainer Framework runs the entry point with ‘mpirun’ if more than one instance is used.

  • num_processes (int) – Total number of processes to run the entry point with. By default, the Chainer Framework runs one process per GPU (on GPU instances), or one process per host (on CPU instances).

  • process_slots_per_host (int) – The number of processes that can run on each instance. By default, this is set to the number of GPUs on the instance (on GPU instances), or one (on CPU instances).

  • additional_mpi_options (str) – String of options to the ‘mpirun’ command used to run the entry point. For example, ‘-X NCCL_DEBUG=WARN’ will pass that option string to the mpirun command.

  • source_dir (str) – Path (absolute or relative) to a directory with any other training source code dependencies aside from the entry point file (default: None). Structure within this directory are preserved when training on Amazon SageMaker.

  • hyperparameters (dict) – Hyperparameters that will be used for training (default: None). The hyperparameters are made accessible as a dict[str, str] to the training code on SageMaker. For convenience, this accepts other types for keys and values, but str() will be called to convert them before training.

  • py_version (str) – Python version you want to use for executing your model training code. Defaults to None. Required unless image_uri is provided.

  • framework_version (str) – Chainer version you want to use for executing your model training code. Defaults to None. Required unless image_uri is provided. List of supported versions: https://github.com/aws/sagemaker-python-sdk#chainer-sagemaker-estimators.

  • image_uri (str) –

    If specified, the estimator will use this image for training and hosting, instead of selecting the appropriate SageMaker official image based on framework_version and py_version. It can be an ECR url or dockerhub image and tag.

    Examples
    • 123412341234.dkr.ecr.us-west-2.amazonaws.com/my-custom-image:1.0

    • custom-image:latest

    If framework_version or py_version are None, then image_uri is required. If also None, then a ValueError will be raised.

  • **kwargs – Additional kwargs passed to the Framework constructor.

Tip

You can find additional parameters for initializing this class at Framework and EstimatorBase.

hyperparameters()

Return hyperparameters used by your custom Chainer code during training.

create_model(model_server_workers=None, role=None, vpc_config_override='VPC_CONFIG_DEFAULT', entry_point=None, source_dir=None, dependencies=None, **kwargs)

Create a SageMaker ChainerModel object that can be deployed to an Endpoint.

Parameters
  • model_server_workers (int) – Optional. The number of worker processes used by the inference server. If None, server will use one worker per vCPU.

  • role (str) – The ExecutionRoleArn IAM Role ARN for the Model, which is also used during transform jobs. If not specified, the role from the Estimator will be used.

  • vpc_config_override (dict[str, list[str]]) –

    Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator.

    • ’Subnets’ (list[str]): List of subnet ids.

    • ’SecurityGroupIds’ (list[str]): List of security group ids.

  • entry_point (str) – Path (absolute or relative) to the local Python source file which should be executed as the entry point to training. If source_dir is specified, then entry_point must point to a file located at the root of source_dir. If not specified, the training entry point is used.

  • source_dir (str) – Path (absolute or relative) to a directory with any other serving source code dependencies aside from the entry point file. If not specified, the model source directory from training is used.

  • dependencies (list[str]) – A list of paths to directories (absolute or relative) with any additional libraries that will be exported to the container. If not specified, the dependencies from training are used. This is not supported with “local code” in Local Mode.

  • **kwargs – Additional kwargs passed to the ChainerModel constructor.

Returns

A SageMaker ChainerModel object. See ChainerModel() for full details.

Return type

sagemaker.chainer.model.ChainerModel

Chainer Model

class sagemaker.chainer.model.ChainerModel(model_data, role, entry_point, image_uri=None, framework_version=None, py_version=None, predictor_cls=<class 'sagemaker.chainer.model.ChainerPredictor'>, model_server_workers=None, **kwargs)

Bases: sagemaker.model.FrameworkModel

An Chainer SageMaker Model that can be deployed to a SageMaker Endpoint.

Initialize an ChainerModel.

Parameters
  • model_data (str) – The S3 location of a SageMaker model data .tar.gz file.

  • role (str) – An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if it needs to access an AWS resource.

  • entry_point (str) – Path (absolute or relative) to the Python source file which should be executed as the entry point to model hosting. If source_dir is specified, then entry_point must point to a file located at the root of source_dir.

  • image_uri (str) – A Docker image URI (default: None). If not specified, a default image for Chainer will be used. If framework_version or py_version are None, then image_uri is required. If image_uri is also None, then a ValueError will be raised.

  • framework_version (str) – Chainer version you want to use for executing your model training code. Defaults to None. Required unless image_uri is provided.

  • py_version (str) – Python version you want to use for executing your model training code. Defaults to None. Required unless image_uri is provided.

  • predictor_cls (callable[str, sagemaker.session.Session]) – A function to call to create a predictor with an endpoint name and SageMaker Session. If specified, deploy() returns the result of invoking this function on the created endpoint name.

  • model_server_workers (int) – Optional. The number of worker processes used by the inference server. If None, server will use one worker per vCPU.

  • **kwargs – Keyword arguments passed to the FrameworkModel initializer.

Tip

You can find additional parameters for initializing this class at FrameworkModel and Model.

prepare_container_def(instance_type=None, accelerator_type=None, serverless_inference_config=None)

Return a container definition with framework configuration set in model environment.

Parameters
  • instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’.

  • accelerator_type (str) – The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, ‘ml.eia1.medium’.

  • serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig) – Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to find image URIs.

Returns

A container definition object usable with the CreateModel API.

Return type

dict[str, str]

serving_image_uri(region_name, instance_type, accelerator_type=None, serverless_inference_config=None)

Create a URI for the serving image.

Parameters
  • region_name (str) – AWS region where the image is uploaded.

  • instance_type (str) – SageMaker instance type. Used to determine device type (cpu/gpu/family-specific optimized).

  • serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig) – Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to determine device type.

Returns

The appropriate image URI based on the given parameters.

Return type

str

Chainer Predictor

class sagemaker.chainer.model.ChainerPredictor(endpoint_name, sagemaker_session=None, serializer=<sagemaker.serializers.NumpySerializer object>, deserializer=<sagemaker.deserializers.NumpyDeserializer object>)

Bases: sagemaker.predictor.Predictor

A Predictor for inference against Chainer Endpoints.

This is able to serialize Python lists, dictionaries, and numpy arrays to multidimensional tensors for Chainer inference.

Initialize an ChainerPredictor.

Parameters
  • endpoint_name (str) – The name of the endpoint to perform inference on.

  • sagemaker_session (sagemaker.session.Session) – Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed. If not specified, the estimator creates one using the default AWS configuration chain.

  • serializer (sagemaker.serializers.BaseSerializer) – Optional. Default serializes input data to .npy format. Handles lists and numpy arrays.

  • deserializer (sagemaker.deserializers.BaseDeserializer) – Optional. Default parses the response from .npy format to numpy array.