托管运行 R 代码的端点的最佳和正确方法是什么?

问题描述

我认为加载模型并调用端点来调用 R 的 predict(object,newdata,...) 函数一定是一个相对常见的用例。我想通过在 R 端使用 plumber自定义 AWS Sagemaker 容器来做到这一点。这个 example 提供了所有细节,我认为,this bit of documentation 还解释了容器应该如何构建和反应。 我按照这些文件的步骤,但我得到了

生产变体 AllTraffic 的主容器未通过 ping 运行状况检查。请检查此终端节点的 CloudWatch 日志。

几分钟后在 Sagemaker 控制台中,端点创建失败。

这是我的容器:

# --- Dockerfile
FROM rocker/r-base
RUN apt-get -y update && apt-get install -y libsodium-dev libcurl4-openssl-dev
RUN apt-get install -y  \
    ca-certificates

RUN R -e "install.packages(c('lme4','plumber'))"

ADD ./plumber.R /

ENTRYPOINT ["R","-e","plumber::pr_run(plumber::pr('plumber.R'),port=8080)",\
            "--no-save"]
# --- plumber.R
library(plumber)
library(lme4)

prefix <- '/opt/ml'
print(dir('/opt/ml',recursive = TRUE))
model <- readRDS(file.path(prefix,'model','model.RDS'))

#* @apiTitle Guess the likelihood of something

#' Ping to show server is there
#' @get /ping
function() {
  print(paste('successfully pinged at',Sys.time()))
  return('')}

#' Parse input and return prediction from model
#' @param req The http request sent
#' @post /invocations
function(req) {
  print(paste('invocation triggered at',Sys.time()))
  conn <- textConnection(gsub('\\\\n','\n',req$postBody))
  data <- read.csv(conn)
  close(conn)
  
  print(data)
  
  predict(model,data,allow.new.levels = TRUE,type = 'response')
}

然后使用以下代码创建端点:

# run_on_sagemaker.py
# [...]
create_model_response = sm.create_model(
    ModelName=model_name,ExecutionRoleArn=role,PrimaryContainer={
        'Image': image_uri,'ModelDataUrl': s3_model_location
    }
)
create_endpoint_config_response = sm.create_endpoint_config(
    EndpointConfigName=endpoint_config_name,ProductionVariants=[{
        'InstanceType': instance_type,'InitialInstanceCount': 1,'ModelName': model_name,'VariantName': 'AllTraffic'}])

print("Endpoint Config Arn: " + create_endpoint_config_response['EndpointConfigArn'])

print('Endpoint Response:')
create_endpoint_response = sm.create_endpoint(
    EndpointName=endpoint_name,EndpointConfigName=endpoint_config_name)
print(create_endpoint_response['EndpointArn'])

resp = sm.describe_endpoint(EndpointName=endpoint_name)
status = resp['EndpointStatus']
print("Status: " + status)

try:
    sm.get_waiter('endpoint_in_service').wait(EndpointName=endpoint_name)
finally:
    resp = sm.describe_endpoint(EndpointName=endpoint_name)
    status = resp['EndpointStatus']
    print("Arn: " + resp['EndpointArn'])
    print("Status: " + status)
    if status != 'InService':
        raise Exception('Endpoint creation did not succeed')
    print(create_model_response['ModelArn'])

大多数代码实际上是从上面提到的例子中复制的,我注意到最显着的区别是在我的容器中模型会立即加载,而在示例中每次调用时都会加载模型对象(这肯定会变慢回复下来了,所以我想知道,为什么?)。

当它在本地运行时,Cloudwatch 上的日志等于容器的输出,并且表明没有失败。在本地我可以查询容器 curl -d "data\nin\ncsv\nformat" -i localhost:8080/invocations 并且它工作正常并返回对 POST 数据中每一行的预测。此外,curl localhost:8080/ping 返回 [""],我认为应该如此。并且它没有显示出缓慢的迹象,模型对象的大小为 4.4MiB(尽管这个简单版本运行后会大大扩展)。

终端上的错误

Traceback (most recent call last):
  File "run_on_sagemaker.py",line 57,in <module>
    sm.get_waiter('endpoint_in_service').wait(EndpointName=endpoint_name)
  File "[...]/lib/python3.8/site-packages/botocore/waiter.py",line 53,in wait
    Waiter.wait(self,**kwargs)
  File "[...]/lib/python3.8/site-packages/botocore/waiter.py",line 320,in wait
    raise WaiterError(
botocore.exceptions.WaiterError: Waiter EndpointInService Failed: Waiter encountered a terminal failure state

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "run_on_sagemaker.py",line 64,in <module>
    raise Exception('Endpoint creation did not succeed')

那么,为什么这会在 Sagemaker 控制台上失败?这是一个方法吗,有没有更好的方法,我该如何做进一步的诊断?通常,我也无法获取运行您自己的 R 容器的 AWS 示例(见上文),所以我想知道运行 Sagemaker 模型的 R 预测的最佳方法是什么。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...