ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

armcompute mali demo using Android Studio

Open Subarukun opened this issue 9 years ago • 20 comments

Hi,

We have created a camera preview app using Android Studio for Android platforms/phones . We want the camera frame to be processed by the Mali armcompute and to be previewed.(The demo that you have provided uses the standalone linux kind of application instead of Android Studio)

It would be great if you could guide us to run the sample app with armcompute Mali, using Android Studio on Android platforms. We can use the steps to build our application as specified above.

Thanks.

Subarukun avatar Jun 14 '17 11:06 Subarukun

I'm afraid we're doing all our development in C/C++ so we don't have any examples of how to use Android Studio to create an application. However some other people seem to have managed to get it working: #52 / #135

AnthonyBarbier avatar Jun 27 '17 07:06 AnthonyBarbier

I was able to build and run sample convolution example from armcompute library on Android Mobile using Android Studio.

But when I try to run the 1st standlaone convolution layer from Alexnet. Its crashing during the convolution.configure.Can you look into the below code and point me if there is any issue in the code

    float *wghts = (float*)calloc((11*11*96*3) ,sizeof(float));
    float *bias = (float*)calloc(96 ,sizeof(float));
    float *input = (float*)calloc((227*227*3) ,sizeof(float));


    CLScheduler::get().default_init();

    CLTensor ConvLayerWgtTensor;
    CLTensor ConvLayerBiasTensor;
    CLTensor IpLayerTensor;
    CLTensor OpLayerTensor;
    CLConvolutionLayer ConvLayer;

    ConvLayerWgtTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)11, (long unsigned int)11, (long unsigned int)3,(long unsigned int)96), 1, DataType::F32));
    ConvLayerBiasTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)96), 1, DataType::F32));
    IpLayerTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)227, (long unsigned int)227, (long unsigned int)3), 1, DataType::F32));
    OpLayerTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)55, (long unsigned int)55, (long unsigned int)96), 1, DataType::F32));


    ConvLayer.configure(&IpLayerTensor, &ConvLayerWgtTensor, &ConvLayerBiasTensor, &OpLayerTensor, PadStrideInfo(4,4,0,0,DimensionRoundingType::FLOOR));


    ConvLayerWgtTensor.allocator()->allocate();
    ConvLayerBiasTensor.allocator()->allocate();
    IpLayerTensor.allocator()->allocate();
    OpLayerTensor.allocator()->allocate();


    fillIpToTensor((unsigned char*)wghts,ConvLayerWgtTensor);
    fillIpToTensor((unsigned char*)bias,ConvLayerBiasTensor);
    fillIpToTensor((unsigned char*)input,IpLayerTensor);

    ConvLayer.run();

    // Make sure all the OpenCL jobs are done executing:
    CLScheduler::get().sync();

    free(wghts);
    free(bias);
    free(input);

Crash is at the createkernel during configure in below code: _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);

Thanks..

Subarukun avatar Jun 27 '17 12:06 Subarukun

You need to call configure before you call allocate (cf this issue)

Also, try to use a debug build of the library or at least one with asserts enabled in order to have more information about what's wrong

AnthonyBarbier avatar Jun 27 '17 12:06 AnthonyBarbier

As per the code I am calling the ConvLayer.configure before allocation of CLTensors. So that wouldn't be the issue.

Coming to where precisely the crash is happening .I was able to trace it back to "object_ = ::clCreateKernel(program(), name, &error); " failing with error returning as -46 ,since its unable to create the gemm_interleave4x4_32bit kernel.

Could you please run this piece of code on your working setup and let me know if everything works.

Thanks.

Subarukun avatar Jun 28 '17 04:06 Subarukun

I was able to run the clconvolution layer on my mobile using android studio and is also bit-exact. Thanks

Subarukun avatar Jun 30 '17 06:06 Subarukun

Dear Subarukun,

Congratulation on successfully running cl_convolution on your device, I want to run too and get bit-exact.

Can you help me?

In Studio, folder "libs", what are the librares needed?

I put libarm_compute-static.a only, and can only run all the NEON sample, but when I put libOpenCL.so (stub, not the real one), the app crashed.

Thanks,

yge58 avatar Sep 29 '17 20:09 yge58

Can you give more details: do you have a libOpenCL.so on your platform? What error message do you get in logcat when trying to start your application?


