使用不同推理脚本的多数据模型。

0

【以下的问题经过翻译处理】 我在这里找到了一个类似的帖子(https://repost.aws/questions/QUEVxelof3TmimoLt1Kd1SBA/how-to-configure-our-own-inference-py-for-two-different-py-torch-models-in-multi-data-model-to-build-single-endpoint-and-call-both-models-from-there),但我希望我的情况会更简单一些。

问题:在多端点中是否有一种提供每个模型的两个单独的推理脚本的方法,或者需要创建一些动态/自定义推理脚本来处理两者?

我使用SageMaker Python SDK Scikit-learn processing/models构建了两个模型管道:

一个是用于请求推理时返回群集预测和质心距离的聚类模型;另一个只是PCA,请求推理时返回3个主成分。

由于数据的奇怪格式和输出的提供方式,两个模型都使用自定义推理脚本(例如预测与转换)。

从我所看到的MultiDataModel示例中,当传递模型信息时,它仅接受单个entry_point用于inference.py,并且稍后“添加”模型:

cluster_model = SKLearnModel( model_data=cluster_artifact, role=role, entry_point="scripts/cluster_inference.py", sagemaker_session=sagemaker_session ) pca_model = SKLearnModel( model_data=pca_artifact, role=role, entry_point="scripts/pca_inference.py", sagemaker_session=sagemaker_session ) mme = MultiDataModel( name='model', model_data_prefix=model_data_prefix, model= cluster_model, sagemaker_session=sagemaker_session, )

作为单独的端点部署,两者都可以按预期执行推理,但我无法将其作为一个端点工作。

以下是我最近收到的错误信息,但很难理解是哪里出了问题,因为在我的推理脚本和调用端点时,序列化应该被正确处理了。

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (503) from primary with message "{
  "code": 503,
  "type": "InternalServerException",
  "message": "Unsupported model output data type."}".

任何指引都非常感谢

profile picture
EXPERTE
gefragt vor 8 Monaten49 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 你好!

简而言之,我们的SageMaker scikit-learn容器目前不支持模型特定的推断脚本。

您在MultiDataModel对象中引用的entry_point脚本将用于所有模型的推断脚本。如果在脚本中添加了日志记录,您将能够在CloudWatch日志中查看它们。

如果您有一些需要在特定模型上执行的预处理/后处理脚本,则需要将它们全部写在一个通用的inference.py文件中。然后,在调用端点时,在数据中添加一些额外的属性,并让相同的脚本读取这些额外的属性,以便它知道要执行哪个预处理/后处理脚本。

需要注意的一件事是,尽管您在MultiDataModel中引用了一个模型对象,即

mme = MultiDataModel(
    name='model',
    model_data_prefix=model_data_prefix,
    model= cluster_model,
    sagemaker_session=sagemaker_session,
)

但从模型对象中提取的唯一信息是image_uri和entry_point,这些信息在端点部署期间是必需的。

'model_data_prefix'中的所有model.tar.gz都不应该具有inference.py,因为这会使容器混淆并强制其返回默认的处理程序,因此您可能会收到ModelError。

可以尝试以下操作:

cluster_model = SKLearnModel(
    model_data=cluster_artifact,
    role=role,
    entry_point="scripts/cluster_inference.py",
    sagemaker_session=sagemaker_session
)
pca_model = SKLearnModel(
    model_data=pca_artifact,
    role=role,
    entry_point="scripts/pca_inference.py",
    sagemaker_session=sagemaker_session
)
mme = MultiDataModel(
    name='model',
    model_data_prefix=model_data_prefix,	#make sure the directory of this prefix is empty, i.e. no models in this location
    model= cluster_model,
    sagemaker_session=sagemaker_session,
)


list(mme.list_models()) # this should be empty

mme.add_model(model_data_source=cluster_artifact, model_data_path='cluster_artifact.tar.gz')	#make sure model artifact doesn't contain inference.py
mme.add_model(model_data_source=pca_artifact, model_data_path='pca_artifact.tar.gz') #make sure model artifact doesn't contain inference.py

list(mme.list_models()) # there should be two models listed now, if you look at the location of model_data_prefix, there should also be two model artifact

output_cluster = predictor.predict(data='<your-data>', target_model='cluster_artifact.tar.gz')
print(output_cluster) #this should work since it's using the inference.py from cluster_inference.py

output_pca = predictor.predict(data='<your-data>', target_model='pca_artifact.tar.gz')
print(output_pca) 	#this might fail since it's using cluster_inference.py, add this model's inference script into cluster_inference.py to make it work 

我知道这种方法并不理想,因为如果您有一个新模型和新的预处理/后处理脚本,您需要重新部署端点,以便新脚本生效。

实际上,我们刚刚在我们的TensorFlow容器中添加了支持,允许使用特定于模型的推理脚本:https://github.com/aws/deep-learning-containers/pull/2680

您可以在此处请求为我们的scikit容器添加相同的功能:https://github.com/aws/sagemaker-scikit-learn-container/issues

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen

Relevanter Inhalt