Installing DyNet for Python

Installing a Release CPU Version

Python bindings to DyNet are supported for both Python 2.x and 3.x.If you want to install a release version of DyNet and don’t need to run on GPU, you cansimply run

pipinstalldynet

Installing a Cutting-edge and/or GPU Version

If you want the most recent features of DyNet from the development branch, or want GPUcompute capability, you’ll want to install DyNet from source.

Before doing so, you will need to make sure that several packages are installed.For example onUbuntu Linux:

sudoapt-getupdatesudoapt-getinstallpython-pipbuild-essentialcmakemercurial

Or onmacOS, first make sure the Apple Command Line Tools are installed, thenget CMake, and Mercurial with either homebrew or macports:

xcode-select--installbrewinstallcmakehgpython# Using homebrew.sudoportinstallcmakemercurialpy-pip# Using macports.

OnWindows, seeWindows Support.

(Currently, since the pip installation will build from source, you need to install
cython ahead:pipinstallcython.)

Once these packages are installed, the following will download, build and installDyNet. Note that compiling DyNet may take a long time, up to 10 minutes or more, but aslong as you see “Running setup.py install for dynet” with the moving progresswheel, things should be running.

pip install git+https://github.com/clab/dynet#egg=dynet

If you have CUDA installed on your system and want to install with GPU support, youcan instead run the following command.

BACKEND=cuda pip install git+https://github.com/clab/dynet#egg=dynet

Alternatively, you can add the following to yourrequirements.txt (for CUDA supportyou will need to make sure thatBACKEND=cuda is in your environmental variables whenDyNet is installed):

git+https://github.com/clab/dynet#egg=dynet

You can also manually set the directory of the cuDNN library as follows:

CUDNN_ROOT=/path/to/cudnnBACKEND=cuda pip install git+https://github.com/clab/dynet#egg=dynet

If installation usingpip fails, if you copy-and-paste the entire log that youget after running thepip command into agithub issue,we will help you debug. You can also try to install DyNet manually as listed below.

Manual Installation

The following is a list of all the commands needed to perform a manual install:

# Installing Python DyNet:pip install cython# if you don't have it already.mkdir dynet-basecd dynet-base# getting dynet and eigengit clone https://github.com/clab/dynet.githg clone https://bitbucket.org/eigen/eigen -r b2e267d# -r NUM specified a known working revisioncd dynetmkdir buildcd build# without GPU support (if you get an error that Eigen cannot be found, try using the full path to Eigen)cmake .. -DEIGEN3_INCLUDE_DIR=../../eigen -DPYTHON=`which python`# or with GPU support (if you get an error that Eigen cannot be found, try using the full path to Eigen)cmake .. -DEIGEN3_INCLUDE_DIR=../../eigen -DPYTHON=`which python` -DBACKEND=cudamake -j2# replace 2 with the number of available corescd pythonpython ../../setup.py build --build-dir=.. --skip-build install# add `--user` for a user-local install.# this should suffice, but on some systems you may need to add the following line to your# init files in order for the compiled .so files be accessible to Python.# /path/to/dynet/build/dynet is the location in which libdynet.dylib resides.exportDYLD_LIBRARY_PATH=/path/to/dynet/build/dynet/:$DYLD_LIBRARY_PATH# if the environment is Linux, use LD_LIBRARY_PATH instead.exportLD_LIBRARY_PATH=/path/to/dynet/build/dynet/:$LD_LIBRARY_PATH

To explain these one-by-one, first we get DyNet:

cd$HOMEmkdir dynet-basecd dynet-basegit clone https://github.com/clab/dynet.gitcd dynetgit submodule init# To be consistent with DyNet's installation instructions.git submodule update# To be consistent with DyNet's installation instructions.

Then get Eigen:

cd$HOMEcd dynet-basehg clone https://bitbucket.org/eigen/eigen/ -r b2e267d

(-r NUM specifies a known working revision of Eigen. You can remove this in order to get the bleedingedge Eigen, with the risk of some compile breaks, and the possible benefit of added optimizations.)

We also need to make sure thecython module is installed. (you canreplacepip with your favorite package manager, such asconda,or install within a virtual environment)

pip install cython

To simplify the following steps, we can set a bash variable to holdwhere we have saved the main directories of DyNet and Eigen. In case youhave gotten DyNet and Eigen differently from the instructions above andsaved them in different location(s), these variables will be helpful:

PATH_TO_DYNET=$HOME/dynet-base/dynet/PATH_TO_EIGEN=$HOME/dynet-base/eigen/

Compile DyNet.

This is pretty much the same process as compiling DyNet, with theaddition of the-DPYTHON= flag, pointing to the location of yourPython interpreter.

Assuming that thecmake command found all the needed libraries anddidn’t fail, themake command will take a while, and compile DyNetas well as the Python bindings. You can changemake-j2 to a highernumber, depending on the available cores you want to use whilecompiling.

You now have a working Python binding inside ofbuild/dynet. Toverify this is working:

cd$PATH_TO_DYNET/build/pythonpython

then, within Python:

import dynet as dyprint dy.__version__pc= dy.ParameterCollection()

In order to install the module so that it is accessible from everywherein the system, run the following:

cd$PATH_TO_DYNET/build/pythonpython ../../setup.pyEIGEN3_INCLUDE_DIR=$PATH_TO_EIGEN build --build-dir=.. --skip-build install --user

The--user switch will install the module in your localsite-packages, and works without root privileges. To install the moduleto the system site-packages (for all users), or to the currentvirtualenv(if you are on one), runpython../../setup.pyEIGEN3_INCLUDE_DIR=$PATH_TO_EIGENbuild--build-dir=..--skip-buildinstall without this switch.

