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 open OpenCL library on this device - undefined symbol: clGetCommandBufferInfoKHR #886

Open
AIWintermuteAI opened this issue Feb 14, 2025 · 4 comments
Assignees
Labels
status:awaiting user response awaiting user response status:need more data need more data type:support Support Issues

Comments

@AIWintermuteAI
Copy link

When using TFLite shared lib with GPU delegate (compiled from the tag 2.16.1 by following https://ai.google.dev/edge/litert/build/cmake#cross-compilation and applying the patch from this issue) in Android studio, I get

2025-02-14 16:00:03.179  7456-7456  xample.test_cpp         com.example.test_cpp                 W  Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
2025-02-14 16:00:03.188  7456-7456  CompatChangeReporter    com.example.test_cpp                 D  Compat change id reported: 309578419; UID 10208; state: ENABLED
2025-02-14 16:00:03.204  7456-7456  xample.test_cpp         com.example.test_cpp                 W  Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
2025-02-14 16:00:03.221  7456-7456  tflite                  com.example.test_cpp                 I  Initialized TensorFlow Lite runtime.
2025-02-14 16:00:03.222  7456-7456  tflite                  com.example.test_cpp                 I  Created TensorFlow Lite delegate for GPU.
2025-02-14 16:00:03.231  7456-7456  tflite                  com.example.test_cpp                 I  Replacing 46 out of 46 node(s) with delegate (TfLiteGpuDelegateV2) node, yielding 1 partitions for the whole graph.
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  Can not open OpenCL library on this device - undefined symbol: clGetCommandBufferInfoKHR
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  Falling back to OpenGL
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  TfLiteGpuDelegate Init: OpenGL-based API disabled
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 I  Created 0 GPU delegate kernels.
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  TfLiteGpuDelegate Prepare: delegate is not initialized
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  Node number 46 (TfLiteGpuDelegateV2) failed to prepare.
2025-02-14 16:00:03.240  7456-7456  tflite                  com.example.test_cpp                 E  Restored original execution plan after delegate application failure.
2025-02-14 16:00:03.240  7456-7456  MAIN                    com.example.test_cpp                 E  Inference error code -3
2025-02-14 16:00:03.303  7456-7456  HWUI                    com.example.test_cpp                 W  Unknown dataspace 0
2025-02-14 16:00:03.361  7456-7471  EGL_emulation           com.example.test_cpp                 I  Opening libGLESv1_CM_emulation.so
2025-02-14 16:00:03.361  7456-7471  EGL_emulation           com.example.test_cpp                 I  Opening libGLESv2_emulation.so
2025-02-14 16:00:03.379  7456-7471  HWUI                    com.example.test_cpp                 W  Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2025-02-14 16:00:03.379  7456-7471  HWUI                    com.example.test_cpp                 W  Failed to initialize 101010-2 format, error = EGL_SUCCESS

That happens both when running the app in Emulator (Android 15 Medium Phone API 35) on Mac M1, and also when running on the physcial phone connected with wireless debugging (Samsung A34 running Android 13 Tiramisu). The error appears here (I'm around 95% sure this is the place)

        auto* delegate = TfLiteGpuDelegateV2Create(/*default options=*/nullptr);
        if (new_state->interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
            printf("ERROR: ModifyGraphWithDelegate failed\n");
        }

I have already tried solution offered in most of the places, which is adding libOpenCL.so to Android Manifest...

Image

Unfortunately, the error still persists.

@gaikwadrahul8 gaikwadrahul8 self-assigned this Feb 18, 2025
@gaikwadrahul8 gaikwadrahul8 added the type:support Support Issues label Feb 18, 2025
@gaikwadrahul8
Copy link
Contributor

Hi, @pkgoogle
Please take a look into this issue. Thank you.

@AIWintermuteAI
Copy link
Author

@pkgoogle the issue is not solved, let me know if I need to provide you with more information!

@pkgoogle
Copy link

Hi @AIWintermuteAI, can you please be more specific about your tfl shared_lib building steps? Preferably your exact building steps

I'm assuming one of the steps includes this one.

cmake -DCMAKE_TOOLCHAIN_FILE=<NDK path>/build/cmake/android.toolchain.cmake \
  -DANDROID_ABI=arm64-v8a ../tensorflow_src/tensorflow/lite

Which NDK version did you use? any potential deviations from any other steps?

@pkgoogle pkgoogle added status:need more data need more data status:awaiting user response awaiting user response labels Feb 24, 2025
@AIWintermuteAI
Copy link
Author

@pkgoogle yes, in fact, since I build in container, I can provide the full Dockerfile and build commands

FROM --platform=linux/amd64 ubuntu:20.04

WORKDIR /app
ARG DEBIAN_FRONTEND=noninteractive
ENV TF_VERSION=2.16.1
ENV CMAKE_VERSION=3.22.1

# APT packages
RUN apt update && apt install -y zip \
                                 wget \
                                 curl \
                                 xz-utils \
                                 lsb-release \
                                 software-properties-common \
                                 apt-transport-https \
                                 ca-certificates \
                                 make \
                                 git \
                                 unzip \
                                 python3-pip \
                                 python3-numpy \
                                 libtool-bin \
                                 openjdk-17-jdk

# # Install recent CMake
RUN mkdir -p /opt/cmake && \
    cd /opt/cmake && \
    wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-Linux-x86_64.sh && \
    sh cmake-${CMAKE_VERSION}-Linux-x86_64.sh --prefix=/opt/cmake --skip-license && \
    ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake

# # Install LLVM
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \
    wget https://apt.llvm.org/llvm.sh && \
    chmod +x llvm.sh && \
    ./llvm.sh 17

RUN ln -s $(which clang-17) /usr/bin/clang && \
    ln -s $(which clang++-17) /usr/bin/clang++

# Install Android command line tools
RUN wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip && \
    unzip commandlinetools-linux-11076708_latest.zip -d /opt/android-sdk && \
    rm commandlinetools-linux-11076708_latest.zip

ENV ANDROID_VERSION=35
ARG NDK_VERSION=27.2.12479018
# 25.2.9519653

ENV ANDROID_SDK_ROOT=/opt/android-sdk
ENV PATH=$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:$PATH

RUN mkdir -p $ANDROID_SDK_ROOT/licenses && \
    echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > $ANDROID_SDK_ROOT/licenses/android-sdk-license && \
    echo "84831b9409646a918e30573bab4c9c91346d8abd" > $ANDROID_SDK_ROOT/licenses/android-sdk-preview-license

RUN $ANDROID_SDK_ROOT/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_SDK_ROOT --update

# Install Android SDK
RUN $ANDROID_SDK_ROOT/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_SDK_ROOT --install \
    "cmake;${CMAKE_VERSION}" \
    "ndk;${NDK_VERSION}" \
    "build-tools;35.0.1" \
    "platforms;android-${ANDROID_VERSION}" \
    "platform-tools"

# Download TF
RUN wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/refs/tags/v${TF_VERSION}.zip

RUN unzip tensorflow.zip && \
    rm tensorflow.zip

RUN BAZEL_VERSION=$(head -n 1 tensorflow-${TF_VERSION}/.bazelversion) && \
BAZEL_INSTALLER="https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" && \
wget -O bazel-installer.sh $BAZEL_INSTALLER && \
chmod +x bazel-installer.sh && \
./bazel-installer.sh

# patch TF for for DFLATBUFFERS_LOCALE_INDEPENDENT=0
RUN sed -i '37i\
set(FLATBUFFERS_LOCALE_INDEPENDENT 0)' tensorflow-${TF_VERSION}/tensorflow/lite/tools/cmake/modules/flatbuffers.cmake && \
    sed -i '38i\
include(CheckCXXSymbolExists)' tensorflow-${TF_VERSION}/tensorflow/lite/tools/cmake/modules/flatbuffers.cmake && \
    sed -i '39i\
add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)' tensorflow-${TF_VERSION}/tensorflow/lite/tools/cmake/modules/flatbuffers.cmake

RUN ln -s $(which python3) /usr/bin/python

It is a shared container, so there are a few things that are not necessary for Android CMake build, but I left them for completeness.
The commands:

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
TF_VERSION='2.16.1'
ANDROID_PLATFORM=28

cd $SCRIPTPATH/tensorflow-${TF_VERSION}
mkdir -p tflite_build
cd tflite_build
# https://github.com/google/flatbuffers/issues/7587
# for DFLATBUFFERS_LOCALE_INDEPENDENT=0
cmake \
    -DCMAKE_C_FLAGS="${FLAGS}" \
    -DCMAKE_CXX_FLAGS="${FLAGS}" \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DFLATBUFFERS_LOCALE_INDEPENDENT=0 \
    -DCMAKE_TOOLCHAIN_FILE=$ANDROID_SDK_ROOT/ndk/27.2.12479018/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=arm64-v8a \
    -DANDROID_PLATFORM=android-${ANDROID_PLATFORM} \
    ../tensorflow/lite/

cd $SCRIPTPATH/tensorflow-${TF_VERSION}/tflite_build
cmake --build . -j $((`nproc` - 1))

FLAGS is not set for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:awaiting user response awaiting user response status:need more data need more data type:support Support Issues
Projects
None yet
Development

No branches or pull requests

3 participants