google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://ai.google.dev/edge/mediapipe
Apache License 2.0
26.83k stars 5.09k forks source link

how to build com.google.mediapipe:tasks-genai for android..need steps please #5633

Open BalajiPolisetty2207 opened 2 days ago

BalajiPolisetty2207 commented 2 days ago

OS Platform and Distribution

Android

Compiler version

No response

Programming Language and version

Java

Installed using virtualenv? pip? Conda?(if python)

No response

MediaPipe version

No response

Bazel version

No response

XCode and Tulsi versions (if iOS)

No response

Android SDK and NDK versions (if android)

No response

Android AAR (if android)

None

OpenCV version (if running on desktop)

No response

Describe the problem

need steps to build com.google.mediapipe:tasks-genai for android

Complete Logs

need steps to build com.google.mediapipe:tasks-genai for android
calbot commented 1 day ago

Build the Docker image. Run it on an intel computer...

docker build --tag=mediapipe .

If you want to make changes to the mediapipe sources mount the mediapipe directory

docker run -it --name mediapipe_local_mnt -v "$(pwd):/mediapipe" mediapipe:latest bash
bazel build -c opt --config=android_arm64 //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar

If you need to connect into your docker container again later... docker start -ai mediapipe_local_mnt

You might need to install some dependencies INSIDE the docker container like zip which should be something like apt-get install -y zipor add it to the list in the Dockerfile

Build result goes in bazel-bin/mediapipe/tasks/java/com/google/mediapipe/tasks/genai/tasks_genai.aar

So cp it to your mediapipe folder to get access outside the docker container

If you include the aar in your lib directory in android you may need to manually add the dependencies for the tasks_genai.aar You can see the existing dependencies for the genai lib by running something like this in your android folder (adjust name for your androidApp) ./gradlew :androidApp:dependencies --configuration debugCompileClasspath

THIS COULD BE WRONG. I'M NOT A DEV ON mediapipe team or a bazel expert.

BalajiPolisetty2207 commented 1 day ago

when i am running this command bazel build -c opt --config=android_arm64 //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar in intel windows pc i am getting zlib error .

root@5486d71acc20:/mediapipe# bazel build -c opt --config=android_arm64 //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar Starting local Bazel server and connecting to it... DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_absl' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_benchmark' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'flatbuffers' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11_bazel' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'XNNPACK' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pthreadpool' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'cpuinfo' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_googlesource_code_re2' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_protobuf' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_googletest' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_github_gflags_gflags' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'zlib' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'rules_android_ndk' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_rules_apple' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_rules_swift' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_apple_support' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'xctestrunner' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11_protobuf' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_github_glog_glog' because it already exists. WARNING: The major revision of the Android NDK referenced by android_ndk_repository rule 'androidndk' is 22. The major revisions supported by Bazel are [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]. Bazel will attempt to treat the NDK as if it was r21. This may cause compilation and linkage problems. Please download a supported NDK version. DEBUG: Rule 'rules_cc' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "691a29db9c336349e48e04c5c2f4873f2890af5cbfa6e51f4de87fefe6169294" DEBUG: Repository rules_cc instantiated at: /mediapipe/WORKSPACE:52:13: in Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl:372:31: in INFO: Repository zlib instantiated at: /mediapipe/WORKSPACE:147:13: in Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl:372:31: in WARNING: Download from http://zlib.net/fossils/zlib-1.2.13.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.ContentLengthMismatchException Bytes read 1464677 but wanted 1497445 ERROR: An error occurred during the fetch of repository 'zlib': Traceback (most recent call last): File "/root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl", line 132, column 45, in _http_archive_impl download_info = ctx.download_and_extract( Error in download_and_extract: java.io.IOException: Error downloading [http://zlib.net/fossils/zlib-1.2.13.tar.gz] to /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/zlib/temp15394260953468651184/zlib-1.2.13.tar.gz: Bytes read 1464677 but wanted 1497445 ERROR: /mediapipe/WORKSPACE:147:13: fetching http_archive rule //external:zlib: Traceback (most recent call last): File "/root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl", line 132, column 45, in _http_archive_impl download_info = ctx.download_and_extract( Error in download_and_extract: java.io.IOException: Error downloading [http://zlib.net/fossils/zlib-1.2.13.tar.gz] to /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/zlib/temp15394260953468651184/zlib-1.2.13.tar.gz: Bytes read 1464677 but wanted 1497445 INFO: Repository android_opencv instantiated at: /mediapipe/WORKSPACE:448:13: in Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl:372:31: in ERROR: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/com_google_protobuf/BUILD:208:11: @com_google_protobuf//:protobuf depends on @zlib//:zlib in repository @zlib which failed to fetch. no such package '@zlib//': java.io.IOException: Error downloading [http://zlib.net/fossils/zlib-1.2.13.tar.gz] to /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/zlib/temp15394260953468651184/zlib-1.2.13.tar.gz: Bytes read 1464677 but wanted 1497445 ERROR: Analysis of target '//mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar' failed; build aborted: INFO: Elapsed time: 38.240s INFO: 0 processes. FAILED: Build did NOT complete successfully (217 packages loaded, 12736 targets configured) Fetching http://zlib.net/fossils/zlib-1.2.13.tar.gz; 1.4 MiB (1,464,640B) Fetching https://github.com/opencv/opencv/releases/download/4.10.0/opencv-4.10.0-android-sdk.zip; 43.3 MiB (45,381,432B)

