How can I turn a network into stub codes?

Great. What kind of android devices will you be running on? 32 or 64 bit?

1 Like

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 :slight_smile: 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 :slight_smile: 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.

1 Like

it worked!thanks.

uwu