使用Sagemaker调用预训练自定义PyTorch模型端点的调用超时问题 [Inference]

我有一个基于PyTorch的预训练模型(contextualized_topic_models),并使用AWS sagemaker脚本模型进行了部署。然而,当我尝试调用端点进行推理时,无论我尝试什么方法,它总是返回“调用超时错误”。我尝试了不同类型的输入,并更改了input_fn()函数,但仍然不起作用。

我已经在Colab上运行了我的inference.py脚本(未连接到aws服务器),每个函数似乎都能正常工作,并返回预期的预测结果。

我已经调试这个问题4天了,甚至在梦里也在想这个问题… 我将非常感激任何帮助。

这是我的部署脚本。

from sagemaker.pytorch.model import PyTorchModel
pytorch_model = PyTorchModel(
    model_data=pretrained_model_data,
    entry_point="inference.py",
    role=role,
    framework_version="1.8.1",
    py_version="py36",
    sagemaker_session=sess,
)
endpoint_name = "topic-modeling-inference"
# Deploy
predictor = pytorch_model.deploy(initial_instance_count = 1,
                                 instance_type = "ml.g4dn.xlarge",
                                 endpoint_name = endpoint_name)

端点测试(预测)脚本

# Test the model
import json
sm = boto3.client('sagemaker-runtime')
endpoint_name = "topic-modeling-inference"
prompt = [
    "Here is a piece of cake."
        ]
promptbody = [x.encode('utf-8') for x in prompt]
promptbody = promptbody[0]
#body= bytes(prompt[0], 'utf-8')
#tryout = prompt[0]
response = sm.invoke_endpoint(EndpointName=endpoint_name,
                              ContentType="text/csv",
                              Body=promptbody #Body=tryout.encode(encoding='UTF-8'))
print(response)
#result = json.loads(response['Body'].read().decode('utf-8'))
#print(result)

我的inference.py脚本的一部分

def predict_fn(input_data, model):
    input_data_features = tp10.transform(text_for_contextual=input_data)
    topic_prediction = model.get_doc_topic_distribution(input_data_features, n_samples=20)
    topicID = np.argmax(topic_prediction)
    topicID = int(topicID.astype('str'))
    return topicID
    #prediction = model.get_topic_lists(20)[np.argmax(topic_prediction)]
    #return prediction
def input_fn(request_body, request_content_type):
    if request_content_type == "application/json":
        request = json.loads(request_body)
    else:
        request = request_body
    return request
def output_fn(prediction, response_content_type):
    if response_content_type == "application/json":
        response = str(json.dumps(prediction))
    else:
        response = str(json.dumps(prediction))
    return response

任何帮助或指导都将非常有用。提前感谢您。


回答:

我建议查看端点的CloudWatch日志,看看是否有任何调用到达端点。

如果有,请查看同一日志文件中是否有任何错误地发送了响应。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注