Build aar with models using tfops
Hi @LobnaMazhar 

This seems to be an error caused by Jax library.

Have you configured https://www.tensorflow.org/lite/android/lite_build#configure_workspace_and_bazelrc and set the options accordingly?

Also, can you please provide toy tflite model inorder to reproduce the issue?

Thanks. Hi @pjpratik 

Yes, I followed the configurations and I made sure it was successful by printing the content of the `.tf_configure.bazelrc` file as follows

> > more .tf_configure.bazelrc
build --action_env PYTHON_BIN_PATH="/usr/bin/python3"
build --action_env PYTHON_LIB_PATH="/usr/lib/python3/dist-packages"
build --python_path="/usr/bin/python3"
build:opt --copt=-Wno-sign-compare
build:opt --host_copt=-Wno-sign-compare
build --action_env ANDROID_NDK_HOME="/android/ndk"
build --action_env ANDROID_NDK_API_LEVEL="21"
build --action_env ANDROID_BUILD_TOOLS_VERSION="34.0.0"
build --action_env ANDROID_SDK_API_LEVEL="33"
build --action_env ANDROID_SDK_HOME="/android/sdk"
test --flaky_test_attempts=3
test --test_size_filters=small,medium
test:v1 --test_tag_filters=-benchmark-test,-no_oss,-oss_excluded,-gpu,-oss_serial
test:v1 --build_tag_filters=-benchmark-test,-no_oss,-oss_excluded,-gpu
test:v2 --test_tag_filters=-benchmark-test,-no_oss,-oss_excluded,-gpu,-oss_serial,-v1only
test:v2 --build_tag_filters=-benchmark-test,-no_oss,-oss_excluded,-gpu,-v1only

Are there any extra steps that I should do to install the Jax library in separate?

Regarding the used tflite model, unfortunately I won't be able to share it because it is confidential to my job. I tried another converted version of the model, and I ended up with a different error.

