#!/bin/bash

source /home/miniconda3/bin/activate ci

# Install lib(conda or pip)
#conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing

# Download code from github
cd /tmp
rm -rf pytorch
git clone https://github.com/pytorch/pytorch
cd pytorch
git checkout v1.5.0
git submodule sync
git submodule update --init --recursive

# Patch
# issue: https://github.com/pytorch/pytorch/issues/33124
# /tmp/pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/src/q8gemm/8x8-dq-aarch64-neon.S
cd /tmp/pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/src/q8gemm
sed -i 's#MOV V8.4s, V9.4s#MOV V8.16b, V9.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV v10.4s, v11.4s#MOV v10.16b, v11.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV v12.4s, V13.4s#MOV v12.16b, V13.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV V14.4s, V15.4s#MOV V14.16b, V15.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV V16.4s, V17.4s#MOV V16.16b, V17.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV V18.4s, V19.4s#MOV V18.16b, V19.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV V20.4s, V21.4s#MOV V20.16b, V21.16b#g' 8x8-dq-aarch64-neon.S
sed -i 's#MOV V22.4s, V23.4s#MOV V22.16b, V23.16b#g' 8x8-dq-aarch64-neon.S

# Install pytorch
cd /tmp/pytorch
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python setup.py install

# Package
python setup.py bdist_wheel
