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

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
3
readonly CUDA_VERSIONS="10.2,11.3,11.6,11.7"
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

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

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

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

confirm() {
  echo "Continue? [yes/no]:"
  read confirm
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
45
  if [[ ! ${confirm} == "yes" ]]; then
46
47
48
49
    exit 0
  fi
}

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
50
# Parse flags.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
while getopts "cdfg:ho:p:s" flag; do
  case "${flag}" in
    c)
      cpu=1
      ;;
    d)
      dry_run=1
      ;;
    f)
      force_create=1
      ;;
    g)
      cuda_version=${OPTARG}
      ;;
    h)
      usage
      exit 0
      ;;
    o)
      output_path=${OPTARG}
      ;;
    p)
      python_version=${OPTARG}
      ;;
    s)
      always_yes=1
      ;;
    :)
      echo "Error: -${OPTARG} requires an argument."
      exit 1
      ;;
    *)
      usage
      exit 1
      ;;
  esac
87
88
done

89
if [[ -n ${cuda_version} && ${cpu} -eq 1 ]]; then
90
91
92
93
  echo "Only one mode can be specified."
  exit 1
fi

94
if [[ -z ${cuda_version} && -z ${cpu} ]]; then
95
96
97
98
99
  usage
  exit 1
fi

# Set up CPU mode.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
100
if [[ ${cpu} -eq 1 ]]; then
101
102
103
104
105
  torchversion=${TORCH_VERSION}"+cpu"
  name="dgl-dev-cpu"
fi

# Set up GPU mode.
106
107
if [[ -n ${cuda_version} ]]; then
  if ! validate ${CUDA_VERSIONS} ${cuda_version}; then
108
109
110
111
112
113
    echo "Error: Invalid CUDA version."
    usage
    exit 1
  fi

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

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

120
121
122
123
124
# Set python version.
if [[ -z "${python_version}" ]]; then
  python_version=${PYTHON_VERSION}
fi

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

# Prepare the conda environment yaml file.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
130
131
rand=$(echo "${RANDOM}" | md5sum | head -c 20)
mkdir -p /tmp/${rand}
132
133
134
135
136
137
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}
138
139
140

# Ask for final confirmation.
echo "--------------------------------------------------"
141
cat ${yaml_path}
142
143
echo "--------------------------------------------------"
echo "Create a conda enviroment with the config?"
144
[[ -n "${always_yes}" ]] || confirm
145

146
147
148
149
150
151
# 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

152
# Create conda environment.
153
154
155
156
157
158
159
160
161
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
162
163

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