You should now have a working python binding (thedynet module).

Note however that the installation relies on the compiled DyNet librarybeing in$PATH_TO_DYNET/build/dynet, so make sure not to move itfrom there.

Now, check that everything works:

cd$PATH_TO_DYNETcd examples/pythonpython xor.pypython rnnlm.py rnnlm.py

Alternatively, if the following script works for you, then yourinstallation is likely to be working:

importdynetasdypc=dy.ParameterCollection()

If it doesn’t work and you get an error similar to the following:

ImportError: dlopen(/Users/sneharajana/.python-eggs/dyNET-0.0.0-py2.7-macosx-10.11-intel.egg-tmp/_dynet.so, 2): Library not loaded: @rpath/libdynet.dylibReferenced from: /Users/sneharajana/.python-eggs/dyNET-0.0.0-py2.7-macosx-10.11-intel.egg-tmp/_dynet.soReason: image not found``

then you may need to run the following (and add it to your shell init files):

# OSXexportDYLD_LIBRARY_PATH=/path/to/dynet/build/dynet/:$DYLD_LIBRARY_PATH# LinuxexportLD_LIBRARY_PATH=/path/to/dynet/build/dynet/:$LD_LIBRARY_PATH

# /path/to/dynet/build/dynet is the location in which libdynet.so(libdynet.dylib under osx) resides.

Anaconda Support

Anaconda is a popular package management system for Python, and DyNet can be installed into this environment.First, make sure that you install all the necessary packages according to the instructions at the top of this page.Then create an Anaconda environment and activate it as below:

sourceactivatemy_environment_name

After this, you should be able to install using pip or manual installation as normal.

Windows Support

You can also use Python on Windows, including GPU and MKL support. For simplicity, we recommendusing a Python distribution that already has Cython installed. The following has been tested to work:

  1. Install WinPython 2.7.10 (comes with Cython already installed).
  2. Compile DyNet according to the directions in the Windows C++ documentation (Windows Support), and additionally add the following flag when executingcmake:-DPYTHON=/path/to/your/python.exe.
  3. Open a command prompt and setVS90COMNTOOLS to the path to your Visual Studio “Common7/Tools” directory. One easy way to do this is a command such as:
setVS90COMNTOOLS=%VS140COMNTOOLS%
  1. Open dynet.sln from this command prompt and build the “Release” version of the solution.
  2. Follow the rest of the instructions above for testing the build and installing it for other users

Note, currently only the Release version works. Also, if you compile with CUDA and/or cuDNN, ensuretheir respective DLLs are in your PATH environment variable when you run Python.

GPU/MKL Support

Installing on GPU

For installing on a computer with GPU, first install CUDA. The followinginstructions assume CUDA is installed.

The installation process is pretty much the same, while adding the-DBACKEND=cuda flag to thecmake stage:

cmake .. -DEIGEN3_INCLUDE_DIR=$PATH_TO_EIGEN -DPYTHON=$PATH_TO_PYTHON -DBACKEND=cuda

If you know the CUDA architecture supported by your GPU (e.g. by referencingthis page)you can speed compilation significantly by adding-DCUDA_ARCH=XXX whereXXX is your architecture number.If CUDA is installed in a non-standard location andcmake cannotfind it, you can specify also-DCUDA_TOOLKIT_ROOT_DIR=/path/to/cuda.

Now, build the Python modules (as above, we assume Cython is installed):

After runningmake-j2, you should have the file_dynet.so in thebuild/python folder.

As before,cdbuild/python followed bypython../../setup.pyEIGEN3_INCLUDE_DIR=$PATH_TO_EIGENbuild--build-dir=..--skip-buildinstall--user will install the module.

cuDNN support

When running DyNet with CUDA on GPUs, some of DyNet’s functionality(e.g. conv2d) depends on theNVIDIA cuDNN libraries.CMake will automatically detect cuDNN in the CUDA installation path(i.e./usr/local/cuda) and enable it if detected.

If CMake is unable to find cuDNN automatically, try settingCUDNN_ROOT, such as

-DCUDNN_ROOT="/path/to/CUDNN"

However, if you don’t have cuDNN installed, the dependent functionalitywill be automatically disabled and an error will be throwed during runtime if you tryto use them.

Using the GPU from Python

The preferred way to make DyNet use the GPU under Python is to importdynet as usual:

importdynet

Then tell it to use the GPU by using the commandline switch--dynet-gpu or the GPU switches detailedhere when invoking the program. This option lets thesame code work with either the GPU or the CPU version depending on howit is invoked.

Alternatively, you can also select whether the CPU or GPU should beused by usingdynet_config module:

importdynet_configdynet_config.set_gpu()importdynet

This may be useful if you want to decide programmatically whether touse the CPU or GPU. Importantly, it is not suggested to useimport_dynetany more.

Running with MKL

If you’ve built DyNet to use MKL (using-DMKL or-DMKL_ROOT), Python sometimes has difficulty findingthe MKL shared libraries. You can try settingLD_LIBRARY_PATH to point to your MKL library directory.If that doesn’t work, try setting the following environment variable (supposing, for example,your MKL libraries are located at/opt/intel/mkl/lib/intel64):

exportLD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_def.so:/opt/intel/mkl/lib/intel64/libmkl_avx2.so:/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so:/opt/intel/mkl/lib/intel64/libmkl_intel_thread.so:/opt/intel/lib/intel64_lin/libiomp5.so