> + bazel build -c opt --cxxopt=--std=c++17 --fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a --define=android_dexmerger_tool=d8_dexmerger --define=android_incremental_dexing_tool=d8_dexbuilder --define=xnn_enable_arm_fp16=false --host_crosstool_top=@bazel_tools//tools/cpp:toolchain //tmp:tensorflow-lite
> INFO: Options provided by the client:
>   Inherited 'common' options: --isatty=1 --terminal_columns=179
> INFO: Reading rc options for 'build' from /tensorflow_src/.bazelrc:
>   Inherited 'common' options: --experimental_repo_remote_exec
> INFO: Reading rc options for 'build' from /etc/bazel.bazelrc:
>   'build' options: --action_env=DOCKER_CACHEBUSTER=1687840109499695775 --host_action_env=DOCKER_HOST_CACHEBUSTER=1687840109559438170
> INFO: Reading rc options for 'build' from /tensorflow_src/.bazelrc:
>   'build' options: --define framework_shared_object=true --define tsl_protobuf_header_only=true --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=standalone -c opt --announce_rc --define=grpc_no_ares=true --noincompatible_remove_legacy_whole_archive --enable_platform_specific_config --define=with_xla_support=true --config=short_logs --config=v2 --define=no_aws_support=true --define=no_hdfs_support=true --experimental_cc_shared_library --experimental_link_static_libraries_once=false --incompatible_enforce_config_setting_visibility
> INFO: Reading rc options for 'build' from /tensorflow_src/.tf_configure.bazelrc:
>   'build' options: --action_env PYTHON_BIN_PATH=/usr/bin/python3 --action_env PYTHON_LIB_PATH=/usr/lib/python3/dist-packages --python_path=/usr/bin/python3 --action_env ANDROID_NDK_HOME=/android/ndk --action_env ANDROID_NDK_API_LEVEL=21 --action_env ANDROID_BUILD_TOOLS_VERSION=34.0.0 --action_env ANDROID_SDK_API_LEVEL=33 --action_env ANDROID_SDK_HOME=/android/sdk
> INFO: Reading rc options for 'build' from /tensorflow_src/.bazelrc:
>   'build' options: --deleted_packages=tensorflow/compiler/mlir/tfrt,tensorflow/compiler/mlir/tfrt/benchmarks,tensorflow/compiler/mlir/tfrt/jit/python_binding,tensorflow/compiler/mlir/tfrt/jit/transforms,tensorflow/compiler/mlir/tfrt/python_tests,tensorflow/compiler/mlir/tfrt/tests,tensorflow/compiler/mlir/tfrt/tests/ir,tensorflow/compiler/mlir/tfrt/tests/analysis,tensorflow/compiler/mlir/tfrt/tests/jit,tensorflow/compiler/mlir/tfrt/tests/lhlo_to_tfrt,tensorflow/compiler/mlir/tfrt/tests/lhlo_to_jitrt,tensorflow/compiler/mlir/tfrt/tests/tf_to_corert,tensorflow/compiler/mlir/tfrt/tests/tf_to_tfrt_data,tensorflow/compiler/mlir/tfrt/tests/saved_model,tensorflow/compiler/mlir/tfrt/transforms/lhlo_gpu_to_tfrt_gpu,tensorflow/core/runtime_fallback,tensorflow/core/runtime_fallback/conversion,tensorflow/core/runtime_fallback/kernel,tensorflow/core/runtime_fallback/opdefs,tensorflow/core/runtime_fallback/runtime,tensorflow/core/runtime_fallback/util,tensorflow/core/tfrt/eager,tensorflow/core/tfrt/eager/backends/cpu,tensorflow/core/tfrt/eager/backends/gpu,tensorflow/core/tfrt/eager/core_runtime,tensorflow/core/tfrt/eager/cpp_tests/core_runtime,tensorflow/core/tfrt/gpu,tensorflow/core/tfrt/run_handler_thread_pool,tensorflow/core/tfrt/runtime,tensorflow/core/tfrt/saved_model,tensorflow/core/tfrt/graph_executor,tensorflow/core/tfrt/saved_model/tests,tensorflow/core/tfrt/tpu,tensorflow/core/tfrt/utils,tensorflow/core/tfrt/utils/debug
> INFO: Found applicable config definition build:short_logs in file /tensorflow_src/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
> INFO: Found applicable config definition build:v2 in file /tensorflow_src/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
> INFO: Found applicable config definition build:linux in file /tensorflow_src/.bazelrc: --define=build_with_onednn_v2=true --host_copt=-w --copt=-Wno-all --copt=-Wno-extra --copt=-Wno-deprecated --copt=-Wno-deprecated-declarations --copt=-Wno-ignored-attributes --copt=-Wno-array-bounds --copt=-Wunused-result --copt=-Werror=unused-result --copt=-Wswitch --copt=-Werror=switch --copt=-Wno-error=unused-but-set-variable --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib --define=INCLUDEDIR=$(PREFIX)/include --define=PROTOBUF_INCLUDE_PATH=$(PREFIX)/include --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --config=dynamic_kernels --experimental_guard_against_concurrent_changes
> INFO: Found applicable config definition build:dynamic_kernels in file /tensorflow_src/.bazelrc: --define=dynamic_loaded_kernels=true --copt=-DAUTOLOAD_DYNAMIC_KERNELS
> INFO: Analyzed target //tmp:tensorflow-lite (1 packages loaded, 527 targets configured).
> INFO: Found 1 target...
> **ERROR: /tensorflow_src/tmp/BUILD:4:30: gen_selected_ops failed: (Segmentation fault): generate_op_registrations failed: error executing command bazel-out/k8-opt-exec-50AE0418/bin/tensorflow/lite/tools/generate_op_registrations '--namespace=' ... (remaining 3 arguments skipped)
> Target //tmp:tensorflow-lite failed to build
> Use --verbose_failures to see the command lines of failed build steps.
> INFO: Elapsed time: 1.788s, Critical Path: 0.90s
> INFO: 2 processes: 2 internal.
> FAILED: Build did NOT complete successfully**
> 

**Question**  Should I specify the used tf-ops while running the command as follows?
```
sh tensorflow/lite/tools/build_aar.sh \
  --input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
  --target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
  --tflite_custom_ops_srcs=/e/f/file1.cc,/g/h/file2.h \
  --tflite_custom_ops_deps=dep1,dep2
```
As I'm using the simpler command
```
bash tensorflow/lite/tools/build_aar.sh \
  --input_models=model1,model2 \
  --target_archs=x86,x86_64,arm64-v8a,armeabi-v7a`
