Skip to content

Commit

Permalink
FIX/Fix: upgrade Tensorflow to work with m1 architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaCaceres committed Sep 20, 2023
1 parent e22bb7a commit c46a573
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rundocker: builddocker
sh -c "cd /home/sage/tii-claasp && make install && cd /home/sage/tii-claasp && exec /bin/bash"

builddocker-m1:
docker build -f docker/Dockerfile --platform linux/x86_64 -t $(DOCKER_IMG_NAME) .
docker build --build-arg="GUROBI_ARCH=armlinux64" -f docker/Dockerfile --platform linux/aarch64 -t $(DOCKER_IMG_NAME) .

rundocker-m1: builddocker-m1
docker run -i -p 8888:8888 --mount type=bind,source=`pwd`,target=/home/sage/tii-claasp -t $(DOCKER_IMG_NAME) \
Expand Down Expand Up @@ -89,3 +89,9 @@ distclean: clean

copyright: install
python3 create_copyright.py

local-installation:
./configure.sh

local-installation-m1:
./configure.sh armlinux64
15 changes: 0 additions & 15 deletions claasp/cipher_modules/neural_network_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@

def neural_network_blackbox_distinguisher_tests(cipher, nb_samples=10000,
hidden_layers=[32, 32, 32], number_of_epochs=10, rounds_to_train=[]):
"""
.. WARNING::
Tensorflow is used in this method, and currently it is not supported for Apple Silicon chip (M1).
"""
results = {
"neural_network_blackbox_distinguisher_tests": {
"input_parameters": {
Expand Down Expand Up @@ -64,11 +59,6 @@ def neural_network_blackbox_distinguisher_tests(cipher, nb_samples=10000,

def update_partial_result(cipher, component_output_ids, ds, index, hidden_layers, labels, number_of_epochs,
partial_result, blackbox=True, rounds_to_train=[]):
"""
.. WARNING::
Tensorflow is used in this method, and currently it is not supported for Apple Silicon chip (M1).
"""
# noinspection PyUnresolvedReferences
input_lengths = cipher.inputs_bit_size
if rounds_to_train:
Expand Down Expand Up @@ -169,11 +159,6 @@ def create_structure(base_output, cipher, index):

def neural_network_differential_distinguisher_tests(cipher, nb_samples=10000, hidden_layers=[32, 32, 32],
number_of_epochs=10, diff=[0x01], rounds_to_train=[]):
"""
.. WARNING::
Tensorflow is used in this method, and currently it is not supported for Apple Silicon chip (M1).
"""
results = {
"neural_network_differential_distinguisher_tests": {
"input_parameters": {
Expand Down
3 changes: 2 additions & 1 deletion configure.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

apt-get -qq update
apt-get install -y python3
python3 create_bash_script.py
python3 create_bash_script.py $1
chmod +x dependencies_script.sh
./dependencies_script.sh
source ~/.bashrc
rm dependencies_script.sh
27 changes: 20 additions & 7 deletions create_bash_script.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import sys

gurobi_arch = sys.argv[1] or 'linux64'

with open('docker/Dockerfile', 'r') as f:
dockerfile_lines = f.readlines()

Expand All @@ -17,21 +21,30 @@
docker_command += line
bash_instruction = ''
if docker_command.startswith('RUN'):
bash_instruction = docker_command.split('RUN')[1]
command = docker_command.split('RUN')[1].strip()
if 'GUROBI_ARCH' in command:
bash_instruction = command.replace('${GUROBI_ARCH}', gurobi_arch)
else:
bash_instruction = command
elif docker_command.startswith('ENV'):
command = docker_command.split('ENV')[1]
command = docker_command.split('ENV')[1].strip()
environment_variable = command.split('=')[0]
if environment_variable not in environment_variables:
environment_variables.append(environment_variable)
bash_instruction = f'export {command}'
elif docker_command.startswith('ARG'):
command = docker_command.split('ARG')[1]
bash_instruction = f'export {command}'
command = docker_command.split('ARG')[1].strip()
bash_instruction = 'export '
if 'GUROBI_ARCH' in command:
bash_instruction += f'GUROBI_ARCH={gurobi_arch}'
else:
bash_instruction += command
elif docker_command.startswith('WORKDIR'):
directory = docker_command.split('WORKDIR')[1]
bash_instruction = f'cd {directory}'
directory = docker_command.split('WORKDIR')[1].strip()
if directory != '/home/sage/tii-claasp':
bash_instruction = f'cd {directory}'
elif docker_command.startswith('COPY'):
command = docker_command.split('COPY')[1]
command = docker_command.split('COPY')[1].strip()
bash_instruction = f'cp {command}'
else:
docker_command = ''
Expand Down
38 changes: 19 additions & 19 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM ubuntu:22.10
FROM ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive
ARG GUROBI_ARCH=linux64

# Install dependencies with apt-get
RUN apt-get -qq update
Expand All @@ -15,27 +16,23 @@ RUN apt-get install --no-install-recommends -y gfortran \
zlib1g-dev
RUN apt-get install -y build-essential \
cmake \
dieharder=3.31.1.3-1 \
dieharder=3.31.1.2-1build1 \
git \
latexmk=1:4.77-1 \
latexmk=1:4.76-1 \
python3-cryptominisat=5.8.0+dfsg1-2 \
sagemath=9.5-4build3 \
sagemath=9.5-4 \
wget \
z3

COPY docker/sitecustomize.py /usr/lib/python3.10/sitecustomize.py

COPY required_dependencies/assess.c /opt/
COPY required_dependencies/utilities.c /opt/
COPY required_dependencies/utilities.h /opt/

WORKDIR /opt

RUN wget https://packages.gurobi.com/10.0/gurobi10.0.0_linux64.tar.gz \
&& tar -xf gurobi10.0.0_linux64.tar.gz \
&& rm gurobi10.0.0_linux64.tar.gz
RUN wget https://packages.gurobi.com/10.0/gurobi10.0.0_${GUROBI_ARCH}.tar.gz \
&& tar -xf gurobi10.0.0_${GUROBI_ARCH}.tar.gz \
&& rm gurobi10.0.0_${GUROBI_ARCH}.tar.gz

ENV GUROBI_HOME="/opt/gurobi1000/linux64"
ENV GUROBI_HOME="/opt/gurobi1000/${GUROBI_ARCH}"
ENV PATH="${PATH}:${GUROBI_HOME}/bin"
ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"

Expand Down Expand Up @@ -70,7 +67,7 @@ ENV PATH="${PATH}:/opt/scip-803/build/bin"

# Installing SageMath tools
RUN sage -pip install bitstring==4.0.1 \
keras==2.11.0 \
keras==2.13.1 \
minizinc==0.5.0 \
pandas==1.5.2 \
prettytable==3.6.0 \
Expand All @@ -82,13 +79,17 @@ RUN sage -pip install bitstring==4.0.1 \
setuptools==59.6.0 \
sphinx==4.5.0 \
sphinxcontrib-bibtex==2.5.0 \
tensorflow==2.11.0 \
tensorflow==2.13.0 \
pytest==7.2.1 \
pytest-cov==4.0.0 \
pytest-xdist==3.2.0 \
pytest-benchmark==4.0.0

# Installing nist sts
COPY required_dependencies/assess.c /opt/
COPY required_dependencies/utilities.c /opt/
COPY required_dependencies/utilities.h /opt/

RUN curl -O -s https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/sts-2_1_2.zip \
&& unzip sts-2_1_2.zip
RUN mv /opt/assess.c /opt/sts-2.1.2/sts-2.1.2/src/
Expand Down Expand Up @@ -156,10 +157,9 @@ RUN wget https://www.labri.fr/perso/lsimon/downloads/softwares/glucose-syrup-4.1
&& tar -xf glucose-syrup-4.1.tgz \
&& rm glucose-syrup-4.1.tgz

RUN cd glucose-syrup-4.1/simp/ \
&& make \
&& cd ../parallel/ \
&& make
RUN make glucose-syrup-4.1/simp

RUN make glucose-syrup-4.1/parallel

WORKDIR /opt

Expand Down Expand Up @@ -296,4 +296,4 @@ RUN rm -rf /opt/MiniZincIDE-2.6.4-bundle-linux-x86_64/lib/libselinux.so.1
RUN rm -rf /opt/MiniZincIDE-2.6.4-bundle-linux-x86_64/lib/libsystemd.so.0
RUN rm -rf /opt/MiniZincIDE-2.6.4-bundle-linux-x86_64/lib/libcrypt.so.1

WORKDIR "/home/sage/tii-claasp"
WORKDIR /home/sage/tii-claasp
13 changes: 10 additions & 3 deletions docs/USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ After the installation, we need to enter to the sage terminal with the command:
After that we are ready to go and can use the library as specified in the [usage](#usage) section.

### Manual installation
To install the dependencies manually, you only need to execute the following script from the
root directory of the project:
To install the dependencies manually, you can do it through make command or executing a script from the
root directory of the project.

$ ./configure.sh
#### Make command
You need to have `make` installed for this execution.
- For m1 macs, run ```make local-installation-m1```
- For other machines, run ```make local-installation```

#### Script execution
- For m1 macs, run ```./configure.sh armlinux64```
- For other machines, run ```./configure.sh```

## Documentation

Expand Down

0 comments on commit c46a573

Please sign in to comment.