Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not import torch when I use rknntoolkit #1195

Open
cyz6668 opened this issue Jul 3, 2024 · 6 comments
Open

can not import torch when I use rknntoolkit #1195

cyz6668 opened this issue Jul 3, 2024 · 6 comments

Comments

@cyz6668
Copy link

cyz6668 commented Jul 3, 2024

image

Cmdline: com.example.aisdkdemo
2024-07-04 09:12:12.956  2322-2322  DEBUG                   pid-2322                             A  pid: 2279, tid: 2279, name: ample.aisdkdemo  >>> com.example.aisdkdemo <<<
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #01 pc 00000000000b2ba0  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/libc++_shared.so (BuildId: ece72a2ebc3774a1be9fd21271258acd3bcdfaa7)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #02 pc 00000000000aec8c  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/libc++_shared.so (__gxx_personality_v0+348) (BuildId: ece72a2ebc3774a1be9fd21271258acd3bcdfaa7)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #03 pc 000000000042b5c0  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/librknnrt.so (BuildId: 9bd7e2cc3b9d53b043e904374f30458cb737cf85)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #04 pc 000000000042b8e4  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/librknnrt.so (BuildId: 9bd7e2cc3b9d53b043e904374f30458cb737cf85)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #05 pc 0000000000400254  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/librknnrt.so (__cxa_throw+112) (BuildId: 9bd7e2cc3b9d53b043e904374f30458cb737cf85)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #06 pc 000000000008eaec  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/libc++_shared.so (BuildId: ece72a2ebc3774a1be9fd21271258acd3bcdfaa7)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #07 pc 00000000000699d8  /data/app/~~c96yS3XIzg6HR8MU7gl-Og==/com.example.aisdkdemo-p4zKoiVCBMPiTHLWPOb-TA==/lib/arm64/libc++_shared.so (std::__ndk1::locale::use_facet(std::__ndk1::locale::id&) const+176) (BuildId: ece72a2ebc3774a1be9fd21271258acd3bcdfaa7)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #08 pc 0000000000772e80  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #09 pc 00000000008d6390  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #10 pc 00000000008d58d4  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so (torch::Library::_fallback(torch::CppFunction&&) &+456)
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #11 pc 0000000000787594  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #12 pc 000000000078a9a0  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #13 pc 00000000005f4ba4  /data/data/com.example.aisdkdemo/files/chaquopy/AssetFinder/requirements/chaquopy/lib/libtorch_cpu.so
2024-07-04 09:12:12.957  2322-2322  DEBUG                   pid-2322                             A        #18 pc 0000000000013d00  /data/data/com.example.aisdkdemo/files/chaquopy/bootstrap-native/arm64-v8a/_ctypes.so (BuildId: 4dc5359df5aa62d606a9095b0bd1ca7e3631e708)

I use rknntoolkit to deploy my models.But I found its sdk conflict with torch.I try both torch==1.4.0 1.8.1.They dont work. How can I solve the problem? When I strip out the rknntolkit part ,it works.

Chaquopy version
12.0.1

build.gradle file

<!-- Please include at least the "chaquopy", "python" and "ndk" sections. -->
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
    id 'com.chaquo.python'
}
apply plugin: 'kotlin-android'

android {
    compileSdk 30
    packagingOptions {
//        pickFirst 'libs/arm64-v8a/librknn_api.so'
        pickFirst 'libs/arm64-v8a/libxidingai.so'
        pickFirst 'libs/arm64-v8a/librknnrt.so'
    }
    defaultConfig {
        applicationId "com.example.aisdkdemo"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters  "arm64-v8a" //"armeabi-v7a",
        }
        python {
            // 指定python路径
            buildPython "C:\\Program Files\\Python310\\python.exe"// "E:\\Anaconda3\\envs\\gunicorn_flask\\python.exe"
            pip{
//                options ("--extra-index-url", "https://pypi.tuna.tsinghua.edu.cn/simple/")
//                install "opencv-python"
//                install "numpy"
//                install "wave"
//                install "scipy"
//                install "matplotlib"
//                install "opencv-contrib-python"
//                install "pillow"
                install "torch"
//                install "torchvision"
//                install "os"
            }

        }
        externalNativeBuild {
            cmake {
                cppFlags ''
                arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_PLATFORM=android-21", "-DANDROID_STL=c++_shared"
                abiFilters 'arm64-v8a'
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
            version '3.10.2'
        }
    }
    buildFeatures {
        viewBinding true
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/jniLibs']
            jni.srcDirs = ['src/cpp']
        }
    }
    ndkVersion '21.4.7075529' //16.1.4479499'
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation group: 'androidx.lifecycle', name: 'lifecycle-runtime-ktx', version: '2.3.1'
    implementation group: 'androidx.lifecycle', name: 'lifecycle-livedata-ktx', version: '2.3.1'
    implementation group: 'androidx.lifecycle', name: 'lifecycle-viewmodel-ktx', version: '2.3.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'


    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation "androidx.core:core-ktx:1.3.1"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
