Azure Machine Learning Studio: cannot deploy model with rpy2 as a dependency

Viewed 1275

I am trying to deploy a custom model on Azure Machine Learning Studio that needs rpy2 (Python wrapper for R) to run. So, I created the following yml file (myenv.yml), specifying the required dependency (besides other stuff),

# Conda environment specification. The dependencies defined in this file will
# be automatically provisioned for runs with userManagedDependencies=False.

# Details about the Conda environment file format:
# https://conda.io/docs/user-guide/tasks/manage-environments.html#create-env-file-manually

name: project_environment

dependencies:
  # The python interpreter version.
  # Currently Azure ML only supports 3.5.2 and later.
- python=3.6.2    
- pip:
  - azureml-train-automl-runtime==1.8.0.post1
  - inference-schema
  - azureml-explain-model==1.8.0
  - azureml-defaults==1.8.0
  - rpy2==3.3.5
- dill==0.3.2
- numpy>=1.16.0,<=1.16.2
- pandas>=0.21.0,<=0.23.4
- scikit-learn>=0.19.0,<=0.20.3
- py-xgboost<=0.90
- fbprophet==0.5
- psutil>=5.2.2,<6.0.0

channels:
- anaconda
- conda-forge

and then ran the following script (based on https://docs.microsoft.com/en-us/azure/machine-learning/how-to-troubleshoot-deployment):

from azureml.core.environment import Environment
from azureml.core.model import Model, InferenceConfig
from azureml.core import Workspace
from azureml.core.webservice import AciWebservice

ws = Workspace.from_config()
model = Model(workspace = ws, name = 'test-rpy2') # this is my (registered) model that needs rpy2 to run

# create inference configuration based on the requirements defined in the YAML
myenv = Environment.from_conda_specification(name = "myenv", file_path = "myenv.yml")

inference_config = InferenceConfig(entry_script = "score.py", environment = myenv) # score.py: my custom scoring file where rpy2 is imported

# deploy the model
aci_config = AciWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1)

service = Model.deploy(workspace         = ws,
                       name              = 'test-rpy2',
                       models            = [model],
                       inference_config  = inference_config,
                       deployment_config = aci_config)

service.wait_for_deployment(show_output=True)

However, I get the following error:

    Error: rpy2 in API mode cannot be built without R in the PATH or R_HOME defined. Correct this or force ABI mode-only by defining the environment variable RPY2_CFFI_MODE=ABI

I expected that the issue would be fixed by adding the line

myenv.environment_variables['RPY2_CFFI_MODE'] = 'ABI'

right after the definition of myenv, but the exact same error shows up again.

Does anyone have any idea on how to make it work?

1 Answers

To me, the message is saying that you can't use rpy2 because R is not installed in the Docker image. I think the fix is that You'll have to add R as a conda dependency. Here's some guidance. I'm not an expert on R+conda but try adding r-base and r-essentials as conda deps.

another note -- I strongly recommend minimizing conda dependencies when using the azureml-sdk because azureml-sdk is only available on PyPI. This doc explains the challenges well. Below is my guess on what it would look like, but i'm not confident on the mapping b/w anaconda and pip packagenames/versions.

dependencies:
- python=3.6.2
- r-base 
- r-essentials
- py-xgboost<=0.90   
- pip:
  - azureml-train-automl-runtime==1.8.0.post1
  - inference-schema
  - azureml-explain-model==1.8.0
  - azureml-defaults==1.8.0
  - rpy2==3.3.5
  - dill==0.3.2
  - numpy>=1.16.0,<=1.16.2
  - pandas>=0.21.0,<=0.23.4
  - scikit-learn>=0.19.0,<=0.20.3
  - fbprophet==0.5
  - psutil>=5.2.2,<6.0.0

channels:
- anaconda
- conda-forge
Related