-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup_env.sh
executable file
·260 lines (233 loc) · 9.96 KB
/
setup_env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
###############################################################################
# Define env variables for paths
###############################################################################
if [ "$#" -eq 1 ]; then
QNLP_ROOT=$1
else
QNLP_ROOT=$PWD
fi
NLTK_DATA=${QNLP_ROOT}/third_party/install/nltk_data
###############################################################################
# Declare associative arrays to hold external dependencies
###############################################################################
declare -a GITHUB_REPOS
declare -a PIP_PACKAGES
declare -a CONDA_PACKAGES
declare -a PYTHON_CMDS
GITHUB_REPOS=( "iqusoft/intel-qs"
"catchorg/Catch2::v2.7.0"
"CLIUtils/CLI11"
"pybind/pybind11"
"mpi4py/mpi4py"
)
PIP_PACKAGES=( "multimethod" #used for multiple dispatch of certain function
"pyvis" # used to present interactive graph of token relationships
"ortools" # used to solve TSP for basis token ordering
"cython" # used during Python module setup (mpi4py)
"spacy" # additional NLP lib for tagging
)
CONDA_PACKAGES=("nltk"
"jupyter"
"numpy"
"scipy"
"tabulate"
"mkl-include"
"mkl-devel"
"mkl-static"
"networkx"
"matplotlib"
"tqdm::conda-forge"
"pandas"
)
CMDS=( "python -m nltk.downloader -d ${NLTK_DATA} all" #Download additional NLTK models
"python -m spacy download en_core_web_sm" #Download additional spacy models
)
###############################################################################
# Create all directories required by QNLP project
# build, third_party, third_party/{downloads, install}
###############################################################################
function setupDirs(){
declare -a dirs=( "${QNLP_ROOT}/build"
"${QNLP_ROOT}/third_party"
"${QNLP_ROOT}/third_party/downloads"
"${QNLP_ROOT}/third_party/install"
"${QNLP_ROOT}/install"
"${NLTK_DATA}"
)
for i in "${dirs[@]}";
do
if [ ! -d "${i}" ];then
mkdir -p "${i}"
fi
done
}
###############################################################################
# Set Conda version based on current OS.
# Assumes Nix systems only (MacOS not officially supported)
###############################################################################
export SYS=$(uname -s)
if [[ "${SYS}" == "Linux" ]];then
CONDA="Miniconda3-latest-Linux-x86_64.sh"
elif [[ "${SYS}" == "Darwin" ]];then
CONDA="Miniconda3-latest-MacOSX-x86_64.sh"
else
echo "Unsupported configuration."
exit
fi
###############################################################################
# Fetch miniconda installer
###############################################################################
function fetchConda(){
echo "### fetchConda() ###"
cd ${QNLP_ROOT}/third_party/downloads
if [ ! -e "${CONDA}" ]; then #if conda does not already exist, acquire.
echo "Conda installer not found. Acquiring."
#wget https://repo.continuum.io/miniconda/${CONDA} --no-check-certificate
curl -OL https://repo.continuum.io/miniconda/${CONDA}
fi
cd -
}
###############################################################################
# Acquire all requested TAR files, untar them into an OS independent named directory
###############################################################################
installTAR(){
#Loop through arrays, acquire TAR, and install the packages
for s in $(seq 0 $(( ${#TAR_FILES[@]} -1 )) ); do
echo "Acquiring TAR files from ${TAR_FILES[${s}]}"
if [[ "${TAR_FILES[${s}]}" =~ "::" ]]; then
PC=${TAR_FILES[${s}]} #Package::channel
#Split string and download package with specified name after ::
URL="${PC%::*}"
FILENAME="${PC#*::}"
echo ${QNLP_ROOT}/third_party/downloads/${PC#*::}
if [ ! -f "${QNLP_ROOT}/third_party/downloads/${FILENAME}" ]; then
curl -o ${QNLP_ROOT}/third_party/downloads/${FILENAME} -J -L "${URL}"
fi
mkdir ${QNLP_ROOT}/third_party/install/${FILENAME%%.*}
tar xvf ${QNLP_ROOT}/third_party/downloads/${FILENAME} -C \
${QNLP_ROOT}/third_party/install/${FILENAME%%.*} \
--strip-components 1
else
FILENAME=$(basename ${TAR_FILES[${s}]})
pushd . > /dev/null && cd ${QNLP_ROOT}/third_party/downloads/
if [ ! -f ${FILENAME} ]; then
curl -O -J -L ${TAR_FILES[${s}]}
fi
tar xvf ${FILENAME} -C ${QNLP_ROOT}/third_party/install/
popd > /dev/null
fi
done
}
###############################################################################
# Install miniconda and necessary packages
###############################################################################
function condaEnvSetup(){
echo "### condaEnvSetup() ###"
chmod +x ${QNLP_ROOT}/third_party/downloads/${CONDA}
${QNLP_ROOT}/third_party/downloads/${CONDA} -b -p ${QNLP_ROOT}/third_party/install/intel-qnlp_conda;
source ${QNLP_ROOT}/third_party/install/intel-qnlp_conda/bin/activate ;
conda update -n base conda -y;
conda create -n intel-qnlp -y python=3.7;
conda activate intel-qnlp #Activate said environment
}
###############################################################################
# Fetch SDKs if they do not exist already in Python environment
###############################################################################
function fetchPackages(){
echo "### fetchPackages() ###"
source ${QNLP_ROOT}/third_party/install/intel-qnlp_conda/bin/activate ;
conda activate intel-qnlp #Activate said environment
# Loop through arrays and install the packages and repos
if [ "${#GITHUB_REPOS[@]}" -gt 0 ]; then
pushd . &> /dev/null
cd $QNLP_ROOT/third_party
for s in $(seq 0 $(( ${#GITHUB_REPOS[@]} -1 )) ); do
echo ${GITHUB_REPOS[${s}]}
PC=${GITHUB_REPOS[${s}]} #Package::channel
if [[ "${GITHUB_REPOS[${s}]}" =~ "::" ]]; then
git clone https://github.com/${PC%::*}
PCC=${PC#*/}
cd ${PCC%::*}
git checkout ${PC#*::}
cd -
else
git clone https://github.com/${GITHUB_REPOS[${s}]}
fi
done
popd &> /dev/null
fi
# CONDA
if [ "${#CONDA_PACKAGES[@]}" -gt 0 ]; then
for s in $(seq 0 $(( ${#CONDA_PACKAGES[@]} -1 )) ); do
echo "Installing ${CONDA_PACKAGES[${s}]}"
if [[ "${CONDA_PACKAGES[${s}]}" =~ "::" ]]; then
PC=${CONDA_PACKAGES[${s}]} #Package::channel
#Split string and install package from specified channel
conda install ${PC%::*} -y -c ${PC#*::}
else
conda install ${CONDA_PACKAGES[${s}]} -y
fi
done
fi
# PIP
if [ "${#PIP_PACKAGES[@]}" -gt 0 ]; then
for s in $(seq 0 $(( ${#PIP_PACKAGES[@]} -1 )) ); do
echo ${PIP_PACKAGES[${s}]}
python -m pip install --no-binary all --compile ${PIP_PACKAGES[${s}]}
done
fi
# Bash commands
if [ "${#CMDS[@]}" -gt 0 ]; then
for s in $(seq 0 $(( ${#CMDS[@]} -1 )) ); do
echo "\"${CMDS[${s}]}\""
${CMDS[${s}]}
done
fi
}
###############################################################################
#Update conda environment and all installed packages
###############################################################################
function condaUpdateAll(){
source ${QNLP_ROOT}/third_party/install/intel-qnlp_conda/bin/activate ;
conda update --all -y
}
###############################################################################
# main
###############################################################################
LOG_NAME="SetupEnv"
setupDirs > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2) &&
fetchConda > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2) &&
condaEnvSetup > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2) &&
fetchPackages > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2) &&
condaUpdateAll > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2) &&
# Create MKLROOT dir mirroring actual MKL to avoid discrepencies
pushd . &> /dev/null
conda activate intel-qnlp
echo "Creating MKLROOT-style directory structure in ${CONDA_PREFIX}/lib"
cd ${CONDA_PREFIX}/lib
mkdir intel64 && cd intel64
for i in $(ls .. | grep mkl); do ln -sf $PWD/../$i .; done
for i in $(ls .. | grep libiomp); do ln -sf $PWD/../$i .; done
popd
#Force pip install of scipy/numpy as conda version has issues with MKL
pip install --upgrade --force-reinstall scipy
if [ $? -ne 0 ]; then
echo "Installation failed. Please check logs for further information." > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2)
else
echo "Installation succeeded. If you encounter any problems please report them to here, or the respective package authors." > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2)
echo "To load the environment run: source ${QNLP_ROOT}/load_env.sh" > >(tee -a ${LOG_NAME}_out.log) 2> >(tee -a ${LOG_NAME}_err.log >&2)
fi
cat > ${QNLP_ROOT}/load_env.sh << EOL
#!/bin/bash
source ${QNLP_ROOT}/third_party/install/intel-qnlp_conda/bin/activate ;
export PATH="${QNLP_ROOT}/install":"\${PATH}"
export NLTK_DATA="${NLTK_DATA}"
export QNLP_ROOT="${QNLP_ROOT}"
if [ -z "\${MKLROOT}" ];
then
export MKLROOT="${CONDA_PREFIX}"
fi
conda activate intel-qnlp
EOL
###############################################################################