0

I'm running a python script with PyTorch/Graphviz. It executes in a SageMaker notebook instance, but not in SageMaker Studio.

It appears the notebook instance with kernel conda_pytorch_p39 already contains an installation of Graphviz so the script just works as is and I get my Graphviz png.

When using SageMaker Studio with kernel PyTorch 1.8 Python 3.6 GPU, it seems to be a lot more complicated. Only installing torchviz didn't work so I tried installing graphviz via conda install -c fastchan python-graphviz.

I know that on Windows, Linux, macOS we need the path of graphviz executable but I didn't need to provide that in my SageMaker notebook instance.

SageMaker Studio Notebook (SageMaker kernel: PyTorch 1.8 Python 3.6 GPU)

The notebook I am debugging.

#Torchviz install
%pip install torchviz
[out]:
Installing collected packages: torchviz
Successfully installed torchviz-0.0.2
location: /opt/conda/lib/python3.6/site-packages/torchviz

# Tried installing python-graphviz
%conda install -c fastchan python-graphviz
[out]:
environment location: /opt/conda
  added / updated specs:
    - python-graphviz
Downloading and Extracting Packages
graphviz-2.42.3 
python-graphviz-0.16

from whych import whych
whych("graphviz")
[out]:
Python executable: /opt/conda/bin/python
Module "graphviz" found at location: /opt/conda/lib/python3.6/site-packages/graphviz

#However:
%cd /opt/conda/lib/python3.6/site-packages/graphviz
[out]:
No such file or directory
#It cannot find python3.6 even though the kernel is PyTorch 1.8 Python 3.6 GPU

# For testing only
import graphviz as gv
[out]:
AttributeError: module 'graphviz.backend' has no attribute 'ENCODING'

from torchviz import make_dot
make_dot(loss_tensor)

#Attempted fix:
import os
from torchviz import make_dot
os.environ['PATH'] += os.pathsep + '/opt/conda/lib/python3.6/site-packages/graphviz'
make_dot(loss_tensor)

[out]:
AttributeError: module 'graphviz.backend' has no attribute 'ENCODING'

#PATH
'/opt/amazon/openmpi/bin:/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tmp/miniconda3/condabin:/tmp/anaconda3/condabin:/tmp/miniconda2/condabin:/tmp/anaconda2/condabin'
Edison
  • 11,881
  • 5
  • 42
  • 50
  • common problem - you need the "real Graphviz package, or need to get it in your path - see https://stackoverflow.com/questions/73653801/problem-with-graphviz-executablenotfound-error/73654783#73654783 – sroush Jan 15 '23 at 02:08
  • Thanks. I tried installing that already using `sudo yum install graphviz `. It installed but I couldn't find it anywhere in the system. These notebooks are on Linux so why doesn't `apt` work? And why did I not ned to install Graphviz in the SageMaker notebook instance but I do in Studio? – Edison Jan 15 '23 at 02:15
  • `%cat /etc/os-release` NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/". Therefore, `sudo yum install graphviz `. – Edison Jan 15 '23 at 02:23
  • running the `whych` command I get `Module "graphviz" found at location: /opt/conda/lib/python3.6/site-packages/graphviz` and `Module "torchviz" found at location: /opt/conda/lib/python3.6/site-packages/torchviz`. But oddly I cannot cd into the `/opt/conda/lib/python3.6` directory. I can only cd into `/opt/conda/lib`. – Edison Jan 15 '23 at 03:10
  • note that you have an old version of Graphviz - built in 2018. I suggest updating it if possible. – sroush Jan 15 '23 at 15:30
  • Thanks. Yes I changed the SageMaker kernel to `PyTorch 1.12 Python 3.8 GPU` so that I can use graphviz 0.20.1. – Edison Jan 16 '23 at 03:21

1 Answers1

0

The SageMaker Studio image/kernel PyTorch 1.8 Python 3.6 GPU is not compatible with graphviz 0.20. I had to downgrade to 0.19.1 using pip install --force-reinstall graphviz==0.18. In the end I changed the SageMaker Studio kernel to Python 3.8 to upgrade to graphviz 0.20.1.

You can install graphviz 0.18.0 -> 0.19.1 with py3.6 and graphviz 0.20.0 -> 0.20.1 with py3.9.

In both SageMaker notebooks you do not need to add the path for graphviz like you do on Windows and macOS.

Edison
  • 11,881
  • 5
  • 42
  • 50