thnets icon indicating copy to clipboard operation
thnets copied to clipboard

thnets on Android

Open yonadavs opened this issue 9 years ago • 24 comments

According to @culurciello, it should be easy to build this to run on Android. Any document/example as to how it could be done? Could it be really as easy as setting $CC to my android gcc? Thanks.

yonadavs avatar Feb 11 '16 08:02 yonadavs

No. You have to give the right options to gcc, the best thing is to set up the Android.mk and Application.mk, so the NDK will set the proper options. In Android.mk I've added LOCAL_CFLAGS += -c -fopenmp -fPIC -DARM -D__NEON__ -mcpu=cortex-a9 -mfpu=neon -O3, in Application.mk add APP_ABI := armeabi-v7a-hard APP_CFLAGS += -fopenmp APP_LDFLAGS += -fopenmp.

mvitez avatar Feb 11 '16 08:02 mvitez

I am interested in that too, and am waiting for a concrete working example for Android before starting actual work with it.

Thanks

On 11-02-16 9:22 AM, yonadavs wrote:

According to @culurciello https://github.com/culurciello, it should be easy to build this to run on Android. Any document/example as to how it could be done? Could it be really as easy as setting |$CC| to my android gcc? Thanks.

seeingwithsound avatar Feb 11 '16 08:02 seeingwithsound

@mvitez, thanks for the swift reply! Just trying to catch up on your meaning: I add all the source files to a project under /jni/, add them to Android.mk inLOCAL_SRC_FILES`, then add what you mentioned to Android.mk & Application.mk, then build it as a separate library? or inside my own .so?

yonadavs avatar Feb 11 '16 08:02 yonadavs

As you prefer.

mvitez avatar Feb 11 '16 08:02 mvitez

Great! I'll try this now.

yonadavs avatar Feb 11 '16 08:02 yonadavs

I recently had some more time to invest on building Thnets for android, and I'm still unable to do it. It builds fine, but when running it, it crashes at sgemm.c blas_init() at line: saa[i] = malloc(BUFFER_SIZE);, on the first loop pass. It crashes when I set buffer size to , and when I use a different pointer, so it definitely isn't an OOM issue.

yonadavs avatar Jun 07 '16 10:06 yonadavs

The issue was with the redefinition of malloc to debug_malloc, where fopen is used but fopen isn't available in Android. Funny thing is, it should only be defined if MEMORYDEBUG is defined, which I haven't done, but it still defines it...

yonadavs avatar Jun 08 '16 08:06 yonadavs

Excellent. I was about to start checking on Android now, good that you solved it. memory.h and memory.c should not be included at all if MEMORYDEBUG is not defined.

mvitez avatar Jun 08 '16 08:06 mvitez

I'm now running into another problem calling THProcessFloat. Chasing it down, I found it crashes inside sgemm_kernel in sgemm_kernel_4x4_vfpv3.S. Due to my limited Assembly talent, I'm going to assume the problem is in the input I give to THProcessFloat.. for reference, I used it like this: float *result; int outwidth, outheight, n; n = THProcessFloat(net, encoded, 1, 231, 231, &result), &outwidth, &outheight);

(231 is the size from this example using this NN)

I guess the net parameter is fine, as the init completed, and THLastError returns 0. Any pointers to what might be wrong?

yonadavs avatar Jun 08 '16 15:06 yonadavs

I wouldn't know. sgemm_kernel_4x4_vfpv3.S reads from internal buffers and writes to the output buffer, all are allocated by thnets, so it appears to be some problem internal to thnets. I will try to compile it for Android by myself and see.

mvitez avatar Jun 08 '16 15:06 mvitez

I've tried now that network with a 231x231 image on a Nexus 5 and the test succeeded. But I've tried it from ssh in a pure C application. I've added some instructions at the end of the readme if you want to try in this way.

mvitez avatar Jun 08 '16 16:06 mvitez

Well, since my app is both Java and C, I tried making it work this way. I've put some time into it, but it still crashes mid sgemm_kernel with a signal 11..

yonadavs avatar Jun 26 '16 11:06 yonadavs

I am sorry, but I don't know how I could help from here. I have tested this library on the devices that I have and this problem does not appear on my devices.

mvitez avatar Jun 26 '16 11:06 mvitez

I also have the same problem as yonadavs with sgemm_kernel. The app crashes with a signal 11. yonadavs, did you find the solution?

nevenp avatar Jul 12 '16 01:07 nevenp

Currently I don't have any evidence of this, but try to increase the buffer size in https://github.com/mvitez/thnets/blob/master/OpenBLAS-stripped/sgemm.c#L78, e.g. multiply it by 2 or 4.

mvitez avatar Jul 12 '16 18:07 mvitez

I have also meet this problem. happend at sgemm_kernel_4x4_vfpv3.S.

austingg avatar Dec 06 '16 10:12 austingg

after some investigation, I have found the program crash at spatialconvolutionMM when call kernal operation. @mvitez have u run android test on other mobile device ? I wonder whether this is devices dependent problem.

austingg avatar Dec 07 '16 14:12 austingg

Unfortunately I only have a Nexus 5 and it's there where I tested. And it does not crash there.

mvitez avatar Dec 07 '16 14:12 mvitez

@yonadavs @nevenp What's your mobile devices ? Nexus 5 or other ?

austingg avatar Dec 08 '16 01:12 austingg

@mvitez I have used the whole version of openblas build for android with USE_THREAD=0, it worked without crash

austingg avatar Dec 09 '16 10:12 austingg

Sure, of course, there is some problem in my integration of OpenBLAS. Have you tried my suggestion given in the comment of July 12th? And using the option -a 1?

mvitez avatar Dec 09 '16 10:12 mvitez

@mvitez I tried but still with option -a 2, I will try again. Thanks a lot.

austingg avatar Dec 09 '16 10:12 austingg

@yonadavs and @austingg are you maybe using APP_ABI := armeabi-v7a inside Application.mk? I've seen that armeabi-v7a-hard is not supported anymore in the NDK. If you used armeabi-v7a, it could not work and it crashed. I've just pushed a small fix to work with armeabi-v7a.

mvitez avatar Jan 21 '17 15:01 mvitez

@mvitez yes, I used armeabi-v7a, since newer NDK complains armeabi-v7a-hard, thanks a lot.

austingg avatar Jan 22 '17 02:01 austingg