@calbot @kuaashish please help how i can proceed.

calbot commented 1 day ago

I'm not really sure. I don't remember having this problem. Another thing is inside your docker container run if you haven't... bash setup_android_sdk_and_ndk.sh and maybe you need to run in your docker container apt-get install zip

Also, see these docs... https://github.com/google-ai-edge/mediapipe/blob/master/docs/getting_started/android.md https://github.com/google-ai-edge/mediapipe/blob/master/docs/getting_started/android_archive_library.md https://github.com/google-ai-edge/mediapipe/blob/master/docs/getting_started/android_solutions.md

The error is a little strange. It's sounds like it's getting the wrong version of zlib or maybe just a network or server error (java.io.IOException). Maybe try again. Try clean bazel. I forget the command for that.

BalajiPolisetty2207 commented 1 day ago

Thank you @calbot i am able to proceed now and now i am getting this error

root@a343c9897bb2:/mediapipe# bazel build -c opt --config=android_arm64 //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar Starting local Bazel server and connecting to it... DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_absl' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_benchmark' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'flatbuffers' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11_bazel' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'XNNPACK' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pthreadpool' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'cpuinfo' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_googlesource_code_re2' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_protobuf' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_google_googletest' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_github_gflags_gflags' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'zlib' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'rules_android_ndk' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_rules_apple' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_rules_swift' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'build_bazel_apple_support' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'xctestrunner' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'pybind11_protobuf' because it already exists. DEBUG: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/org_tensorflow/third_party/repo.bzl:132:14: Warning: skipping import of repository 'com_github_glog_glog' because it already exists. DEBUG: Rule 'rules_cc' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "691a29db9c336349e48e04c5c2f4873f2890af5cbfa6e51f4de87fefe6169294" DEBUG: Repository rules_cc instantiated at: /mediapipe/WORKSPACE:41:13: in Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl:372:31: in WARNING: /mediapipe/mediapipe/framework/tool/BUILD:204:24: in cc_library rule //mediapipe/framework/tool:field_data_cc_proto: target '//mediapipe/framework/tool:field_data_cc_proto' depends on deprecated target '@com_google_protobuf//:cc_wkt_protos': Only for backward compatibility. Do not use. WARNING: /mediapipe/mediapipe/framework/BUILD:69:24: in cc_library rule //mediapipe/framework:calculator_cc_proto: target '//mediapipe/framework:calculator_cc_proto' depends on deprecated target '@com_google_protobuf//:cc_wkt_protos': Only for backward compatibility. Do not use. DEBUG: Rule 'android_opencv' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "464d55abe64b53af3aeb73197cf4620535abd909916d06e6a8b7c32712013966" DEBUG: Repository android_opencv instantiated at: /mediapipe/WORKSPACE:437:13: in Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/external/bazel_tools/tools/build_defs/repo/http.bzl:372:31: in WARNING: /mediapipe/mediapipe/tasks/java/com/google/mediapipe/tasks/genai/BUILD:37:26: in srcs attribute of android_library rule //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai_android_lib: please do not import '//mediapipe/tasks/java/com/google/mediapipe/tasks/core:LlmTaskRunner.java' directly. You should either move the file to this package or depend on an appropriate rule there. Since this rule was created by the macro 'mediapipe_tasks_genai_aar', the error might have been caused by the macro implementation INFO: Analyzed target //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar (221 packages loaded, 13958 targets configured). INFO: Found 1 target... WARNING: failed to create one or more convenience symlinks for prefix 'bazel-': cannot create symbolic link bazel-mediapipe -> /root/.cache/bazel/_bazel_root/4884d566396e9b67b62185751879ad14/execroot/mediapipe: /mediapipe/bazel-mediapipe (File exists) INFO: From Compiling src/google/protobuf/message_lite.cc [for tool]: In file included from /usr/include/string.h:535, from external/com_google_protobuf/src/google/protobuf/stubs/port.h:39, from external/com_google_protobuf/src/google/protobuf/stubs/common.h:48, from external/com_google_protobuf/src/google/protobuf/message_lite.h:45, from external/com_google_protobuf/src/google/protobuf/message_lite.cc:36: In function 'void memcpy(void, const void, size_t)', inlined from 'uint8_t google::protobuf::io::EpsCopyOutputStream::WriteRaw(const void, int, uint8_t)' at external/com_google_protobuf/src/google/protobuf/io/coded_stream.h:706:16, inlined from 'virtual uint8_t google::protobuf::internal::ImplicitWeakMessage::_InternalSerialize(uint8_t, google::protobuf::io::EpsCopyOutputStream) const' at external/com_google_protobuf/src/google/protobuf/implicit_weak_message.h:84:28, inlined from 'bool google::protobuf::MessageLite::SerializePartialToZeroCopyStream(google::protobuf::io::ZeroCopyOutputStream) const' at external/com_google_protobuf/src/google/protobuf/message_lite.cc:412:30: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:29:33: warning: 'void __builtin___memcpy_chk(void, const void*, long unsigned int, long unsigned int)' specified size between 18446744071562067968 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=] 29 | return builtin___memcpy_chk (dest, src, len, | ~~~~~^~~~~~~~ 30 | __glibc_objsize0 (__dest)); | ~~~~~~ INFO: From Executing genrule @org_tensorflow//tensorflow/lite/acceleration/configuration:configuration_schema: When you use --proto, that you should check for conformity yourself, using the existing --conform INFO: From Generating flatbuffer files for metadata_schema_cc_srcs: //mediapipe/tasks/metadata:metadata_schema_cc_srcs:

