From 44d6c8c37f706a9d85c814340ee3ac0c7f7ed7b4 Mon Sep 17 00:00:00 2001 From: Johnson Zhang Date: Fri, 20 Jun 2025 12:09:52 +0800 Subject: [PATCH 1/3] [BACKEND] Update Arm China NPU "Zhouyi" Backend Support --- third_party/aipu/CMakeLists.txt | 1 + third_party/aipu/backend/aipu_torch_dev.cpp | 44 +++- third_party/aipu/backend/analysis/__init__.py | 6 +- .../backend/analysis/determine_vfactor.py | 16 +- .../analysis/get_linalg_generic_size.py | 25 ++ third_party/aipu/backend/codegen.py | 244 +++++++++++++----- third_party/aipu/backend/compiler.py | 24 +- third_party/aipu/backend/driver.py | 12 +- .../aipu/backend/transform/__init__.py | 3 +- .../aipu/backend/transform/binding_tid.py | 35 ++- .../transform/canonical_const_dtype.py | 48 ++++ third_party/aipu/include/Passes/Passes.td | 20 ++ third_party/aipu/language/aipu/libdevice.py | 47 +--- third_party/aipu/lib/Passes/CMakeLists.txt | 1 + .../Passes/SCFLoopBufferizationPreprocess.cpp | 56 ++++ third_party/aipu/lib/main.cpp | 5 +- .../aipu/python/test/test_codegen_dma.py | 103 +++++--- .../aipu/test/convert_bool_arg_to_i8.mlir | 9 + third_party/aipu/test/lit.cfg | 4 + .../one-shot-bufferize-preprocessing.mlir | 26 ++ third_party/aipu/tools/CMakeLists.txt | 1 + .../aipu/tools/aipu-opt/CMakeLists.txt | 18 ++ third_party/aipu/tools/aipu-opt/aipu-opt.cpp | 15 ++ 23 files changed, 611 insertions(+), 152 deletions(-) create mode 100644 third_party/aipu/backend/analysis/get_linalg_generic_size.py create mode 100644 third_party/aipu/backend/transform/canonical_const_dtype.py create mode 100644 third_party/aipu/lib/Passes/SCFLoopBufferizationPreprocess.cpp create mode 100644 third_party/aipu/test/convert_bool_arg_to_i8.mlir create mode 100644 third_party/aipu/test/lit.cfg create mode 100644 third_party/aipu/test/one-shot-bufferize-preprocessing.mlir create mode 100644 third_party/aipu/tools/CMakeLists.txt create mode 100644 third_party/aipu/tools/aipu-opt/CMakeLists.txt create mode 100644 third_party/aipu/tools/aipu-opt/aipu-opt.cpp diff --git a/third_party/aipu/CMakeLists.txt b/third_party/aipu/CMakeLists.txt index 2ab8d44cb..691d3e393 100644 --- a/third_party/aipu/CMakeLists.txt +++ b/third_party/aipu/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(include) add_subdirectory(lib) +add_subdirectory(tools) add_triton_plugin(TritonAIPU ${CMAKE_CURRENT_SOURCE_DIR}/triton_aipu.cc) target_include_directories(TritonAIPU PRIVATE ${CMAKE_SOURCE_DIR}/third_party/flir/include) diff --git a/third_party/aipu/backend/aipu_torch_dev.cpp b/third_party/aipu/backend/aipu_torch_dev.cpp index dc5dfc6b8..31036a944 100644 --- a/third_party/aipu/backend/aipu_torch_dev.cpp +++ b/third_party/aipu/backend/aipu_torch_dev.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include #include @@ -228,9 +230,29 @@ Tensor aipu_copy_from(const Tensor &self, const Tensor &dst, auto status = aipu_memcpy(aipu_ctx_, dst.data_ptr(), self.data_ptr(), self.nbytes(), kind); AIPU_DRIVER_HANDLE_ERROR(status); - return self; + return dst; } +Tensor aipu_copy_from_and_resize(const Tensor &self, const Tensor &dst) { + if (self.sizes() != dst.sizes()) { + auto new_dst = + custom_empty_symint(self.sizes(), self.scalar_type(), c10::nullopt, + c10::nullopt, c10::nullopt, c10::nullopt); + auto kind = AIPU_MEMCPY_HOST_TO_DEVICE; + if (StrStartsWith(self.device().str(), "aipu")) { + kind = AIPU_MEMCPY_DEVICE_TO_HOST; + if (StrStartsWith(dst.device().str(), "aipu")) { + kind = AIPU_MEMCPY_DEVICE_TO_DEVICE; + } + } + auto aipu_ctx_ = AIPUAllocator::aipu_ctx_; + auto status = aipu_memcpy(aipu_ctx_, new_dst.data_ptr(), self.data_ptr(), + self.nbytes(), kind); + AIPU_DRIVER_HANDLE_ERROR(status); + return new_dst; + } + return aipu_copy_from(self, dst, false); +} template