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

3
readonly CUDA_VERSIONS="10.2,11.3,11.6,11.7,11.8"
4
readonly TORCH_VERSION="1.12.0"
5
readonly PYTHON_VERSION="3.7"
6
7
8
9
10
11
12

usage() {
cat << EOF
usage: bash $0 OPTIONS
examples:
  bash $0 -c
  bash $0 -g 11.7
13
  bash $0 -g 11.7 -p 3.8
14
  bash $0 -g 11.7 -p 3.8 -t 1.13.0
15
  bash $0 -c -n dgl-dev-cpu
16

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
17
Create a developement environment for DGL developers.
18
19
20
21

OPTIONS:
  -h           Show this message.
  -c           Create dev environment in CPU mode.
22
23
24
  -d           Only display environment YAML file instead of creating it.
  -f           Force creation of environment (removing a previously existing 
               environment of the same name).
25
  -g           Create dev environment in GPU mode with specified CUDA version,
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
26
               supported: ${CUDA_VERSIONS}.
27
  -n           Specify the name of the environment.
28
29
  -o           Save environment YAML file to specified path.
  -p           Create dev environment based on specified python version.
30
  -s           Run silently which indicates always 'yes' for any confirmation.
31
32
  -t           Create dev environment based on specified PyTorch version such
               as '1.13.0'.
33
34
35
36
37
EOF
}

validate() {
  values=$(echo "$1" | tr "," "\n")
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
38
  for value in ${values}
39
  do
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
40
    if [[ "${value}" == $2 ]]; then
41
42
43
44
45
46
47
48
49
      return 0
    fi
  done
  return 1
}

confirm() {
  echo "Continue? [yes/no]:"
  read confirm
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
50
  if [[ ! ${confirm} == "yes" ]]; then
51
52
53
54
    exit 0
  fi
}

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
55
# Parse flags.
56
while getopts "cdfg:hn:o:p:st:" flag; do
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  case "${flag}" in
    c)
      cpu=1
      ;;
    d)
      dry_run=1
      ;;
    f)
      force_create=1
      ;;
    g)
      cuda_version=${OPTARG}
      ;;
    h)
      usage
      exit 0
      ;;
74
75
76
    n)
      name=${OPTARG}
      ;;
77
78
79
80
81
82
83
84
85
    o)
      output_path=${OPTARG}
      ;;
    p)
      python_version=${OPTARG}
      ;;
    s)
      always_yes=1
      ;;
86
87
88
    t)
      torch_version=${OPTARG}
      ;;
89
90
91
92
93
94
95
96
97
    :)
      echo "Error: -${OPTARG} requires an argument."
      exit 1
      ;;
    *)
      usage
      exit 1
      ;;
  esac
98
99
done

100
if [[ -n ${cuda_version} && ${cpu} -eq 1 ]]; then
101
102
103
104
  echo "Only one mode can be specified."
  exit 1
fi

105
if [[ -z ${cuda_version} && -z ${cpu} ]]; then
106
107
108
109
  usage
  exit 1
fi

110
111
112
113
if [[ -z "${torch_version}" ]]; then
  torch_version=${TORCH_VERSION}
fi

114
# Set up CPU mode.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
115
if [[ ${cpu} -eq 1 ]]; then
116
  torchversion=${torch_version}"+cpu"
117
118
119
  if [[ -z "${name}" ]]; then
    name="dgl-dev-cpu"
  fi
120
121
122
fi

# Set up GPU mode.
123
124
if [[ -n ${cuda_version} ]]; then
  if ! validate ${CUDA_VERSIONS} ${cuda_version}; then
125
126
127
128
129
130
    echo "Error: Invalid CUDA version."
    usage
    exit 1
  fi

  echo "Confirm the installed CUDA version matches the specified one."
131
  [[ -n "${always_yes}" ]] || confirm
132

133
  torchversion=${torch_version}"+cu"${cuda_version//[-._]/}
134
135
136
  if [[ -z "${name}" ]]; then
    name="dgl-dev-gpu-"${cuda_version//[-._]/}
  fi
137
138
fi

139
140
141
142
143
# Set python version.
if [[ -z "${python_version}" ]]; then
  python_version=${PYTHON_VERSION}
fi

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

# Prepare the conda environment yaml file.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
149
150
rand=$(echo "${RANDOM}" | md5sum | head -c 20)
mkdir -p /tmp/${rand}
151
152
153
154
155
156
yaml_path="/tmp/${rand}/dgl_dev.yml"
cp script/dgl_dev.yml.template ${yaml_path}
sed -i "s|__NAME__|${name}|g" ${yaml_path}
sed -i "s|__PYTHON_VERSION__|${python_version}|g" ${yaml_path}
sed -i "s|__TORCH_VERSION__|${torchversion}|g" ${yaml_path}
sed -i "s|__DGL_HOME__|${PWD}|g" ${yaml_path}
157
158
159

# Ask for final confirmation.
echo "--------------------------------------------------"
160
cat ${yaml_path}
161
162
echo "--------------------------------------------------"
echo "Create a conda enviroment with the config?"
163
[[ -n "${always_yes}" ]] || confirm
164

165
166
167
168
169
170
# Save YAML file to specified path
if [[ -n "${output_path}" ]]; then
  cp ${yaml_path} ${output_path}
  echo "Environment YAML file has been saved to ${output_path}."
fi

171
# Create conda environment.
172
173
174
175
176
177
178
179
180
if [[ -z "${dry_run}" ]]; then
  conda_args=""
  if [[ -n "${force_create}" ]]; then
    conda_args="${conda_args} --force "
  fi
  conda env create -f ${yaml_path} ${conda_args}
else
  echo "Running in dry mode, so creation of conda environment is skipped."
fi
181
182

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