@mhsmith
Copy link
Member

mhsmith commented Jul 3, 2024

How exactly did you add the librknnrt.so and libc++_shared.so libraries to your app? If you did it manually, please explain where you got them from and where you copied them to. Or if you used a Gradle dependency, please give its name.

Please also post the error message as text, rather than an image. That way other people with similar problems are more likely to find it in searches, and one of them may be able to help you.

@cyz6668
Copy link
Author

cyz6668 commented Jul 3, 2024

@mhsmith I add librknnrt.so manually. But libc++_shared.so automatically.
I put librknnrt.so in app/src/main/lib/arm64-v8a and use CMakeLists.txt to compile it. This is the CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)
project("aisdkdemo")

include_directories(${CMAKE_SOURCE_DIR}/src/include)
include_directories(${CMAKE_SOURCE_DIR}/src/include/opencv)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/)

set(lib_DIR ${CMAKE_SOURCE_DIR}/src/main/libs)
file(GLOB_RECURSE WRAPPER_SRCS ${CMAKE_SOURCE_DIR}/src/main/cpp/*.cc)

add_library(
        aisdkdemo

        SHARED
        ${WRAPPER_SRCS})

add_library(xdai SHARED IMPORTED)
set_target_properties(xdai PROPERTIES IMPORTED_LOCATION ${lib_DIR}/${ANDROID_ABI}/libxidingai.so)

add_library(rknn SHARED IMPORTED)
set_target_properties(rknn PROPERTIES IMPORTED_LOCATION ${lib_DIR}/${ANDROID_ABI}/librknn_api.so)

add_library(rknnrt SHARED IMPORTED)
set_target_properties(rknnrt PROPERTIES IMPORTED_LOCATION ${lib_DIR}/${ANDROID_ABI}/librknnrt.so)



find_library(
        log-lib
        log)


target_link_libraries( 
        aisdkdemo
        xdai
        rknn
        rknnrt
        -ljnigraphics
        ${log-lib})

As for the Libc++_shared.so, I only mentioned it in build.gradle(:app).

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
}
apply plugin: 'kotlin-android'

android {
    compileSdk 30
    packagingOptions {
        pickFirst 'libs/arm64-v8a/librknn_api.so'
        pickFirst 'libs/arm64-v8a/libxidingai.so'
        pickFirst 'libs/arm64-v8a/librknnrt.so'
    }
    defaultConfig {
        applicationId "com.example.aisdkdemo"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ''
                arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_PLATFORM=android-21", "-DANDROID_STL=c++_shared"
                abiFilters 'arm64-v8a'
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
            version '3.10.2'
        }
    }
    buildFeatures {
        viewBinding true
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/jniLibs']
            jni.srcDirs = ['src/cpp']
        }
    }
    ndkVersion '16.1.4479499'
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation group: 'androidx.lifecycle', name: 'lifecycle-runtime-ktx', version: '2.3.1'
    implementation group: 'androidx.lifecycle', name: 'lifecycle-livedata-ktx', version: '2.3.1'
    implementation group: 'androidx.lifecycle', name: 'lifecycle-viewmodel-ktx', version: '2.3.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'


    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation "androidx.core:core-ktx:1.3.1"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

@mhsmith
Copy link
Member

mhsmith commented Jul 6, 2024

And where exactly did you get librknnrt.so from? Please give complete instructions, including links and version numbers.

@cyz6668
Copy link
Author

cyz6668 commented Jul 8, 2024

@mhsmith https://github.com/airockchip/rknn-toolkit2/blob/master/rknpu2/runtime/Android/librknn_api/arm64-v8a/librknnrt.so I got librknnrt.so from here. About instructions,I simply link so file in CMakeLists.txt I mentioned before.

@cyz6668
Copy link
Author

cyz6668 commented Jul 8, 2024

@mhsmith I need to infer my model on rockship RKNN3588 and develop our deployment SDK using Android. And we have python scripts developed before and we want them to deal with the results from the model deploment. So we need chaquopy to import these scripts.

@mhsmith
Copy link
Member

mhsmith commented Jul 8, 2024

Thanks for the information. I'm not very familiar with torch, and I'm not familiar with RKNN at all, so I can't be certain about what the problem is.

The only thing that stands out to me is that in the stack trace at the top of this page, libtorch_cpu, which was originally linked against Chaquopy's copy of libc++_shared, is instead calling into the libc++_shared which was added to the app by your own CMake build.

Chaquopy's copy came from NDK version 22.1.7171670, so try setting your app's ndkVersion to the same value. This means that your app will contain two identical copies of the same library, but because they both have the same SONAME, only one of them should actually be loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants