在 SageMaker 中调用 MLflow 部署的 NLP 模型时,获取输入对象的 dtype 与预期的 dtype
青葱年少 nlp 461

原文标题Getting `dtype of input object does not match expected dtype

我使用 MLflow 的sagemaker.deploy()在 SageMaker 中部署了 Huggingface Transformer 模型。

在记录模型时,我使用infer_signature(np.array(test_example), loaded_model.predict(test_example))来推断输入和输出签名。

模型部署成功。当尝试查询我得到的模型时ModelError(下面的完整回溯)。

为了查询模型,我使用的是完全相同的test_example,我用于infer_signature()

test_example = [['This is the subject', 'This is the body']]

唯一的区别是,在查询部署的模型时,我没有将测试示例包装在np.array()中,因为它不是json-可序列化的。

为了查询模型,我尝试了两种不同的方法:

import boto3

SAGEMAKER_REGION = 'us-west-2'
MODEL_NAME = '...'

client = boto3.client("sagemaker-runtime", region_name=SAGEMAKER_REGION)

# Approach 1
client.invoke_endpoint(
                EndpointName=MODEL_NAME,
                Body=json.dumps(test_example),
                ContentType="application/json",
            )

# Approach 2
client.invoke_endpoint(
                EndpointName=MODEL_NAME,
                Body=pd.DataFrame(test_example).to_json(orient="split"),
                ContentType="application/json; format=pandas-split",
            )

但它们会导致相同的错误。

将不胜感激您的建议。

谢谢!

注意:我使用的是 Python 3,所有字符串都是 unicode。

---------------------------------------------------------------------------
ModelError                                Traceback (most recent call last)
<ipython-input-89-d09862a5f494> in <module>
      2                 EndpointName=MODEL_NAME,
      3                 Body=test_example,
----> 4                 ContentType="application/json; format=pandas-split",
      5             )

~/anaconda3/envs/amazonei_tensorflow_p36/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    393                     "%s() only accepts keyword arguments." % py_operation_name)
    394             # The "self" in this scope is referring to the BaseClient.
--> 395             return self._make_api_call(operation_name, kwargs)
    396 
    397         _api_call.__name__ = str(py_operation_name)

~/anaconda3/envs/amazonei_tensorflow_p36/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    723             error_code = parsed_response.get("Error", {}).get("Code")
    724             error_class = self.exceptions.from_code(error_code)
--> 725             raise error_class(parsed_response, operation_name)
    726         else:
    727             return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{"error_code": "BAD_REQUEST", "message": "dtype of input object does not match expected dtype <U0"}". See https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=/aws/sagemaker/Endpoints/bec-sagemaker-model-test-app in account 543052680787 for more information.

环境信息:

{'channels': ['defaults', 'conda-forge', 'pytorch'],
 'dependencies': ['python=3.6.10',
  'pip==21.3.1',
  'pytorch=1.10.2',
  'cudatoolkit=10.2',
  {'pip': ['mlflow==1.22.0',
    'transformers==4.17.0',
    'datasets==1.18.4',
    'cloudpickle==1.3.0']}],
 'name': 'bert_bec_test_env'}

原文链接:https://stackoverflow.com//questions/71408963/getting-dtype-of-input-object-does-not-match-expected-dtype-u0-when-invoking

回复

我来回复
  • Aleksander Molak的头像
    Aleksander Molak 评论

    在将字符串发送到模型之前,我将字符串编码为数字。接下来,我在模型包装器中添加了一个代码,将数字解码回字符串。这种解决方法没有问题。

    据我了解,这可能表明 MLflow 的字符串类型检查存在问题。

    在此处添加了一个问题:https://github.com/mlflow/mlflow/issues/5474

    2年前 0条评论