taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.41k stars 2.27k forks source link

Simple noob example for AOT android #8508

Closed ko-work closed 5 months ago

ko-work commented 5 months ago

Hi! Thanks for your work.

I would like to use Taichi with android. Based on the tests I am trying to create a very simple initial example, but I am getting all zeros. https://github.com/taichi-dev/taichi/blob/52b24f3e09c093610b1ecf69b5e33cbc66b7bd6d/tests/cpp/aot/python_scripts/shared_array_aot_test_.py and https://github.com/taichi-dev/taichi/blob/52b24f3e09c093610b1ecf69b5e33cbc66b7bd6d/c_api/tests/c_api_aot_test.cpp

ubuntu 22.04 ndk version 25.1.8937393 taichi commit 52b24f3e09c093610b1ecf69b5e33cbc66b7bd6d

I have built android taichi with the scripts/build-taichi-android.sh (-DTI_WITH_VULKAN=ON).

Here is the code:

app.py

import numpy as np
import taichi as ti

ti.init(arch=ti.vulkan)
v_arr = np.zeros(100).astype(np.float32)

@ti.kernel
def update_values(v: ti.types.ndarray(ndim=1)):
    for i in range(100):
        v[i] = 15

m = ti.aot.Module()
m.add_kernel(update_values, template_args={"v": v_arr})
m.archive("update_values.tcm")

app.cpp

#include <taichi/cpp/taichi.hpp>

int main(int argc, const char** argv) {
  uint32_t N = 100;
  ti::Runtime runtime(TI_ARCH_VULKAN);
  ti::AotModule aot_mod = runtime.load_aot_module("update_values.tcm");
  ti::Kernel k_run = aot_mod.get_kernel("run");

  ti::NdArray<float> v_array =
      runtime.allocate_ndarray<float>({N}, {}, true);

  k_run.push_arg(v_array);
  k_run.launch();
  runtime.wait();

  float *res = reinterpret_cast<float *>(v_array.map());
  for (int i = 0; i < N; ++i) {
    std::cout << res[i];
  }
  v_array.unmap();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)

set(APP_NAME update_val)
project(${APP_NAME} LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(${APP_NAME} app.cpp)

include_directories(${PROJECT_SOURCE_DIR}/third_party/taichi/include)
add_library(taichi SHARED IMPORTED)
set_target_properties(taichi PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/third_party/taichi/lib/libtaichi_c_api.so)

target_link_libraries(${APP_NAME} taichi)

the command:

the log of running python and cmake:

[Taichi] version 1.7.0, llvm 15.0.4, commit 2fd24490, linux, python 3.10.12
[Taichi] Starting on arch=x64
-- The C compiler identification is Clang 14.0.6
-- The CXX compiler identification is Clang 14.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /path/to/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /path/to/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /path/to/taichi_example/test_aot_issue/build
[ 50%] Building CXX object CMakeFiles/update_val.dir/app.cpp.o
[100%] Linking CXX executable update_val
[100%] Built target update_val

On device in the folder there is the so and the executable.

The output:

LD_LIBRARY_PATH=. ./update_val
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

The result is the same with cpu gles and vulkan (changed in both python and cpp)

ko-work commented 5 months ago

I forgot to upload tcm archive

ko-work commented 5 months ago

And I was using inconsistent architectures which caused bad keys when reading the module did not see at first in the log, also module names were wrong.

here is an updated example just in case someone would came across this test_aot_issue.zip