From: yge58 [email protected] Sent: 29 Sep 2017 21:29 To: ARM-software/ComputeLibrary Cc: Subscribed Subject: Re: [ARM-software/ComputeLibrary] armcompute mali demo using Android Studio (#137)

Dear Subarukun,

Congratulation on successfully running cl_convolution on your device, I want to run too and get bit-exact.

Can you help me?

In Studio, folder "libs", what are the librares needed?

I put libarm_compute-static.a only, and can only run all the NEON sample, but when I put libOpenCL.so (stub, not the real one), the app crashed.

Thanks,

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/ARM-software/ComputeLibrary/issues/137#issuecomment-333231095, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AZt0Sp4LPws8tR1e7J84mrOc-ElOQlQ5ks5snVMkgaJpZM4N5xtw.

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

developer-compute avatar Sep 29 '17 20:09 developer-compute

Sorry, I referenced this with https://github.com/ARM-software/ComputeLibrary/issues/231

Huawei mate 9, /system/vendor/lib or /lib64

Everything went well with NEON only, build, debug, run through Android Studio.

Well, the "libOpenCL.so" is stub, not the real one.

Should I "adb pull" the real one from device to Android Studio?

I tried, not working.

Many thanks

yge58 avatar Sep 29 '17 21:09 yge58

No error displayed, cannot even debug the android app.

The app just crashes.

yge58 avatar Sep 29 '17 21:09 yge58

I will try this one https://github.com/arrayfire/androidcl

yge58 avatar Sep 29 '17 21:09 yge58

In /system/vendor/lib64:

libOpenCL.so
libOpenCL.so.1
libOpenCL.so.1.1
libOpenCLFoundation.so

Should I copy them all or just libOpenCL.so?

Many thanks,

yge58 avatar Sep 29 '17 21:09 yge58

You don't need to pull any library from your device, just link against the stub libOpenCL.so

At runtime the library will dlopen the real one as you can see here

What does logcat show when the application dies?

You could try to hardcode the path to libOpenCL.so in the file I mentioned above and see if it fixes your issue.

AnthonyBarbier avatar Sep 30 '17 10:09 AnthonyBarbier

Many thanks first,

I am an Android newbie completely. NDK is hard to set up right.

I will try this openCV/openCL sample first http://docs.opencv.org/trunk/d7/dbd/tutorial_android_ocl_intro.html

yge58 avatar Sep 30 '17 14:09 yge58

And I sincerely hope your ARM professional could provide a sample including, openCV + openCL + openGL + arm compute library

from openCV doc, they achieved "direct OpenCL calls shows 25-27 fps"

yge58 avatar Sep 30 '17 14:09 yge58

the caffe2 on android is only 6 fps, use CPU only.

and Tensorflow, well, I guess no openCL support.

So ARM Compute library is very promising as far as I can see.

Thanks

yge58 avatar Sep 30 '17 14:09 yge58

Hi @yge58 ,

and thanks for using the Compute Library :)

Regarding your problem, it might be related to some behaviour changes in Android 7 regarding dynamically linking against non-NDK libraries. Please have a look at here https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk (NDK Apps Linking to Platform Libraries)

Starting in Android 7.0, the system prevents apps from dynamically linking against non-NDK libraries, which may cause your app to crash

Unfortunately I'm not 100 % sure as without logcat is hard to tell. Anyway in order to catch library link errors in the logcat, I would suggest to have something like this when you need to load your native library:

// Used to load the 'native-lib' library on application startup.
static {
    try {
        System.loadLibrary("native-lib");
    } catch (UnsatisfiedLinkError e) {
        e.printStackTrace();
}

When you launch the app, try to grep "the name of your app" or "dlopen" to catch possible errors in your logcat.

Let us know if you are able to have more information from your logcat.

Hope this can help you, Gian Marco

gmiodice avatar Sep 30 '17 21:09 gmiodice

--------- beginning of crash 10-02 10:34:36.600 19388-19388/com.example.root.demoacl E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.root.demoacl, PID: 19388 java.lang.UnsatisfiedLinkError: dlopen failed: library "../../../../libs/arm64-v8a/libOpenCL-stub.so" not found at java.lang.Runtime.loadLibrary0(Runtime.java:994) at java.lang.System.loadLibrary(System.java:1533) at com.example.root.demoacl.MainActivity.(MainActivity.java:11) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1083) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2682) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6524) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 10-02 10:34:36.618 19388-19388/? I/Process: Sending signal. PID: 19388 SIG: 9

yge58 avatar Oct 02 '17 14:10 yge58

Thanks a lot,

The doc clearly states that "The following table describes the behavior you should expect to see from an app depending on its use of private native libraries and its target API level "

When Target API level is 24 or higher, "Runtime access via dynamic linker " - Restricted "Android 7.0 (API level 24) behavior" - Runtime Error (as shown above)

The way to work around is to either,

  1. set Target API 23 or lower,
  2. hoping Google engineers make libOpenCL.so NDK public! public! public!

Thanks a ton.

yge58 avatar Oct 02 '17 14:10 yge58

When I test neon sample, no error, because ARM compute library is linked statically.

My last question is how to make a static linked openCL library against ARM compute library, or is it possible?

yge58 avatar Oct 02 '17 15:10 yge58

You can't link statically against OpenCL, it's only available as a dynamic library. The main reason being that the OpenCL driver is made of two parts: one userspace and one kernel space, and these two parts need to match for it to work, as a result only shared libraries are distributed. (This is true for all platforms supporting OpenCL as far as I know, not just Arm)

AnthonyBarbier avatar Oct 02 '17 15:10 AnthonyBarbier