1

这是来自 ML newbee 的一个问题 :-)

我正在使用无服务器框架构建 AWS StepFunction,其中一个步骤旨在使用 HuggingFace 深度学习容器 (DLC) 部署 Sagemaker 端点。

问题是我无法让 Lambda 与 SageMaker 一起工作(构建估算器)。

我拥有的解决方案之一是使用 SageMaker studio 手动启动端点,但我真的希望将所有内容都包含在代码中。

这是我试图让 Sagemaker 工作的方法

def installPack(package):
    import subprocess
    import sys
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])

installPack('sagemaker')
from sagemaker.huggingface import HuggingFaceModel
import sagemaker 

role = sagemaker.get_execution_role()

# Hub Model configuration. https://huggingface.co/models
hub = {
        'HF_MODEL_ID':'distilbert-base-uncased-distilled-squad', # model_id from hf.co/models
        'HF_TASK':'question-answering' # NLP task you want to use for predictions
        }

# create Hugging Face Model Class
huggingface_model = sagemaker.HuggingFaceModel(
            env=hub,
            role=role, # iam role with permissions to create an Endpoint
            transformers_version="4.6", # transformers version used
            pytorch_version="1.7", # pytorch version used
            py_version="py36", # python version of the DLC

..........

我得到的错误是

WARNING: The directory '/home/sbx_user1051/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.

(...然后有很多行日志,例如 Collecting pyparsing>=2.0.2 Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)...

Downloading pox-0.3.0-py2.py3-none-any.whl (30 kB)
Collecting multiprocess>=0.70.12
Downloading multiprocess-0.70.12.2-py38-none-any.whl (128 kB)
Using legacy 'setup.py install' for sagemaker, since package 'wheel' is not installed.
Using legacy 'setup.py install' for protobuf3-to-dict, since package 'wheel' is not installed.
Installing collected packages: dill, zipp, pytz, pyparsing, protobuf, ppft, pox, numpy, multiprocess, smdebug-rulesconfig, protobuf3-to-dict, pathos, pandas, packaging, importlib-metadata, google-pasta, attrs, sagemaker
ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/home/sbx_user1051'
4

1 回答 1

0

找到答案:

  1. API 使用 Sagemaker Studio 部署,如此处所述
  1. 来自 Lambda 的推理代码是这样的:
    import os, io, boto3, json

    ENDPOINT_NAME = "huggingface-pytorch-inference-xxxxxxxxxxxxxxxxx"
    runtime= boto3.client('runtime.sagemaker')

    inputs = {'inputs': {
                        'question': 'What is used for inference?',
                        'context': 'My Name is Philipp and I live in Nuremberg. This model is used with sagemaker for inference.'}
    }
    payload = json.dumps(inputs, indent=2).encode('utf-8')

    print(f"payload: {type(payload)}, {payload}")
        
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                        ContentType='text/csv',
                                        Body=payload)
于 2021-09-14T11:17:50.617 回答