warning: /mediapipe/mediapipe/tasks/metadata/metadata_schema.fbs:486: 21: warning: field names should be lowercase snake_case, got: sentencePiece_model

ERROR: /mediapipe/mediapipe/tasks/cc/metadata/BUILD:14:11: Compiling mediapipe/tasks/cc/metadata/metadata_extractor.cc failed: (Exit 1): clang failed: error executing command (from target //mediapipe/tasks/cc/metadata:metadata_extractor) external/androidndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -no-canonical-prefixes '--target=aarch64-linux-android21' -fdiagnostics-color -Wa,--noexecstack '-std=gnu++17' -Wc++2a-extensions ... (remaining 133 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging In file included from mediapipe/tasks/cc/metadata/metadata_extractor.cc:30: bazel-out/android-arm64-v8a-opt/bin/mediapipe/tasks/cc/metadata/metadata_parser.h:24:50: error: expected expression inline constexpr char kMetadataParserVersion[] = "1.5.0 ^ bazel-out/android-arm64-v8a-opt/bin/mediapipe/tasks/cc/metadata/metadata_parser.h:25:3: error: expected ';' after top level declarator "; ^ ; 2 errors generated. Target //mediapipe/tasks/java/com/google/mediapipe/tasks/genai:tasks_genai.aar failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 197.254s, Critical Path: 77.73s INFO: 1965 processes: 418 internal, 1547 linux-sandbox. FAILED: Build did NOT complete successfully .

Any insights how we can resolve this

calbot commented 1 day ago

I don't know. Sorry

BalajiPolisetty2207 commented 1 day ago

@kuaashish please help

BalajiPolisetty2207 commented 18 hours ago

@calbot @kuaashish i am using ndk 21will this cause any issue while generting the code ?which version i need to use ?

calbot commented 16 hours ago

I have 21 as well in my WORKSPACE file and it works for me. Are you using docker?