```
as specified [here](https://www.tensorflow.org/lite/android/lite_build#configure_workspace_and_bazelrc) Hi @LobnaMazhar, the simpler command should suffice as the script should look into the input models for the ops being used. Are you using custom ops? your other command seems to suggest so, perhaps can you or your team produce a simpler toy model with just the custom ops so that we can reproduce without violating confidentiality? Also this will help us identify if those are the cause of the issue. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61101">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61101">No</a>

I am trying to build a small aar for specific ML models where one of them is using tfops, I don't want to use the nighty version due to its huge size. 

I am following this [documentation](https://www.tensorflow.org/lite/android/lite_build#configure_workspace_and_bazelrc), 
I downloaded the provided Dockerfile and I updated the following fields

- commandlinetools to the latest version 9477386_latest
- ANDROID_API_LEVEL to 33
- ANDROID_BUILD_TOOLS_VERSION to 34.0.0. 

I started the docker container, configured the workspace, cloned tensorflow from this [repository](https://github.com/tensorflow/tensorflow.git) and checked out to branch r2.13, downloaded bazel 
```
 > bazel version
Bazelisk version: v1.17.0
Starting local Bazel server and connecting to it...
Build label: 5.3.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Aug 23 00:45:53 2022 (1661215553)
Build timestamp: 1661215553
Build timestamp as int: 1661215553
```

------------------------

### System information

-   **OS Platform and Distribution -> Linux**:
-   **TensorFlow downloaded from [source](https://github.com/tensorflow/tensorflow.git)**: r2.13
-   **Python version**: 3.11
-   **Bazel version**: 5.3.0

------------------------

When I run the bash command ` bash tensorflow/lite/tools/build_aar.sh   --input_models=model1.tflite,model2.tflite   --target_archs=x86,x86_64,arm64-v8a,armeabi-v7a` with my models it fails with the following error log

...
INFO: Analyzed target //tmp:tensorflow-lite-select-tf-ops (461 packages loaded, 50228 targets configured).
INFO: Found 1 target...
ERROR: /tensorflow_src/tensorflow/BUILD:1646:19: Action tensorflow/_api/v2/v2.py [for host] failed: (Exit 1): bash failed: error executing command /bin/bash -c ... (remaining 1 argument skipped)
2023-06-27 22:43:43.289759: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE3, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File "/root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py", line 856, in <module>
    main()
  File "/root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py", line 830, in main
    importlib.import_module(package)
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/lite/python/lite.py", line 34, in <module>
    from tensorflow.lite.python.convert import convert_graphdef as _convert_graphdef
  File "/root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/lite/python/convert.py", line 29, in <module>
    from tensorflow.lite.python import util
  File "/root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/lite/python/util.py", line 52, in <module>
    from jax import xla_computation as _xla_computation
  File "/usr/local/lib/python3.11/dist-packages/jax/__init__.py", line 35, in <module>
    from jax import config as _config_module
  File "/usr/local/lib/python3.11/dist-packages/jax/config.py", line 17, in <module>
    from jax._src.config import config  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/jax/_src/config.py", line 27, in <module>
    from jax._src import lib
  File "/usr/local/lib/python3.11/dist-packages/jax/_src/lib/__init__.py", line 85, in <module>
    cpu_feature_guard.check_cpu_features()
RuntimeError: This version of jaxlib was built using AVX instructions, which your CPU and/or operating system do not support. You may be able work around this issue by building jaxlib from source.
Target //tmp:tensorflow-lite-select-tf-ops failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /tensorflow_src/tensorflow/python/tools/BUILD:284:10 Middleman _middlemen/tensorflow_Spython_Stools_Sprint_Uselective_Uregistration_Uheader-runfiles failed: (Exit 1): bash failed: error executing command /bin/bash -c ... (remaining 1 argument skipped)
INFO: Elapsed time: 252.099s, Critical Path: 49.42s
INFO: 952 processes: 16 internal, 936 local.
FAILED: Build did NOT complete successfully