This document describes how to setup all the dependencies to run the notebooks in this repository in following platforms:
- Local (Linux, MacOS or Windows) or DSVM (Linux or Windows)
- Azure Databricks
- Compute environments
- Setup guide for Local or DSVM
- Setup guide for Azure Databricks
- Prepare Azure Databricks for Operationalization
Depending on the type of recommender system and the notebook that needs to be run, there are different computational requirements. Currently, this repository supports Python CPU, Python GPU and PySpark.
- A machine running Linux, MacOS or Windows
- Anaconda with Python version >= 3.6
- This is pre-installed on Azure DSVM such that one can run the following steps directly. To setup on your local machine, Miniconda is a quick way to get started.
- Apache Spark (this is only needed for the PySpark environment).
As a pre-requisite to install the dependencies with Conda, make sure that Anaconda and the package manager Conda are both up to date:
conda update conda -n root
conda update anaconda # use 'conda install anaconda' if the package is not installed
We provide a script, generate_conda_file.py, to generate a conda-environment yaml file which you can use to create the target environment using the Python version 3.6 with all the correct dependencies.
Assuming the repo is cloned as Recommenders
in the local system, to install a default (Python CPU) environment:
cd Recommenders
python scripts/generate_conda_file.py
conda env create -f reco_base.yaml
You can specify the environment name as well with the flag -n
.
Click on the following menus to see how to install Python GPU and PySpark environments:
Python GPU environment
Assuming that you have a GPU machine, to install the Python GPU environment:
cd Recommenders
python scripts/generate_conda_file.py --gpu
conda env create -f reco_gpu.yaml
PySpark environment
To install the PySpark environment:
cd Recommenders
python scripts/generate_conda_file.py --pyspark
conda env create -f reco_pyspark.yaml
Additionally, if you want to test a particular version of spark, you may pass the --pyspark-version argument:
python scripts/generate_conda_file.py --pyspark-version 2.4.0
Full (PySpark & Python GPU) environment
With this environment, you can run both PySpark and Python GPU notebooks in this repository. To install the environment:
cd Recommenders
python scripts/generate_conda_file.py --gpu --pyspark
conda env create -f reco_full.yaml
NOTE - for PySpark environments (
reco_pyspark
andreco_full
), we need to set the environment variablesPYSPARK_PYTHON
andPYSPARK_DRIVER_PYTHON
to point to the conda python executable.Click on the following menus to see details:
Linux or MacOS
To set these variables every time the environment is activated, we can follow the steps of this guide. Assuming that we have installed the environment in
/anaconda/envs/reco_pyspark
, create the file/anaconda/envs/reco_pyspark/etc/conda/activate.d/env_vars.sh
and add:```bash #!/bin/sh export PYSPARK_PYTHON=/anaconda/envs/reco_pyspark/bin/python export PYSPARK_DRIVER_PYTHON=/anaconda/envs/reco_pyspark/bin/python export SPARK_HOME_BACKUP=$SPARK_HOME unset SPARK_HOME ```
This will export the variables every time we do
conda activate reco_pyspark
. To unset these variables when we deactivate the environment, create the file/anaconda/envs/reco_pyspark/etc/conda/deactivate.d/env_vars.sh
and add:```bash #!/bin/sh unset PYSPARK_PYTHON unset PYSPARK_DRIVER_PYTHON export SPARK_HOME=$SPARK_HOME_BACKUP unset SPARK_HOME_BACKUP ```
Windows
To set these variables every time the environment is activated, we can follow the steps of this guide. Assuming that we have installed the environment in
c:\anaconda\envs\reco_pyspark
, create the filec:\anaconda\envs\reco_pyspark\etc\conda\activate.d\env_vars.bat
and add:@echo off set PYSPARK_PYTHON=c:\anaconda\envs\reco_pyspark\python.exe set PYSPARK_DRIVER_PYTHON=c:\anaconda\envs\reco_pyspark\python.exe set SPARK_HOME_BACKUP=%SPARK_HOME% set SPARK_HOME= set PYTHONPATH_BACKUP=%PYTHONPATH% set PYTHONPATH=
This will export the variables every time we do
conda activate reco_pyspark
. To unset these variables when we deactivate the environment, create the filec:\anaconda\envs\reco_pyspark\etc\conda\deactivate.d\env_vars.bat
and add:@echo off set PYSPARK_PYTHON= set PYSPARK_DRIVER_PYTHON= set SPARK_HOME=%SPARK_HOME_BACKUP% set SPARK_HOME_BACKUP= set PYTHONPATH=%PYTHONPATH_BACKUP% set PYTHONPATH_BACKUP=
We can register our created conda environment to appear as a kernel in the Jupyter notebooks.
conda activate my_env_name
python -m ipykernel install --user --name my_env_name --display-name "Python (my_env_name)"
If you are using the DSVM, you can connect to JupyterHub by browsing to https://your-vm-ip:8000
.
- We found that there can be problems if the Spark version of the machine is not the same as the one in the conda file. You can use the option
--pyspark-version
to address this issue. - When running Spark on a single local node it is possible to run out of disk space as temporary files are written to the user's home directory. To avoid this on a DSVM, we attached an additional disk to the DSVM and made modifications to the Spark configuration. This is done by including the following lines in the file at
/dsvm/tools/spark/current/conf/spark-env.sh
.
SPARK_LOCAL_DIRS="/mnt"
SPARK_WORKER_DIR="/mnt"
SPARK_WORKER_OPTS="-Dspark.worker.cleanup.enabled=true, -Dspark.worker.cleanup.appDataTtl=3600, -Dspark.worker.cleanup.interval=300, -Dspark.storage.cleanupFilesAfterExecutorExit=true"
- Databricks Runtime version 4.3 (Apache Spark 2.3.1, Scala 2.11) or greater
- Python 3
An example of how to create an Azure Databricks workspace and an Apache Spark cluster within the workspace can be found from here. To utilize deep learning models and GPUs, you may setup GPU-enabled cluster. For more details about this topic, please see Azure Databricks deep learning guide.
You can setup the repository as a library on Databricks either manually or by running an installation script. Both options assume you have access to a provisioned Databricks workspace and cluster and that you have appropriate permissions to install libraries.
Quick install
This option utilizes an installation script to do the setup, and it requires additional dependencies in the environment used to execute the script.
To run the script, following prerequisites are required:
Setup CLI authentication for Azure Databricks CLI (command-line interface). Please find details about how to create a token and set authentication here. Very briefly, you can install and configure your environment with the following commands.
conda activate reco-pyspark databricks configure --token
Get the target cluster id and start the cluster if its status is TERMINATED.
- You can get the cluster id from the databricks CLI with:
databricks clusters list
- If required, you can start the cluster with:
databricks clusters start --cluster-id <CLUSTER_ID>`
The installation script has a number of options that can also deal with different databricks-cli profiles, install a version of the mmlspark library, overwrite the libraries, or prepare the cluster for operationalization. For all options, please see:
python scripts/databricks_install.py -h
Once you have confirmed the databricks cluster is RUNNING, install the modules within this repository with the following commands.
cd Recommenders
python scripts/databricks_install.py <CLUSTER_ID>
Note If you are planning on running through the sample code for operationalization here, you need to prepare the cluster for operationalization. You can do so by adding an additional option to the script run. <CLUSTER_ID> is the same as that mentioned above, and can be identified by running databricks clusters list
and selecting the appropriate cluster.
python ./scripts/databricks_install.py --prepare-o16n <CLUSTER_ID>
See below for details.
Manual setup
To install the repo manually onto Databricks, follow the steps:
-
Clone the Microsoft Recommenders repository to your local computer.
-
Zip the contents inside the Recommenders folder (Azure Databricks requires compressed folders to have the
.egg
suffix, so we don't use the standard.zip
):cd Recommenders zip -r Recommenders.egg .
-
Once your cluster has started, go to the Databricks workspace, and select the
Home
button. -
Your
Home
directory should appear in a panel. Right click within your directory, and selectImport
. -
In the pop-up window, there is an option to import a library, where it says:
(To import a library, such as a jar or egg, click here)
. Selectclick here
. -
In the next screen, select the option
Upload Python Egg or PyPI
in the first menu. -
Next, click on the box that contains the text
Drop library egg here to upload
and use the file selector to choose theRecommenders.egg
file you just created, and selectOpen
. -
Click on the
Create library
. This will upload the egg and make it available in your workspace. -
Finally, in the next menu, attach the library to your cluster.
After installation, you can now create a new notebook and import the utilities from Databricks in order to confirm that the import worked.
import reco_utils
- For the reco_utils import to work on Databricks, it is important to zip the content correctly. The zip has to be performed inside the Recommenders folder, if you zip directly above the Recommenders folder, it won't work.
This repository includes an end-to-end example notebook that uses Azure Databricks to estimate a recommendation model using matrix factorization with Alternating Least Squares, writes pre-computed recommendations to Azure Cosmos DB, and then creates a real-time scoring service that retrieves the recommendations from Cosmos DB. In order to execute that notebook, you must install the Recommenders repository as a library (as described above), AND you must also install some additional dependencies. With the Quick install method, you just need to pass an additional option to the installation script.
Quick install
This option utilizes the installation script to do the setup. Just run the installation script
with an additional option. If you have already run the script once to upload and install the Recommenders.egg
library, you can also add an --overwrite
option:
python scripts/databricks_install.py --overwrite --prepare-o16n <CLUSTER_ID>
This script does all of the steps described in the Manual setup section below.
Manual setup
You must install three packages as libraries from PyPI:
azure-cli==2.0.56
azureml-sdk[databricks]==1.0.8
pydocumentdb==2.3.3
You can follow instructions here for details on how to install packages from PyPI.
Additionally, you must install the spark-cosmosdb connector on the cluster. The easiest way to manually do that is to:
- Download the appropriate jar from MAVEN. NOTE This is the appropriate jar for spark versions
2.3.X
, and is the appropriate version for the recommended Azure Databricks run-time detailed above. - Upload and install the jar by:
- Log into your
Azure Databricks
workspace - Select the
Clusters
button on the left. - Select the cluster on which you want to import the library.
- Select the
Upload
andJar
options, and click in the box that has the textDrop JAR here
in it. - Navigate to the downloaded
.jar
file, select it, and clickOpen
. - Click on
Install
. - Restart the cluster.
- Log into your