如何在Azure ML中使用MLflow加载已记录/保存的模型?

huangapple go评论93阅读模式
英文:

How to use MLlfow to load the logged/saved model in Azure ML?

问题

我想部署经过训练的 ML 模型通过 AZURE ML 在线端点。

我已经在工作空间上注册了我的模型。

现在当我尝试使用 cutome score.py 来加载模型时,我得到以下错误 -

错误信息显示在 /azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/azureml_inference_server_http/server/user_script.py 中,需要在其中更新 map_location=torch.device('cpu')。但是 mlflow.pyfunc.load_model() 没有参数可以访问 map_location,因此需要在代码中找到合适的位置进行更新。

英文:

I want to deploy the trained ML model via AZURE ML online endppoints.

I have already registered my model on the workspace.

Now I am getting following error when I am trying to load the model using cutome score.py for mlflow.pyfunc.load_model()

This is my code -

  1. model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "use-case1-model")
  2. model = mlflow.pyfunc.load_model(model_path)

score.py

  1. import logging
  2. import os
  3. import json
  4. import mlflow
  5. from io import StringIO
  6. from mlflow.pyfunc.scoring_server import infer_and_parse_json_input, predictions_to_json
  7. import sys
  8. from time import strftime, localtime
  9. from collections import Counter
  10. from pytorch_transformers import BertTokenizer
  11. import random
  12. import numpy as np
  13. import torch
  14. from tqdm import tqdm
  15. def init():
  16. global model
  17. # "model" is the path of the mlflow artifacts when the model was registered. For automl
  18. # models, this is generally "mlflow-model".
  19. model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "use-case1-model")
  20. model = mlflow.pyfunc.load_model(model_path)
  21. logging.info("Init complete")
  22. def run(raw_data):
  23. data = json.loads(raw_data)
  24. title = json.dumps(data["title"])
  25. att = json.dumps(data["attributes"])
  26. output = model.predict([tensor_t,tensor_a])
  27. predict_list = output.tolist()[0]
  28. result = StringIO()
  29. predictions_to_json(predict_list,result)
  30. return result.getvalue()

Error that I am getting -

  1. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/azureml_inference_server_http/server/user_script.py", line 117, in invoke_init
  2. self._user_init()
  3. File "/var/azureml-app/dependencies/score.py", line 21, in init
  4. model = mlflow.pyfunc.load_model(model_path)
  5. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pyfunc/__init__.py", line 735, in load_model
  6. model_impl = importlib.import_module(conf[MAIN])._load_pyfunc(data_path)
  7. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pytorch/__init__.py", line 735, in _load_pyfunc
  8. return _PyTorchWrapper(_load_model(path, **kwargs))
  9. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/mlflow/pytorch/__init__.py", line 643, in _load_model
  10. return torch.load(model_path, **kwargs)
  11. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 809, in load
  12. return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  13. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1172, in _load
  14. result = unpickler.load()
  15. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1142, in persistent_load
  16. typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
  17. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 1116, in load_tensor
  18. wrap_storage=restore_location(storage, location),
  19. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 217, in default_restore_location
  20. result = fn(storage, location)
  21. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 182, in _cuda_deserialize
  22. device = validate_cuda_device(location)
  23. File "/azureml-envs/azureml_9a3b1e0a66d72d612aebc12b4a285f72/lib/python3.9/site-packages/torch/serialization.py", line 166, in validate_cuda_device
  24. raise RuntimeError('Attempting to deserialize object on a CUDA '
  25. RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

How and where can I update map_location=torch.device('cpu') ? mlflow.pyfunc.load_model() doesnt have a parameter to access map_location and as the packages is installed in docker i cannot make changes to serilaization.py

答案1

得分: 0

根据错误日志,您正在尝试在CUDA设备上反序列化一个对象,但torch.cuda.is_available()返回False,这是因为您在仅CPU的机器上运行。要解决此问题,您需要更新torch.load函数以指定map_location=torch.device('cpu')来将存储映射到CPU。

由于mlflow.pyfunc.load_model()函数没有map_location参数,您可以使用一个**kwargs参数,该参数可以传递任何额外的关键字参数给torch.load()函数。

要解决这个问题,在您的score.py文件中添加*{'map_location': torch.device('cpu')}

  1. model = mlflow.pyfunc.load_model(model_path, *{'map_location': torch.device('cpu')})

或者使用下面的代码(更新后的解决方案):

  1. model = mlflow.pytorch.load_model(model_path, map_location=torch.device('cpu'))

示例:

  1. import mlflow
  2. import torch
  3. path = "./deploy/credit_defaults_model/"
  4. model = mlflow.pyfunc.load_model(path, *{'map_location': torch.device('cpu')})
英文:

As per the error logs, you are attempting to deserialize an object on a CUDA device, but torch.cuda.is_available() is returning False, which is due to running on a CPU only machine. To resolve this issue, you need to update the torch.load function to specify map_location=torch.device('cpu') to map the storages to the CPU.

Since the mlflow.pyfunc.load_model() function does not have a map_location argument, you can use a **kwargs argument that can pass any additional keyword arguments to the torch.load() function.

To solve the issue, add *{'map_location': torch.device('cpu')}in your score.py file.

  1. model = mlflow.pyfunc.load_model(model_path, *{'map_location': torch.device('cpu')})

or Use below code:(Updated solution)

model = mlflow.pytorch.load_model(model_path, map_location=torch.device('cpu'))

Example:

  1. import mlflow
  2. import torch
  3. path="./deploy/credit_defaults_model/"
  4. model = mlflow.pyfunc.load_model(path, *{'map_location': torch.device('cpu')})

huangapple
  • 本文由 发表于 2023年5月25日 21:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定