Great. What kind of android devices will you be running on? 32 or 64 bit?
Emmm,probably both.Or will x32 build support x64 too?
@huzpsb Usually devices will either be 64 or 32 bit. It matters for lower level native code (c/c++) - one of the other tests weâd need to run is seeing the size if binaries for 32 or 64 bit are included. Unless you can have 1 apk per architecture (32/64) which might be possible.
I"m not sure what your platform allows though. Probably one APK only? With it being at 3MB it should be possible to stay under the limit even with 2 architectures present. The test I ran was only on my laptop so weâd want to make sure the full APK build was still small enough.
Anything under 10MB would be fine. If it works alike 3*2=6 , then it would be definitely fine for me.
@huzpsb yes of course. Main concern is the actual real world full APK size with all your other libraries though. Just donât want to oversell this till we see the final result thereâs some initial promise here but not much else.
At least you gave me hope to evit updating the stub code exported from the moudle each time.
Honestly speaking,it is not appealing for me to export the calculation graph each time.Just,if I have other choiceâŚ
@huzpsb yeah you donât really have to âexportâ so much as âinclude only the ops you needâ. If you donât change the network then you donât have to change the binaries. Itâs a bit like what the stubs do. Part of what it does when it creates a binary is looks at your graph and figures out what it needs to run.
We do that through modular compilation of the code base.
I can tryâŚAnd if it can be a build-tool that automize that jobâŚIt would be excellent!
@huzpsb that might be doable with docker. If so let me give it a shot.
will docker toolbox do? Iâm on windows 7
@huzpsb yeah will need a bit but I can set something up based on our android builds. Those normally run on linux. Just make sure youâre familiar with docker volumes.
Ok iâve got my docker started.Whatâs the next step?
@huzpsb not more tonight Iâll try to get a sample docker file for you tomorrow based on the build here. First I just wanted to verify if youâre up for giving this a shot. If you want though, feel free to research how our github actions look:
What Iâll need to do is configure a maven command that runs the steps here in the correct container.
Normally this is how we upload snapshot builds for different platforms.
Beyond that, the end result should be an understanding of docker volumes then from there youâll end up with a gradle file with 3 dependencies: nd4j-native (no classifier), nd4j-native (android-arm64), nd4j-native (android-arm32) Each of those classifiers will also bundle an executable that should theoretically be close to the same size as what I produced locally on my laptop.
Each classifier contains the pre compiled code for the relevant platforms. The nd4j-native contains the API youâll run.
Oh,I forgot about the timezone.Good night!
@huzpsb Hereâs 2 docker files that emulate our cross compilation build on github actions that are generic and will include all ops and data types by default (eg: not what you want quite please see below for more instructions on your particular use case):
64 bit:
FROM ubuntu:18.04
RUN apt-get update && apt-get -yq install git cmake wget unzip openjdk-11-jdk curl python3
RUN apt-get -yq update && apt-get install -y build-essential unzip libssl-dev && \
curl -fsSL http://cmake.org/files/v3.19/cmake-3.19.0.tar.gz | tar xz && cd cmake-3.19.0 && \
./configure --prefix=/opt/cmake && make -j2 && make install && cd .. && rm -r cmake-3.19.0
RUN curl -fsSL https://github.com/google/protobuf/releases/download/v3.8.0/protobuf-cpp-3.8.0.tar.gz \
| tar xz && \
cd protobuf-3.8.0 && \
./configure --prefix=/opt/protobuf && \
make -j2 && \
make install && \
cd .. && \
rm -rf protobuf-3.8.0
RUN curl https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz && tar xvf apache-maven-3.8.6-bin.tar.gz && mv apache-maven-3.8.6 /root/mvn
RUN git clone https://github.com/eclipse/deeplearning4j
RUN cd deeplearning4j && mkdir openblas_home && \
wget https://repo1.maven.org/maven2/org/bytedeco/openblas/0.3.19-1.5.7/openblas-0.3.19-1.5.7-android-arm64.jar && \
unzip openblas-0.3.19-1.5.7-android-arm64.jar
ENV OPENBLAS_PATH=/root/deeplearning4j/openblas_home/lib/arm64-v8a
ENV PATH=/root/mvn/bin:/opt/protobuf/bin:/opt/cmake/bin:${PATH}
ENV NDK_VERSION=r21d
ENV CURRENT_TARGET=android-arm64
ENV LIBND4J_CLASSIFIER=android-arm64
RUN cd /root/deeplearning4j && ./libnd4j/pi_build.sh
32 bit:
FROM ubuntu:18.04
RUN apt-get update && apt-get -yq install git wget unzip openjdk-11-jdk curl python3
RUN apt-get -yq update && apt-get install -y build-essential unzip libssl-dev && \
curl -fsSL http://cmake.org/files/v3.19/cmake-3.19.0.tar.gz | tar xz && cd cmake-3.19.0 && \
./configure --prefix=/opt/cmake && make -j2 && make install && cd .. && rm -r cmake-3.19.0
RUN curl -fsSL https://github.com/google/protobuf/releases/download/v3.8.0/protobuf-cpp-3.8.0.tar.gz \
| tar xz && \
cd protobuf-3.8.0 && \
./configure --prefix=/opt/protobuf && \
make -j2 && \
make install && \
cd .. && \
rm -rf protobuf-3.8.0
RUN wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz && tar xvf apache-maven-3.8.6-bin.tar.gz && mv apache-maven-3.8.6 /root/mvn
RUN cd /root && git clone https://github.com/eclipse/deeplearning4j
RUN cd /root/deeplearning4j && mkdir openblas_home && cd openblas_home && \
wget https://repo1.maven.org/maven2/org/bytedeco/openblas/0.3.19-1.5.7/openblas-0.3.19-1.5.7-android-arm.jar && \
unzip openblas-0.3.19-1.5.7-android-arm.jar
ENV OPENBLAS_PATH=/root/deeplearning4j/openblas_home/lib/armeabi-v7a
ENV BUILD_USING_MAVEN=1
ENV PATH=/root/mvn/bin:/opt/protobuf/bin:/opt/cmake/bin:${PATH}
ENV NDK_VERSION=r21d
ENV CURRENT_TARGET=android-arm
ENV LIBND4J_CLASSIFIER=android-arm
RUN cd /root/deeplearning4j/libnd4j && ./pi_build.sh
Youâll have to take the output of this and install it locally with the correct classifiers via volumes. That or take the contents of the container and export it to a tar file. Youâll want to use docker save for that: docker save | Docker Documentation
You can get the container images for each based on the final output looking something like this:
Removing intermediate container e6152f285ce1
---> feb74581b2c5
Successfully built feb74581b2c5
After you get the relevant target directories from each container (found under /root/deeplearning4j/nd4j/nd4j-backend-impls/nd4j-native/target)
Youâll want to locally install those using mvn clean install with file. See something like this:
Note that youâll want to install the no classifier jar once and the jars with a classifier suffix (android-arm,android-arm64) with separate commands.
Now to modify these generic instructions for your use case. I already mentioned above that in order to trim the jar files down to the 2 or 3 MB youâll want to add additional parameters to the build in the docker file. You will do that by setting an environment variable that modifies the compilation of the c++ library to only use the needed data types and operations we discovered your MLP needs. (In this case, add, matmul, and softmax) with the float data type being the main one you need. Note that what we are targeting here will be an environment variable that updates our cross compilation script for edge devices (phones, raspberry pis, jetson nanos)
Iâll apply those modifications to the docker files as follows:
64 bit:
FROM ubuntu:18.04
RUN apt-get update && apt-get -yq install git cmake wget unzip openjdk-11-jdk curl python3
RUN apt-get -yq update && apt-get install -y build-essential unzip libssl-dev && \
curl -fsSL http://cmake.org/files/v3.19/cmake-3.19.0.tar.gz | tar xz && cd cmake-3.19.0 && \
./configure --prefix=/opt/cmake && make -j2 && make install && cd .. && rm -r cmake-3.19.0
RUN curl -fsSL https://github.com/google/protobuf/releases/download/v3.8.0/protobuf-cpp-3.8.0.tar.gz \
| tar xz && \
cd protobuf-3.8.0 && \
./configure --prefix=/opt/protobuf && \
make -j2 && \
make install && \
cd .. && \
rm -rf protobuf-3.8.0
RUN curl https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz && tar xvf apache-maven-3.8.6-bin.tar.gz && mv apache-maven-3.8.6 /root/mvn
RUN git clone https://github.com/eclipse/deeplearning4j --branch ag_importcache_graalvm
RUN cd deeplearning4j && mkdir openblas_home && \
wget https://repo1.maven.org/maven2/org/bytedeco/openblas/0.3.19-1.5.7/openblas-0.3.19-1.5.7-android-arm64.jar && \
unzip openblas-0.3.19-1.5.7-android-arm64.jar
ENV OPENBLAS_PATH=/root/deeplearning4j/openblas_home/lib/arm64-v8a
ENV PATH=/root/mvn/bin:/opt/protobuf/bin:/opt/cmake/bin:${PATH}
ENV NDK_VERSION=r21d
ENV CURRENT_TARGET=android-arm64
ENV LIBND4J_CLASSIFIER=android-arm64
ENV MODULES="-Dlibnd4j.operations='softmax;add,matmul' -Dlibnd4j.datatypes=float -Dlibnd4j.lto=ON"
RUN cd /root/deeplearning4j && ./libnd4j/pi_build.sh
32 bit:
FROM ubuntu:18.04
RUN apt-get update && apt-get -yq install git wget unzip openjdk-11-jdk curl python3
RUN apt-get -yq update && apt-get install -y build-essential unzip libssl-dev && \
curl -fsSL http://cmake.org/files/v3.19/cmake-3.19.0.tar.gz | tar xz && cd cmake-3.19.0 && \
./configure --prefix=/opt/cmake && make -j2 && make install && cd .. && rm -r cmake-3.19.0
RUN curl -fsSL https://github.com/google/protobuf/releases/download/v3.8.0/protobuf-cpp-3.8.0.tar.gz \
| tar xz && \
cd protobuf-3.8.0 && \
./configure --prefix=/opt/protobuf && \
make -j2 && \
make install && \
cd .. && \
rm -rf protobuf-3.8.0
RUN wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz && tar xvf apache-maven-3.8.6-bin.tar.gz && mv apache-maven-3.8.6 /root/mvn
RUN cd /root && git clone https://github.com/eclipse/deeplearning4j --branch ag_importcache_graalvm
RUN cd /root/deeplearning4j && mkdir openblas_home && cd openblas_home && \
wget https://repo1.maven.org/maven2/org/bytedeco/openblas/0.3.19-1.5.7/openblas-0.3.19-1.5.7-android-arm.jar && \
unzip openblas-0.3.19-1.5.7-android-arm.jar
ENV OPENBLAS_PATH=/root/deeplearning4j/openblas_home/lib/armeabi-v7a
ENV BUILD_USING_MAVEN=1
ENV PATH=/root/mvn/bin:/opt/protobuf/bin:/opt/cmake/bin:${PATH}
ENV NDK_VERSION=r21d
ENV CURRENT_TARGET=android-arm
ENV LIBND4J_CLASSIFIER=android-arm
ENV MODULES="-Dlibnd4j.operations='softmax;add,matmul' -Dlibnd4j.datatypes=float -Dlibnd4j.lto=ON"
RUN cd /root/deeplearning4j/libnd4j && ./pi_build.sh
Note the MODULES environment variable and I modified the cloning to clone from a current branch Iâm working on that helped with the size reduction that I mentioned above. Your size may vary with the arm devices but it shouldnât hurt to just run the builds and see if you can build your APK with this.
Good luck and let me know if you have any questions.
I canât get throughâŚ
I put your content into a file named Dockerfile F:\docker\idk
Then I typed cd /d/docker/
then docker build idk
thatâs what I got
GaoZH@GaoZH-Think MINGW64 /f/docker
$ docker build idk
time="2022-06-24T14:46:32+08:00" level=error msg="failed to dial gRPC: cannot connect to th
e Docker daemon. Is 'docker daemon' running on this host?: open //./pipe/docker_engine: The
system cannot find the file specified."
error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/build?buildargs=%7B%
7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=
0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&session=o
jrdcf7wqkt210lxjek1b34l1&shmsize=0&target=&ulimits=null&version=1: open //./pipe/docker_eng
ine: The system cannot find the file specified. In the default daemon configuration on Wind
ows, the docker client must be run elevated to connect. This error may also indicate that t
he docker daemon is not running.
@huzpsb that has nothing to do with the file or anything on the forum. Thatâs just your own installation not working. Make sure your docker daemon is starting and you have permissions to run it.
Ah,I know the problem.
The online package of protobuf is not available in China Mainland because of a policy called Great Firewall.
But,thereâs absolutely nothing I can do about it.I mean,I canât go abroad just to compile an app,right?
=.=
@huzpsb most people I know in China are familiar with VPNs and itâs generally not a problem. Thereâs only so much I can do for you here.
I already did all the steps required for you to get everything setup. Protobuf shouldnât be that big of a leap for you.
You can always turn it off after youâre done compiling the image. I"m not sure what step youâre stuck on if itâs the protobuf being downloaded by maven or this one: https://github.com/google/protobuf/releases/download/v3.8.0/protobuf-cpp-3.8.0.tar.gz
This oneâs already on github though. Github shouldnât be blocked by china afaik.
it worked!thanks.
uwu