我有一个基于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日志,看看是否有任何调用到达端点。
如果有,请查看同一日志文件中是否有任何错误地发送了响应。