create_dev_conda_env.sh 2.56 KB
Newer Older
1
2
#!/bin/bash

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
3
4
readonly CUDA_VERSIONS="10.2,11.3,11.6,11.7"
readonly TORCH_VERSION="1.12.0"
5
6
7
8
9
10
11
12

usage() {
cat << EOF
usage: bash $0 OPTIONS
examples:
  bash $0 -c
  bash $0 -g 11.7

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
13
Create a developement environment for DGL developers.
14
15
16
17
18

OPTIONS:
  -h           Show this message.
  -c           Create dev environment in CPU mode.
  -g           Create dev environment in GPU mode with specified CUDA version,
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
19
               supported: ${CUDA_VERSIONS}.
20
  -s           Run silently which indicates always 'yes' for any confirmation.
21
22
23
24
25
EOF
}

validate() {
  values=$(echo "$1" | tr "," "\n")
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
26
  for value in ${values}
27
  do
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
28
    if [[ "${value}" == $2 ]]; then
29
30
31
32
33
34
35
36
37
      return 0
    fi
  done
  return 1
}

confirm() {
  echo "Continue? [yes/no]:"
  read confirm
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
38
  if [[ ! ${confirm} == "yes" ]]; then
39
40
41
42
    exit 0
  fi
}

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
43
# Parse flags.
44
while getopts "cg:hs" flag; do
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
45
  if [[ ${flag} == "c" ]]; then
46
    cpu=1
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
47
48
49
  elif [[ ${flag} == "g" ]]; then
    gpu=${OPTARG}
  elif [[ ${flag} == "h" ]]; then
50
51
    usage
    exit 0
52
53
  elif [[ ${flag} == "s" ]]; then
    always_yes=1
54
55
56
57
58
59
  else
    usage
    exit 1
  fi
done

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
60
if [[ -n ${gpu} && ${cpu} -eq 1 ]]; then
61
62
63
64
  echo "Only one mode can be specified."
  exit 1
fi

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
65
if [[ -z ${gpu} && -z ${cpu} ]]; then
66
67
68
69
70
  usage
  exit 1
fi

# Set up CPU mode.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
71
if [[ ${cpu} -eq 1 ]]; then
72
73
74
75
76
  torchversion=${TORCH_VERSION}"+cpu"
  name="dgl-dev-cpu"
fi

# Set up GPU mode.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
77
if [[ -n ${gpu} ]]; then
78
79
80
81
82
83
84
  if ! validate ${CUDA_VERSIONS} ${gpu}; then
    echo "Error: Invalid CUDA version."
    usage
    exit 1
  fi

  echo "Confirm the installed CUDA version matches the specified one."
85
  [[ -n "${always_yes}" ]] || confirm
86
87
88
89
90
91

  torchversion=${TORCH_VERSION}"+cu"${gpu//[-._]/}
  name="dgl-dev-gpu"
fi

echo "Confirm you are excuting the script from your DGL root directory."
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
92
echo "Current working directory: ${PWD}"
93
[[ -n "${always_yes}" ]] || confirm
94
95

# Prepare the conda environment yaml file.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
96
97
98
99
100
101
rand=$(echo "${RANDOM}" | md5sum | head -c 20)
mkdir -p /tmp/${rand}
cp script/dgl_dev.yml.template /tmp/${rand}/dgl_dev.yml
sed -i "s|__NAME__|${name}|g" /tmp/${rand}/dgl_dev.yml
sed -i "s|__TORCH_VERSION__|${torchversion}|g" /tmp/${rand}/dgl_dev.yml
sed -i "s|__DGL_HOME__|${PWD}|g" /tmp/${rand}/dgl_dev.yml
102
103
104

# Ask for final confirmation.
echo "--------------------------------------------------"
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
105
cat /tmp/${rand}/dgl_dev.yml
106
107
echo "--------------------------------------------------"
echo "Create a conda enviroment with the config?"
108
[[ -n "${always_yes}" ]] || confirm
109
110

# Create conda environment.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
111
conda env create -f /tmp/${rand}/dgl_dev.yml
112
113

# Clean up created tmp conda environment yaml file.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
114
rm -rf /tmp/${rand}
115
exit 0