

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Distribuisci un modello compilato utilizzando SDK SageMaker
<a name="neo-deployment-hosting-services-sdk"></a>

Devi soddisfare la sezione dei [prerequisiti](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites) se il modello è stato compilato utilizzando AWS SDK per Python (Boto3) o la console Amazon SageMaker AI. AWS CLI Segui uno dei seguenti casi d'uso per distribuire un modello compilato con SageMaker Neo in base a come hai compilato il tuo modello.

**Topics**
+ [Se hai compilato il modello utilizzando l'SDK SageMaker](#neo-deployment-hosting-services-sdk-deploy-sm-sdk)
+ [Se hai compilato il modello utilizzando o MXNet PyTorch](#neo-deployment-hosting-services-sdk-deploy-sm-boto3)
+ [Se hai compilato il tuo modello utilizzando Boto3, SageMaker console o la CLI per TensorFlow](#neo-deployment-hosting-services-sdk-deploy-sm-boto3-tensorflow)

## Se hai compilato il modello utilizzando l'SDK SageMaker
<a name="neo-deployment-hosting-services-sdk-deploy-sm-sdk"></a>

L'handle di oggetto [sagemaker.Model](https://sagemaker.readthedocs.io/en/stable/api/inference/model.html?highlight=sagemaker.Model) per il modello compilato fornisce la funzione [deploy()](https://sagemaker.readthedocs.io/en/stable/api/inference/model.html?highlight=sagemaker.Model#sagemaker.model.Model.deploy), che consente di creare un endpoint per servire richieste di inferenza. La funzione consente di impostare il numero e il tipo di istanze che vengono utilizzate per l'endpoint. È necessario scegliere un'istanza per la quale è stato compilato il modello. Ad esempio, nel job compilato nella sezione [Compile a Model (Amazon SageMaker SDK)](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-job-compilation-sagemaker-sdk.html), questo è. `ml_c5` 

```
predictor = compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.c5.4xlarge')

# Print the name of newly created endpoint
print(predictor.endpoint_name)
```

## Se hai compilato il modello utilizzando o MXNet PyTorch
<a name="neo-deployment-hosting-services-sdk-deploy-sm-boto3"></a>

Crea il modello di SageMaker intelligenza artificiale e implementalo utilizzando l'API deploy () nell'ambito del modello specifico del framework. APIs Perché è [MXNetModel MXNet](https://sagemaker.readthedocs.io/en/stable/frameworks/mxnet/sagemaker.mxnet.html?highlight=MXNetModel#mxnet-model) e for, lo è. PyTorch [ PyTorchModel](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html?highlight=PyTorchModel#sagemaker.pytorch.model.PyTorchModel) Quando si crea e si distribuisce un modello di SageMaker intelligenza artificiale, è necessario impostare la variabile di `MMS_DEFAULT_RESPONSE_TIMEOUT` ambiente su `500` e specificare il `entry_point` parametro come script di inferenza (`inference.py`) e il `source_dir` parametro come posizione della directory (`code`) dello script di inferenza. Per preparare lo script di inferenza (`inference.py`), segui la fase Prerequisiti. 

L'esempio seguente mostra come utilizzare queste funzioni per implementare un modello compilato utilizzando SageMaker AI SDK per Python: 

------
#### [ MXNet ]

```
from sagemaker.mxnet import MXNetModel

# Create SageMaker model and deploy an endpoint
sm_mxnet_compiled_model = MXNetModel(
    model_data={{'insert S3 path of compiled MXNet model archive'}},
    role='AmazonSageMaker-ExecutionRole',
    entry_point='inference.py',
    source_dir='code',
    framework_version='1.8.0',
    py_version='py3',
    image_uri={{'insert appropriate ECR Image URI for MXNet'}},
    env={'MMS_DEFAULT_RESPONSE_TIMEOUT': '500'},
)

# Replace the example instance_type below to your preferred instance_type
predictor = sm_mxnet_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge')

# Print the name of newly created endpoint
print(predictor.endpoint_name)
```

------
#### [ PyTorch 1.4 and Older ]

```
from sagemaker.pytorch import PyTorchModel

# Create SageMaker model and deploy an endpoint
sm_pytorch_compiled_model = PyTorchModel(
    model_data={{'insert S3 path of compiled PyTorch model archive'}},
    role='AmazonSageMaker-ExecutionRole',
    entry_point='inference.py',
    source_dir='code',
    framework_version='1.4.0',
    py_version='py3',
    image_uri={{'insert appropriate ECR Image URI for PyTorch'}},
    env={'MMS_DEFAULT_RESPONSE_TIMEOUT': '500'},
)

# Replace the example instance_type below to your preferred instance_type
predictor = sm_pytorch_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge')

# Print the name of newly created endpoint
print(predictor.endpoint_name)
```

------
#### [ PyTorch 1.5 and Newer ]

```
from sagemaker.pytorch import PyTorchModel

# Create SageMaker model and deploy an endpoint
sm_pytorch_compiled_model = PyTorchModel(
    model_data='insert S3 path of compiled PyTorch model archive',
    role='AmazonSageMaker-ExecutionRole',
    entry_point='inference.py',
    source_dir='code',
    framework_version='1.5',
    py_version='py3',
    image_uri='insert appropriate ECR Image URI for PyTorch',
)

# Replace the example instance_type below to your preferred instance_type
predictor = sm_pytorch_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge')

# Print the name of newly created endpoint
print(predictor.endpoint_name)
```

------

**Nota**  
Le policy `AmazonSageMakerFullAccess` e `AmazonS3ReadOnlyAccess`devono essere collegate al ruolo IAM di `AmazonSageMaker-ExecutionRole`. 

## Se hai compilato il tuo modello utilizzando Boto3, SageMaker console o la CLI per TensorFlow
<a name="neo-deployment-hosting-services-sdk-deploy-sm-boto3-tensorflow"></a>

Costruisci un oggetto `TensorFlowModel`, quindi richiama deploy: 

```
role='AmazonSageMaker-ExecutionRole'
model_path={{'S3 path for model file'}}
framework_image={{'inference container arn'}}
tf_model = TensorFlowModel(model_data=model_path,
                framework_version='1.15.3',
                role=role, 
                image_uri=framework_image)
instance_type='ml.c5.xlarge'
predictor = tf_model.deploy(instance_type=instance_type,
                    initial_instance_count=1)
```

Per ulteriori informazioni, consulta [Distribuzione diretta da artefatti del modello](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/deploying_tensorflow_serving.html#deploying-directly-from-model-artifacts). 

Da [questo elenco](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-container-images.html) puoi selezionare un'immagine Docker (URI Amazon ECR) che soddisfi le tue esigenze. 

[Per ulteriori informazioni su come costruire un `TensorFlowModel` oggetto, consulta l'SDK. SageMaker ](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-serving-model) 

**Nota**  
La tua prima richiesta di inferenza potrebbe avere una latenza elevata se distribuisci il modello su una GPU. Questo perché alla prima richiesta di inferenza viene creato un kernel di calcolo ottimizzato. Ti consigliamo di creare un file di riscaldamento delle richieste di inferenza e di archiviarlo insieme al file di modello prima di inviarlo a un TFX. Questo processo è noto come “riscaldamento” del modello. 

Il seguente frammento di codice mostra come produrre il file di riscaldamento per l'esempio di classificazione delle immagini nella sezione dei [prerequisiti](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites): 

```
import tensorflow as tf
from tensorflow_serving.apis import classification_pb2
from tensorflow_serving.apis import inference_pb2
from tensorflow_serving.apis import model_pb2
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_log_pb2
from tensorflow_serving.apis import regression_pb2
import numpy as np

with tf.python_io.TFRecordWriter("tf_serving_warmup_requests") as writer:       
    img = np.random.uniform(0, 1, size=[224, 224, 3]).astype(np.float32)
    img = np.expand_dims(img, axis=0)
    test_data = np.repeat(img, 1, axis=0)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'compiled_models'
    request.model_spec.signature_name = 'serving_default'
    request.inputs['Placeholder:0'].CopyFrom(tf.compat.v1.make_tensor_proto(test_data, shape=test_data.shape, dtype=tf.float32))
    log = prediction_log_pb2.PredictionLog(
    predict_log=prediction_log_pb2.PredictLog(request=request))
    writer.write(log.SerializeToString())
```

Per ulteriori informazioni su come «riscaldare» il modello, consulta la pagina [TensorFlow TFX](https://www.tensorflow.org/tfx/serving/saved_model_warmup).