未验证 提交 7af1a258 编写于 作者: H hong19860320 提交者: GitHub

[Core] Add the graph optimization of subblocks for transformer model (#3947)

* [Core][ARM] Fix beam_search, eltwise_mul supports broadcast and int64_t data type, add print op and kernel, add exeception
test=develop

* Fix the dims of parent idx of the arm kernel of beam_search op

* elementwise_mul supports int64_t data type with broadcasting

* Add print op and kernel for debugging

* Support throwing the exception when the internal error occurs

* Refine while and conditional_block op kernel

* Support the graph optimization on subblocks

* Pass program_desc and block_idx into the kernel of the control flow ops(while/conditional_block/subgraph), and create the RuntimeProgram online, it make it possiable to call the control flow ops recursively

*Add unit test for masked transformer model
上级 f358cdb8
...@@ -98,6 +98,7 @@ lite_option(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK "Enable light-weight framework" OF ...@@ -98,6 +98,7 @@ lite_option(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK "Enable light-weight framework" OF
lite_option(LITE_WITH_PROFILE "Enable profile mode in lite framework" OFF) lite_option(LITE_WITH_PROFILE "Enable profile mode in lite framework" OFF)
lite_option(LITE_WITH_PRECISION_PROFILE "Enable precision profile in profile mode ON in lite" OFF) lite_option(LITE_WITH_PRECISION_PROFILE "Enable precision profile in profile mode ON in lite" OFF)
lite_option(LITE_WITH_LOG "Enable log printing or not." ON) lite_option(LITE_WITH_LOG "Enable log printing or not." ON)
lite_option(LITE_WITH_EXCEPTION "Enable throwing the exception when error occurs in lite" OFF)
lite_option(LITE_WITH_NVTX "Enable nvtx or not, please enable LITE_WITH_CUDA first." OFF) lite_option(LITE_WITH_NVTX "Enable nvtx or not, please enable LITE_WITH_CUDA first." OFF)
lite_option(LITE_ON_TINY_PUBLISH "Publish tiny predictor lib." OFF) lite_option(LITE_ON_TINY_PUBLISH "Publish tiny predictor lib." OFF)
lite_option(LITE_ON_MODEL_OPTIMIZE_TOOL "Build the model optimize tool" OFF) lite_option(LITE_ON_MODEL_OPTIMIZE_TOOL "Build the model optimize tool" OFF)
......
...@@ -190,6 +190,10 @@ if (LITE_WITH_LOG) ...@@ -190,6 +190,10 @@ if (LITE_WITH_LOG)
add_definitions("-DLITE_WITH_LOG") add_definitions("-DLITE_WITH_LOG")
endif() endif()
if (LITE_WITH_EXCEPTION)
add_definitions("-DLITE_WITH_EXCEPTION")
endif()
if (LITE_ON_TINY_PUBLISH) if (LITE_ON_TINY_PUBLISH)
add_definitions("-DLITE_ON_TINY_PUBLISH") add_definitions("-DLITE_ON_TINY_PUBLISH")
endif() endif()
......
...@@ -80,6 +80,17 @@ if (ARM_TARGET_LANG STREQUAL "clang") ...@@ -80,6 +80,17 @@ if (ARM_TARGET_LANG STREQUAL "clang")
elseif(ARM_TARGET_ARCH_ABI STREQUAL "armv7") elseif(ARM_TARGET_ARCH_ABI STREQUAL "armv7")
set(triple arm-v7a-linux-android) set(triple arm-v7a-linux-android)
set(LITE_WITH_OPENMP OFF CACHE STRING "Due to libomp's bug(For ARM64, it has been fixed by https://reviews.llvm.org/D19879, but still exists on ARM32), disable OpenMP on armv7 when cross-compiling using Clang" FORCE) set(LITE_WITH_OPENMP OFF CACHE STRING "Due to libomp's bug(For ARM64, it has been fixed by https://reviews.llvm.org/D19879, but still exists on ARM32), disable OpenMP on armv7 when cross-compiling using Clang" FORCE)
if(ANDROID_STL_TYPE MATCHES "^c\\+\\+_")
# Use CMAKE_CXX_STANDARD_LIBRARIES_INIT to ensure libunwind and libc++ is linked in the right order
set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_CXX_STANDARD_LIBRARIES_INIT} ${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ARCH_ABI}/libunwind.a")
if(ANDROID_STL_TYPE STREQUAL "c++_shared")
set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_CXX_STANDARD_LIBRARIES_INIT} ${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ARCH_ABI}/libc++_shared.so")
elseif(ANDROID_STL_TYPE STREQUAL "c++_static")
set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_CXX_STANDARD_LIBRARIES_INIT} ${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ARCH_ABI}/libc++_static.a")
else()
message(FATAL_ERROR "Invalid Android STL TYPE: ${ANDROID_STL_TYPE}.")
endif()
endif()
else() else()
message(FATAL_ERROR "Clang do not support this ${ARM_TARGET_ARCH_ABI}, use armv8 or armv7") message(FATAL_ERROR "Clang do not support this ${ARM_TARGET_ARCH_ABI}, use armv8 or armv7")
endif() endif()
......
...@@ -23,6 +23,21 @@ if(ANDROID) ...@@ -23,6 +23,21 @@ if(ANDROID)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -llog -fPIC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -llog -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -llog -fPIC") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -llog -fPIC")
# Don't re-export libgcc symbols
set(REMOVE_ATOMIC_GCC_SYMBOLS "-Wl,--exclude-libs,libatomic.a -Wl,--exclude-libs,libgcc.a")
set(CMAKE_SHARED_LINKER_FLAGS "${REMOVE_ATOMIC_GCC_SYMBOLS} ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${REMOVE_ATOMIC_GCC_SYMBOLS} ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${REMOVE_ATOMIC_GCC_SYMBOLS} ${CMAKE_EXE_LINKER_FLAGS}")
# Only the libunwind.a from clang(with libc++) provide C++ exception handling support for 32-bit ARM
# Refer to https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Unwinding
if (ARM_TARGET_LANG STREQUAL "clang" AND ARM_TARGET_ARCH_ABI STREQUAL "armv7" AND ANDROID_STL_TYPE MATCHES "^c\\+\\+_")
set(REMOVE_UNWIND_SYMBOLS "-Wl,--exclude-libs,libunwind.a")
set(CMAKE_SHARED_LINKER_FLAGS "${REMOVE_UNWIND_SYMBOLS} ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${REMOVE_UNWIND_SYMBOLS} ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${REMOVE_UNWIND_SYMBOLS} ${CMAKE_EXE_LINKER_FLAGS}")
endif()
endif() endif()
if(ARMLINUX) if(ARMLINUX)
...@@ -59,14 +74,13 @@ function(check_linker_flag) ...@@ -59,14 +74,13 @@ function(check_linker_flag)
endfunction() endfunction()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if((LITE_WITH_OPENCL AND (ARM_TARGET_LANG STREQUAL "clang")) OR LITE_WITH_PYTHON OR LITE_WITH_EXCEPTION OR (NOT LITE_ON_TINY_PUBLISH))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -fasynchronous-unwind-tables -funwind-tables")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-asynchronous-unwind-tables -fno-unwind-tables")
endif()
if (LITE_ON_TINY_PUBLISH) if (LITE_ON_TINY_PUBLISH)
if((NOT LITE_WITH_PYTHON)) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -Ofast -Os -fomit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
endif()
if(LITE_WITH_OPENCL AND (ARM_TARGET_LANG STREQUAL "clang"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -Ofast -Os -fomit-frame-pointer -fno-asynchronous-unwind-tables -fno-unwind-tables")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -ffunction-sections") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -ffunction-sections")
check_linker_flag(-Wl,--gc-sections) check_linker_flag(-Wl,--gc-sections)
endif() endif()
......
...@@ -54,6 +54,11 @@ find_library(NPU_DDK_IR_BUILD_FILE NAMES hiai_ir_build ...@@ -54,6 +54,11 @@ find_library(NPU_DDK_IR_BUILD_FILE NAMES hiai_ir_build
PATHS ${NPU_DDK_ROOT}/${NPU_SUB_LIB_PATH} PATHS ${NPU_DDK_ROOT}/${NPU_SUB_LIB_PATH}
NO_DEFAULT_PATH) NO_DEFAULT_PATH)
# Added in HiAI DDK 320 or later version
find_library(NPU_DDK_HCL_FILE NAMES hcl
PATHS ${NPU_DDK_ROOT}/${NPU_SUB_LIB_PATH}
NO_DEFAULT_PATH)
if(NOT NPU_DDK_HIAI_FILE) if(NOT NPU_DDK_HIAI_FILE)
message(FATAL_ERROR "Can not find NPU_DDK_HIAI_FILE in ${NPU_DDK_ROOT}") message(FATAL_ERROR "Can not find NPU_DDK_HIAI_FILE in ${NPU_DDK_ROOT}")
else() else()
...@@ -78,5 +83,13 @@ else() ...@@ -78,5 +83,13 @@ else()
set_property(TARGET npu_ddk_ir_build PROPERTY IMPORTED_LOCATION ${NPU_DDK_IR_BUILD_FILE}) set_property(TARGET npu_ddk_ir_build PROPERTY IMPORTED_LOCATION ${NPU_DDK_IR_BUILD_FILE})
endif() endif()
set(npu_runtime_libs npu_ddk_hiai CACHE INTERNAL "npu ddk runtime libs") if(NOT NPU_DDK_HCL_FILE)
# message(FATAL_ERROR "Can not find NPU_DDK_HCL_FILE in ${NPU_DDK_ROOT}")
else()
message(STATUS "Found NPU_DDK HCL Library: ${NPU_DDK_HCL_FILE}")
add_library(npu_ddk_hcl SHARED IMPORTED GLOBAL)
set_property(TARGET npu_ddk_hcl PROPERTY IMPORTED_LOCATION ${NPU_DDK_HCL_FILE})
endif()
set(npu_runtime_libs npu_ddk_hiai npu_ddk_hcl CACHE INTERNAL "npu ddk runtime libs")
set(npu_builder_libs npu_ddk_ir npu_ddk_ir_build CACHE INTERNAL "npu ddk builder libs") set(npu_builder_libs npu_ddk_ir npu_ddk_ir_build CACHE INTERNAL "npu ddk builder libs")
...@@ -45,6 +45,7 @@ if (WITH_TESTING) ...@@ -45,6 +45,7 @@ if (WITH_TESTING)
lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "resnet50.tar.gz") lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "resnet50.tar.gz")
lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "inception_v4_simple.tar.gz") lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "inception_v4_simple.tar.gz")
lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "MobileNetV1_quant.tar.gz") lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "MobileNetV1_quant.tar.gz")
lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "transformer_with_mask_fp32.tar.gz")
endif() endif()
if(NOT LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) if(NOT LITE_WITH_LIGHT_WEIGHT_FRAMEWORK)
lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "GoogleNet_inference.tar.gz") lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "GoogleNet_inference.tar.gz")
......
...@@ -37,8 +37,7 @@ void Predictor::SaveModel(const std::string &dir, ...@@ -37,8 +37,7 @@ void Predictor::SaveModel(const std::string &dir,
if (!program_) { if (!program_) {
GenRuntimeProgram(); GenRuntimeProgram();
} }
program_->SaveOpInfosToProgram(program_desc_.get()); program_->SaveToProgram(program_desc_);
program_->UpdateVarsOfProgram(program_desc_.get());
switch (model_type) { switch (model_type) {
case lite_api::LiteModelType::kProtobuf: case lite_api::LiteModelType::kProtobuf:
SaveModelPb(dir, *program_->exec_scope(), *program_desc_.get(), true); SaveModelPb(dir, *program_->exec_scope(), *program_desc_.get(), true);
...@@ -58,17 +57,21 @@ void Predictor::SaveModel(const std::string &dir, ...@@ -58,17 +57,21 @@ void Predictor::SaveModel(const std::string &dir,
void Predictor::SaveOpKernelInfo(const std::string &model_dir) { void Predictor::SaveOpKernelInfo(const std::string &model_dir) {
std::set<std::string> ops_info; std::set<std::string> ops_info;
std::set<std::string> kernels_info; std::set<std::string> kernels_info;
const auto &instructions_ = program_->instructions(); auto block_size = program_->block_size();
for (auto &node : instructions_) { for (size_t block_idx = 0; block_idx < block_size; ++block_idx) {
// parse op type infomation const auto &insts = program_->instructions(block_idx);
auto op = node.op()->op_info(); for (auto &inst : insts) {
ops_info.insert(op->Type()); // parse op type infomation
// parse kernel type information auto op = inst.op()->op_info();
std::string kernel_type_str = ops_info.insert(op->Type());
node.kernel()->op_type() + "," + TargetRepr(node.kernel()->target()) + // parse kernel type information
"," + PrecisionRepr(node.kernel()->precision()) + "," + std::string kernel_type_str =
DataLayoutRepr(node.kernel()->layout()) + "," + node.kernel()->alias(); inst.kernel()->op_type() + "," + TargetRepr(inst.kernel()->target()) +
kernels_info.insert(kernel_type_str); "," + PrecisionRepr(inst.kernel()->precision()) + "," +
DataLayoutRepr(inst.kernel()->layout()) + "," +
inst.kernel()->alias();
kernels_info.insert(kernel_type_str);
}
} }
// get souce_file name from op type and kernel type // get souce_file name from op type and kernel type
...@@ -170,9 +173,9 @@ void Predictor::PrepareFeedFetch() { ...@@ -170,9 +173,9 @@ void Predictor::PrepareFeedFetch() {
std::vector<const cpp::OpDesc *> feeds; std::vector<const cpp::OpDesc *> feeds;
std::vector<const cpp::OpDesc *> fetchs; std::vector<const cpp::OpDesc *> fetchs;
const auto &insts = program_->instructions(); const auto &insts = program_->instructions(kRootBlockIdx);
for (size_t i = 0; i < program_->num_instructions(); i++) { for (auto &inst : insts) {
const auto &op = insts[i].op()->op_info(); const auto &op = inst.op()->op_info();
if (op->Type() == "feed") { if (op->Type() == "feed") {
feeds.push_back(op); feeds.push_back(op);
} else if (op->Type() == "fetch") { } else if (op->Type() == "fetch") {
...@@ -255,7 +258,6 @@ void Predictor::Build(const lite_api::CxxConfig &config, ...@@ -255,7 +258,6 @@ void Predictor::Build(const lite_api::CxxConfig &config,
} else { } else {
LOG(INFO) << "Load model from file."; LOG(INFO) << "Load model from file.";
} }
Build(model_path, Build(model_path,
model_file, model_file,
param_file, param_file,
...@@ -296,10 +298,10 @@ void Predictor::Build(const std::string &model_path, ...@@ -296,10 +298,10 @@ void Predictor::Build(const std::string &model_path,
Build(program_desc_, valid_places, passes); Build(program_desc_, valid_places, passes);
} }
void Predictor::Build(const std::shared_ptr<cpp::ProgramDesc> &desc, void Predictor::Build(const std::shared_ptr<cpp::ProgramDesc> &program_desc,
const std::vector<Place> &valid_places, const std::vector<Place> &valid_places,
const std::vector<std::string> &passes) { const std::vector<std::string> &passes) {
program_desc_ = desc; program_desc_ = program_desc;
// `inner_places` is used to optimize passes // `inner_places` is used to optimize passes
std::vector<Place> inner_places = valid_places; std::vector<Place> inner_places = valid_places;
for (auto &valid_place : valid_places) { for (auto &valid_place : valid_places) {
...@@ -336,7 +338,7 @@ void Predictor::Build(const std::shared_ptr<cpp::ProgramDesc> &desc, ...@@ -336,7 +338,7 @@ void Predictor::Build(const std::shared_ptr<cpp::ProgramDesc> &desc,
Place{TARGET(kARM), PRECISION(kInt8)}); Place{TARGET(kARM), PRECISION(kInt8)});
} }
Program program(*desc.get(), scope_, inner_places); Program program(program_desc_, scope_, inner_places);
valid_places_ = inner_places; valid_places_ = inner_places;
core::KernelPickFactor factor; core::KernelPickFactor factor;
......
...@@ -52,12 +52,12 @@ class LITE_API Predictor { ...@@ -52,12 +52,12 @@ class LITE_API Predictor {
// Create a predictor with the weight variable scope set. // Create a predictor with the weight variable scope set.
explicit Predictor(const std::shared_ptr<lite::Scope>& root_scope) explicit Predictor(const std::shared_ptr<lite::Scope>& root_scope)
: scope_(root_scope) {} : scope_(root_scope) {}
Predictor(const std::shared_ptr<cpp::ProgramDesc>& desc, Predictor(const std::shared_ptr<cpp::ProgramDesc>& program_desc,
const std::shared_ptr<Scope>& root, const std::shared_ptr<Scope>& root_scope,
const std::vector<Place>& valid_places, const std::vector<Place>& valid_places,
const std::vector<std::string>& var_names = {}) const std::vector<std::string>& vars_to_clone = {})
: program_desc_(desc), scope_(root) { : program_desc_(program_desc), scope_(root_scope) {
Program program(*desc.get(), scope_, valid_places, var_names); Program program(program_desc_, scope_, valid_places, vars_to_clone);
optimizer_ = Optimizer(std::move(program), valid_places); optimizer_ = Optimizer(std::move(program), valid_places);
exec_scope_ = optimizer_.exec_scope(); exec_scope_ = optimizer_.exec_scope();
valid_places_ = valid_places; valid_places_ = valid_places;
...@@ -79,30 +79,28 @@ class LITE_API Predictor { ...@@ -79,30 +79,28 @@ class LITE_API Predictor {
lite_api::LiteModelType model_type = lite_api::LiteModelType::kProtobuf, lite_api::LiteModelType model_type = lite_api::LiteModelType::kProtobuf,
bool memory_from_memory = false); bool memory_from_memory = false);
void Build(const std::shared_ptr<cpp::ProgramDesc>& desc, void Build(const std::shared_ptr<cpp::ProgramDesc>& program_desc,
const std::vector<Place>& valid_places, const std::vector<Place>& valid_places,
const std::vector<std::string>& passes = {}); const std::vector<std::string>& passes = {});
std::shared_ptr<Predictor> Clone() const { std::shared_ptr<Predictor> Clone() const {
auto predictor = return std::make_shared<Predictor>(program_desc_, scope_, valid_places_);
std::make_shared<Predictor>(program_desc_, scope_, valid_places_);
return predictor;
} }
std::shared_ptr<Predictor> Clone( std::shared_ptr<Predictor> Clone(
const std::vector<std::string>& var_names) const { const std::vector<std::string>& vars_to_clone) const {
CHECK(program_desc_) << "Both program and scope of current predicotr " CHECK(program_desc_) << "Both program and scope of current predicotr "
"should be not be nullptr in Clone mode."; "should be not be nullptr in Clone mode.";
CHECK(scope_) << "Both program and scope of current predicotr should be " CHECK(scope_) << "Both program and scope of current predicotr should be "
"not be nullptr in Clone mode."; "not be nullptr in Clone mode.";
auto predictor = std::make_shared<Predictor>( auto predictor = std::make_shared<Predictor>(
program_desc_, scope_, valid_places_, var_names); program_desc_, scope_, valid_places_, vars_to_clone);
for (auto i : var_names) { for (auto var_name : vars_to_clone) {
predictor->exec_scope_->LocalVar(i); predictor->exec_scope_->LocalVar(var_name);
auto* tensor = predictor->scope_->Var(i)->GetMutable<lite::Tensor>(); auto* tensor = predictor->scope_->Var(var_name)->GetMutable<Tensor>();
auto* sub_tensor = auto* sub_tensor =
predictor->exec_scope_->Var(i)->GetMutable<lite::Tensor>(); predictor->exec_scope_->Var(var_name)->GetMutable<Tensor>();
sub_tensor->CopyDataFrom(*tensor); sub_tensor->CopyDataFrom(*tensor);
} }
return predictor; return predictor;
...@@ -140,6 +138,7 @@ class LITE_API Predictor { ...@@ -140,6 +138,7 @@ class LITE_API Predictor {
// get a const tensor according to its name // get a const tensor according to its name
const lite::Tensor* GetTensor(const std::string& name) const; const lite::Tensor* GetTensor(const std::string& name) const;
const RuntimeProgram& runtime_program() const; const RuntimeProgram& runtime_program() const;
Scope* scope() { return scope_.get(); }
// This method is disabled in mobile, for unnecessary dependencies required. // This method is disabled in mobile, for unnecessary dependencies required.
void SaveModel( void SaveModel(
......
...@@ -74,8 +74,10 @@ void CxxPaddleApiImpl::Init(const lite_api::CxxConfig &config) { ...@@ -74,8 +74,10 @@ void CxxPaddleApiImpl::Init(const lite_api::CxxConfig &config) {
mode_ = config.power_mode(); mode_ = config.power_mode();
threads_ = config.threads(); threads_ = config.threads();
#ifdef LITE_WITH_NPU #ifdef LITE_WITH_NPU
// Store the model-level configuration into scope for kernels, and use
// exe_scope to store the execution-level configuration
Context<TargetType::kNPU>::SetSubgraphModelCacheDir( Context<TargetType::kNPU>::SetSubgraphModelCacheDir(
config.subgraph_model_cache_dir()); raw_predictor_->scope(), config.subgraph_model_cache_dir());
#endif #endif
#if (defined LITE_WITH_X86) && (defined PADDLE_WITH_MKLML) && \ #if (defined LITE_WITH_X86) && (defined PADDLE_WITH_MKLML) && \
!(defined LITE_ON_MODEL_OPTIMIZE_TOOL) !(defined LITE_ON_MODEL_OPTIMIZE_TOOL)
......
...@@ -22,16 +22,16 @@ namespace lite { ...@@ -22,16 +22,16 @@ namespace lite {
void LightPredictor::Build(const std::string& lite_model_file, void LightPredictor::Build(const std::string& lite_model_file,
bool model_from_memory) { bool model_from_memory) {
if (model_from_memory) { if (model_from_memory) {
LoadModelNaiveFromMemory(lite_model_file, scope_.get(), &cpp_program_desc_); LoadModelNaiveFromMemory(
lite_model_file, scope_.get(), program_desc_.get());
} else { } else {
LoadModelNaiveFromFile(lite_model_file, scope_.get(), &cpp_program_desc_); LoadModelNaiveFromFile(lite_model_file, scope_.get(), program_desc_.get());
} }
// For weight quantization of post training, load the int8/16 weights // For weight quantization of post training, load the int8/16 weights
// for optimized model, and dequant it to fp32. // for optimized model, and dequant it to fp32.
DequantizeWeight(); DequantizeWeight();
BuildRuntimeProgram(program_desc_);
BuildRuntimeProgram(cpp_program_desc_);
PrepareFeedFetch(); PrepareFeedFetch();
} }
...@@ -43,15 +43,15 @@ void LightPredictor::Build(const std::string& model_dir, ...@@ -43,15 +43,15 @@ void LightPredictor::Build(const std::string& model_dir,
switch (model_type) { switch (model_type) {
#ifndef LITE_ON_TINY_PUBLISH #ifndef LITE_ON_TINY_PUBLISH
case lite_api::LiteModelType::kProtobuf: case lite_api::LiteModelType::kProtobuf:
LoadModelPb(model_dir, "", "", scope_.get(), &cpp_program_desc_); LoadModelPb(model_dir, "", "", scope_.get(), program_desc_.get());
break; break;
#endif #endif
case lite_api::LiteModelType::kNaiveBuffer: { case lite_api::LiteModelType::kNaiveBuffer: {
if (model_from_memory) { if (model_from_memory) {
LoadModelNaiveFromMemory( LoadModelNaiveFromMemory(
model_buffer, param_buffer, scope_.get(), &cpp_program_desc_); model_buffer, param_buffer, scope_.get(), program_desc_.get());
} else { } else {
LoadModelNaive(model_dir, scope_.get(), &cpp_program_desc_); LoadModelNaive(model_dir, scope_.get(), program_desc_.get());
} }
break; break;
} }
...@@ -60,7 +60,7 @@ void LightPredictor::Build(const std::string& model_dir, ...@@ -60,7 +60,7 @@ void LightPredictor::Build(const std::string& model_dir,
} }
DequantizeWeight(); DequantizeWeight();
BuildRuntimeProgram(cpp_program_desc_); BuildRuntimeProgram(program_desc_);
PrepareFeedFetch(); PrepareFeedFetch();
} }
...@@ -109,16 +109,17 @@ std::vector<std::string> LightPredictor::GetOutputNames() { ...@@ -109,16 +109,17 @@ std::vector<std::string> LightPredictor::GetOutputNames() {
} }
// append the names of inputs and outputs into input_names_ and output_names_ // append the names of inputs and outputs into input_names_ and output_names_
void LightPredictor::PrepareFeedFetch() { void LightPredictor::PrepareFeedFetch() {
const cpp::ProgramDesc& prog = cpp_program_desc_; std::vector<const cpp::OpDesc*> feeds;
auto current_block = prog.GetBlock<cpp::BlockDesc>(0); std::vector<const cpp::OpDesc*> fetchs;
std::vector<cpp::OpDesc const*> feeds; std::shared_ptr<const cpp::ProgramDesc> program_desc = program_desc_;
std::vector<cpp::OpDesc const*> fetchs; auto main_block = program_desc->GetBlock<cpp::BlockDesc>(kRootBlockIdx);
for (size_t i = 0; i < current_block->OpsSize(); i++) { auto op_size = main_block->OpsSize();
auto op = current_block->GetOp<cpp::OpDesc>(i); for (size_t op_idx = 0; op_idx < op_size; ++op_idx) {
if (op->Type() == "feed") { auto op_desc = main_block->GetOp<cpp::OpDesc>(op_idx);
feeds.push_back(op); if (op_desc->Type() == "feed") {
} else if (op->Type() == "fetch") { feeds.push_back(op_desc);
fetchs.push_back(op); } else if (op_desc->Type() == "fetch") {
fetchs.push_back(op_desc);
} }
} }
input_names_.resize(feeds.size()); input_names_.resize(feeds.size());
...@@ -133,55 +134,35 @@ void LightPredictor::PrepareFeedFetch() { ...@@ -133,55 +134,35 @@ void LightPredictor::PrepareFeedFetch() {
} }
} }
void LightPredictor::BuildRuntimeProgram(const cpp::ProgramDesc& prog) { void LightPredictor::BuildRuntimeProgram(
std::vector<Instruction> insts; const std::shared_ptr<const cpp::ProgramDesc>& program_desc) {
// 1. Create op first auto* exe_scope = &scope_->NewScope();
Program program(prog, scope_, {}); // Prepare workspace
scope_->Var("feed")->GetMutable<std::vector<lite::Tensor>>();
// 2. Create Instructs scope_->Var("fetch")->GetMutable<std::vector<lite::Tensor>>();
#ifdef LITE_WITH_OPENCL CHECK(program_desc);
using OpenCLContext = Context<TargetType::kOpenCL>; auto block_size = program_desc->BlocksSize();
std::unique_ptr<KernelContext> local_ctx(new KernelContext()); CHECK(block_size);
local_ctx->As<OpenCLContext>().InitOnce(); for (size_t block_idx = 0; block_idx < block_size; ++block_idx) {
#endif auto block_desc = program_desc->GetBlock<cpp::BlockDesc>(block_idx);
auto var_size = block_desc->VarsSize();
// Create the kernels of the target places, and filter out the specific for (size_t var_idx = 0; var_idx < var_size; ++var_idx) {
// kernel with the target alias. auto var_desc = block_desc->GetVar<cpp::VarDesc>(var_idx);
for (auto& op : program.ops()) { if (!var_desc->Persistable()) {
auto kernel_type = op->op_info()->GetAttr<std::string>(kKernelTypeAttr); exe_scope->Var(var_desc->Name());
std::string op_type, alias; } else {
Place place; if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") continue;
KernelBase::ParseKernelType(kernel_type, &op_type, &alias, &place); scope_->Var(var_desc->Name());
auto kernels = op->CreateKernels({place}); }
// filter out a kernel
auto it = std::find_if(
kernels.begin(), kernels.end(), [&](std::unique_ptr<KernelBase>& it) {
return it->alias() == alias;
});
CHECK(it != kernels.end());
#ifdef LITE_WITH_OPENCL
if ((*it)->target() == TARGET(kOpenCL)) {
std::unique_ptr<KernelContext> ctx(new KernelContext());
(*local_ctx).As<OpenCLContext>().CopySharedTo(&ctx->As<OpenCLContext>());
(*it)->SetContext(std::move(ctx));
} else {
(*it)->SetContext(ContextScheduler::Global().NewContext((*it)->target()));
} }
#else
(*it)->SetContext(ContextScheduler::Global().NewContext((*it)->target()));
#endif
insts.emplace_back(op, std::move(*it));
} }
program_.reset(new RuntimeProgram(std::move(insts))); // Only extracting the ops and generate the runtime program from the main
// block desc
CHECK(program.exec_scope()); program_.reset(new RuntimeProgram(program_desc, exe_scope, kRootBlockIdx));
program_->set_exec_scope(program.exec_scope());
} }
void LightPredictor::DequantizeWeight() { void LightPredictor::DequantizeWeight() {
const cpp::ProgramDesc& cpp_desc = cpp_program_desc_; std::shared_ptr<const cpp::ProgramDesc> program_desc = program_desc_;
#define PROCESS_CONV2D_DATA() \ #define PROCESS_CONV2D_DATA() \
for (int64_t i = 0; i < ch; ++i) { \ for (int64_t i = 0; i < ch; ++i) { \
for (int64_t j = 0; j < offset; ++j) { \ for (int64_t j = 0; j < offset; ++j) { \
...@@ -207,10 +188,9 @@ void LightPredictor::DequantizeWeight() { ...@@ -207,10 +188,9 @@ void LightPredictor::DequantizeWeight() {
} }
return result; return result;
}; };
Tensor tmp_tensor; Tensor tmp_tensor;
for (size_t i = 0; i < cpp_desc.BlocksSize(); i++) { for (size_t i = 0; i < program_desc->BlocksSize(); i++) {
auto* block = cpp_desc.GetBlock<cpp::BlockDesc>(i); auto* block = program_desc->GetBlock<cpp::BlockDesc>(i);
for (size_t k = 0; k < block->OpsSize(); ++k) { for (size_t k = 0; k < block->OpsSize(); ++k) {
auto* op_desc = block->GetOp<cpp::OpDesc>(k); auto* op_desc = block->GetOp<cpp::OpDesc>(k);
if (is_weight_quantized_op(op_desc)) { if (is_weight_quantized_op(op_desc)) {
......
...@@ -46,6 +46,7 @@ class LITE_API LightPredictor { ...@@ -46,6 +46,7 @@ class LITE_API LightPredictor {
LightPredictor(const std::string& lite_model_file, LightPredictor(const std::string& lite_model_file,
bool model_from_memory = false) { bool model_from_memory = false) {
scope_ = std::make_shared<Scope>(); scope_ = std::make_shared<Scope>();
program_desc_ = std::make_shared<cpp::ProgramDesc>();
Build(lite_model_file, model_from_memory); Build(lite_model_file, model_from_memory);
} }
...@@ -57,6 +58,7 @@ class LITE_API LightPredictor { ...@@ -57,6 +58,7 @@ class LITE_API LightPredictor {
lite_api::LiteModelType model_type = lite_api::LiteModelType model_type =
lite_api::LiteModelType::kNaiveBuffer) { lite_api::LiteModelType::kNaiveBuffer) {
scope_ = std::make_shared<Scope>(); scope_ = std::make_shared<Scope>();
program_desc_ = std::make_shared<cpp::ProgramDesc>();
Build(model_dir, model_buffer, param_buffer, model_type, model_from_memory); Build(model_dir, model_buffer, param_buffer, model_type, model_from_memory);
} }
...@@ -78,6 +80,7 @@ class LITE_API LightPredictor { ...@@ -78,6 +80,7 @@ class LITE_API LightPredictor {
std::vector<std::string> GetInputNames(); std::vector<std::string> GetInputNames();
std::vector<std::string> GetOutputNames(); std::vector<std::string> GetOutputNames();
void PrepareFeedFetch(); void PrepareFeedFetch();
Scope* scope() { return scope_.get(); }
private: private:
void Build(const std::string& lite_model_file, void Build(const std::string& lite_model_file,
...@@ -91,14 +94,15 @@ class LITE_API LightPredictor { ...@@ -91,14 +94,15 @@ class LITE_API LightPredictor {
lite_api::LiteModelType model_type = lite_api::LiteModelType::kProtobuf, lite_api::LiteModelType model_type = lite_api::LiteModelType::kProtobuf,
bool model_from_memory = false); bool model_from_memory = false);
void BuildRuntimeProgram(const cpp::ProgramDesc& prog); void BuildRuntimeProgram(
const std::shared_ptr<const cpp::ProgramDesc>& program_desc);
void DequantizeWeight(); void DequantizeWeight();
private: private:
std::shared_ptr<Scope> scope_; std::shared_ptr<Scope> scope_;
std::unique_ptr<RuntimeProgram> program_; std::unique_ptr<RuntimeProgram> program_;
cpp::ProgramDesc cpp_program_desc_; std::shared_ptr<cpp::ProgramDesc> program_desc_;
std::vector<std::string> input_names_; std::vector<std::string> input_names_;
std::vector<std::string> output_names_; std::vector<std::string> output_names_;
}; };
......
...@@ -38,8 +38,10 @@ void LightPredictorImpl::Init(const lite_api::MobileConfig& config) { ...@@ -38,8 +38,10 @@ void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
threads_ = config.threads(); threads_ = config.threads();
#ifdef LITE_WITH_NPU #ifdef LITE_WITH_NPU
// Store the model-level configuration into scope for kernels, and use
// exe_scope to store the execution-level configuration
Context<TargetType::kNPU>::SetSubgraphModelCacheDir( Context<TargetType::kNPU>::SetSubgraphModelCacheDir(
config.subgraph_model_cache_dir()); raw_predictor_->scope(), config.subgraph_model_cache_dir());
#endif #endif
} }
......
...@@ -54,6 +54,7 @@ USE_MIR_PASS(mlu_postprocess_pass); ...@@ -54,6 +54,7 @@ USE_MIR_PASS(mlu_postprocess_pass);
USE_MIR_PASS(weight_quantization_preprocess_pass); USE_MIR_PASS(weight_quantization_preprocess_pass);
USE_MIR_PASS(apu_subgraph_pass); USE_MIR_PASS(apu_subgraph_pass);
USE_MIR_PASS(quantized_op_attributes_inference_pass); USE_MIR_PASS(quantized_op_attributes_inference_pass);
USE_MIR_PASS(control_flow_op_unused_inputs_and_outputs_eliminate_pass)
USE_MIR_PASS(lite_scale_activation_fuse_pass); USE_MIR_PASS(lite_scale_activation_fuse_pass);
USE_MIR_PASS(__xpu__resnet_fuse_pass); USE_MIR_PASS(__xpu__resnet_fuse_pass);
USE_MIR_PASS(__xpu__resnet_cbam_fuse_pass); USE_MIR_PASS(__xpu__resnet_cbam_fuse_pass);
......
...@@ -234,7 +234,7 @@ void beam_search(const Tensor *pre_ids, ...@@ -234,7 +234,7 @@ void beam_search(const Tensor *pre_ids,
selected_ids->Resize(dims); selected_ids->Resize(dims);
selected_scores->Resize(dims); selected_scores->Resize(dims);
if (parent_idx) { if (parent_idx) {
parent_idx->Resize(dims); parent_idx->Resize({static_cast<int64_t>(num_instances)});
} }
auto *selected_ids_data = selected_ids->mutable_data<int64_t>(); auto *selected_ids_data = selected_ids->mutable_data<int64_t>();
auto *selected_scores_data = selected_scores->mutable_data<float>(); auto *selected_scores_data = selected_scores->mutable_data<float>();
......
...@@ -747,6 +747,16 @@ void elementwise_mul<int>(const int* dinx, ...@@ -747,6 +747,16 @@ void elementwise_mul<int>(const int* dinx,
} }
} }
template <>
void elementwise_mul<int64_t>(const int64_t* dinx,
const int64_t* diny,
int64_t* dout,
int num) {
for (int i = 0; i < num; i++) {
dout[i] = dinx[i] * diny[i];
}
}
template <> template <>
void elementwise_mul_relu<float>(const float* dinx, void elementwise_mul_relu<float>(const float* dinx,
const float* diny, const float* diny,
...@@ -801,6 +811,17 @@ void elementwise_mul_relu<float>(const float* dinx, ...@@ -801,6 +811,17 @@ void elementwise_mul_relu<float>(const float* dinx,
} }
} }
template <>
void elementwise_mul_relu<int64_t>(const int64_t* dinx,
const int64_t* diny,
int64_t* dout,
int num) {
for (int i = 0; i < num; i++) {
int64_t tmp = dinx[i] * diny[i];
dout[i] = tmp > 0 ? tmp : 0;
}
}
template <> template <>
void elementwise_mul_broadcast<float>(const float* dinx, void elementwise_mul_broadcast<float>(const float* dinx,
const float* diny, const float* diny,
...@@ -935,6 +956,29 @@ void elementwise_mul_broadcast<int>(const int* dinx, ...@@ -935,6 +956,29 @@ void elementwise_mul_broadcast<int>(const int* dinx,
} }
} }
template <>
void elementwise_mul_broadcast<int64_t>(const int64_t* dinx,
const int64_t* diny,
int64_t* dout,
int batch,
int channels,
int num) {
#pragma omp parallel for collapse(2)
for (int i = 0; i < batch; ++i) {
for (int j = 0; j < channels; ++j) {
int offset = (i * channels + j) * num;
const int64_t* dinx_ptr = dinx + offset;
const int64_t diny_data = diny[j];
int64_t* dout_ptr = dout + offset;
for (int k = 0; k < num; ++k) {
*dout_ptr = *dinx_ptr * diny_data;
dout_ptr++;
dinx_ptr++;
}
}
}
}
template <> template <>
void elementwise_mul_relu_broadcast<float>(const float* dinx, void elementwise_mul_relu_broadcast<float>(const float* dinx,
const float* diny, const float* diny,
...@@ -1014,6 +1058,30 @@ void elementwise_mul_relu_broadcast<float>(const float* dinx, ...@@ -1014,6 +1058,30 @@ void elementwise_mul_relu_broadcast<float>(const float* dinx,
} }
} }
template <>
void elementwise_mul_relu_broadcast<int64_t>(const int64_t* dinx,
const int64_t* diny,
int64_t* dout,
int batch,
int channels,
int num) {
#pragma omp parallel for collapse(2)
for (int i = 0; i < batch; ++i) {
for (int j = 0; j < channels; ++j) {
int offset = (i * channels + j) * num;
const int64_t* dinx_ptr = dinx + offset;
const int64_t diny_data = diny[j];
int64_t* dout_ptr = dout + offset;
for (int k = 0; k < num; ++k) {
int64_t tmp = *dinx_ptr * diny_data;
*dout_ptr = tmp > 0 ? tmp : 0;
dout_ptr++;
dinx_ptr++;
}
}
}
}
template <> template <>
void elementwise_max<float>(const float* dinx, void elementwise_max<float>(const float* dinx,
const float* diny, const float* diny,
......
...@@ -33,7 +33,7 @@ std::shared_ptr<hiai::AiModelMngerClient> Device::Load( ...@@ -33,7 +33,7 @@ std::shared_ptr<hiai::AiModelMngerClient> Device::Load(
// Check HiAI DDK version // Check HiAI DDK version
const char* ddk_version = model_client->GetVersion(); const char* ddk_version = model_client->GetVersion();
if (ddk_version) { if (ddk_version) {
LOG(INFO) << "[NPU] HiAI DDK version: " << ddk_version; VLOG(3) << "[NPU] HiAI DDK version: " << ddk_version;
} else { } else {
LOG(WARNING) << "[NPU] Unable to get HiAI DDK version!"; LOG(WARNING) << "[NPU] Unable to get HiAI DDK version!";
} }
......
...@@ -32,25 +32,27 @@ void TestCase::CreateInstruction() { ...@@ -32,25 +32,27 @@ void TestCase::CreateInstruction() {
#endif #endif
if (enable_subgraph_op) { if (enable_subgraph_op) {
// Create a new block desc to wrap the original op desc // Create a new block desc to wrap the original op desc
auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
int sub_block_idx = 0; int sub_block_idx = 0;
auto sub_block_desc = new cpp::BlockDesc(); auto sub_block_desc = sub_program_desc->AddBlock<cpp::BlockDesc>();
sub_block_desc->ClearOps(); sub_block_desc->ClearOps();
sub_block_desc->ClearVars(); sub_block_desc->ClearVars();
auto sub_block_op_desc = sub_block_desc->AddOp<cpp::OpDesc>(); auto sub_op_desc = sub_block_desc->AddOp<cpp::OpDesc>();
*sub_block_op_desc = *op_desc_; *sub_op_desc = *op_desc_;
// Add the block desc into the subgraph op which used to replace the // Add the block desc into the subgraph op which used to replace the
// original op // original op
op_desc_.reset(new cpp::OpDesc()); op_desc_.reset(new cpp::OpDesc());
op_desc_->SetType("subgraph"); op_desc_->SetType("subgraph");
op_desc_->SetAttr<int32_t>("sub_block", sub_block_idx); op_desc_->SetAttr<int32_t>("sub_block", sub_block_idx);
auto in_names = sub_block_op_desc->input_vars(); auto in_names = sub_op_desc->input_vars();
auto out_names = sub_block_op_desc->output_vars(); auto out_names = sub_op_desc->output_vars();
op_desc_->SetInput("Inputs", in_names); op_desc_->SetInput("Inputs", in_names);
op_desc_->SetOutput("Outputs", out_names); op_desc_->SetOutput("Outputs", out_names);
op_desc_->SetAttr<std::vector<std::string>>("input_data_names", in_names); op_desc_->SetAttr<std::vector<std::string>>("input_data_names", in_names);
op_desc_->SetAttr<std::vector<std::string>>("output_data_names", out_names); op_desc_->SetAttr<std::vector<std::string>>("output_data_names", out_names);
op = LiteOpRegistry::Global().Create(op_desc().Type()); op = LiteOpRegistry::Global().Create(op_desc().Type());
static_cast<operators::SubgraphOp*>(op.get())->SetSubBlock(sub_block_desc); static_cast<operators::SubgraphOp*>(op.get())->SetProgramDesc(
sub_program_desc);
} else { } else {
op = LiteOpRegistry::Global().Create(op_desc().Type()); op = LiteOpRegistry::Global().Create(op_desc().Type());
} }
...@@ -60,7 +62,7 @@ void TestCase::CreateInstruction() { ...@@ -60,7 +62,7 @@ void TestCase::CreateInstruction() {
// filter out the target kernel // filter out the target kernel
CHECK(!kernels.empty()) << "No kernel found for place " CHECK(!kernels.empty()) << "No kernel found for place "
<< place_.DebugString(); << place_.DebugString();
auto it = std::remove_if( auto it = std::find_if(
kernels.begin(), kernels.end(), [&](std::unique_ptr<KernelBase>& k) { kernels.begin(), kernels.end(), [&](std::unique_ptr<KernelBase>& k) {
return k->alias() == alias_; return k->alias() == alias_;
}); });
...@@ -234,19 +236,6 @@ bool TestCase::CheckPrecision(const std::string& var_name, ...@@ -234,19 +236,6 @@ bool TestCase::CheckPrecision(const std::string& var_name,
return success; return success;
} }
TestCase::~TestCase() {
if (op_desc_->Type() == "subgraph") {
// Release the subblock desc of Subgraph op
auto subgraph_op = const_cast<operators::SubgraphOp*>(
static_cast<const operators::SubgraphOp*>(instruction_->op()));
CHECK(subgraph_op);
auto sub_block_desc = subgraph_op->GetSubBlock();
if (sub_block_desc) {
delete sub_block_desc;
}
}
}
} // namespace arena } // namespace arena
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
...@@ -46,7 +46,7 @@ class TestCase { ...@@ -46,7 +46,7 @@ class TestCase {
base_scope_(new Scope) { base_scope_(new Scope) {
ctx_ = ContextScheduler::Global().NewContext(place_.target); ctx_ = ContextScheduler::Global().NewContext(place_.target);
} }
virtual ~TestCase(); virtual ~TestCase() {}
void Prepare() { void Prepare() {
PrepareData(); PrepareData();
......
...@@ -17,10 +17,6 @@ ...@@ -17,10 +17,6 @@
namespace paddle { namespace paddle {
namespace lite { namespace lite {
#ifdef LITE_WITH_NPU
std::string Context<TargetType::kNPU>::subgraph_model_cache_dir_{""}; // NOLINT
#endif
#ifdef LITE_WITH_MLU #ifdef LITE_WITH_MLU
int Context<TargetType::kMLU>::next_queue_id_{0}; int Context<TargetType::kMLU>::next_queue_id_{0};
std::map<int, int> Context<TargetType::kMLU>::queue_id_map_; std::map<int, int> Context<TargetType::kMLU>::queue_id_map_;
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "lite/core/device_info.h" #include "lite/core/device_info.h"
#include "lite/core/scope.h"
#include "lite/core/target_wrapper.h" #include "lite/core/target_wrapper.h"
#include "lite/core/tensor.h" #include "lite/core/tensor.h"
#include "lite/utils/all.h" #include "lite/utils/all.h"
...@@ -84,15 +85,19 @@ class Context<TargetType::kNPU> { ...@@ -84,15 +85,19 @@ class Context<TargetType::kNPU> {
NPUContext& operator=(const NPUContext& ctx) {} NPUContext& operator=(const NPUContext& ctx) {}
std::string name() const { return "NPUContext"; } std::string name() const { return "NPUContext"; }
static void SetSubgraphModelCacheDir(std::string subgraph_model_cache_dir) { static void SetSubgraphModelCacheDir(Scope* scope,
subgraph_model_cache_dir_ = subgraph_model_cache_dir; std::string subgraph_model_cache_dir) {
auto var = scope->Var("SUBGRAPH_MODEL_CACHE_DIR");
CHECK(var);
auto data = var->GetMutable<std::string>();
CHECK(data);
*data = subgraph_model_cache_dir;
} }
static std::string SubgraphModelCacheDir() { static std::string SubgraphModelCacheDir(Scope* scope) {
return subgraph_model_cache_dir_; auto var = scope->FindVar("SUBGRAPH_MODEL_CACHE_DIR");
if (!var) return "";
return var->Get<std::string>();
} }
private:
static std::string subgraph_model_cache_dir_;
}; };
#endif #endif
......
...@@ -33,6 +33,7 @@ lite_cc_library(mir_passes ...@@ -33,6 +33,7 @@ lite_cc_library(mir_passes
elimination/identity_dropout_eliminate_pass.cc elimination/identity_dropout_eliminate_pass.cc
elimination/elementwise_mul_constant_eliminate_pass.cc elimination/elementwise_mul_constant_eliminate_pass.cc
elimination/remove_tf_redundant_ops_pass.cc elimination/remove_tf_redundant_ops_pass.cc
elimination/control_flow_op_unused_inputs_and_outputs_eliminate_pass.cc
static_kernel_pick_pass.cc static_kernel_pick_pass.cc
variable_place_inference_pass.cc variable_place_inference_pass.cc
type_target_cast_pass.cc type_target_cast_pass.cc
......
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lite/core/mir/elimination/control_flow_op_unused_inputs_and_outputs_eliminate_pass.h"
#include <algorithm>
#include <list>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "lite/core/mir/pass_registry.h"
namespace paddle {
namespace lite {
namespace mir {
// Remove all of the unused nodes from the contorl flow op and update the inputs
// and outputs of the op info The unused nodes are defined as the nodes which
// are only linked to the control flow op nodes but nerver linked to the other
// op nodes.
//
// For example:
// graph[0]: main block
// in_x
// in_f | in_z(unused node)
// \ | /
// \ | /
// in_w ------- while ------- in_y(unused_node)
// / |
// / |
// (unused node)out_y |
// out_x
//
// graph[1]: sub block
// in_x
// |
// |
// conv2d----in_f
// |
// |
// fc ------in_w
// |
// |
// softmax
// |
// |
// out_x
//
// After the pass is applied:
// in_x
// in_f |
// \ |
// \ |
// in_w ------- while
// |
// |
// |
// out_x
// Remove the var node from var2rm if it is recursively referred to any op in
// the subblock
void CollectUnusedInputOutputNodes(
int block_idx,
std::vector<std::unique_ptr<mir::SSAGraph>>* graphs,
const std::unordered_set<std::string>& control_flow_op_types,
std::unordered_map<std::string, Node*>* in_vars2rm,
std::unordered_map<std::string, Node*>* out_vars2rm) {
auto block_size = graphs->size();
for (auto& op_node : (*graphs)[block_idx]->StmtTopologicalOrder()) {
if (!op_node->IsStmt()) continue;
auto op_info = op_node->AsStmt().op_info();
auto op_type = op_info->Type();
if (control_flow_op_types.count(op_type)) {
int sub_block_idx = op_info->GetAttr<int32_t>("sub_block");
CHECK(block_idx >= 0 && block_idx < block_size);
CollectUnusedInputOutputNodes(sub_block_idx,
graphs,
control_flow_op_types,
in_vars2rm,
out_vars2rm);
} else {
for (auto& var_node : op_node->inlinks) {
auto& var_name = var_node->AsArg().name;
if (in_vars2rm->count(var_name)) {
in_vars2rm->erase(var_name);
}
}
for (auto& var_node : op_node->outlinks) {
auto& var_name = var_node->AsArg().name;
// Tensor array may be only used as the output vars in the sublock
if (in_vars2rm->count(var_name)) {
in_vars2rm->erase(var_name);
}
if (out_vars2rm->count(var_name)) {
out_vars2rm->erase(var_name);
}
}
}
}
}
// Remove the unused var nodes from the graph and update the op_info of the
// control flow op
void RemoveNodesFromGraphAndUpdateOpInfo(
SSAGraph* graph,
Node* op_node,
const std::unordered_map<std::string, Node*>& in_vars2rm,
const std::unordered_map<std::string, Node*>& out_vars2rm) {
auto op_info = op_node->AsStmt().mutable_op_info();
auto op_type = op_info->Type();
// Unlink the in_vars2rm and out_vars2rm from the control flow op node, and
// remove them if nerver used.
for (auto& var_node : in_vars2rm) {
VLOG(3) << "in var node '" << var_node.first << "' is unlinked to "
<< op_type;
RemoveDirectedLink(var_node.second, op_node);
}
for (auto& var_node : out_vars2rm) {
VLOG(3) << "out var node '" << var_node.first << "' is unlinked from "
<< op_type;
RemoveDirectedLink(op_node, var_node.second);
// Unlink from all of the out op nodes.
std::unordered_set<Node*> out_op_nodes;
for (auto* out_op_node : var_node.second->outlinks) {
if (!out_op_nodes.count(out_op_node)) {
out_op_nodes.insert(out_op_node);
}
}
for (auto* out_op_node : out_op_nodes) {
RemoveDirectedLink(var_node.second, out_op_node);
}
}
// Remove the unused nodes from the graph if their inlinks and outlinks are
// empty
std::unordered_set<const Node*> removed_var_nodes;
for (auto& var_node : in_vars2rm) {
if (var_node.second->inlinks.empty() && var_node.second->outlinks.empty() &&
!removed_var_nodes.count(var_node.second)) {
removed_var_nodes.insert(var_node.second);
graph->RemoveNode(var_node.second);
VLOG(3) << "in var node " << var_node.first << " is removed";
}
}
for (auto& var_node : out_vars2rm) {
if (var_node.second->inlinks.empty() && var_node.second->outlinks.empty() &&
!removed_var_nodes.count(var_node.second)) {
removed_var_nodes.insert(var_node.second);
graph->RemoveNode(var_node.second);
VLOG(3) << "out var node " << var_node.first << " is removed";
}
}
// Update the op info of the control flow op
for (auto& input : *op_info->mutable_inputs()) {
for (auto var = input.second.begin(); var != input.second.end();) {
if (in_vars2rm.count(*var)) {
var = input.second.erase(var);
} else {
++var;
}
}
}
for (auto& output : *op_info->mutable_outputs()) {
for (auto var = output.second.begin(); var != output.second.end();) {
if (out_vars2rm.count(*var)) {
var = output.second.erase(var);
} else {
++var;
}
}
}
}
void ControlFlowOpUnusedInputsAndOutputsEliminatePass::SetAllGraphs(
std::vector<std::unique_ptr<mir::SSAGraph>>* graphs) {
CHECK(graphs && !graphs->empty());
graphs_ = graphs;
}
void ControlFlowOpUnusedInputsAndOutputsEliminatePass::Apply(
const std::unique_ptr<SSAGraph>& graph) {
// Remove the unused input and output nodes from the control flow op nodes
// Which are only linked to the control flow op nodes but nerver linked to the
// other op nodes
const std::unordered_set<std::string> control_flow_op_types = {
"while", "conditional_block"};
auto block_size = graphs_->size();
for (auto& op_node : graph->StmtTopologicalOrder()) {
if (!op_node->IsStmt()) continue;
auto op_info = op_node->AsStmt().mutable_op_info();
auto op_type = op_info->Type();
if (!control_flow_op_types.count(op_type)) continue;
int sub_block_idx = op_info->GetAttr<int32_t>("sub_block");
CHECK(sub_block_idx >= 0 && sub_block_idx < block_size);
// Initialize the unused nodes with all of the input and output nodes
std::unordered_map<std::string, Node *> in_vars2rm, out_vars2rm;
for (auto* var_node : op_node->inlinks) {
auto& var_name = var_node->AsArg().name;
if (!in_vars2rm.count(var_name)) {
in_vars2rm.insert(std::pair<std::string, Node*>(var_name, var_node));
}
}
for (auto* var_node : op_node->outlinks) {
auto& var_name = var_node->AsArg().name;
if (!out_vars2rm.count(var_name)) {
out_vars2rm.insert(std::pair<std::string, Node*>(var_name, var_node));
}
}
// Remove the nodes which used in subblock recursively, and the remaining
// nodes are the unused one.
CollectUnusedInputOutputNodes(sub_block_idx,
graphs_,
control_flow_op_types,
&in_vars2rm,
&out_vars2rm);
if (in_vars2rm.size() > 0 || out_vars2rm.size() > 0) {
// Remove the unused nodes from graph, and update the op info of the
// control flow op
RemoveNodesFromGraphAndUpdateOpInfo(
graph.get(), op_node, in_vars2rm, out_vars2rm);
}
}
}
} // namespace mir
} // namespace lite
} // namespace paddle
REGISTER_MIR_PASS(
control_flow_op_unused_inputs_and_outputs_eliminate_pass,
paddle::lite::mir::ControlFlowOpUnusedInputsAndOutputsEliminatePass)
.BindTargets({TARGET(kNPU)});
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <limits>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "lite/core/mir/pass.h"
#include "lite/core/types.h"
namespace paddle {
namespace lite {
namespace mir {
class ControlFlowOpUnusedInputsAndOutputsEliminatePass : public mir::StmtPass {
public:
void Apply(const std::unique_ptr<SSAGraph> &graph) override;
void SetAllGraphs(std::vector<std::unique_ptr<mir::SSAGraph>> *graphs);
private:
std::vector<std::unique_ptr<mir::SSAGraph>> *graphs_;
};
} // namespace mir
} // namespace lite
} // namespace paddle
...@@ -383,10 +383,10 @@ class XPUSingleEncoderFuser : public FuseBase { ...@@ -383,10 +383,10 @@ class XPUSingleEncoderFuser : public FuseBase {
op_desc.SetAttr<std::string>("act_type", act_type_); op_desc.SetAttr<std::string>("act_type", act_type_);
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
auto* single_encoder_stmt = matched.at("q_mul")->stmt(); auto* single_encoder_stmt = matched.at("q_mul")->stmt();
fake_subgraph_op->Attach(op_desc, single_encoder_stmt->op()->scope()); fake_subgraph_op->Attach(op_desc, single_encoder_stmt->op()->scope());
fake_subgraph_op->SetValidPlaces(single_encoder_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(single_encoder_stmt->op()->valid_places());
......
...@@ -373,10 +373,10 @@ class XPUResNetCbamBlock0Fuser : public FuseBase { ...@@ -373,10 +373,10 @@ class XPUResNetCbamBlock0Fuser : public FuseBase {
auto block0_stmt = matched.at("left_conv1")->stmt(); auto block0_stmt = matched.at("left_conv1")->stmt();
// block0_stmt->ResetOp(op_desc, graph->valid_places()); // block0_stmt->ResetOp(op_desc, graph->valid_places());
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
fake_subgraph_op->Attach(op_desc, block0_stmt->op()->scope()); fake_subgraph_op->Attach(op_desc, block0_stmt->op()->scope());
fake_subgraph_op->SetValidPlaces(block0_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(block0_stmt->op()->valid_places());
block0_stmt->SetOp(fake_subgraph_op); block0_stmt->SetOp(fake_subgraph_op);
...@@ -693,10 +693,10 @@ class XPUResNetCbamBlock1Fuser : public FuseBase { ...@@ -693,10 +693,10 @@ class XPUResNetCbamBlock1Fuser : public FuseBase {
auto block1_stmt = matched.at("right_conv1")->stmt(); auto block1_stmt = matched.at("right_conv1")->stmt();
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
fake_subgraph_op->Attach(op_desc, block1_stmt->op()->scope()); fake_subgraph_op->Attach(op_desc, block1_stmt->op()->scope());
fake_subgraph_op->SetValidPlaces(block1_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(block1_stmt->op()->valid_places());
block1_stmt->SetOp(fake_subgraph_op); block1_stmt->SetOp(fake_subgraph_op);
...@@ -932,10 +932,10 @@ class XPUResNetCbamBlock2Fuser : public FuseBase { ...@@ -932,10 +932,10 @@ class XPUResNetCbamBlock2Fuser : public FuseBase {
<< "Y of last fc must have been transposed"; << "Y of last fc must have been transposed";
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
fake_subgraph_op->Attach(op_desc, scope); fake_subgraph_op->Attach(op_desc, scope);
fake_subgraph_op->SetValidPlaces(block2_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(block2_stmt->op()->valid_places());
block2_stmt->SetOp(fake_subgraph_op); block2_stmt->SetOp(fake_subgraph_op);
......
...@@ -315,10 +315,10 @@ class XPUResNetBlock0Fuser : public FuseBase { ...@@ -315,10 +315,10 @@ class XPUResNetBlock0Fuser : public FuseBase {
auto block0_stmt = matched.at("left_conv1")->stmt(); auto block0_stmt = matched.at("left_conv1")->stmt();
// block0_stmt->ResetOp(op_desc, graph->valid_places()); // block0_stmt->ResetOp(op_desc, graph->valid_places());
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
fake_subgraph_op->Attach(op_desc, block0_stmt->op()->scope()); fake_subgraph_op->Attach(op_desc, block0_stmt->op()->scope());
fake_subgraph_op->SetValidPlaces(block0_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(block0_stmt->op()->valid_places());
block0_stmt->SetOp(fake_subgraph_op); block0_stmt->SetOp(fake_subgraph_op);
...@@ -577,10 +577,10 @@ class XPUResNetBlock1Fuser : public FuseBase { ...@@ -577,10 +577,10 @@ class XPUResNetBlock1Fuser : public FuseBase {
auto block1_stmt = matched.at("right_conv1")->stmt(); auto block1_stmt = matched.at("right_conv1")->stmt();
auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto fake_subgraph_op = LiteOpRegistry::Global().Create("subgraph");
// XXX: memleak? auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); sub_program_desc->AddBlock<cpp::BlockDesc>();
static_cast<operators::SubgraphOp*>(fake_subgraph_op.get()) static_cast<operators::SubgraphOp*>(fake_subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
fake_subgraph_op->Attach(op_desc, block1_stmt->op()->scope()); fake_subgraph_op->Attach(op_desc, block1_stmt->op()->scope());
fake_subgraph_op->SetValidPlaces(block1_stmt->op()->valid_places()); fake_subgraph_op->SetValidPlaces(block1_stmt->op()->valid_places());
block1_stmt->SetOp(fake_subgraph_op); block1_stmt->SetOp(fake_subgraph_op);
......
...@@ -39,6 +39,7 @@ void GenerateProgramPass::Apply(const std::unique_ptr<SSAGraph>& graph) { ...@@ -39,6 +39,7 @@ void GenerateProgramPass::Apply(const std::unique_ptr<SSAGraph>& graph) {
nodes_in_order = graph->StmtTopologicalOrder(); nodes_in_order = graph->StmtTopologicalOrder();
} }
insts_.emplace_back();
for (auto& item : nodes_in_order) { for (auto& item : nodes_in_order) {
if (item->IsStmt()) { if (item->IsStmt()) {
auto& stmt = item->AsStmt(); auto& stmt = item->AsStmt();
...@@ -57,7 +58,7 @@ void GenerateProgramPass::Apply(const std::unique_ptr<SSAGraph>& graph) { ...@@ -57,7 +58,7 @@ void GenerateProgramPass::Apply(const std::unique_ptr<SSAGraph>& graph) {
.SetSyncStreams(stmt.sync_streams_); .SetSyncStreams(stmt.sync_streams_);
} }
#endif #endif
insts_.emplace_back(stmt.op(), std::move(stmt.kernels().front())); insts_.back().emplace_back(stmt.op(), std::move(stmt.kernels().front()));
} }
} }
} }
......
...@@ -42,7 +42,7 @@ class GenerateProgramPass : public ProgramPass { ...@@ -42,7 +42,7 @@ class GenerateProgramPass : public ProgramPass {
} }
private: private:
std::vector<Instruction> insts_; std::vector<std::vector<Instruction>> insts_;
}; };
} // namespace mir } // namespace mir
......
...@@ -284,13 +284,19 @@ void MLUPostprocessPass::InsertBefore(SSAGraph* graph, ...@@ -284,13 +284,19 @@ void MLUPostprocessPass::InsertBefore(SSAGraph* graph,
head_node->AsArg().name, head_node->AsArg().name,
cur_node->AsArg().name); cur_node->AsArg().name);
// for subgraph op, modify the BlockDesc // for subgraph op, modify the BlockDesc
auto* sub_block_desc = dynamic_cast<paddle::lite::operators::SubgraphOp*>( auto sub_program_desc = dynamic_cast<paddle::lite::operators::SubgraphOp*>(
inst_node->AsStmt().op().get()) inst_node->AsStmt().op().get())
->GetSubBlock(); ->GetProgramDesc();
for (size_t i = 0; i < sub_block_desc->OpsSize(); ++i) { CHECK(sub_program_desc);
auto* sub_block_op_desc = sub_block_desc->GetOp<cpp::OpDesc>(i); int sub_block_idx =
UpdateInputTo( inst_node->AsStmt().op()->op_info()->GetAttr<int32_t>("sub_block");
sub_block_op_desc, head_node->AsArg().name, cur_node->AsArg().name); auto* sub_block_desc =
sub_program_desc->GetBlock<cpp::BlockDesc>(sub_block_idx);
for (size_t sub_op_idx = 0; sub_op_idx < sub_block_desc->OpsSize();
++sub_op_idx) {
auto* sub_op_desc = const_cast<cpp::OpDesc*>(
sub_block_desc->GetOp<cpp::OpDesc>(sub_op_idx));
UpdateInputTo(sub_op_desc, head_node->AsArg().name, cur_node->AsArg().name);
} }
// recreate the op // recreate the op
...@@ -444,21 +450,27 @@ void MLUPostprocessPass::InsertAfter(SSAGraph* graph, ...@@ -444,21 +450,27 @@ void MLUPostprocessPass::InsertAfter(SSAGraph* graph,
tail_node->AsArg().name, tail_node->AsArg().name,
cur_node->AsArg().name); cur_node->AsArg().name);
// for subgraph op, modify the BlockDesc // for subgraph op, modify the BlockDesc
auto* sub_block_desc = dynamic_cast<paddle::lite::operators::SubgraphOp*>( auto sub_program_desc = dynamic_cast<paddle::lite::operators::SubgraphOp*>(
inst_node->AsStmt().op().get()) inst_node->AsStmt().op().get())
->GetSubBlock(); ->GetProgramDesc();
for (size_t i = 0; i < sub_block_desc->OpsSize(); ++i) { CHECK(sub_program_desc);
auto* sub_block_op_desc = sub_block_desc->GetOp<cpp::OpDesc>(i); int sub_block_idx =
inst_node->AsStmt().op()->op_info()->GetAttr<int32_t>("sub_block");
auto* sub_block_desc =
sub_program_desc->GetBlock<cpp::BlockDesc>(sub_block_idx);
for (size_t sub_op_idx = 0; sub_op_idx < sub_block_desc->OpsSize();
++sub_op_idx) {
auto* sub_op_desc = const_cast<cpp::OpDesc*>(
sub_block_desc->GetOp<cpp::OpDesc>(sub_op_idx));
UpdateOutputTo( UpdateOutputTo(
sub_block_op_desc, tail_node->AsArg().name, cur_node->AsArg().name); sub_op_desc, tail_node->AsArg().name, cur_node->AsArg().name);
/* graph like this /* graph like this
* subgraph_op_0 * subgraph_op_0
* / \ * / \
* / \ * / \
* subgraph_op_1 host_op * subgraph_op_1 host_op
*/ */
UpdateInputTo( UpdateInputTo(sub_op_desc, tail_node->AsArg().name, cur_node->AsArg().name);
sub_block_op_desc, tail_node->AsArg().name, cur_node->AsArg().name);
} }
// recreate the op // recreate the op
...@@ -482,15 +494,22 @@ void MLUPostprocessPass::RecreateOp(Node* inst_node, SSAGraph* graph) { ...@@ -482,15 +494,22 @@ void MLUPostprocessPass::RecreateOp(Node* inst_node, SSAGraph* graph) {
} }
} }
bool MLUPostprocessPass::IsFirstConvInSubgraph(Node* arg_node, Node* inst) { bool MLUPostprocessPass::IsFirstConvInSubgraph(Node* arg_node,
auto* block_desc = Node* inst_node) {
static_cast<operators::SubgraphOp*>(inst->AsStmt().op().get()) auto sub_program_desc = dynamic_cast<paddle::lite::operators::SubgraphOp*>(
->GetSubBlock(); inst_node->AsStmt().op().get())
for (size_t op_idx = 0; op_idx < block_desc->OpsSize(); op_idx++) { ->GetProgramDesc();
auto op_desc = block_desc->GetOp<cpp::OpDesc>(op_idx); CHECK(sub_program_desc);
CHECK(op_desc); int sub_block_idx =
if (op_desc->Type() == "conv2d") { inst_node->AsStmt().op()->op_info()->GetAttr<int32_t>("sub_block");
for (auto& names : op_desc->inputs()) { auto* sub_block_desc =
sub_program_desc->GetBlock<cpp::BlockDesc>(sub_block_idx);
for (size_t sub_op_idx = 0; sub_op_idx < sub_block_desc->OpsSize();
sub_op_idx++) {
auto sub_op_desc = sub_block_desc->GetOp<cpp::OpDesc>(sub_op_idx);
CHECK(sub_op_desc);
if (sub_op_desc->Type() == "conv2d") {
for (auto& names : sub_op_desc->inputs()) {
if (std::find(names.second.begin(), if (std::find(names.second.begin(),
names.second.end(), names.second.end(),
arg_node->AsArg().name) != names.second.end()) { arg_node->AsArg().name) != names.second.end()) {
...@@ -746,19 +765,23 @@ std::pair<bool, std::string> CheckOutputAndInsert( ...@@ -746,19 +765,23 @@ std::pair<bool, std::string> CheckOutputAndInsert(
// insert cast op on mlu, to avoid cast on cpu // insert cast op on mlu, to avoid cast on cpu
void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node, void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node,
const Type* subgraph_type) { const Type* subgraph_type) {
auto subgraph_op = subgraph_node->AsStmt().op(); CHECK_EQ(subgraph_node->AsStmt().op()->Type(), "subgraph");
CHECK_EQ(subgraph_op->Type(), "subgraph"); auto subgraph_op =
auto op = dynamic_cast<operators::SubgraphOp*>(subgraph_op.get()); dynamic_cast<operators::SubgraphOp*>(subgraph_node->AsStmt().op().get());
CHECK(op); CHECK(subgraph_op);
auto block_desc = op->GetSubBlock(); auto sub_program_desc = subgraph_op->GetProgramDesc();
CHECK(sub_program_desc);
int sub_block_idx = subgraph_op->op_info()->GetAttr<int32_t>("sub_block");
auto* sub_block_desc = const_cast<cpp::BlockDesc*>(
sub_program_desc->GetBlock<cpp::BlockDesc>(sub_block_idx));
// create a new block desc to keep op sequence correct // create a new block desc to keep op sequence correct
cpp::BlockDesc* new_block_desc = new cpp::BlockDesc(); cpp::BlockDesc new_block_desc;
new_block_desc->ClearOps(); new_block_desc.ClearOps();
new_block_desc->ClearVars(); new_block_desc.ClearVars();
new_block_desc->SetIdx(block_desc->Idx()); new_block_desc.SetIdx(sub_block_desc->Idx());
new_block_desc->SetParentIdx(block_desc->ParentIdx()); new_block_desc.SetParentIdx(sub_block_desc->ParentIdx());
new_block_desc->SetForwardBlockIdx(block_desc->ForwardBlockIdx()); new_block_desc.SetForwardBlockIdx(sub_block_desc->ForwardBlockIdx());
// find all IO that is not weight or persist // find all IO that is not weight or persist
std::list<std::string> i_names, o_names; std::list<std::string> i_names, o_names;
...@@ -769,8 +792,8 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node, ...@@ -769,8 +792,8 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node,
auto input_name = input->AsArg().name; auto input_name = input->AsArg().name;
if (!(input->AsArg().is_weight || input->AsArg().is_persist)) { if (!(input->AsArg().is_weight || input->AsArg().is_persist)) {
i_names.emplace_back(input_name); i_names.emplace_back(input_name);
auto ret = CheckInputAndInsert(op->scope(), auto ret = CheckInputAndInsert(subgraph_op->scope(),
new_block_desc, &new_block_desc,
input_name, input_name,
input->AsArg().type, input->AsArg().type,
subgraph_type); subgraph_type);
...@@ -783,8 +806,8 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node, ...@@ -783,8 +806,8 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node,
auto output_name = output->AsArg().name; auto output_name = output->AsArg().name;
if (!(output->AsArg().is_weight || output->AsArg().is_persist)) { if (!(output->AsArg().is_weight || output->AsArg().is_persist)) {
o_names.emplace_back(output_name); o_names.emplace_back(output_name);
auto ret = CheckOutputAndInsert(op->scope(), auto ret = CheckOutputAndInsert(subgraph_op->scope(),
block_desc, sub_block_desc,
output_name, output_name,
output->AsArg().type, output->AsArg().type,
subgraph_type); subgraph_type);
...@@ -795,46 +818,48 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node, ...@@ -795,46 +818,48 @@ void MLUPostprocessPass::AdjustSubgraph(Node* subgraph_node,
} }
// update input and output // update input and output
for (size_t op_idx = 0; op_idx < block_desc->OpsSize(); ++op_idx) { for (size_t sub_op_idx = 0; sub_op_idx < sub_block_desc->OpsSize();
auto desc = block_desc->GetOp<cpp::OpDesc>(op_idx); ++sub_op_idx) {
auto new_desc = new_block_desc->AddOp<cpp::OpDesc>(); auto sub_op_desc = sub_block_desc->GetOp<cpp::OpDesc>(sub_op_idx);
*new_desc = *desc; auto new_op_desc = new_block_desc.AddOp<cpp::OpDesc>();
*new_op_desc = *sub_op_desc;
if (desc->Type() != "layout" && desc->Type() != "cast") {
auto op_input_args = new_desc->InputArgumentNames(); if (sub_op_desc->Type() != "layout" && sub_op_desc->Type() != "cast") {
auto op_input_args = new_op_desc->InputArgumentNames();
for (auto& input_arg : op_input_args) { for (auto& input_arg : op_input_args) {
auto op_input = new_desc->Input(input_arg); auto op_input = new_op_desc->Input(input_arg);
for (auto& it : i_names) { for (auto& it : i_names) {
auto index = std::find(op_input.begin(), op_input.end(), it); auto index = std::find(op_input.begin(), op_input.end(), it);
if (index != op_input.end() && if (index != op_input.end() &&
node_replace.find(it) != node_replace.end()) { node_replace.find(it) != node_replace.end()) {
index = op_input.erase(index); index = op_input.erase(index);
op_input.emplace(index, node_replace.at(it)); op_input.emplace(index, node_replace.at(it));
VLOG(4) << new_desc->Type() << "] change input from " << it VLOG(4) << new_op_desc->Type() << "] change input from " << it
<< " to " << node_replace.at(it); << " to " << node_replace.at(it);
} }
} }
new_desc->SetInput(input_arg, op_input); new_op_desc->SetInput(input_arg, op_input);
} }
auto op_output_args = new_desc->OutputArgumentNames(); auto op_output_args = new_op_desc->OutputArgumentNames();
for (auto& output_arg : op_output_args) { for (auto& output_arg : op_output_args) {
auto op_output = new_desc->Output(output_arg); auto op_output = new_op_desc->Output(output_arg);
for (auto& it : o_names) { for (auto& it : o_names) {
auto index = std::find(op_output.begin(), op_output.end(), it); auto index = std::find(op_output.begin(), op_output.end(), it);
if (index != op_output.end() && if (index != op_output.end() &&
node_replace.find(it) != node_replace.end()) { node_replace.find(it) != node_replace.end()) {
index = op_output.erase(index); index = op_output.erase(index);
op_output.emplace(index, node_replace.at(it)); op_output.emplace(index, node_replace.at(it));
VLOG(4) << new_desc->Type() << "] change output from " << it VLOG(4) << new_op_desc->Type() << "] change output from " << it
<< " to " << node_replace.at(it); << " to " << node_replace.at(it);
} }
} }
new_desc->SetOutput(output_arg, op_output); new_op_desc->SetOutput(output_arg, op_output);
} }
} }
} }
op->SetSubBlock(new_block_desc);
*sub_block_desc = new_block_desc;
} }
void ModifyValidPlaces(SSAGraph* graph, bool use_mlu_cast) { void ModifyValidPlaces(SSAGraph* graph, bool use_mlu_cast) {
......
...@@ -153,60 +153,61 @@ Node *SSAGraph::GraphCreateInstructNode( ...@@ -153,60 +153,61 @@ Node *SSAGraph::GraphCreateInstructNode(
} }
void SSAGraph::Build(const Program &program, void SSAGraph::Build(const Program &program,
const std::vector<Place> &valid_places) { const std::vector<Place> &valid_places,
int block_idx) {
CHECK(node_storage_.empty()); CHECK(node_storage_.empty());
auto weights_name = program.weights(); auto weights = program.weights();
auto is_weights = [&](const std::string &name) -> bool { auto is_weight = [&](const std::string &name) -> bool {
auto it = std::find(weights_name.begin(), weights_name.end(), name); auto it = std::find(weights.begin(), weights.end(), name);
if (it == weights_name.end()) return false; if (it == weights.end()) return false;
return true; return true;
}; };
std::map<std::string, PrecisionType> var_types = program.var_data_type(); auto var_type_map = program.var_type_map();
std::map<std::string, mir::Node *> arg_update_node_map;
std::map<std::string, mir::Node *> arg_update_node_map_; for (auto &op : program.ops(block_idx)) {
for (auto &op : program.ops()) {
VLOG(3) << op->op_info()->Type(); VLOG(3) << op->op_info()->Type();
auto *op_node = GraphCreateInstructNode(op, valid_places); auto *op_node = GraphCreateInstructNode(op, valid_places);
for (const std::string &name : op->op_info()->input_names()) { auto *op_info = op->op_info();
const auto &op_type = op_info->Type();
for (const auto &var_name : op_info->input_names()) {
mir::Node *arg_node = nullptr; mir::Node *arg_node = nullptr;
if (arg_update_node_map_.count(name)) { if (arg_update_node_map.count(var_name)) {
arg_node = arg_update_node_map_.at(name); arg_node = arg_update_node_map.at(var_name);
} else { } else {
node_storage_.emplace_back(); node_storage_.emplace_back();
arg_node = &node_storage_.back(); arg_node = &node_storage_.back();
arg_node->AsArg(name, node_storage_.size() - 1); arg_node->AsArg(var_name, node_storage_.size() - 1);
arg_update_node_map_[name] = arg_node; arg_update_node_map[var_name] = arg_node;
} }
if (var_types.count(name)) { if (var_type_map.count(var_name)) {
if (!arg_node->arg()->type) { if (!arg_node->arg()->type) {
arg_node->arg()->type = LiteType::GetTensorTy( arg_node->arg()->type = var_type_map[var_name];
TARGET(kUnk), var_types[name], DATALAYOUT(kUnk));
} }
// Store the original data type of the output tensors for // Store the original data type of the output tensors for
// type_precision_cast_pass, to keep the consistency between the // type_precision_cast_pass, to keep the consistency between the
// output types of original graph and optimized graph's // output types of original graph and optimized graph's
if (op->op_info()->Type() == "fetch") { if (op_type == "fetch") {
op->mutable_op_info()->SetAttr<int>( op->mutable_op_info()->SetAttr<int>(
"data_type", static_cast<int>(var_types[name])); "data_type",
static_cast<int>(var_type_map[var_name]->precision()));
} }
} }
if (is_weights(name)) arg_node->AsArg().is_weight = true; if (is_weight(var_name)) arg_node->AsArg().is_weight = true;
CHECK(arg_node->IsRoleSet()); CHECK(arg_node->IsRoleSet());
DirectedLink(arg_node, op_node); DirectedLink(arg_node, op_node);
} }
for (const std::string &name : op->op_info()->output_names()) { for (const auto &var_name : op->op_info()->output_names()) {
node_storage_.emplace_back(); node_storage_.emplace_back();
auto *arg_node = &node_storage_.back(); auto *arg_node = &node_storage_.back();
arg_node->AsArg(name, node_storage_.size() - 1); arg_node->AsArg(var_name, node_storage_.size() - 1);
arg_update_node_map_[name] = arg_node; arg_update_node_map[var_name] = arg_node;
if (var_types.count(name) && !arg_node->arg()->type) { if (var_type_map.count(var_name) && !arg_node->arg()->type) {
arg_node->arg()->type = LiteType::GetTensorTy( arg_node->arg()->type = var_type_map[var_name];
TARGET(kUnk), var_types[name], DATALAYOUT(kUnk));
} }
if (is_weights(name)) arg_node->AsArg().is_weight = true; if (is_weight(var_name)) arg_node->AsArg().is_weight = true;
CHECK(arg_node->IsRoleSet()); CHECK(arg_node->IsRoleSet());
DirectedLink(op_node, arg_node); DirectedLink(op_node, arg_node);
} }
......
...@@ -35,9 +35,13 @@ class GraphBase {}; ...@@ -35,9 +35,13 @@ class GraphBase {};
class SSAGraph : GraphBase { class SSAGraph : GraphBase {
public: public:
// @param program: the op program // @param program: the target program with vars and ops
// @param valid_places: the valid places user set for the system. // @param valid_places: the valid places user set for the system.
void Build(const Program &program, const std::vector<Place> &valid_places); // @param block_idx: the block index in the target program, default is 0(main
// block)
void Build(const Program &program,
const std::vector<Place> &valid_places,
int block_idx = kRootBlockIdx);
void RemoveNode(const mir::Node *node); void RemoveNode(const mir::Node *node);
std::vector<mir::Node *> StmtTopologicalOrder(); std::vector<mir::Node *> StmtTopologicalOrder();
......
...@@ -411,16 +411,17 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph, ...@@ -411,16 +411,17 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph,
cpp::OpDesc subgraph_op_desc; cpp::OpDesc subgraph_op_desc;
subgraph_op_desc.SetType("subgraph"); subgraph_op_desc.SetType("subgraph");
// Create a new sub block desc for storing all of Ops and Vars of the target // Create a program desc and a block desc for storing all of Ops and Vars of
// subgraph and sub_block_idx is set as a attribute of subgraph op, // the target subgraph and sub_block_idx is set as a attribute of subgraph op,
// sub_block_idx < 0 means it's a new subgraph op // sub_block_idx = 0 means it's a new subgraph op
int sub_block_idx = -(subgraph_idx + 1); auto sub_program_desc = std::make_shared<cpp::ProgramDesc>();
auto sub_block_desc = new cpp::BlockDesc(); int sub_block_idx = 0;
auto sub_block_desc = sub_program_desc->AddBlock<cpp::BlockDesc>();
sub_block_desc->ClearOps(); sub_block_desc->ClearOps();
sub_block_desc->ClearVars(); sub_block_desc->ClearVars();
for (auto &op_node : subgraph_nodes) { for (auto &op_node : subgraph_nodes) {
auto sub_block_op_desc = sub_block_desc->AddOp<cpp::OpDesc>(); auto sub_op_desc = sub_block_desc->AddOp<cpp::OpDesc>();
*sub_block_op_desc = *op_node->AsStmt().op_info(); *sub_op_desc = *op_node->AsStmt().op_info();
} }
subgraph_op_desc.SetAttr<int32_t>("sub_block", sub_block_idx); subgraph_op_desc.SetAttr<int32_t>("sub_block", sub_block_idx);
...@@ -437,13 +438,13 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph, ...@@ -437,13 +438,13 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph,
&local_var_nodes, &local_var_nodes,
&unused_var_nodes); &unused_var_nodes);
// A simplified model without the original weight/local/unused nodes on the // A simplified model without the original weight/local/unused nodes on the
// subgraph ops will be saved only if 'SUBGRAPH_DISABLE_ONLINE_MODE' is set to // subgraph ops will be saved only if 'SUBGRAPH_ONLINE_MODE' is set to
// true and Predictor->Run(...), Predictor->Save(...) is called. // true(default) and Predictor->Run(...), Predictor->Save(...) is called.
std::set<Node *> input_var_nodes(idata_var_nodes.begin(), std::set<Node *> input_var_nodes(idata_var_nodes.begin(),
idata_var_nodes.end()); idata_var_nodes.end());
std::set<Node *> output_var_nodes(odata_var_nodes.begin(), std::set<Node *> output_var_nodes(odata_var_nodes.begin(),
odata_var_nodes.end()); odata_var_nodes.end());
if (!GetBoolFromEnv(SUBGRAPH_DISABLE_ONLINE_MODE)) { if (GetBoolFromEnv(SUBGRAPH_ONLINE_MODE, true)) {
input_var_nodes.insert(weight_var_nodes.begin(), weight_var_nodes.end()); input_var_nodes.insert(weight_var_nodes.begin(), weight_var_nodes.end());
output_var_nodes.insert(local_var_nodes.begin(), local_var_nodes.end()); output_var_nodes.insert(local_var_nodes.begin(), local_var_nodes.end());
output_var_nodes.insert(unused_var_nodes.begin(), unused_var_nodes.end()); output_var_nodes.insert(unused_var_nodes.begin(), unused_var_nodes.end());
...@@ -476,7 +477,7 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph, ...@@ -476,7 +477,7 @@ void SubgraphFuser::InsertNewNode(SSAGraph *graph,
subgraph_op_desc.SetOutput("Outputs", output_var_names); subgraph_op_desc.SetOutput("Outputs", output_var_names);
auto subgraph_op = LiteOpRegistry::Global().Create("subgraph"); auto subgraph_op = LiteOpRegistry::Global().Create("subgraph");
static_cast<operators::SubgraphOp *>(subgraph_op.get()) static_cast<operators::SubgraphOp *>(subgraph_op.get())
->SetSubBlock(sub_block_desc); ->SetProgramDesc(sub_program_desc);
auto any_op = (*subgraph_nodes.begin())->AsStmt().op(); auto any_op = (*subgraph_nodes.begin())->AsStmt().op();
subgraph_op->Attach(subgraph_op_desc, any_op->scope()); subgraph_op->Attach(subgraph_op_desc, any_op->scope());
......
...@@ -141,12 +141,11 @@ std::vector<std::string> AddFetchDesc( ...@@ -141,12 +141,11 @@ std::vector<std::string> AddFetchDesc(
} }
TEST(Subgraph, detect_simple_model) { TEST(Subgraph, detect_simple_model) {
cpp::ProgramDesc program_desc; auto program_desc = std::make_shared<cpp::ProgramDesc>();
std::vector<Place> valid_places{{TARGET(kHost), PRECISION(kFloat)}}; std::vector<Place> valid_places{{TARGET(kHost), PRECISION(kFloat)}};
auto scope = std::make_shared<Scope>(); auto scope = std::make_shared<Scope>();
// Build a simple network // Build a simple network
program_desc.ClearBlocks(); auto* block_desc = program_desc->AddBlock<cpp::BlockDesc>();
auto* block_desc = program_desc.AddBlock<cpp::BlockDesc>();
block_desc->ClearOps(); block_desc->ClearOps();
block_desc->ClearVars(); block_desc->ClearVars();
auto* var_desc = block_desc->AddVar<cpp::VarDesc>(); auto* var_desc = block_desc->AddVar<cpp::VarDesc>();
...@@ -181,13 +180,13 @@ TEST(Subgraph, detect_custom_model) { ...@@ -181,13 +180,13 @@ TEST(Subgraph, detect_custom_model) {
"the path of model files."; "the path of model files.";
return; return;
} }
cpp::ProgramDesc program_desc; auto program_desc = std::make_shared<cpp::ProgramDesc>();
auto scope = std::make_shared<Scope>(); auto scope = std::make_shared<Scope>();
LoadModelPb(FLAGS_model_dir, LoadModelPb(FLAGS_model_dir,
FLAGS_model_file, FLAGS_model_file,
FLAGS_params_file, FLAGS_params_file,
scope.get(), scope.get(),
&program_desc, program_desc.get(),
!FLAGS_model_file.empty() && !FLAGS_params_file.empty(), !FLAGS_model_file.empty() && !FLAGS_params_file.empty(),
false); false);
std::vector<Place> valid_places({ std::vector<Place> valid_places({
......
...@@ -36,14 +36,20 @@ void UpdateInputsForSubgraph(OpLite* op, ...@@ -36,14 +36,20 @@ void UpdateInputsForSubgraph(OpLite* op,
op_desc->GetAttr<std::vector<std::string>>("input_data_names"); op_desc->GetAttr<std::vector<std::string>>("input_data_names");
std::replace(input_data_names.begin(), input_data_names.end(), from, to); std::replace(input_data_names.begin(), input_data_names.end(), from, to);
op_desc->SetAttr("input_data_names", input_data_names); op_desc->SetAttr("input_data_names", input_data_names);
auto* subblock_desc = static_cast<operators::SubgraphOp*>(op)->GetSubBlock(); auto sub_program_desc =
CHECK(subblock_desc); static_cast<operators::SubgraphOp*>(op)->GetProgramDesc();
for (size_t i = 0; i < subblock_desc->OpsSize(); i++) { CHECK(sub_program_desc);
auto* subblock_op_desc = subblock_desc->GetOp<cpp::OpDesc>(i); int sub_block_idx = op_desc->GetAttr<int32_t>("sub_block");
for (auto& subblock_op_input : *subblock_op_desc->mutable_inputs()) { auto sub_block_desc =
for (auto& subblock_var_name : subblock_op_input.second) { sub_program_desc->GetBlock<cpp::BlockDesc>(sub_block_idx);
if (subblock_var_name == from) { for (size_t sub_op_idx = 0; sub_op_idx < sub_block_desc->OpsSize();
subblock_var_name = to; sub_op_idx++) {
auto sub_op_desc = const_cast<cpp::OpDesc*>(
sub_block_desc->GetOp<cpp::OpDesc>(sub_op_idx));
for (auto& sub_op_input : *sub_op_desc->mutable_inputs()) {
for (auto& sub_var_name : sub_op_input.second) {
if (sub_var_name == from) {
sub_var_name = to;
} }
} }
} }
......
...@@ -59,25 +59,46 @@ class VariablePlaceInferencePass : public DebugPass { ...@@ -59,25 +59,46 @@ class VariablePlaceInferencePass : public DebugPass {
} }
// Set the type of the weight // Set the type of the weight
void SetWeightType(Node* w, void SetWeightType(Node* weight_node,
const LiteType& type, const LiteType& type,
const std::map<std::string, bool>& lite_with_targets) { const std::map<std::string, bool>& with_targets) {
VLOG(4) << "type.precision():" << PrecisionRepr(type.precision()); VLOG(4) << "type.precision():" << PrecisionRepr(type.precision());
if (lite_with_targets.at("kFPGA")) { if (with_targets.at("kFPGA")) {
w->AsArg().type = LiteType::GetTensorTy( weight_node->AsArg().type = LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW)); TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW));
} else if (lite_with_targets.at("kOpenCL")) { } else if (with_targets.at("kOpenCL")) {
w->AsArg().type = LiteType::GetTensorTy( weight_node->AsArg().type = LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW)); TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW));
} else if (lite_with_targets.at("kCUDA")) { } else if (with_targets.at("kCUDA")) {
w->AsArg().type = LiteType::GetTensorTy( weight_node->AsArg().type = LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW)); TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kNCHW));
} else { } else {
w->AsArg().type = LiteType::GetTensorTy( weight_node->AsArg().type = LiteType::GetTensorTy(
TARGET(kHost), type.precision(), DATALAYOUT(kNCHW)); TARGET(kHost), type.precision(), DATALAYOUT(kNCHW));
} }
} }
// Update a's kUnk fields from b's fields.
void UpdateTypeFrom(const Type** a, const Type* b) {
auto target = (*a)->target();
auto precision = (*a)->precision();
auto layout = (*a)->layout();
if (target == TARGET(kUnk)) {
target = b->target();
}
if (precision == PRECISION(kUnk)) {
precision = b->precision();
}
if (layout == DATALAYOUT(kUnk)) {
layout = b->layout();
}
if ((*a)->IsTensor() && b->IsTensor()) {
*a = LiteType::GetTensorTy(target, precision, layout);
} else if ((*a)->IsTensorList() && b->IsTensorList()) {
*a = LiteType::GetTensorListTy(target, precision, layout);
}
}
void InferenceArgumentPlace(SSAGraph* graph) { void InferenceArgumentPlace(SSAGraph* graph) {
auto& valid_places = graph->valid_places(); auto& valid_places = graph->valid_places();
auto valid_places_has_target = [&](TargetType t) -> bool { auto valid_places_has_target = [&](TargetType t) -> bool {
...@@ -88,122 +109,90 @@ class VariablePlaceInferencePass : public DebugPass { ...@@ -88,122 +109,90 @@ class VariablePlaceInferencePass : public DebugPass {
} }
return false; return false;
}; };
std::map<std::string, bool> lite_with_targets{ std::map<std::string, bool> with_targets{
{"kOpenCL", valid_places_has_target(TARGET(kOpenCL))}, {"kOpenCL", valid_places_has_target(TARGET(kOpenCL))},
{"kCUDA", valid_places_has_target(TARGET(kCUDA))}, {"kCUDA", valid_places_has_target(TARGET(kCUDA))},
{"kFPGA", valid_places_has_target(TARGET(kFPGA))}}; {"kFPGA", valid_places_has_target(TARGET(kFPGA))}};
VLOG(4) << "lite_with_targets['kOpenCL']:" << lite_with_targets["kOpenCL"]; VLOG(4) << "with_targets['kOpenCL']:" << with_targets["kOpenCL"];
VLOG(4) << "lite_with_targets['kFPGA']:" << lite_with_targets["kFPGA"]; VLOG(4) << "with_targets['kFPGA']:" << with_targets["kFPGA"];
VLOG(3) << "param-type-registry:\n" << ParamTypeRegistry::Global(); VLOG(3) << "param-type-registry:\n" << ParamTypeRegistry::Global();
for (auto& x : graph->StmtTopologicalOrder()) { for (auto& node : graph->StmtTopologicalOrder()) {
auto& inst = x->AsStmt(); auto& inst = node->AsStmt();
const auto* op_info = inst.op_info();
const auto& op_type = op_info->Type();
auto& kernel = inst.picked_kernel();
// The IoCopyOp is a tool operator, it won't support the type inference. // The IoCopyOp is a tool operator, it won't support the type inference.
// in fpga, we has io_copy+cali+layout tool ops, so we need type inference // in fpga, we has io_copy+cali+layout tool ops, so we need type inference
// for // for tool operator
// tool operator if ((!with_targets["kFPGA"]) && (!with_targets["kOpenCL"])) {
if ((!lite_with_targets["kFPGA"]) && (!lite_with_targets["kOpenCL"])) { VLOG(3) << "skip 'io_copy' if target is FPGA and OpenCL";
VLOG(3) << "inst.op_type() == 'io_copy', continue"; if (op_type == "io_copy") continue;
if (inst.op_type() == "io_copy") continue;
} }
// deal with inputs
VLOG(4) << "Infering op " << inst.op_info()->Repr();
// TODO(zhaolong): Add check if the node's name in op's arguments.
auto get_argname = [&]( // Infering the input and output variable's place according to the
const std::string& node_name, // declaration of I/O arguments of the picked kernel of the op
const std::map<std::string, std::vector<std::string>>& argname_map) VLOG(4) << "Op " << op_info->Repr();
-> std::string { for (auto* in_node : node->inlinks) {
for (auto& ele : argname_map) { auto& var = in_node->AsArg();
auto it = const auto& var_name = var.name;
std::find(ele.second.begin(), ele.second.end(), node_name); auto* var_type = &var.type;
if (it != ele.second.end()) return ele.first; std::string arg_name;
} CHECK(op_info->GetInputArgname(var_name, &arg_name))
return ""; << "Can not find the input argument for var " << var_name;
}; VLOG(4) << " - input arg name:" << arg_name << " var name:" << var_name;
const auto* decl_type = kernel.GetInputDeclType(arg_name);
for (auto* x_in : x->inlinks) { if (!(*var_type)) {
std::string node_name = x_in->AsArg().name; VLOG(4) << "set type " << *decl_type << " " << var_name;
std::string arg_name = get_argname(node_name, inst.op_info()->inputs()); if (var.is_weight) {
CHECK(arg_name.size() > 0) << "can not found op arguments for node " SetWeightType(in_node, *decl_type, with_targets);
<< node_name;
VLOG(4) << "-- input arg_name:" << arg_name << " "
<< "-- node name:" << node_name;
auto type = inst.picked_kernel().GetInputDeclType(arg_name);
if (!x_in->AsArg().type) {
VLOG(4) << "set type " << *type << " " << x_in->AsArg().name;
if (x_in->AsArg().is_weight) {
SetWeightType(x_in, *type, lite_with_targets);
} else { } else {
x_in->AsArg().type = type; *var_type = decl_type;
} }
} else if (x_in->AsArg().type->target() == TARGET(kUnk) && } else if (!(*var_type)->place().is_valid()) {
x_in->AsArg().type->precision() != PRECISION(kUnk) &&
x_in->AsArg().type->layout() == DATALAYOUT(kUnk)) {
// If is quantization, infer the Int8 type. // If is quantization, infer the Int8 type.
if (type->precision() == PRECISION(kInt8)) { if (decl_type->precision() == PRECISION(kInt8)) {
x_in->AsArg().type = type; *var_type = decl_type;
} else { } else {
PrecisionType tmp_ptype = x_in->AsArg().type->precision(); UpdateTypeFrom(var_type, decl_type);
x_in->AsArg().type = LiteType::GetTensorTy(
type->target(), tmp_ptype, type->layout());
} }
} }
} }
for (auto* out_node : node->outlinks) {
VLOG(4) << "inst " << inst.op_info()->Repr(); auto& var = out_node->AsArg();
for (auto* x_out : x->outlinks) { const auto& var_name = var.name;
std::string node_name = x_out->AsArg().name; auto* var_type = &var.type;
std::string arg_name = std::string arg_name;
get_argname(node_name, inst.op_info()->outputs()); CHECK(op_info->GetOutputArgname(var_name, &arg_name))
CHECK(arg_name.size() > 0) << "can not found op arguments for node " << "Can not find the output argument for var " << var_name;
<< node_name << " in Inst " VLOG(4) << " - output arg name:" << arg_name
<< inst.op_type(); << " var name:" << var_name;
VLOG(4) << "-- output arg_name " << arg_name; const auto* decl_type = kernel.GetOutputDeclType(arg_name);
auto type = inst.picked_kernel().GetOutputDeclType(arg_name); if (!(*var_type)) {
if (!x_out->AsArg().type) { VLOG(4) << "set type " << *decl_type << " " << var_name;
VLOG(4) << "set type " << *type << " " << x_out->AsArg().name; if (var.is_weight) {
if (x_out->AsArg().is_weight) { SetWeightType(out_node, *decl_type, with_targets);
SetWeightType(x_out, *type, lite_with_targets);
} else { } else {
x_out->AsArg().type = type; *var_type = decl_type;
} }
} else if (x_out->AsArg().type->target() == TARGET(kUnk) && } else if (!(*var_type)->place().is_valid()) {
x_out->AsArg().type->precision() != PRECISION(kUnk) &&
x_out->AsArg().type->layout() == DATALAYOUT(kUnk)) {
// If is quantization, infer the Int8 type. // If is quantization, infer the Int8 type.
if (type->precision() == PRECISION(kInt8)) { if (decl_type->precision() == PRECISION(kInt8) ||
x_out->AsArg().type = type; (decl_type->precision() == PRECISION(kFP16) &&
} else if (type->precision() == PRECISION(kFP16) && decl_type->target() != TARGET(kOpenCL))) {
type->target() != TARGET(kOpenCL)) { *var_type = decl_type;
x_out->AsArg().type = type;
} else { } else {
PrecisionType tmp_ptype = x_out->AsArg().type->precision(); UpdateTypeFrom(var_type, decl_type);
x_out->AsArg().type = LiteType::GetTensorTy(
type->target(), tmp_ptype, type->layout());
} }
} }
} }
} }
} }
// Update me's kUnk fields by other's fields.
void UpdatePlace(Place* me, const Place& other) {
CHECK(other.is_valid());
if (me->target == TARGET(kUnk)) {
me->target = other.target;
}
if (me->precision == PRECISION(kUnk)) {
me->precision = other.precision;
}
if (me->layout == DATALAYOUT(kUnk)) {
me->layout = other.layout;
}
}
private: private:
// The default target for arguments, e.g. load weights to CPU memory for CUDA // The default target for arguments, e.g. load weights to CPU memory for
// computation by default. // CUDA computation by default.
TargetType argument_default_target_{TARGET(kHost)}; TargetType argument_default_target_{TARGET(kHost)};
}; };
......
...@@ -99,7 +99,7 @@ class OpLite : public Registry { ...@@ -99,7 +99,7 @@ class OpLite : public Registry {
std::vector<std::unique_ptr<KernelBase>> CreateKernels( std::vector<std::unique_ptr<KernelBase>> CreateKernels(
const std::vector<Place> &places, const std::string &kernel_type = ""); const std::vector<Place> &places, const std::string &kernel_type = "");
lite::Scope *scope() { return scope_; } Scope *scope() { return scope_; }
// Assign op param to kernel. // Assign op param to kernel.
virtual void AttachKernel(KernelBase *kernel) = 0; virtual void AttachKernel(KernelBase *kernel) = 0;
...@@ -169,7 +169,7 @@ class OpLite : public Registry { ...@@ -169,7 +169,7 @@ class OpLite : public Registry {
} }
protected: protected:
lite::Scope *scope_{nullptr}; Scope *scope_{nullptr};
std::unique_ptr<KernelBase> kernel_; std::unique_ptr<KernelBase> kernel_;
std::string op_type_; std::string op_type_;
std::vector<Place> valid_places_; std::vector<Place> valid_places_;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "lite/core/mir/elimination/control_flow_op_unused_inputs_and_outputs_eliminate_pass.h"
#include "lite/core/mir/generate_program_pass.h" #include "lite/core/mir/generate_program_pass.h"
#include "lite/core/mir/pass_manager.h" #include "lite/core/mir/pass_manager.h"
#include "lite/core/mir/pass_utils.h" #include "lite/core/mir/pass_utils.h"
...@@ -36,6 +37,9 @@ namespace lite { ...@@ -36,6 +37,9 @@ namespace lite {
* lite::Optimizer optimize a program. It utilize the mir passes to analysis the * lite::Optimizer optimize a program. It utilize the mir passes to analysis the
* program and export an optimized program. * program and export an optimized program.
*/ */
// TODO(hong1986032) Support the following passes for the subblocks
const std::set<std::string> kSubblockUnsupportedPasses(
{"memory_optimize_pass"});
class Optimizer { class Optimizer {
public: public:
Optimizer() {} Optimizer() {}
...@@ -60,14 +64,20 @@ class Optimizer { ...@@ -60,14 +64,20 @@ class Optimizer {
program_ = &program; program_ = &program;
valid_places_ = valid_places; valid_places_ = valid_places;
CHECK(!valid_places.empty()) << "At least one valid_place should be set"; CHECK(!valid_places.empty()) << "At least one valid_place should be set";
CHECK(!graph_) << "duplicate optimize found"; CHECK(graphs_.empty()) << "duplicate optimize found";
graph_.reset(new mir::SSAGraph); auto block_size = program.block_size();
graph_->Build(program, valid_places); for (size_t block_idx = 0; block_idx < block_size; ++block_idx) {
graph_->SetValidPlaces(valid_places); std::unique_ptr<mir::SSAGraph> graph;
graph.reset(new mir::SSAGraph);
graph->Build(program, valid_places, block_idx);
graph->SetValidPlaces(valid_places);
graphs_.emplace_back(std::move(graph));
}
SpecifyKernelPickTactic(kernel_pick_factor); SpecifyKernelPickTactic(kernel_pick_factor);
InitTargetTypeTransformPass(); InitTargetTypeTransformPass();
InitControlFlowOpUnusedInputsAndOutputsEliminatePass();
if (passes.empty() || passes.size() == 1) { if (passes.empty() || passes.size() == 1) {
std::vector<std::string> passes_local{ std::vector<std::string> passes_local{
...@@ -112,6 +122,7 @@ class Optimizer { ...@@ -112,6 +122,7 @@ class Optimizer {
"apu_subgraph_pass", "apu_subgraph_pass",
"rknpu_subgraph_pass", "rknpu_subgraph_pass",
"mlu_subgraph_pass", "mlu_subgraph_pass",
"control_flow_op_unused_inputs_and_outputs_eliminate_pass",
"static_kernel_pick_pass", // pick original kernel from graph "static_kernel_pick_pass", // pick original kernel from graph
"remove_tf_redundant_ops_pass", "remove_tf_redundant_ops_pass",
...@@ -176,62 +187,15 @@ class Optimizer { ...@@ -176,62 +187,15 @@ class Optimizer {
exec_scope_ = program.exec_scope(); exec_scope_ = program.exec_scope();
} }
const lite::Scope* exec_scope() const { return exec_scope_; } const Scope* exec_scope() const { return exec_scope_; }
// Set shape(dims) infos of var descs to scope var.
// developer can write pass using input / output tensor dims of op.
//
// Example: If you have node `Node* softmax_node`,
// you can get dims of output tensor in passes:
//
// auto* scope = softmax_node->AsStmt().op()->scope();
// auto softmax_out_arg_name =
// softmax_node->outlinks.front()->AsArg().name;
// auto softmax_out_tensor =
// scope->FindVar(softmax_out_arg_name)->Get<lite::Tensor>();
// softmax_out_dims = softmax_out_tensor.dims();
void SetVarDescShapeToScopeVar() {
auto dims_to_str_func = [](std::vector<int64_t> shape) -> std::string {
std::string str_res;
for (size_t i = 0; i < shape.size(); ++i) {
str_res += std::to_string(shape[i]);
if (i != shape.size() - 1) {
str_res += "x";
}
}
return str_res;
};
auto* program_desc = program_->program_desc();
VLOG(5) << "program_desc->BlocksSize():" << program_desc->BlocksSize();
auto blocks_desc = program_desc->GetBlocks();
for (size_t bidx = 0; bidx < blocks_desc.size(); ++bidx) {
auto block_desc = blocks_desc[bidx];
auto vars_desc = block_desc.GetVars();
for (size_t vidx = 0; vidx < vars_desc.size(); ++vidx) {
auto var_desc = vars_desc[vidx];
VLOG(5) << var_desc.Name() << " "
<< dims_to_str_func(var_desc.GetShape());
if (var_desc.Name() == "feed" || var_desc.Name() == "fetch") continue;
auto* var = program_->exec_scope()->FindVar(var_desc.Name());
auto tensor = var->GetMutable<lite::Tensor>();
if (tensor->dims().size() == 0 && var_desc.GetShape().size() != 0) {
VLOG(5) << "var_desc.Name():" << var_desc.Name()
<< " shape:" << dims_to_str_func(var_desc.GetShape());
tensor->Resize(var_desc.GetShape());
}
VLOG(5) << "var_desc.Name():" << var_desc.Name()
<< " shape:" << dims_to_str_func(var_desc.GetShape())
<< " tensor:" << tensor->dims();
}
}
}
// Generate a new program based on the mir graph. // Generate a new program based on the mir graph.
std::unique_ptr<RuntimeProgram> GenRuntimeProgram() { std::unique_ptr<RuntimeProgram> GenRuntimeProgram() {
auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>( auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>(
"generate_program_pass"); "generate_program_pass");
pass->Apply(graph_); for (auto& graph : graphs_) {
pass->Apply(graph);
}
auto program = pass->GenProgram(); auto program = pass->GenProgram();
CHECK(exec_scope_); CHECK(exec_scope_);
program->set_exec_scope(exec_scope_); program->set_exec_scope(exec_scope_);
...@@ -247,27 +211,38 @@ class Optimizer { ...@@ -247,27 +211,38 @@ class Optimizer {
pass->SetValidPlaces(valid_places_); pass->SetValidPlaces(valid_places_);
} }
void InitControlFlowOpUnusedInputsAndOutputsEliminatePass() {
auto* pass =
mir::PassManager::Global()
.LookUp<mir::ControlFlowOpUnusedInputsAndOutputsEliminatePass>(
"control_flow_op_unused_inputs_and_outputs_eliminate_pass");
CHECK(pass);
CHECK(!graphs_.empty());
pass->SetAllGraphs(&graphs_);
}
// Generate C++ code which combines the inference program, model and weights. // Generate C++ code which combines the inference program, model and weights.
void GenCode(const std::string& code_dir); void GenCode(const std::string& code_dir);
const mir::SSAGraph& ssa_graph() const { const mir::SSAGraph& ssa_graph(int block_idx = kRootBlockIdx) const {
CHECK(graph_); CHECK(!graphs_.empty());
return *graph_; CHECK(graphs_[block_idx]);
return *graphs_[block_idx];
} }
mir::SSAGraph* mutable_ssa_graph() { mir::SSAGraph* mutable_ssa_graph(int block_idx = kRootBlockIdx) {
CHECK(graph_); CHECK(!graphs_.empty());
return graph_.get(); CHECK(graphs_[block_idx]);
return graphs_[block_idx].get();
} }
lite::Scope* exec_scope() { return exec_scope_; } Scope* exec_scope() { return exec_scope_; }
protected: protected:
void SpecifyKernelPickTactic(core::KernelPickFactor factor); void SpecifyKernelPickTactic(core::KernelPickFactor factor);
// Specify the passes and run them. // Specify the passes and run them.
void RunPasses(const std::vector<std::string>& passes) { void RunPasses(const std::vector<std::string>& passes) {
SetVarDescShapeToScopeVar();
for (auto& x : passes) { for (auto& x : passes) {
LOG(INFO) << "== Running pass: " << x; LOG(INFO) << "== Running pass: " << x;
mir::Pass* pass = mir::PassManager::Global().LookUp(x); mir::Pass* pass = mir::PassManager::Global().LookUp(x);
...@@ -285,16 +260,23 @@ class Optimizer { ...@@ -285,16 +260,23 @@ class Optimizer {
LOG(INFO) << " - Skip " << x LOG(INFO) << " - Skip " << x
<< " because the target or kernel does not match."; << " because the target or kernel does not match.";
} else { } else {
pass->Apply(graph_); // Check the pass whether it is supported for processing subblocks
if (kSubblockUnsupportedPasses.count(x)) {
pass->Apply(graphs_[kRootBlockIdx]);
} else {
for (auto& graph : graphs_) {
pass->Apply(graph);
}
}
LOG(INFO) << "== Finished running: " << x; LOG(INFO) << "== Finished running: " << x;
} }
} }
} }
private: private:
std::unique_ptr<mir::SSAGraph> graph_; std::vector<std::unique_ptr<mir::SSAGraph>> graphs_;
std::vector<Place> valid_places_; std::vector<Place> valid_places_;
lite::Scope* exec_scope_{}; Scope* exec_scope_{};
Program* program_{}; Program* program_{};
}; };
......
此差异已折叠。
...@@ -41,61 +41,72 @@ static const char kKernelTypeAttr[] = "__@kernel_type_attr@__"; ...@@ -41,61 +41,72 @@ static const char kKernelTypeAttr[] = "__@kernel_type_attr@__";
// - scope: which contains all the weights // - scope: which contains all the weights
struct Program { struct Program {
public: public:
explicit Program(const std::shared_ptr<Scope>& root) { scope_ = root; } explicit Program(const std::shared_ptr<Scope>& root_scope) {
Program(const cpp::ProgramDesc& desc, scope_ = root_scope;
const std::shared_ptr<Scope>& root, }
Program(const std::shared_ptr<cpp::ProgramDesc>& program_desc,
const std::shared_ptr<Scope>& root_scope,
const std::vector<Place>& valid_places, const std::vector<Place>& valid_places,
const std::vector<std::string>& var_names = {}) const std::vector<std::string>& vars_to_clone = {})
: scope_(root), valid_places_(valid_places) { : scope_(root_scope),
desc_.CopyFrom(desc); valid_places_(valid_places),
program_desc_(program_desc) {
CHECK(scope_) << "scope should be init first"; CHECK(scope_) << "scope should be init first";
VLOG(4) << "prepare work"; VLOG(4) << "prepare work";
PrepareWorkspace(desc, var_names); PrepareWorkspace(program_desc_, vars_to_clone);
VLOG(4) << "build desc"; VLOG(4) << "build desc";
Build(desc); Build(program_desc_);
VLOG(4) << "build desc finished"; VLOG(4) << "build desc finished";
} }
std::unique_ptr<Program> Clone() const { std::unique_ptr<Program> Clone() const {
std::unique_ptr<Program> res(new Program(desc_, scope_, valid_places_)); return std::unique_ptr<Program>(
return res; new Program(program_desc_, scope_, valid_places_));
} }
const std::list<std::string>& weights() const { return weights_; } const std::list<std::string>& weights() const { return weights_; }
const std::list<std::string>& tmp_vars() const { return tmp_vars_; } const std::list<std::string>& vars() const { return vars_; }
std::list<std::string>* mutable_weights() { return &weights_; } std::list<std::string>* mutable_weights() { return &weights_; }
std::list<std::string>* mutable_tmp_vars() { return &tmp_vars_; } std::list<std::string>* mutable_vars() { return &vars_; }
const std::list<std::shared_ptr<OpLite>>& ops() const { return ops_; } const std::list<std::shared_ptr<OpLite>>& ops(
std::list<std::shared_ptr<OpLite>>* mutable_ops() { return &ops_; } int block_idx = kRootBlockIdx) const {
return ops_[block_idx];
}
std::list<std::shared_ptr<OpLite>>* mutable_ops(
int block_idx = kRootBlockIdx) {
return &ops_[block_idx];
}
lite::Scope* exec_scope() { return exec_scope_; } size_t block_size() { return ops_.size(); }
lite::Scope* scope() { return scope_.get(); }
cpp::ProgramDesc* program_desc() { return &desc_; } Scope* exec_scope() { return exec_scope_; }
Scope* scope() { return scope_.get(); }
const std::map<std::string, PrecisionType>& var_data_type() const { cpp::ProgramDesc* program_desc() { return program_desc_.get(); }
return var_data_type_;
const std::map<std::string, const Type*>& var_type_map() const {
return var_type_map_;
} }
private: private:
// Build from a program and scope. // Build from a program and scope.
void Build(const cpp::ProgramDesc& program); void Build(const std::shared_ptr<cpp::ProgramDesc>& program_desc);
// Create temporary variables. // Create temporary variables.
void PrepareWorkspace(const cpp::ProgramDesc& program, void PrepareWorkspace(const std::shared_ptr<cpp::ProgramDesc>& program_desc,
const std::vector<std::string>& var_names = {}); const std::vector<std::string>& vars_to_clone = {});
private: private:
std::map<std::string, PrecisionType> var_data_type_; std::map<std::string, const Type*> var_type_map_;
std::list<std::string> tmp_vars_; std::list<std::string> vars_;
std::list<std::string> weights_; std::list<std::string> weights_;
std::list<std::shared_ptr<OpLite>> ops_; std::vector<std::list<std::shared_ptr<OpLite>>> ops_;
// the scope to run the kernels, NOTE this is the execution scope. // the scope to run the kernels, NOTE this is the execution scope.
std::shared_ptr<lite::Scope> scope_; std::shared_ptr<Scope> scope_;
std::vector<Place> valid_places_; std::vector<Place> valid_places_;
// Runtime scope. // Runtime scope.
lite::Scope* exec_scope_{}; Scope* exec_scope_{};
cpp::ProgramDesc desc_; std::shared_ptr<cpp::ProgramDesc> program_desc_;
}; };
struct Instruction { struct Instruction {
...@@ -173,8 +184,22 @@ struct Instruction { ...@@ -173,8 +184,22 @@ struct Instruction {
*/ */
class LITE_API RuntimeProgram { class LITE_API RuntimeProgram {
public: public:
explicit RuntimeProgram(std::vector<Instruction>&& insts) explicit RuntimeProgram(std::vector<std::vector<Instruction>>&& insts)
: instructions_(std::move(insts)) { : instructions_(std::move(insts)) {
Init();
}
explicit RuntimeProgram(
const std::shared_ptr<const cpp::ProgramDesc>& program_desc,
Scope* exec_scope,
int block_idx = kRootBlockIdx);
~RuntimeProgram() {
#ifdef LITE_WITH_PROFILE
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kCreate);
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kDispatch);
#endif // LITE_WITH_PROFILE
}
void Init() {
if (instructions_.empty()) { if (instructions_.empty()) {
LOG(FATAL) << "no instructions"; LOG(FATAL) << "no instructions";
} }
...@@ -183,7 +208,7 @@ class LITE_API RuntimeProgram { ...@@ -183,7 +208,7 @@ class LITE_API RuntimeProgram {
#endif #endif
#ifdef LITE_WITH_NVTX #ifdef LITE_WITH_NVTX
const NVTXAnnotator& annotator = NVTXAnnotator::Global(); const NVTXAnnotator& annotator = NVTXAnnotator::Global();
for (auto& inst : instructions_) { for (auto& inst : instructions_[kRootBlockIdx]) {
NVTXRangeAnnotation annotation = annotator.AnnotateBlock(); NVTXRangeAnnotation annotation = annotator.AnnotateBlock();
register_layer_names_.push_back(annotator.RegisterString( register_layer_names_.push_back(annotator.RegisterString(
const_cast<paddle::lite::OpLite*>(inst.op())->Type().c_str())); const_cast<paddle::lite::OpLite*>(inst.op())->Type().c_str()));
...@@ -191,41 +216,38 @@ class LITE_API RuntimeProgram { ...@@ -191,41 +216,38 @@ class LITE_API RuntimeProgram {
register_layer_names_.push_back(annotator.RegisterString("one_loop")); register_layer_names_.push_back(annotator.RegisterString("one_loop"));
#endif #endif
} }
~RuntimeProgram() {
#ifdef LITE_WITH_PROFILE
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kCreate);
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kDispatch);
#endif // LITE_WITH_PROFILE
}
void Run(); void Run();
void set_exec_scope(lite::Scope* x) { exec_scope_ = x; } void set_exec_scope(Scope* x) { exec_scope_ = x; }
lite::Scope* exec_scope() { return exec_scope_; } Scope* exec_scope() { return exec_scope_; }
size_t num_instructions() const { return instructions_.size(); } const std::vector<Instruction>& instructions(
int block_idx = kRootBlockIdx) const {
return instructions_[block_idx];
}
const std::vector<Instruction>& instructions() const { return instructions_; } std::vector<Instruction>* mutable_instructions(
int block_idx = kRootBlockIdx) {
return &instructions_[block_idx];
}
// `SaveOpInfosToProgram` will update the op list(ops_) of the block 0 size_t block_size() { return instructions_.size(); }
// in ProgramDesc.
void SaveOpInfosToProgram(cpp::ProgramDesc* desc);
// `UpdateVarsOfProgram` will update the var list(vars_) of the block 0 in // Update the ops and vars of all of blocks to the given program_desc
// ProgramDesc. Namely, if a new var created in some passes, its var_desc will // according to the instructions
// be added in vars_. void SaveToProgram(std::shared_ptr<cpp::ProgramDesc> program_desc);
void UpdateVarsOfProgram(cpp::ProgramDesc* desc);
private: private:
RuntimeProgram(const RuntimeProgram&) = delete; RuntimeProgram(const RuntimeProgram&) = delete;
std::vector<Instruction> instructions_; std::vector<std::vector<Instruction>> instructions_;
lite::Scope* exec_scope_{}; Scope* exec_scope_{};
#ifdef LITE_WITH_PROFILE #ifdef LITE_WITH_PROFILE
profile::Profiler profiler_; profile::Profiler profiler_;
void set_profiler() { void set_profiler() {
for (auto i = instructions_.begin(); i != instructions_.end(); ++i) { for (auto& inst : instructions_[kRootBlockIdx]) {
i->set_profiler(&profiler_); inst.set_profiler(&profiler_);
} }
} }
#endif #endif
......
...@@ -37,7 +37,7 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -37,7 +37,7 @@ bool SubgraphEngine::BuildDeviceProgram() {
subgraph::apu::Graph graph; subgraph::apu::Graph graph;
int neuron_errCode = NeuronModel_create(&model_); int neuron_errCode = NeuronModel_create(&model_);
if (NEURON_NO_ERROR != neuron_errCode) { if (NEURON_NO_ERROR != neuron_errCode) {
LOG(WARNING) << "Fail to create model"; LOG(WARNING) << "[APU] Failed to create the neuron model!";
return false; return false;
} }
graph.set_model(model_); graph.set_model(model_);
...@@ -46,11 +46,12 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -46,11 +46,12 @@ bool SubgraphEngine::BuildDeviceProgram() {
// Convert all of ops and their input vars and weights and added into the APU // Convert all of ops and their input vars and weights and added into the APU
// NIR graph // NIR graph
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = const_cast<OpLite*>(inst.op()); auto op = const_cast<OpLite*>(inst.op());
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -70,55 +71,38 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -70,55 +71,38 @@ bool SubgraphEngine::BuildDeviceProgram() {
} }
} }
// Get input tensor // Get the index of input tensors
std::vector<uint32_t> ins; std::vector<uint32_t> input_indices;
origin_itensors_.resize(input_names_.size());
origin_idims_.resize(input_names_.size());
for (int i = 0; i < input_names_.size(); i++) { for (int i = 0; i < input_names_.size(); i++) {
origin_itensors_[i] = scope_->FindMutableTensor(input_names_[i]); CHECK(graph.Has(input_names_[i])) << "[APU] Failed to find input node "
CHECK(origin_itensors_[i]); << input_names_[i];
origin_idims_[i] = origin_itensors_[i]->dims(); auto index = graph.Get(input_names_[i])->index();
VLOG(3) << "subgraph input name: " << i << ", " << input_names_[i] << ":" input_indices.push_back(index);
<< origin_idims_[i].production(); VLOG(3) << "[APU] Input[" << i << "] name " << input_names_[i] << " dims "
// Get input index << origin_itensors_[i]->dims() << " index " << index;
int idx;
if (graph.Has(input_names_[i])) {
ins.push_back(graph.Get(input_names_[i])->index());
VLOG(3) << "input idx: " << graph.Get(input_names_[i])->index();
} else {
LOG(WARNING) << "Fail to find input: " << input_names_[i];
return false;
}
} }
// Get output tensor // Get the index of output tensors
std::vector<uint32_t> outs; std::vector<uint32_t> output_indices;
origin_otensors_.resize(output_names_.size());
origin_odims_.resize(output_names_.size());
for (int i = 0; i < output_names_.size(); i++) { for (int i = 0; i < output_names_.size(); i++) {
origin_otensors_[i] = scope_->FindMutableTensor(output_names_[i]); CHECK(graph.Has(output_names_[i])) << "[APU] Failed to find output node "
CHECK(origin_otensors_[i]); << output_names_[i];
origin_odims_[i] = origin_otensors_[i]->dims();
VLOG(3) << "subgraph output name: " << i << ", " << output_names_[i] << ":"
<< origin_odims_[i].production();
origin_otensors_[i]->mutable_data<int8_t>(); origin_otensors_[i]->mutable_data<int8_t>();
// Get input index auto index = graph.Get(output_names_[i])->index();
if (graph.Has(output_names_[i])) { output_indices.push_back(index);
outs.push_back(graph.Get(output_names_[i])->index()); VLOG(3) << "[APU] Output[" << i << "] name " << output_names_[i] << " dims "
VLOG(3) << "output idx: " << graph.Get(output_names_[i])->index(); << origin_otensors_[i]->dims() << " index " << index;
} else {
LOG(WARNING) << "Fail to find output: " << output_names_[i];
return false;
}
} }
VLOG(3) << "ins size: " << ins.size() << " outs size:" << outs.size(); // Indentify the input and output tensors of the neuron model
// Set subgraph input/output NeuronModel_identifyInputsAndOutputs(model_,
NeuronModel_identifyInputsAndOutputs( input_indices.size(),
model_, ins.size(), &ins[0], outs.size(), &outs[0]); &input_indices[0],
output_indices.size(),
&output_indices[0]);
neuron_errCode = NeuronModel_finish(model_); neuron_errCode = NeuronModel_finish(model_);
if (NEURON_NO_ERROR != neuron_errCode) { if (NEURON_NO_ERROR != neuron_errCode) {
LOG(WARNING) << "Fail to create NIR model:" << neuron_errCode; LOG(WARNING) << "[APU] Fail to create NIR model:" << neuron_errCode;
return false; return false;
} }
VLOG(3) << "[APU] APU NIR model created!"; VLOG(3) << "[APU] APU NIR model created!";
...@@ -207,11 +191,11 @@ SubgraphEngine::~SubgraphEngine() { ...@@ -207,11 +191,11 @@ SubgraphEngine::~SubgraphEngine() {
void SubgraphCompute::PrepareForRun() { void SubgraphCompute::PrepareForRun() {
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
engine_.reset(new SubgraphEngine(ctx_.get(), engine_.reset(new SubgraphEngine(ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names));
param.scope));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -31,12 +31,16 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -31,12 +31,16 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext *ctx, SubgraphEngine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names)
Scope *scope) : subgraph::Engine(ctx,
: subgraph::Engine( block_idx,
ctx, block_idx, block_desc, input_names, output_names, scope) {} program_desc,
exec_scope,
input_names,
output_names) {}
~SubgraphEngine(); ~SubgraphEngine();
......
...@@ -75,7 +75,6 @@ add_kernel(generate_proposals_compute_arm ARM extra SRCS generate_proposals_comp ...@@ -75,7 +75,6 @@ add_kernel(generate_proposals_compute_arm ARM extra SRCS generate_proposals_comp
add_kernel(roi_align_compute_arm ARM extra SRCS roi_align_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(roi_align_compute_arm ARM extra SRCS roi_align_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(box_clip_compute_arm ARM extra SRCS box_clip_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(box_clip_compute_arm ARM extra SRCS box_clip_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(assign_value_compute_arm ARM basic SRCS assign_value_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(assign_value_compute_arm ARM basic SRCS assign_value_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(conditional_block_compute_arm ARM extra SRCS conditional_block_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(collect_fpn_proposals_compute_arm ARM extra SRCS collect_fpn_proposals_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(collect_fpn_proposals_compute_arm ARM extra SRCS collect_fpn_proposals_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(distribute_fpn_proposals_compute_arm ARM extra SRCS distribute_fpn_proposals_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(distribute_fpn_proposals_compute_arm ARM extra SRCS distribute_fpn_proposals_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(clip_compute_arm ARM extra SRCS clip_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(clip_compute_arm ARM extra SRCS clip_compute.cc DEPS ${lite_kernel_deps} math_arm)
...@@ -87,7 +86,6 @@ add_kernel(beam_search_decode_compute_arm ARM extra SRCS beam_search_decode_comp ...@@ -87,7 +86,6 @@ add_kernel(beam_search_decode_compute_arm ARM extra SRCS beam_search_decode_comp
add_kernel(lookup_table_compute_arm ARM extra SRCS lookup_table_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(lookup_table_compute_arm ARM extra SRCS lookup_table_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(lookup_table_dequant_compute_arm ARM extra SRCS lookup_table_dequant_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(lookup_table_dequant_compute_arm ARM extra SRCS lookup_table_dequant_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(sequence_softmax_compute_arm ARM extra SRCS sequence_softmax_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(sequence_softmax_compute_arm ARM extra SRCS sequence_softmax_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(while_compute_arm ARM extra SRCS while_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(topk_compute_arm ARM extra SRCS topk_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(topk_compute_arm ARM extra SRCS topk_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(increment_compute_arm ARM extra SRCS increment_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(increment_compute_arm ARM extra SRCS increment_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(beam_search_compute_arm ARM extra SRCS beam_search_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(beam_search_compute_arm ARM extra SRCS beam_search_compute.cc DEPS ${lite_kernel_deps} math_arm)
......
...@@ -202,17 +202,13 @@ void ElementwiseMulCompute<T, PType>::Run() { ...@@ -202,17 +202,13 @@ void ElementwiseMulCompute<T, PType>::Run() {
} }
} }
template <> template <typename T, PrecisionType PType>
void ElementwiseMulCompute<int64_t, PRECISION(kInt64)>::Run() { void ElementwiseMulActivationCompute<T, PType>::Run() {
auto& param = this->template Param<operators::ElementwiseParam>(); auto& param =
lite::arm::math::elementwise_compute_basic<int64_t>(param, "mul", ""); this->template Param<operators::FusionElementwiseActivationParam>();
} auto* x_data = param.X->template data<T>();
auto* y_data = param.Y->template data<T>();
void ElementwiseMulActivationCompute::Run() { auto* out_data = param.Out->template mutable_data<T>();
auto& param = Param<operators::FusionElementwiseActivationParam>();
const float* x_data = param.X->data<float>();
const float* y_data = param.Y->data<float>();
float* out_data = param.Out->mutable_data<float>();
int axis = param.axis; int axis = param.axis;
std::string act_type = param.act_type; std::string act_type = param.act_type;
auto x_dims = param.X->dims(); auto x_dims = param.X->dims();
...@@ -221,21 +217,21 @@ void ElementwiseMulActivationCompute::Run() { ...@@ -221,21 +217,21 @@ void ElementwiseMulActivationCompute::Run() {
if (x_dims.size() < y_dims.size() && if (x_dims.size() < y_dims.size() &&
is_broadcast(y_dims, x_dims, axis, &pre, &n, &post)) { is_broadcast(y_dims, x_dims, axis, &pre, &n, &post)) {
if (act_type == "relu") { if (act_type == "relu") {
lite::arm::math::elementwise_mul_relu_broadcast<float>( lite::arm::math::elementwise_mul_relu_broadcast<T>(
y_data, x_data, out_data, pre, n, post); y_data, x_data, out_data, pre, n, post);
} else { } else {
LOG(FATAL) << "unsupported Activation type: " << act_type; LOG(FATAL) << "unsupported Activation type: " << act_type;
} }
} else if (is_broadcast(x_dims, y_dims, axis, &pre, &n, &post)) { } else if (is_broadcast(x_dims, y_dims, axis, &pre, &n, &post)) {
if (act_type == "relu") { if (act_type == "relu") {
lite::arm::math::elementwise_mul_relu_broadcast( lite::arm::math::elementwise_mul_relu_broadcast<T>(
x_data, y_data, out_data, pre, n, post); x_data, y_data, out_data, pre, n, post);
} else { } else {
LOG(FATAL) << "unsupported Activation type: " << act_type; LOG(FATAL) << "unsupported Activation type: " << act_type;
} }
} else { } else {
if (act_type == "relu") { if (act_type == "relu") {
lite::arm::math::elementwise_mul_relu( lite::arm::math::elementwise_mul_relu<T>(
x_data, y_data, out_data, x_dims.production()); x_data, y_data, out_data, x_dims.production());
} else { } else {
LOG(FATAL) << "unsupported Activation type: " << act_type; LOG(FATAL) << "unsupported Activation type: " << act_type;
...@@ -426,46 +422,60 @@ REGISTER_LITE_KERNEL( ...@@ -426,46 +422,60 @@ REGISTER_LITE_KERNEL(
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using elementwise_mul_float = using elementwise_mul_float_t =
paddle::lite::kernels::arm::ElementwiseMulCompute<float, PRECISION(kFloat)>; paddle::lite::kernels::arm::ElementwiseMulCompute<float, PRECISION(kFloat)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_mul, kARM, kFloat, kNCHW, elementwise_mul_float, def) elementwise_mul, kARM, kFloat, kNCHW, elementwise_mul_float_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using elementwise_mul_int32 = using elementwise_mul_int32_t =
paddle::lite::kernels::arm::ElementwiseMulCompute<int, PRECISION(kInt32)>; paddle::lite::kernels::arm::ElementwiseMulCompute<int, PRECISION(kInt32)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_mul, kARM, kInt32, kNCHW, elementwise_mul_int32, def) elementwise_mul, kARM, kInt32, kNCHW, elementwise_mul_int32_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
.Finalize(); .Finalize();
using elementwise_mul_int64 = using elementwise_mul_int64_t =
paddle::lite::kernels::arm::ElementwiseMulCompute<int64_t, paddle::lite::kernels::arm::ElementwiseMulCompute<int64_t,
PRECISION(kInt64)>; PRECISION(kInt64)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_mul, kARM, kInt64, kNCHW, elementwise_mul_int64, def) elementwise_mul, kARM, kInt64, kNCHW, elementwise_mul_int64_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.Finalize(); .Finalize();
REGISTER_LITE_KERNEL( using fusion_elementwise_mul_activation_float_t = paddle::lite::kernels::arm::
fusion_elementwise_mul_activation, ElementwiseMulActivationCompute<float, PRECISION(kFloat)>;
kARM, REGISTER_LITE_KERNEL(fusion_elementwise_mul_activation,
kFloat, kARM,
kNCHW, kFloat,
paddle::lite::kernels::arm::ElementwiseMulActivationCompute, kNCHW,
def) fusion_elementwise_mul_activation_float_t,
def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using fusion_elementwise_mul_activation_int64_t = paddle::lite::kernels::arm::
ElementwiseMulActivationCompute<int64_t, PRECISION(kInt64)>;
REGISTER_LITE_KERNEL(fusion_elementwise_mul_activation,
kARM,
kInt64,
kNCHW,
fusion_elementwise_mul_activation_int64_t,
def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.Finalize();
REGISTER_LITE_KERNEL(elementwise_max, REGISTER_LITE_KERNEL(elementwise_max,
kARM, kARM,
kFloat, kFloat,
...@@ -489,22 +499,22 @@ REGISTER_LITE_KERNEL( ...@@ -489,22 +499,22 @@ REGISTER_LITE_KERNEL(
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using elementwise_div_fp32 = using elementwise_div_fp32_t =
paddle::lite::kernels::arm::ElementwiseDivCompute<float, PRECISION(kFloat)>; paddle::lite::kernels::arm::ElementwiseDivCompute<float, PRECISION(kFloat)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_div, kARM, kFloat, kNCHW, elementwise_div_fp32, def) elementwise_div, kARM, kFloat, kNCHW, elementwise_div_fp32_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using elementwise_div_int64 = using elementwise_div_int64_t =
paddle::lite::kernels::arm::ElementwiseDivCompute<int64_t, paddle::lite::kernels::arm::ElementwiseDivCompute<int64_t,
PRECISION(kInt64)>; PRECISION(kInt64)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_div, kARM, kInt64, kNCHW, elementwise_div_int64, def) elementwise_div, kARM, kInt64, kNCHW, elementwise_div_int64_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
...@@ -522,11 +532,11 @@ REGISTER_LITE_KERNEL( ...@@ -522,11 +532,11 @@ REGISTER_LITE_KERNEL(
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize(); .Finalize();
using elementwise_mod_int64 = using elementwise_mod_int64_t =
paddle::lite::kernels::arm::ElementwiseModCompute<int64_t, paddle::lite::kernels::arm::ElementwiseModCompute<int64_t,
PRECISION(kInt64)>; PRECISION(kInt64)>;
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
elementwise_mod, kARM, kInt64, kNCHW, elementwise_mod_int64, def) elementwise_mod, kARM, kInt64, kNCHW, elementwise_mod_int64_t, def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
......
...@@ -62,8 +62,8 @@ class ElementwiseMulCompute : public KernelLite<TARGET(kARM), PType> { ...@@ -62,8 +62,8 @@ class ElementwiseMulCompute : public KernelLite<TARGET(kARM), PType> {
virtual ~ElementwiseMulCompute() = default; virtual ~ElementwiseMulCompute() = default;
}; };
class ElementwiseMulActivationCompute template <typename T, PrecisionType PType>
: public KernelLite<TARGET(kARM), PRECISION(kFloat)> { class ElementwiseMulActivationCompute : public KernelLite<TARGET(kARM), PType> {
public: public:
void Run() override; void Run() override;
......
...@@ -533,13 +533,15 @@ TEST(fusion_elementwise_mul_activation_arm, retrive_op) { ...@@ -533,13 +533,15 @@ TEST(fusion_elementwise_mul_activation_arm, retrive_op) {
} }
TEST(fusion_elementwise_mul_activation_arm, init) { TEST(fusion_elementwise_mul_activation_arm, init) {
ElementwiseMulActivationCompute fusion_elementwise_mul_activation; ElementwiseMulActivationCompute<float, PRECISION(kFloat)>
fusion_elementwise_mul_activation;
ASSERT_EQ(fusion_elementwise_mul_activation.precision(), PRECISION(kFloat)); ASSERT_EQ(fusion_elementwise_mul_activation.precision(), PRECISION(kFloat));
ASSERT_EQ(fusion_elementwise_mul_activation.target(), TARGET(kARM)); ASSERT_EQ(fusion_elementwise_mul_activation.target(), TARGET(kARM));
} }
TEST(fusion_elementwise_mul_activation_arm, compute) { TEST(fusion_elementwise_mul_activation_arm, compute) {
ElementwiseMulActivationCompute fusion_elementwise_mul_activation; ElementwiseMulActivationCompute<float, PRECISION(kFloat)>
fusion_elementwise_mul_activation;
operators::FusionElementwiseActivationParam param; operators::FusionElementwiseActivationParam param;
lite::Tensor x, y, output, output_ref; lite::Tensor x, y, output, output_ref;
......
...@@ -20,44 +20,45 @@ namespace lite { ...@@ -20,44 +20,45 @@ namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace arm {
template <typename T> template <typename IndexType, typename DataType>
void GatherFunc(const operators::GatherParam& param) { void GatherFunc(const operators::GatherParam& param) {
auto src_dims = param.X->dims(); auto src_dims = param.X->dims();
auto index_size = param.Index->dims()[0]; auto index_size = param.Index->dims()[0];
auto* p_src = param.X->data<T>(); auto* p_src = param.X->data<DataType>();
const int* p_index = param.Index->data<int>(); const IndexType* p_index = param.Index->data<IndexType>();
auto* p_output = param.Out->mutable_data<T>(); auto* p_output = param.Out->mutable_data<DataType>();
int slice_size = 1; int slice_size = 1;
for (size_t i = 1; i < src_dims.size(); ++i) { for (size_t i = 1; i < src_dims.size(); ++i) {
slice_size *= src_dims[i]; slice_size *= src_dims[i];
} }
for (int i = 0; i < index_size; ++i) { for (int i = 0; i < index_size; ++i) {
int index_ = p_index[i]; IndexType index_ = p_index[i];
memcpy(p_output + i * slice_size, memcpy(p_output + i * slice_size,
p_src + index_ * slice_size, p_src + index_ * slice_size,
slice_size * sizeof(T)); slice_size * sizeof(DataType));
} }
} }
void GatherCompute::Run() { template <typename IndexType>
auto& param = this->Param<operators::GatherParam>(); void GatherCompute<IndexType>::Run() {
auto& param = this->template Param<operators::GatherParam>();
switch (param.X->precision()) { switch (param.X->precision()) {
case PRECISION(kFloat): case PRECISION(kFloat):
GatherFunc<float>(param); GatherFunc<IndexType, float>(param);
break; break;
case PRECISION(kInt8): case PRECISION(kInt8):
GatherFunc<int8_t>(param); GatherFunc<IndexType, int8_t>(param);
break; break;
case PRECISION(kInt16): case PRECISION(kInt16):
GatherFunc<int16_t>(param); GatherFunc<IndexType, int16_t>(param);
break; break;
case PRECISION(kInt32): case PRECISION(kInt32):
GatherFunc<int32_t>(param); GatherFunc<IndexType, int32_t>(param);
break; break;
case PRECISION(kInt64): case PRECISION(kInt64):
GatherFunc<int64_t>(param); GatherFunc<IndexType, int64_t>(param);
break; break;
default: default:
LOG(FATAL) << "Gather does not implement for the " LOG(FATAL) << "Gather does not implement for the "
...@@ -70,9 +71,26 @@ void GatherCompute::Run() { ...@@ -70,9 +71,26 @@ void GatherCompute::Run() {
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(gather,
gather, kARM, kAny, kNCHW, paddle::lite::kernels::arm::GatherCompute, def) kARM,
kAny,
kNCHW,
paddle::lite::kernels::arm::GatherCompute<int32_t>,
def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
.BindInput("Index", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) .BindInput("Index",
{LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
.Finalize();
REGISTER_LITE_KERNEL(gather,
kARM,
kAny,
kNCHW,
paddle::lite::kernels::arm::GatherCompute<int64_t>,
def_int64_idx)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
.BindInput("Index",
{LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
.Finalize(); .Finalize();
...@@ -23,6 +23,7 @@ namespace lite { ...@@ -23,6 +23,7 @@ namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace arm {
template <typename IndexType>
class GatherCompute : public KernelLite<TARGET(kARM), PRECISION(kAny)> { class GatherCompute : public KernelLite<TARGET(kARM), PRECISION(kAny)> {
public: public:
void Run() override; void Run() override;
......
...@@ -28,36 +28,17 @@ namespace lite { ...@@ -28,36 +28,17 @@ namespace lite {
namespace kernels { namespace kernels {
namespace bm { namespace bm {
bool SubgraphEngine::PrepareWorkspaceForDeviceProgram() {
// Obtain the origin input tensors, and create the origin output
// tensors(Don't try to access them before launch the device program or the
// origin program)
PrepareWorkspaceForOriginProgram();
// Create the device input and output tensors, but don't initialize them
// with the dimensions
device_inputs_.resize(input_names_.size());
for (int i = 0; i < input_names_.size(); i++) {
device_inputs_[i].reset(new hiai::AiTensor);
CHECK(device_inputs_[i]);
}
device_outputs_.resize(output_names_.size());
for (int i = 0; i < output_names_.size(); i++) {
device_outputs_[i].reset(new hiai::AiTensor);
CHECK(device_outputs_[i]);
}
return true;
}
bool SubgraphEngine::BuildDeviceProgram() { bool SubgraphEngine::BuildDeviceProgram() {
int status = 0; int status = 0;
subgraph::bm::Graph graph; subgraph::bm::Graph graph;
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
graph.CreateCompilerHandle(); graph.CreateCompilerHandle();
auto& ctx = this->ctx_->template As<BMContext>(); auto& ctx = this->ctx_->template As<BMContext>();
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = const_cast<OpLite*>(inst.op()); auto op = const_cast<OpLite*>(inst.op());
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -93,13 +74,11 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -93,13 +74,11 @@ bool SubgraphEngine::BuildDeviceProgram() {
net_info_ = bmrt_get_network_info(bmrt_hd_, net_names_[0]); net_info_ = bmrt_get_network_info(bmrt_hd_, net_names_[0]);
auto& stage = net_info_->stages[0]; auto& stage = net_info_->stages[0];
// input // input
origin_idims_.resize(input_names_.size());
origin_itensors_.resize(input_names_.size());
device_inputs_.resize(input_names_.size()); device_inputs_.resize(input_names_.size());
for (size_t i = 0; i < input_names_.size(); i++) { for (size_t i = 0; i < input_names_.size(); i++) {
origin_itensors_[i] = scope_->FindMutableTensor(net_info_->input_names[i]); origin_itensors_[i] =
exec_scope_->FindMutableTensor(net_info_->input_names[i]);
CHECK(origin_itensors_[i]); CHECK(origin_itensors_[i]);
origin_idims_[i] = origin_itensors_[i]->dims();
bm_device_mem_t* p_mem = bm_device_mem_t* p_mem =
static_cast<bm_device_mem_t*>(malloc(sizeof(bm_device_mem_t))); static_cast<bm_device_mem_t*>(malloc(sizeof(bm_device_mem_t)));
CHECK(p_mem != nullptr); CHECK(p_mem != nullptr);
...@@ -112,8 +91,6 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -112,8 +91,6 @@ bool SubgraphEngine::BuildDeviceProgram() {
stage.input_shapes[i]); stage.input_shapes[i]);
} }
// output // output
origin_odims_.resize(output_names_.size());
origin_otensors_.resize(output_names_.size());
device_outputs_.resize(net_info_->output_num); device_outputs_.resize(net_info_->output_num);
int out_index = 0; int out_index = 0;
for (int i = 0; i < output_names_.size(); i++) { for (int i = 0; i < output_names_.size(); i++) {
...@@ -121,14 +98,13 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -121,14 +98,13 @@ bool SubgraphEngine::BuildDeviceProgram() {
} }
for (int i = 0; i < net_info_->output_num; i++) { for (int i = 0; i < net_info_->output_num; i++) {
Tensor* t_cur = scope_->FindMutableTensor(net_info_->output_names[i]); Tensor* t_cur = exec_scope_->FindMutableTensor(net_info_->output_names[i]);
CHECK(t_cur != nullptr); CHECK(t_cur != nullptr);
bm_device_mem_t* p_mem = bm_device_mem_t* p_mem =
static_cast<bm_device_mem_t*>(malloc(sizeof(bm_device_mem_t))); static_cast<bm_device_mem_t*>(malloc(sizeof(bm_device_mem_t)));
CHECK(p_mem != nullptr); CHECK(p_mem != nullptr);
if (outname_map_.find(net_info_->output_names[i]) != outname_map_.end()) { if (outname_map_.find(net_info_->output_names[i]) != outname_map_.end()) {
origin_otensors_[out_index] = t_cur; origin_otensors_[out_index] = t_cur;
origin_odims_[out_index] = origin_otensors_[out_index]->dims();
origin_otensors_[out_index]->mutable_data<float>(); origin_otensors_[out_index]->mutable_data<float>();
out_index += 1; out_index += 1;
} }
...@@ -173,11 +149,11 @@ bool SubgraphEngine::LaunchDeviceProgram() { ...@@ -173,11 +149,11 @@ bool SubgraphEngine::LaunchDeviceProgram() {
void SubgraphCompute::PrepareForRun() { void SubgraphCompute::PrepareForRun() {
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
engine_.reset(new SubgraphEngine(ctx_.get(), engine_.reset(new SubgraphEngine(ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names));
param.scope));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -36,15 +36,18 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -36,15 +36,18 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext *ctx, SubgraphEngine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names)
Scope *scope) : subgraph::Engine(ctx,
: subgraph::Engine( block_idx,
ctx, block_idx, block_desc, input_names, output_names, scope) {} program_desc,
exec_scope,
input_names,
output_names) {}
protected: protected:
bool PrepareWorkspaceForDeviceProgram() override;
bool BuildDeviceProgram() override; bool BuildDeviceProgram() override;
bool LaunchDeviceProgram() override; bool LaunchDeviceProgram() override;
......
...@@ -18,6 +18,9 @@ add_kernel(read_from_array_compute_host Host extra SRCS read_from_array_compute. ...@@ -18,6 +18,9 @@ add_kernel(read_from_array_compute_host Host extra SRCS read_from_array_compute.
add_kernel(assign_compute_host Host extra SRCS assign_compute.cc DEPS ${lite_kernel_deps}) add_kernel(assign_compute_host Host extra SRCS assign_compute.cc DEPS ${lite_kernel_deps})
add_kernel(retinanet_detection_output_compute_host Host extra SRCS retinanet_detection_output_compute.cc DEPS ${lite_kernel_deps}) add_kernel(retinanet_detection_output_compute_host Host extra SRCS retinanet_detection_output_compute.cc DEPS ${lite_kernel_deps})
add_kernel(where_index_compute_host Host extra SRCS where_index_compute.cc DEPS ${lite_kernel_deps}) add_kernel(where_index_compute_host Host extra SRCS where_index_compute.cc DEPS ${lite_kernel_deps})
add_kernel(print_compute_host Host extra SRCS print_compute.cc DEPS ${lite_kernel_deps})
add_kernel(while_compute_host Host extra SRCS while_compute.cc DEPS ${lite_kernel_deps} program)
add_kernel(conditional_block_compute_host Host extra SRCS conditional_block_compute.cc DEPS ${lite_kernel_deps} program)
add_kernel(activation_grad_compute_host Host train SRCS activation_grad_compute.cc DEPS ${lite_kernel_deps}) add_kernel(activation_grad_compute_host Host train SRCS activation_grad_compute.cc DEPS ${lite_kernel_deps})
if(LITE_BUILD_EXTRA) if(LITE_BUILD_EXTRA)
......
...@@ -51,3 +51,19 @@ REGISTER_LITE_KERNEL( ...@@ -51,3 +51,19 @@ REGISTER_LITE_KERNEL(
PRECISION(kAny), PRECISION(kAny),
DATALAYOUT(kAny))}) DATALAYOUT(kAny))})
.Finalize(); .Finalize();
REGISTER_LITE_KERNEL(assign,
kHost,
kAny,
kAny,
paddle::lite::kernels::host::AssignCompute,
def_tensor_array)
.BindInput("X",
{LiteType::GetTensorListTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.BindOutput("Out",
{LiteType::GetTensorListTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.Finalize();
...@@ -12,28 +12,21 @@ ...@@ -12,28 +12,21 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "lite/kernels/arm/conditional_block_compute.h" #include "lite/kernels/host/conditional_block_compute.h"
#include <memory>
#include <string>
#include <vector>
#include "lite/backends/arm/math/funcs.h"
#include "lite/core/tensor.h"
#include "lite/core/type_system.h"
namespace paddle { namespace paddle {
namespace lite { namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace host {
void ConditionalBlockCompute::PrepareForRun() { void ConditionalBlockCompute::PrepareForRun() {
auto& param = Param<operators::ConditionalBlockParam>(); auto& param = this->Param<param_t>();
auto cur_scope = param.scope; program_.reset(new RuntimeProgram(
param.program_desc, param.exec_scope, param.block_idx));
executor_ =
std::make_shared<CondExecutor>(param.sub_block, cur_scope, place());
} }
void ConditionalBlockCompute::Run() { void ConditionalBlockCompute::Run() {
auto& param = Param<operators::ConditionalBlockParam>(); auto& param = this->Param<param_t>();
for (auto& out : param.outs) { for (auto& out : param.outs) {
out->clear(); out->clear();
} }
...@@ -43,32 +36,40 @@ void ConditionalBlockCompute::Run() { ...@@ -43,32 +36,40 @@ void ConditionalBlockCompute::Run() {
auto* cond_data = cond->data<bool>(); auto* cond_data = cond->data<bool>();
need_run = cond_data[0]; need_run = cond_data[0];
} else { } else {
auto x = param.x; for (auto input : param.inputs) {
for (auto pt : x) { if (input == nullptr || !input->IsInitialized() ||
if (pt == nullptr || !pt->IsInitialized() || pt->dims().empty()) { input->dims().empty()) {
need_run = false; need_run = false;
break; break;
} }
} }
} }
if (need_run) { if (need_run) {
executor_->Run(); program_->Run();
} }
} }
} // namespace arm } // namespace host
} // namespace kernels } // namespace kernels
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
REGISTER_LITE_KERNEL(conditional_block, REGISTER_LITE_KERNEL(conditional_block,
kARM, kHost,
kFloat, kAny,
kNCHW, kAny,
paddle::lite::kernels::arm::ConditionalBlockCompute, paddle::lite::kernels::host::ConditionalBlockCompute,
def) def)
.BindInput("Input", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("Input",
.BindInput("Cond", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))}) {LiteType::GetTensorListTy(
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))}) TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.BindOutput("Scope", {LiteType::GetTensorTy(TARGET(kARM))}) .BindInput("Cond",
{LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kBool), DATALAYOUT(kAny), -1)})
.BindOutput("Out",
{LiteType::GetTensorListTy(
TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.BindOutput("Scope",
{LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.Finalize(); .Finalize();
...@@ -15,92 +15,30 @@ ...@@ -15,92 +15,30 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <utility> #include <string>
#include <vector> #include <vector>
#include "lite/core/kernel.h" #include "lite/core/kernel.h"
#include "lite/core/op_registry.h" #include "lite/core/op_registry.h"
#include "lite/core/program.h" #include "lite/core/program.h"
#include "lite/operators/conditional_block_op.h"
#ifdef LITE_WITH_PROFILE
#include "lite/core/profile/basic_profiler.h"
#include "lite/core/profile/precision_profiler.h"
#include "lite/core/profile/profiler.h"
#endif
namespace paddle { namespace paddle {
namespace lite { namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace host {
class CondExecutor {
typedef std::shared_ptr<OpLite> OpPtr;
public:
CondExecutor(cpp::BlockDesc *block, Scope *scope, Place place)
: scope_(scope), place_(place) {
int32_t op_size = block->OpsSize();
for (int32_t i = 0; i < op_size; ++i) {
auto &op_desc = *block->template GetOp<cpp::OpDesc>(i);
auto op_type = op_desc.Type();
auto op_handler = lite::LiteOpRegistry::Global().Create(op_desc.Type());
op_handler->Attach(op_desc, scope);
auto hostplace = place_;
hostplace.target = TARGET(kHost);
auto kernels = op_handler->CreateKernels({place_, hostplace});
CHECK_GT(kernels.size(), 0) << "cannot create kernel";
op_handler->AttachKernel(kernels[0].get());
op_handler->SetKernel(kernels);
ops_of_block_.push_back(op_handler);
}
}
void Run() {
#ifdef LITE_WITH_PROFILE
#ifdef LITE_WITH_PRECISION_PROFILE
lite::profile::Profiler profiler;
#endif // LITE_WITH_PRECISION_PROFILE
#endif // LITE_WITH_PROFILE
for (auto &op_handler : ops_of_block_) {
op_handler->CheckShape();
op_handler->InferShape();
#ifdef LITE_WITH_PROFILE
#ifdef LITE_WITH_PRECISION_PROFILE
std::unique_ptr<KernelBase> kernel(op_handler->GetKernel());
Instruction inst(op_handler, std::move(kernel));
inst.set_profiler(&profiler);
#endif // LITE_WITH_PRECISION_PROFILE
#endif // LITE_WITH_PROFILE
op_handler->Run();
#ifdef LITE_WITH_PROFILE
#ifdef LITE_WITH_PRECISION_PROFILE
LITE_PRECISION_PROFILE(inst)
#endif // LITE_WITH_PRECISION_PROFILE
#endif // LITE_WITH_PROFILE
}
}
private:
Scope *scope_;
Place place_;
std::vector<OpPtr> ops_of_block_;
};
class ConditionalBlockCompute class ConditionalBlockCompute
: public KernelLite<TARGET(kARM), PRECISION(kFloat)> { : public KernelLite<TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny)> {
public: public:
using param_t = operators::ConditionalBlockParam; using param_t = operators::ConditionalBlockParam;
void PrepareForRun() override; void PrepareForRun() override;
void Run() override; void Run() override;
virtual ~ConditionalBlockCompute() = default;
private: private:
std::shared_ptr<CondExecutor> executor_; std::unique_ptr<RuntimeProgram> program_;
}; };
} // namespace arm } // namespace host
} // namespace kernels } // namespace kernels
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lite/kernels/host/print_compute.h"
#include <mutex> // NOLINT
#include <string>
#include <vector>
namespace paddle {
namespace lite {
namespace kernels {
namespace host {
const char kForward[] = "FORWARD";
const char kBackward[] = "BACKWARD";
const char kBoth[] = "BOTH";
class TensorFormatter {
public:
TensorFormatter() {}
std::string Format(const Tensor& print_tensor,
const std::string& tensor_name = "",
const std::string& message = "") {
std::stringstream log_stream;
if (!tensor_name.empty()) {
log_stream << "Variable: " << tensor_name << std::endl;
}
if (!message.empty()) {
log_stream << " - message: " << message << std::endl;
}
if (print_tensor_lod_) {
log_stream << " - lod: {";
const LoD& lod = print_tensor.lod();
for (auto level : lod) {
log_stream << "{";
bool is_first = true;
for (auto i : level) {
if (is_first) {
log_stream << i;
is_first = false;
} else {
log_stream << ", " << i;
}
}
log_stream << "}";
}
log_stream << "}" << std::endl;
}
log_stream << " - place: " << TargetToStr(print_tensor.target())
<< std::endl; // TODO(hong19860320) always kHost
if (print_tensor_shape_) {
log_stream << " - shape: " << print_tensor.dims().repr() << std::endl;
}
if (print_tensor_layout_) {
log_stream << " - layout: "
<< DataLayoutToStr(
DATALAYOUT(kNCHW)) // TODO(hong19860320) Query the data
// layout from target tensor
<< std::endl;
}
auto dtype = print_tensor.precision();
if (print_tensor_type_) {
log_stream << " - dtype: " << PrecisionToStr(dtype) << std::endl;
}
if (dtype == PRECISION(kBool)) {
FormatData<bool>(print_tensor, log_stream);
} else if (dtype == PRECISION(kInt8)) {
FormatData<int8_t>(print_tensor, log_stream);
} else if (dtype == PRECISION(kInt16)) {
FormatData<int16_t>(print_tensor, log_stream);
} else if (dtype == PRECISION(kInt32)) {
FormatData<int32_t>(print_tensor, log_stream);
} else if (dtype == PRECISION(kInt64)) {
FormatData<int64_t>(print_tensor, log_stream);
} else if (dtype == PRECISION(kFloat)) {
FormatData<float>(print_tensor, log_stream);
} else {
log_stream << "\tdata: unprintable type: " << PrecisionToStr(dtype)
<< std::endl;
}
return log_stream.str();
}
void Print(const Tensor& print_tensor,
const std::string& tensor_name = "",
const std::string& message = "") {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
std::cout << Format(print_tensor, tensor_name, message);
}
void SetPrintTensorType(bool print_tensor_type) {
print_tensor_type_ = print_tensor_type;
}
void SetPrintTensorShape(bool print_tensor_shape) {
print_tensor_shape_ = print_tensor_shape;
}
void SetPrintTensorLod(bool print_tensor_lod) {
print_tensor_lod_ = print_tensor_lod;
}
void SetPrintTensorLayout(bool print_tensor_layout) {
print_tensor_layout_ = print_tensor_layout;
}
void SetSummarize(int64_t summarize) { summarize_ = summarize; }
private:
template <typename T>
void FormatData(const Tensor& print_tensor, std::stringstream& log_stream) {
int64_t print_size = summarize_ == -1
? print_tensor.numel()
: std::min(summarize_, print_tensor.numel());
const T* data = print_tensor.data<T>(); // Always kHost, so unnessary to
// copy the data from device
log_stream << " - data: [";
if (print_size > 0) {
log_stream << data[0];
for (int64_t i = 1; i < print_size; ++i) {
log_stream << " " << data[i];
}
}
log_stream << "]" << std::endl;
}
int64_t summarize_ = -1;
bool print_tensor_type_ = true;
bool print_tensor_shape_ = true;
bool print_tensor_lod_ = true;
bool print_tensor_layout_ = true;
};
void PrintCompute::Run() {
auto& param = Param<param_t>();
param.out->CopyDataFrom(*param.in);
if ((param.is_forward && param.print_phase == kBackward) ||
(!param.is_forward && param.print_phase == kForward)) {
return;
}
int first_n = param.first_n;
if (first_n > 0 && ++times_ > first_n) return;
TensorFormatter formatter;
const std::string& name = param.print_tensor_name ? param.name : "";
formatter.SetPrintTensorType(param.print_tensor_type);
formatter.SetPrintTensorShape(param.print_tensor_shape);
formatter.SetPrintTensorLod(param.print_tensor_lod);
formatter.SetPrintTensorLayout(param.print_tensor_layout);
formatter.SetSummarize(static_cast<int64_t>(param.summarize));
formatter.Print(*param.in, name, param.message);
}
} // namespace host
} // namespace kernels
} // namespace lite
} // namespace paddle
REGISTER_LITE_KERNEL(
print, kHost, kAny, kAny, paddle::lite::kernels::host::PrintCompute, def)
.BindInput("In",
{LiteType::GetTensorTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.Finalize();
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <algorithm>
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace host {
class PrintCompute
: public KernelLite<TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny)> {
public:
using param_t = operators::PrintParam;
void Run() override;
virtual ~PrintCompute() = default;
private:
mutable int times_{0};
};
} // namespace host
} // namespace kernels
} // namespace lite
} // namespace paddle
...@@ -12,44 +12,44 @@ ...@@ -12,44 +12,44 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "lite/kernels/arm/while_compute.h" #include "lite/kernels/host/while_compute.h"
#include <memory> #include <unordered_map>
#include <string> #include <utility>
#include <vector>
#include "lite/backends/arm/math/funcs.h"
#include "lite/core/tensor.h"
#include "lite/core/type_system.h"
namespace paddle { namespace paddle {
namespace lite { namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace host {
void WhileCompute::PrepareForRun() { void WhileCompute::PrepareForRun() {
auto &param = Param<operators::WhileParam>(); auto &param = this->Param<param_t>();
auto cur_scope = param.scope; program_.reset(new RuntimeProgram(
param.program_desc, param.exec_scope, param.block_idx));
executor_ =
std::make_shared<StepExecutor>(param.sub_block, cur_scope, place());
} }
void WhileCompute::Run() { void WhileCompute::Run() {
auto &param = Param<operators::WhileParam>(); auto &param = this->Param<param_t>();
while (param.cond->data<bool>()[0]) { while (param.cond->data<bool>()[0]) {
executor_->Run(); program_->Run();
} }
} }
} // namespace arm } // namespace host
} // namespace kernels } // namespace kernels
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
REGISTER_LITE_KERNEL( REGISTER_LITE_KERNEL(
while, kARM, kFloat, kNCHW, paddle::lite::kernels::arm::WhileCompute, def) while, kHost, kAny, kAny, paddle::lite::kernels::host::WhileCompute, def)
.BindInput("X", {LiteType::GetTensorListTy(TARGET(kARM), PRECISION(kAny))}) .BindInput("X",
{LiteType::GetTensorListTy(
TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.BindInput("Condition", .BindInput("Condition",
{LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))}) {LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kBool), DATALAYOUT(kAny), -1)})
.BindOutput("Out", .BindOutput("Out",
{LiteType::GetTensorListTy(TARGET(kARM), PRECISION(kAny))}) {LiteType::GetTensorListTy(
.BindOutput("StepScopes", {LiteType::GetTensorTy(TARGET(kARM))}) TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.BindOutput("StepScopes",
{LiteType::GetTensorTy(
TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)})
.Finalize(); .Finalize();
...@@ -15,56 +15,19 @@ ...@@ -15,56 +15,19 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <string>
#include <vector> #include <vector>
#include "lite/core/kernel.h" #include "lite/core/kernel.h"
#include "lite/core/op_registry.h" #include "lite/core/op_registry.h"
#include "lite/operators/while_op.h" #include "lite/core/program.h"
namespace paddle { namespace paddle {
namespace lite { namespace lite {
namespace kernels { namespace kernels {
namespace arm { namespace host {
class StepExecutor { class WhileCompute
typedef std::shared_ptr<OpLite> OpPtr; : public KernelLite<TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny)> {
public:
StepExecutor(cpp::BlockDesc *block, Scope *scope, Place place)
: scope_(scope), place_(place) {
int32_t op_size = block->OpsSize();
for (int32_t i = 0; i < op_size; ++i) {
auto &op_desc = *block->template GetOp<cpp::OpDesc>(i);
auto op_type = op_desc.Type();
auto op_handler = lite::LiteOpRegistry::Global().Create(op_desc.Type());
// VLOG(4) << "while: creating Op [" << op_type << "]";
op_handler->Attach(op_desc, scope);
auto hostplace = place_;
hostplace.target = TARGET(kHost);
auto kernels = op_handler->CreateKernels({place_, hostplace});
CHECK_GT(kernels.size(), 0) << "cannot create kernel";
op_handler->AttachKernel(kernels[0].get());
op_handler->SetKernel(kernels);
ops_of_block_.push_back(op_handler);
}
}
void Run() {
for (auto &op_handler : ops_of_block_) {
// VLOG(4) << op_handler->op_info()->Repr();
op_handler->InferShape();
// VLOG(4) << "while: infered shape";
op_handler->Run();
}
}
private:
Scope *scope_;
Place place_;
std::vector<OpPtr> ops_of_block_;
};
class WhileCompute : public KernelLite<TARGET(kARM), PRECISION(kFloat)> {
public: public:
using param_t = operators::WhileParam; using param_t = operators::WhileParam;
...@@ -74,10 +37,10 @@ class WhileCompute : public KernelLite<TARGET(kARM), PRECISION(kFloat)> { ...@@ -74,10 +37,10 @@ class WhileCompute : public KernelLite<TARGET(kARM), PRECISION(kFloat)> {
virtual ~WhileCompute() = default; virtual ~WhileCompute() = default;
private: private:
std::shared_ptr<StepExecutor> executor_; std::unique_ptr<RuntimeProgram> program_;
}; };
} // namespace arm } // namespace host
} // namespace kernels } // namespace kernels
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
...@@ -43,13 +43,17 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -43,13 +43,17 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext* ctx, SubgraphEngine(KernelContext* ctx,
int block_idx, int block_idx,
cpp::BlockDesc* block_desc, const std::shared_ptr<const cpp::ProgramDesc>& program_desc,
Scope* exec_scope,
const std::vector<std::string>& input_names, const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names, const std::vector<std::string>& output_names,
Scope* scope,
paddle::lite_api::PrecisionType type) paddle::lite_api::PrecisionType type)
: subgraph::Engine( : subgraph::Engine(ctx,
ctx, block_idx, block_desc, input_names, output_names, scope), block_idx,
program_desc,
exec_scope,
input_names,
output_names),
fp_type_(type) { fp_type_(type) {
VLOG(4) << "[MLU] PADDLE_LITE_MLU_SAVE_OFFLINE_MODEL is " VLOG(4) << "[MLU] PADDLE_LITE_MLU_SAVE_OFFLINE_MODEL is "
<< GetBoolFromEnv("PADDLE_LITE_MLU_SAVE_OFFLINE_MODEL"); << GetBoolFromEnv("PADDLE_LITE_MLU_SAVE_OFFLINE_MODEL");
...@@ -103,7 +107,7 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -103,7 +107,7 @@ class SubgraphEngine : public subgraph::Engine {
protected: protected:
bool BuildDeviceProgram() override { bool BuildDeviceProgram() override {
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
if (!error_compile_batch_size_changeable_ && if (!error_compile_batch_size_changeable_ &&
...@@ -128,13 +132,15 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -128,13 +132,15 @@ class SubgraphEngine : public subgraph::Engine {
origin_itensors_.clear(); origin_itensors_.clear();
origin_otensors_.clear(); origin_otensors_.clear();
auto data_order = block_desc_->GetOp<cpp::OpDesc>(0)->Type() == "layout" auto* sub_block_desc =
program_desc_->GetBlock()<cpp::BlockDesc>(block_idx_);
auto data_order = sub_block_desc->GetOp<cpp::OpDesc>(0)->Type() == "layout"
? CNML_NCHW ? CNML_NCHW
: CNML_NHWC; : CNML_NHWC;
// Convert all of input data vars and added into the MLU IR graph // Convert all of input data vars and added into the MLU IR graph
status |= subgraph::REBUILD_WHEN_SHAPE_CHANGED; status |= subgraph::REBUILD_WHEN_SHAPE_CHANGED;
for (auto& input_name : input_names_) { for (auto& input_name : input_names_) {
auto input_tensor = scope_->FindMutableTensor(input_name); auto input_tensor = exec_scope_->FindMutableTensor(input_name);
auto data_type = input_tensor->precision(); auto data_type = input_tensor->precision();
cnmlDataType_t fp_type = PrecisionToDatatype(data_type); cnmlDataType_t fp_type = PrecisionToDatatype(data_type);
origin_itensors_.push_back(input_tensor); origin_itensors_.push_back(input_tensor);
...@@ -161,7 +167,8 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -161,7 +167,8 @@ class SubgraphEngine : public subgraph::Engine {
LOG(INFO) << "START TO CONVERT "; LOG(INFO) << "START TO CONVERT ";
// Convert all of ops and its weights and added into the MLU IR graph // Convert all of ops and its weights and added into the MLU IR graph
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = inst.op(); auto op = inst.op();
CHECK(op); CHECK(op);
std::string op_type = op->op_info()->Type(); std::string op_type = op->op_info()->Type();
...@@ -200,7 +207,7 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -200,7 +207,7 @@ class SubgraphEngine : public subgraph::Engine {
for (auto& output_name : output_names_) { for (auto& output_name : output_names_) {
if (graph->HasNode(output_name)) { if (graph->HasNode(output_name)) {
graph->AddOutput(graph->GetNode(output_name)); graph->AddOutput(graph->GetNode(output_name));
auto output_tensor = scope_->FindMutableTensor(output_name); auto output_tensor = exec_scope_->FindMutableTensor(output_name);
origin_otensors_.push_back(output_tensor); origin_otensors_.push_back(output_tensor);
VLOG(4) << "subgraph output tensor " << output_name << std::endl; VLOG(4) << "subgraph output tensor " << output_name << std::endl;
...@@ -257,7 +264,7 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -257,7 +264,7 @@ class SubgraphEngine : public subgraph::Engine {
for (const auto& input_name : input_names_) { for (const auto& input_name : input_names_) {
tmp = input_name; tmp = input_name;
name += TrimStrings(tmp) + delimiter + input_shape_str; name += TrimStrings(tmp) + delimiter + input_shape_str;
auto input_tensor = scope_->FindMutableTensor(input_name); auto input_tensor = exec_scope_->FindMutableTensor(input_name);
for (const auto& iterm : input_tensor->dims().Vectorize()) { for (const auto& iterm : input_tensor->dims().Vectorize()) {
name += std::to_string(iterm) + delimiter_num; name += std::to_string(iterm) + delimiter_num;
} }
...@@ -266,7 +273,7 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -266,7 +273,7 @@ class SubgraphEngine : public subgraph::Engine {
for (const auto& output_name : output_names_) { for (const auto& output_name : output_names_) {
tmp = output_name; tmp = output_name;
name += TrimStrings(tmp) + delimiter + output_shape_str; name += TrimStrings(tmp) + delimiter + output_shape_str;
auto output_tensor = scope_->FindMutableTensor(output_name); auto output_tensor = exec_scope_->FindMutableTensor(output_name);
for (const auto& iterm : output_tensor->dims().Vectorize()) { for (const auto& iterm : output_tensor->dims().Vectorize()) {
name += std::to_string(iterm) + delimiter_num; name += std::to_string(iterm) + delimiter_num;
} }
...@@ -284,7 +291,8 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -284,7 +291,8 @@ class SubgraphEngine : public subgraph::Engine {
origin_otensors_[i]->Resize(iter->second[i]); origin_otensors_[i]->Resize(iter->second[i]);
} }
} else { } else {
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = inst.op(); auto op = inst.op();
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -475,11 +483,11 @@ class SubgraphCompute ...@@ -475,11 +483,11 @@ class SubgraphCompute
auto& param = this->template Param<param_t>(); auto& param = this->template Param<param_t>();
// LOG(INFO) << "SUBGRAP Prepare RUN index " << param.sub_block_idx; // LOG(INFO) << "SUBGRAP Prepare RUN index " << param.sub_block_idx;
engine_.reset(new SubgraphEngine<Precision>(this->ctx_.get(), engine_.reset(new SubgraphEngine<Precision>(this->ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names,
param.scope,
this->precision())); this->precision()));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -25,11 +25,14 @@ namespace subgraph { ...@@ -25,11 +25,14 @@ namespace subgraph {
Engine::Engine(KernelContext *ctx, Engine::Engine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names)
lite::Scope *scope) : ctx_(ctx),
: ctx_(ctx), block_idx_(block_idx), block_desc_(block_desc), scope_(scope) { block_idx_(block_idx),
program_desc_(program_desc),
exec_scope_(exec_scope) {
input_names_ = input_names; input_names_ = input_names;
output_names_ = output_names; output_names_ = output_names;
// Sort the name of input and output tensors, it's convenient for us to get // Sort the name of input and output tensors, it's convenient for us to get
...@@ -55,12 +58,12 @@ bool Engine::PrepareWorkspaceForOriginProgram() { ...@@ -55,12 +58,12 @@ bool Engine::PrepareWorkspaceForOriginProgram() {
origin_idims_.resize(input_names_.size()); origin_idims_.resize(input_names_.size());
origin_itensors_.resize(input_names_.size()); origin_itensors_.resize(input_names_.size());
for (int i = 0; i < input_names_.size(); i++) { for (int i = 0; i < input_names_.size(); i++) {
origin_itensors_[i] = scope_->FindMutableTensor(input_names_[i]); origin_itensors_[i] = exec_scope_->FindMutableTensor(input_names_[i]);
CHECK(origin_itensors_[i]); CHECK(origin_itensors_[i]);
} }
origin_otensors_.resize(output_names_.size()); origin_otensors_.resize(output_names_.size());
for (int i = 0; i < output_names_.size(); i++) { for (int i = 0; i < output_names_.size(); i++) {
origin_otensors_[i] = scope_->FindMutableTensor(output_names_[i]); origin_otensors_[i] = exec_scope_->FindMutableTensor(output_names_[i]);
CHECK(origin_otensors_[i]); CHECK(origin_otensors_[i]);
} }
return true; return true;
...@@ -69,70 +72,20 @@ bool Engine::PrepareWorkspaceForOriginProgram() { ...@@ -69,70 +72,20 @@ bool Engine::PrepareWorkspaceForOriginProgram() {
bool Engine::BuildOriginProgram() { bool Engine::BuildOriginProgram() {
// TODO(hong19860320) The block_desc need to be divided into subgraphs during // TODO(hong19860320) The block_desc need to be divided into subgraphs during
// the exection time. But only see them as a subgraph now. // the exection time. But only see them as a subgraph now.
origin_program_.clear(); if (!origin_program_) {
for (size_t op_idx = 0; op_idx < block_desc_->OpsSize(); op_idx++) { origin_program_.reset(
auto op_desc = block_desc_->GetOp<cpp::OpDesc>(op_idx); new RuntimeProgram(program_desc_, exec_scope_, block_idx_));
CHECK(op_desc);
std::string op_type = op_desc->Type();
// Create op and pick up the best kernel
auto op = LiteOpRegistry::Global().Create(op_desc->Type());
CHECK(op) << "no Op found for " << op_type;
op->Attach(*op_desc, scope_);
std::unique_ptr<KernelBase> picked_kernel;
if (op_desc->HasAttr(kKernelTypeAttr)) {
// Create op and pick up the best kernel according to the
// kKernelTypeAttr attribute
auto kernel_type = op_desc->GetAttr<std::string>(kKernelTypeAttr);
std::string alias;
Place place;
KernelBase::ParseKernelType(kernel_type, &op_type, &alias, &place);
VLOG(3) << "Found the attr '" << kKernelTypeAttr << "': " << kernel_type
<< " for " << op_type;
auto kernels = op->CreateKernels({place});
CHECK_GT(kernels.size(), 0u) << "No kernels found for " << op_type;
auto it = std::find_if(
kernels.begin(), kernels.end(), [&](std::unique_ptr<KernelBase> &it) {
return it->alias() == alias;
});
CHECK(it != kernels.end());
picked_kernel = std::move(*it);
} else {
// TODO(hong19860320) add kernel picking according to the type of input
// and output tensors
VLOG(3) << "The attr '" << kKernelTypeAttr
<< "' not found, pick the first kernel for " << op_type;
std::vector<std::unique_ptr<KernelBase>> kernels;
#if defined(LITE_WITH_ARM)
kernels = op->CreateKernels({Place{TARGET(kARM)}, Place{TARGET(kHost)}});
#elif defined(LITE_WITH_X86)
kernels = op->CreateKernels({Place{TARGET(kX86)}, Place{TARGET(kHost)}});
#endif
if (kernels.size() > 0) {
picked_kernel = std::move(kernels.front());
} else {
LOG(WARNING) << "No kernels found for " << op_type;
}
}
if (picked_kernel != nullptr) {
picked_kernel->SetContext(
ContextScheduler::Global().NewContext(picked_kernel->target()));
}
origin_program_.emplace_back(std::move(op), std::move(picked_kernel));
} }
CHECK(!origin_program_.empty()) << "no instructions";
return true; return true;
} }
bool Engine::LaunchOriginProgram() { bool Engine::LaunchOriginProgram() {
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
if (!origin_program_.empty()) { if (origin_program_) {
for (auto &inst : origin_program_) { VLOG(3) << "Roll back to run the origin program.";
auto op_type = inst.op()->op_info()->Type(); origin_program_->Run();
if (op_type == "feed" || op_type == "fetch") continue;
inst.Run();
}
return true; return true;
} }
return false; return false;
......
...@@ -30,10 +30,10 @@ class Engine { ...@@ -30,10 +30,10 @@ class Engine {
public: public:
Engine(KernelContext *ctx, Engine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names);
lite::Scope *scope);
virtual ~Engine() = default; virtual ~Engine() = default;
virtual bool Run(); virtual bool Run();
...@@ -54,15 +54,15 @@ class Engine { ...@@ -54,15 +54,15 @@ class Engine {
KernelContext *ctx_{nullptr}; KernelContext *ctx_{nullptr};
int block_idx_{-1}; int block_idx_{-1};
cpp::BlockDesc *block_desc_{nullptr}; const std::shared_ptr<const cpp::ProgramDesc> program_desc_{nullptr};
std::vector<std::string> input_names_; std::vector<std::string> input_names_;
std::vector<std::string> output_names_; std::vector<std::string> output_names_;
Scope *scope_{nullptr}; Scope *exec_scope_{nullptr};
bool is_first_epoch_{true}; bool is_first_epoch_{true};
std::vector<std::vector<int64_t>> origin_idims_; std::vector<std::vector<int64_t>> origin_idims_;
std::vector<Tensor *> origin_itensors_; std::vector<Tensor *> origin_itensors_;
std::vector<Tensor *> origin_otensors_; std::vector<Tensor *> origin_otensors_;
std::vector<Instruction> origin_program_; std::unique_ptr<RuntimeProgram> origin_program_{nullptr};
}; };
} // namespace subgraph } // namespace subgraph
......
...@@ -55,7 +55,8 @@ std::string DeviceProgram::GenerateModelName( ...@@ -55,7 +55,8 @@ std::string DeviceProgram::GenerateModelName(
} }
// Deserialize the generated model, the precisions and dimensions of the origin // Deserialize the generated model, the precisions and dimensions of the origin
// output tensors of the subgraph op into files // output tensors of the subgraph op from the cached configuration file and HiAI
// om file
bool DeviceProgram::LoadFromCacheFile( bool DeviceProgram::LoadFromCacheFile(
const std::vector<std::string>& input_names, const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names, const std::vector<std::string>& output_names,
...@@ -71,7 +72,7 @@ bool DeviceProgram::LoadFromCacheFile( ...@@ -71,7 +72,7 @@ bool DeviceProgram::LoadFromCacheFile(
VLOG(3) << "[NPU] Load model from " << model_path; VLOG(3) << "[NPU] Load model from " << model_path;
std::vector<char> model_buffer; std::vector<char> model_buffer;
if (!ReadFile(model_path, &model_buffer)) { if (!ReadFile(model_path, &model_buffer)) {
LOG(WARNING) << "[NPU] read from " << model_path << " failed!"; LOG(WARNING) << "[NPU] Open " << model_path << " for reading failed!";
return false; return false;
} }
bool model_comp = false; bool model_comp = false;
...@@ -98,9 +99,9 @@ bool DeviceProgram::LoadFromCacheFile( ...@@ -98,9 +99,9 @@ bool DeviceProgram::LoadFromCacheFile(
LOG(WARNING) << "[NPU] read from " << config_path << " failed!"; LOG(WARNING) << "[NPU] read from " << config_path << " failed!";
return false; return false;
} }
std::string config_str(config_buffer.begin(), config_buffer.end()); std::string str(config_buffer.begin(), config_buffer.end());
// Parse the precision and shapes of the output tensors // Parse the precision and shapes of the output tensors
auto output_options = Split<std::string>(config_str, ";"); auto output_options = Split<std::string>(str, ";");
CHECK_EQ(output_options.size(), output_names.size()); CHECK_EQ(output_options.size(), output_names.size());
origin_otypes_.resize(output_names.size()); origin_otypes_.resize(output_names.size());
origin_odims_.resize(output_names.size()); origin_odims_.resize(output_names.size());
...@@ -114,7 +115,7 @@ bool DeviceProgram::LoadFromCacheFile( ...@@ -114,7 +115,7 @@ bool DeviceProgram::LoadFromCacheFile(
} }
bool DeviceProgram::BuildGraphAndCacheToFile( bool DeviceProgram::BuildGraphAndCacheToFile(
const std::vector<Instruction>& origin_program, RuntimeProgram* origin_program,
const std::vector<std::string>& input_names, const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names, const std::vector<std::string>& output_names,
const std::vector<std::vector<int64_t>>& origin_idims, const std::vector<std::vector<int64_t>>& origin_idims,
...@@ -127,10 +128,13 @@ bool DeviceProgram::BuildGraphAndCacheToFile( ...@@ -127,10 +128,13 @@ bool DeviceProgram::BuildGraphAndCacheToFile(
// Convert all of ops and their input vars and weights to HiAI IR nodes, // Convert all of ops and their input vars and weights to HiAI IR nodes,
// then added them into the HiAI IR graph // then added them into the HiAI IR graph
int status = 0; int status = 0;
CHECK(!origin_program.empty()) << "no instructions";
subgraph::npu::Graph graph; subgraph::npu::Graph graph;
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
for (auto& inst : origin_program) { CHECK(origin_program) << "[NPU] The origin program is not initialized!";
CHECK_GT(origin_program->instructions(kRootBlockIdx).size(), 0)
<< "[NPU] No instructions found in the origin program!";
const auto& insts = origin_program->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = const_cast<OpLite*>(inst.op()); auto op = const_cast<OpLite*>(inst.op());
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -149,7 +153,8 @@ bool DeviceProgram::BuildGraphAndCacheToFile( ...@@ -149,7 +153,8 @@ bool DeviceProgram::BuildGraphAndCacheToFile(
// Collect the input and output nodes of the HiAI IR graph // Collect the input and output nodes of the HiAI IR graph
std::vector<ge::Operator> device_inodes; std::vector<ge::Operator> device_inodes;
for (size_t i = 0; i < input_names.size(); i++) { for (size_t i = 0; i < input_names.size(); i++) {
CHECK(graph.Has(input_names[i]) && graph.Get(input_names[i])->is_data()); CHECK(graph.Has(input_names[i]));
CHECK(graph.Get(input_names[i])->is_data());
device_inodes.push_back(*graph.Get(input_names[i])->data()); device_inodes.push_back(*graph.Get(input_names[i])->data());
} }
std::vector<ge::Operator> device_onodes; std::vector<ge::Operator> device_onodes;
...@@ -173,6 +178,9 @@ bool DeviceProgram::BuildGraphAndCacheToFile( ...@@ -173,6 +178,9 @@ bool DeviceProgram::BuildGraphAndCacheToFile(
LOG(WARNING) << "[NPU] Load model failed!"; LOG(WARNING) << "[NPU] Load model failed!";
return false; return false;
} }
// Do not check model compatibility because it assume that the cached om model
// is always compatible with the current device
// Update the precison and dimensions of the origin output tensors
// Update the precison and dimensions of the origin output tensors // Update the precison and dimensions of the origin output tensors
CHECK_EQ(origin_otensors.size(), output_names.size()); CHECK_EQ(origin_otensors.size(), output_names.size());
origin_otypes_.resize(output_names.size()); origin_otypes_.resize(output_names.size());
...@@ -247,7 +255,7 @@ bool DeviceProgram::ShareBufferWithOriginTensors( ...@@ -247,7 +255,7 @@ bool DeviceProgram::ShareBufferWithOriginTensors(
device_idims_[i].GetHeight() * device_idims_[i].GetWidth()); device_idims_[i].GetHeight() * device_idims_[i].GetWidth());
VLOG(3) << "[NPU] Init the input tensors for the device program and share " VLOG(3) << "[NPU] Init the input tensors for the device program and share "
"their buffers with the origin input tensors"; "their buffers with the origin input tensors";
// reinit device tensor will free shared buffer, so copy data to a tmp // Reinit device tensor will free shared buffer, so copy data to a tmp
// tensor // tensor
Tensor tmp; Tensor tmp;
tmp.CopyDataFrom(*(*origin_itensors)[i]); tmp.CopyDataFrom(*(*origin_itensors)[i]);
...@@ -337,8 +345,9 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -337,8 +345,9 @@ bool SubgraphEngine::BuildDeviceProgram() {
if (!device_programs_.count(origin_idims_)) { if (!device_programs_.count(origin_idims_)) {
auto device_program = std::make_shared<DeviceProgram>(); auto device_program = std::make_shared<DeviceProgram>();
// Obtain the model cache dir from the NPU Context of the subgraph op // Obtain the model cache dir from the NPU Context of the subgraph op
auto model_cache_dir = ctx_->As<NPUContext>().SubgraphModelCacheDir(); auto model_cache_dir =
VLOG(3) << "[NPU] Getting subgraph model_cache_dir is: " << model_cache_dir; ctx_->As<NPUContext>().SubgraphModelCacheDir(exec_scope_);
VLOG(3) << "[NPU] Getting subgraph_model_cache_dir: " << model_cache_dir;
// Check and load if the cached model and configuration file exists // Check and load if the cached model and configuration file exists
if (model_cache_dir.empty() || if (model_cache_dir.empty() ||
!device_program->LoadFromCacheFile( !device_program->LoadFromCacheFile(
...@@ -346,11 +355,13 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -346,11 +355,13 @@ bool SubgraphEngine::BuildDeviceProgram() {
// Build the model online, including converting the paddle ops to the HiAI // Build the model online, including converting the paddle ops to the HiAI
// IR nodes, building the HiAI IR graph to the om model, then load it as a // IR nodes, building the HiAI IR graph to the om model, then load it as a
// new HiAI model manager client for inference. // new HiAI model manager client for inference.
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
CHECK(!origin_program_.empty()) << "no instructions"; CHECK(origin_program_) << "[NPU] The origin program is not initialized!";
if (!device_program->BuildGraphAndCacheToFile(origin_program_, CHECK_GT(origin_program_->instructions().size(), 0)
<< "[NPU] No instructions found in the origin program!";
if (!device_program->BuildGraphAndCacheToFile(origin_program_.get(),
input_names_, input_names_,
output_names_, output_names_,
origin_idims_, origin_idims_,
...@@ -391,11 +402,11 @@ bool SubgraphEngine::LaunchDeviceProgram() { ...@@ -391,11 +402,11 @@ bool SubgraphEngine::LaunchDeviceProgram() {
void SubgraphCompute::PrepareForRun() { void SubgraphCompute::PrepareForRun() {
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
engine_.reset(new SubgraphEngine(ctx_.get(), engine_.reset(new SubgraphEngine(ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names));
param.scope));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -41,7 +41,7 @@ class DeviceProgram { ...@@ -41,7 +41,7 @@ class DeviceProgram {
const std::vector<std::vector<int64_t>>& origin_idims, const std::vector<std::vector<int64_t>>& origin_idims,
const std::string& model_cache_dir); const std::string& model_cache_dir);
bool BuildGraphAndCacheToFile( bool BuildGraphAndCacheToFile(
const std::vector<Instruction>& origin_program, RuntimeProgram* origin_program,
const std::vector<std::string>& input_names, const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names, const std::vector<std::string>& output_names,
const std::vector<std::vector<int64_t>>& origin_idims, const std::vector<std::vector<int64_t>>& origin_idims,
...@@ -71,12 +71,16 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -71,12 +71,16 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext* ctx, SubgraphEngine(KernelContext* ctx,
int block_idx, int block_idx,
cpp::BlockDesc* block_desc, const std::shared_ptr<const cpp::ProgramDesc>& program_desc,
Scope* exec_scope,
const std::vector<std::string>& input_names, const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names, const std::vector<std::string>& output_names)
Scope* scope) : subgraph::Engine(ctx,
: subgraph::Engine( block_idx,
ctx, block_idx, block_desc, input_names, output_names, scope) {} program_desc,
exec_scope,
input_names,
output_names) {}
protected: protected:
bool PrepareWorkspaceForDeviceProgram() override; bool PrepareWorkspaceForDeviceProgram() override;
......
...@@ -28,26 +28,6 @@ namespace lite { ...@@ -28,26 +28,6 @@ namespace lite {
namespace kernels { namespace kernels {
namespace rknpu { namespace rknpu {
bool SubgraphEngine::PrepareWorkspaceForDeviceProgram() {
// Obtain the origin input tensors, and create the origin output
// tensors(Don't try to access them before launch the device program or the
// origin program)
PrepareWorkspaceForOriginProgram();
// Create the device input and output tensors, but don't initialize them
// with the dimensions
device_itensors_.resize(input_names_.size());
for (int i = 0; i < input_names_.size(); i++) {
device_itensors_[i].reset(new hiai::AiTensor);
CHECK(device_itensors_[i]);
}
device_otensors_.resize(output_names_.size());
for (int i = 0; i < output_names_.size(); i++) {
device_otensors_[i].reset(new hiai::AiTensor);
CHECK(device_otensors_[i]);
}
return true;
}
bool SubgraphEngine::BuildDeviceProgram() { bool SubgraphEngine::BuildDeviceProgram() {
LOG(INFO) << "[RKNPU]:BuildDeviceProgram"; LOG(INFO) << "[RKNPU]:BuildDeviceProgram";
int status = 0; int status = 0;
...@@ -55,10 +35,11 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -55,10 +35,11 @@ bool SubgraphEngine::BuildDeviceProgram() {
// RKNPU IR graph // RKNPU IR graph
subgraph::rknpu::Graph graph; subgraph::rknpu::Graph graph;
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = const_cast<OpLite*>(inst.op()); auto op = const_cast<OpLite*>(inst.op());
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -76,92 +57,26 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -76,92 +57,26 @@ bool SubgraphEngine::BuildDeviceProgram() {
} }
// Collect the valid input and output nodes in the RKNPU IR graph and update // Collect the valid input and output nodes in the RKNPU IR graph and update
// the input and output names // the input and output names
device_inames_.clear(); device_itensors_.clear();
device_onames_.clear(); device_otensors_.clear();
for (auto& input_name : input_names_) {
LOG(INFO) << "[RKNPU] Input node " << input_name;
if (graph.Has(input_name)) {
LOG(INFO) << input_name << " Precision "
<< PrecisionToStr(graph.Get(input_name)->precision());
device_itensors_.push_back(graph.Get(input_name)->data());
device_inames_.push_back(input_name);
} else {
LOG(WARNING) << "[RKNPU] Input node " << input_name
<< " is ignored because it does not exist.";
}
}
for (auto& output_name : output_names_) {
LOG(INFO) << "[RKNPU] Output node " << output_name;
if (graph.Has(output_name)) {
auto tensor = scope_->FindMutableTensor(output_name);
LOG(INFO) << output_name << " Precision "
<< PrecisionToStr(tensor->precision());
device_otensors_.push_back(graph.Get(output_name)->data());
device_onames_.push_back(output_name);
} else {
LOG(WARNING) << "[RKNPU] Output node " << output_name
<< " is ignored because it does not exist.";
}
}
CHECK(!device_inames_.empty())
<< "[RKNPU] No input nodes found for building NPU model";
CHECK(!device_onames_.empty())
<< "[RKNPU] No output nodes found for building NPU model";
device_program_ = lite::rknpu::Device::Global().Build(
model_name_, graph.GetHandle(), device_itensors_, device_otensors_);
if (device_program_ == nullptr) {
LOG(WARNING) << "[RKNPU] Build model failed!";
return false;
}
// input
origin_idims_.resize(input_names_.size());
origin_itensors_.resize(input_names_.size());
for (size_t i = 0; i < input_names_.size(); i++) { for (size_t i = 0; i < input_names_.size(); i++) {
origin_itensors_[i] = scope_->FindMutableTensor(input_names_[i]); CHECK(graph.Has(input_names_[i])) << "[RKNPU] Failed to find input node "
CHECK(origin_itensors_[i]); << input_names_[i];
origin_idims_[i] = origin_itensors_[i]->dims(); auto node = graph.Get(input_names_[i]);
}
// output
origin_odims_.resize(output_names_.size());
origin_otensors_.resize(output_names_.size());
for (size_t i = 0; i < output_names_.size(); i++) {
origin_otensors_[i] = scope_->FindMutableTensor(output_names_[i]);
CHECK(origin_otensors_[i]);
origin_odims_[i] = origin_otensors_[i]->dims();
auto output_dims = origin_otensors_[i]->dims();
}
origin_idims_.resize(device_inames_.size());
origin_itensors_.resize(device_inames_.size());
device_itensors_.resize(device_inames_.size());
origin_odims_.resize(device_onames_.size());
origin_otensors_.resize(device_onames_.size());
device_otensors_.resize(device_onames_.size());
for (int i = 0; i < device_inames_.size(); i++) {
auto node = graph.Get(device_inames_[i]);
auto precision = node->precision(); auto precision = node->precision();
auto layout = node->layout(); auto layout = node->layout();
origin_itensors_[i] = scope_->FindMutableTensor(device_inames_[i]); LOG(INFO) << "[RKNPU] Inputs[" << i << "] name: " << input_names_[i]
CHECK(origin_itensors_[i]);
origin_idims_[i] = origin_itensors_[i]->dims();
LOG(INFO) << "[RKNPU] Inputs[" << i << "] name: " << device_inames_[i]
<< " precision: " << PrecisionToStr(precision) << " precision: " << PrecisionToStr(precision)
<< " layout: " << DataLayoutToStr(layout); << " layout: " << DataLayoutToStr(layout);
device_itensors_.push_back(node->data());
} }
for (int i = 0; i < device_onames_.size(); i++) { for (size_t i = 0; i < output_names_.size(); i++) {
auto node = graph.Get(device_onames_[i]); CHECK(graph.Has(output_names_[i])) << "[RKNPU] Failed to find output node "
<< output_names_[i];
auto node = graph.Get(output_names_[i]);
auto precision = node->precision(); auto precision = node->precision();
auto layout = node->layout(); auto layout = node->layout();
origin_otensors_[i] = scope_->FindMutableTensor(device_onames_[i]); LOG(INFO) << "[RKNPU] Outputs[" << i << "] name: " << output_names_[i]
CHECK(origin_otensors_[i]);
origin_odims_[i] = origin_otensors_[i]->dims();
LOG(INFO) << "[RKNPU] Outputs[" << i << "] name: " << device_onames_[i]
<< " precision: " << PrecisionToStr(precision) << " precision: " << PrecisionToStr(precision)
<< " layout: " << DataLayoutToStr(layout); << " layout: " << DataLayoutToStr(layout);
// Prepare the device output tensors // Prepare the device output tensors
...@@ -182,11 +97,19 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -182,11 +97,19 @@ bool SubgraphEngine::BuildDeviceProgram() {
origin_otensors_[i]->mutable_data<int64_t>(); origin_otensors_[i]->mutable_data<int64_t>();
break; break;
default: default:
LOG(FATAL) << "[RKNPU] " << device_onames_[i] LOG(FATAL) << "[RKNPU] " << output_names_[i]
<< " can't mutable data with precision type " << " can't mutable data with precision type "
<< PrecisionToStr(precision); << PrecisionToStr(precision);
break; break;
} }
device_otensors_.push_back(node->data());
}
// Create the RKNPU model and set the input and output nodes
device_program_ = lite::rknpu::Device::Global().Build(
model_name_, graph.GetHandle(), device_itensors_, device_otensors_);
if (device_program_ == nullptr) {
LOG(WARNING) << "[RKNPU] Build model failed!";
return false;
} }
return true; return true;
} }
...@@ -196,8 +119,8 @@ bool SubgraphEngine::LaunchDeviceProgram() { ...@@ -196,8 +119,8 @@ bool SubgraphEngine::LaunchDeviceProgram() {
std::vector<rk::nn::InputInfo> inputs; std::vector<rk::nn::InputInfo> inputs;
std::vector<rk::nn::OutputInfo> outputs; std::vector<rk::nn::OutputInfo> outputs;
inputs.resize(device_itensors_.size()); inputs.resize(origin_itensors_.size());
for (size_t i = 0; i < device_itensors_.size(); i++) { for (size_t i = 0; i < origin_itensors_.size(); i++) {
inputs[i].index = i; inputs[i].index = i;
inputs[i].buf = const_cast<void*>(origin_itensors_[i]->raw_data()); inputs[i].buf = const_cast<void*>(origin_itensors_[i]->raw_data());
inputs[i].size = origin_itensors_[i]->memory_size(); inputs[i].size = origin_itensors_[i]->memory_size();
...@@ -207,8 +130,8 @@ bool SubgraphEngine::LaunchDeviceProgram() { ...@@ -207,8 +130,8 @@ bool SubgraphEngine::LaunchDeviceProgram() {
inputs[i].layout = rk::nn::DataLayoutType::NCHW; inputs[i].layout = rk::nn::DataLayoutType::NCHW;
} }
outputs.resize(device_otensors_.size()); outputs.resize(origin_otensors_.size());
for (size_t i = 0; i < device_otensors_.size(); i++) { for (size_t i = 0; i < origin_otensors_.size(); i++) {
outputs[i].index = i; outputs[i].index = i;
outputs[i].buf = const_cast<void*>(origin_otensors_[i]->raw_data()); outputs[i].buf = const_cast<void*>(origin_otensors_[i]->raw_data());
outputs[i].size = origin_otensors_[i]->memory_size(); outputs[i].size = origin_otensors_[i]->memory_size();
...@@ -225,11 +148,11 @@ void SubgraphCompute::PrepareForRun() { ...@@ -225,11 +148,11 @@ void SubgraphCompute::PrepareForRun() {
LOG(INFO) << "[RKNPU]:PrepareForRun"; LOG(INFO) << "[RKNPU]:PrepareForRun";
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
engine_.reset(new SubgraphEngine(ctx_.get(), engine_.reset(new SubgraphEngine(ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names));
param.scope));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -34,15 +34,18 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -34,15 +34,18 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext *ctx, SubgraphEngine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names)
Scope *scope) : subgraph::Engine(ctx,
: subgraph::Engine( block_idx,
ctx, block_idx, block_desc, input_names, output_names, scope) {} program_desc,
exec_scope,
input_names,
output_names) {}
protected: protected:
bool PrepareWorkspaceForDeviceProgram() override;
bool BuildDeviceProgram() override; bool BuildDeviceProgram() override;
bool LaunchDeviceProgram() override; bool LaunchDeviceProgram() override;
......
...@@ -53,10 +53,11 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -53,10 +53,11 @@ bool SubgraphEngine::BuildDeviceProgram() {
// IR graph // IR graph
subgraph::xpu::Graph graph; subgraph::xpu::Graph graph;
const auto& bridges = subgraph::Registry::Instance(); const auto& bridges = subgraph::Registry::Instance();
if (origin_program_.empty()) { if (!origin_program_) {
BuildOriginProgram(); BuildOriginProgram();
} }
for (auto& inst : origin_program_) { const auto& insts = origin_program_->instructions(kRootBlockIdx);
for (auto& inst : insts) {
auto op = const_cast<OpLite*>(inst.op()); auto op = const_cast<OpLite*>(inst.op());
CHECK(op); CHECK(op);
op->CheckShape(); op->CheckShape();
...@@ -123,7 +124,7 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -123,7 +124,7 @@ bool SubgraphEngine::BuildDeviceProgram() {
auto node = graph.Get(device_inames_[i]); auto node = graph.Get(device_inames_[i]);
auto precision = node->precision(); auto precision = node->precision();
auto layout = node->layout(); auto layout = node->layout();
origin_itensors_[i] = scope_->FindMutableTensor(device_inames_[i]); origin_itensors_[i] = exec_scope_->FindMutableTensor(device_inames_[i]);
CHECK(origin_itensors_[i]); CHECK(origin_itensors_[i]);
origin_idims_[i] = origin_itensors_[i]->dims(); origin_idims_[i] = origin_itensors_[i]->dims();
VLOG(3) << "[XPU] Inputs[" << i << "] name: " << device_inames_[i] VLOG(3) << "[XPU] Inputs[" << i << "] name: " << device_inames_[i]
...@@ -147,7 +148,7 @@ bool SubgraphEngine::BuildDeviceProgram() { ...@@ -147,7 +148,7 @@ bool SubgraphEngine::BuildDeviceProgram() {
auto node = graph.Get(device_onames_[i]); auto node = graph.Get(device_onames_[i]);
auto precision = node->precision(); auto precision = node->precision();
auto layout = node->layout(); auto layout = node->layout();
origin_otensors_[i] = scope_->FindMutableTensor(device_onames_[i]); origin_otensors_[i] = exec_scope_->FindMutableTensor(device_onames_[i]);
CHECK(origin_otensors_[i]); CHECK(origin_otensors_[i]);
origin_odims_[i] = origin_otensors_[i]->dims(); origin_odims_[i] = origin_otensors_[i]->dims();
VLOG(3) << "[XPU] Outputs[" << i << "] name: " << device_onames_[i] VLOG(3) << "[XPU] Outputs[" << i << "] name: " << device_onames_[i]
...@@ -220,11 +221,11 @@ bool SubgraphEngine::LaunchDeviceProgram() { ...@@ -220,11 +221,11 @@ bool SubgraphEngine::LaunchDeviceProgram() {
void SubgraphCompute::PrepareForRun() { void SubgraphCompute::PrepareForRun() {
auto& param = this->Param<param_t>(); auto& param = this->Param<param_t>();
engine_.reset(new SubgraphEngine(ctx_.get(), engine_.reset(new SubgraphEngine(ctx_.get(),
param.sub_block_idx, param.block_idx,
param.sub_block_desc, param.program_desc,
param.exec_scope,
param.input_data_names, param.input_data_names,
param.output_data_names, param.output_data_names));
param.scope));
CHECK(engine_); CHECK(engine_);
} }
......
...@@ -31,12 +31,16 @@ class SubgraphEngine : public subgraph::Engine { ...@@ -31,12 +31,16 @@ class SubgraphEngine : public subgraph::Engine {
public: public:
SubgraphEngine(KernelContext *ctx, SubgraphEngine(KernelContext *ctx,
int block_idx, int block_idx,
cpp::BlockDesc *block_desc, const std::shared_ptr<const cpp::ProgramDesc> &program_desc,
Scope *exec_scope,
const std::vector<std::string> &input_names, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::string> &output_names)
Scope *scope) : subgraph::Engine(ctx,
: subgraph::Engine( block_idx,
ctx, block_idx, block_desc, input_names, output_names, scope) {} program_desc,
exec_scope,
input_names,
output_names) {}
protected: protected:
bool PrepareWorkspaceForDeviceProgram() override; bool PrepareWorkspaceForDeviceProgram() override;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "lite/model_parser/base/block_desc.h" #include "lite/model_parser/base/block_desc.h"
#include "lite/model_parser/base/op_desc.h" #include "lite/model_parser/base/op_desc.h"
#include "lite/model_parser/base/program_desc.h" #include "lite/model_parser/base/program_desc.h"
#include "lite/model_parser/base/proto_desc.h"
#include "lite/model_parser/base/traits.h" #include "lite/model_parser/base/traits.h"
#include "lite/model_parser/base/var_desc.h" #include "lite/model_parser/base/var_desc.h"
#include "lite/utils/all.h" #include "lite/utils/all.h"
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
namespace paddle {
namespace lite {
// The Index of first Block in Program. also called root block.
constexpr int kRootBlockIdx = 0;
// The Parent Index of root Block, this block does not exist.
constexpr int kNoneBlockIdx = -1;
} // namespace lite
} // namespace paddle
...@@ -294,9 +294,9 @@ const proto::VarType::TensorDesc &VarDesc::tensor_desc() const { ...@@ -294,9 +294,9 @@ const proto::VarType::TensorDesc &VarDesc::tensor_desc() const {
case proto::VarType::LOD_TENSOR_ARRAY: case proto::VarType::LOD_TENSOR_ARRAY:
return desc_->type().tensor_array().tensor(); return desc_->type().tensor_array().tensor();
default: default:
LOG(FATAL) LOG(WARNING) << "Getting 'tensor_desc' is not supported by the type("
<< "Getting 'tensor_desc' is not supported by the type of var %s." << static_cast<int>(desc_->type().type()) << ") of var "
<< this->Name(); << this->Name();
} }
return framework::proto::VarDesc().type().lod_tensor().tensor(); return framework::proto::VarDesc().type().lod_tensor().tensor();
} }
...@@ -312,10 +312,9 @@ std::vector<proto::VarType::TensorDesc> VarDesc::tensor_descs() const { ...@@ -312,10 +312,9 @@ std::vector<proto::VarType::TensorDesc> VarDesc::tensor_descs() const {
} }
return res; return res;
default: default:
LOG(FATAL) LOG(WARNING) << "Getting 'tensor_descs' is not supported by the type("
<< "Getting 'tensor_descs' is not supported by the type of var " << static_cast<int>(desc_->type().type()) << ") of var "
"%s." << this->Name();
<< this->Name();
} }
return std::vector<proto::VarType::TensorDesc>(); return std::vector<proto::VarType::TensorDesc>();
} }
......
...@@ -115,6 +115,7 @@ add_operator(ctc_align_op_lite extra SRCS ctc_align_op.cc DEPS ${op_DEPS}) ...@@ -115,6 +115,7 @@ add_operator(ctc_align_op_lite extra SRCS ctc_align_op.cc DEPS ${op_DEPS})
add_operator(max_pool_with_index_op extra SRCS max_pool_with_index_op.cc DEPS ${op_DEPS}) add_operator(max_pool_with_index_op extra SRCS max_pool_with_index_op.cc DEPS ${op_DEPS})
add_operator(pixel_shuffle_op extra SRCS pixel_shuffle_op.cc DEPS ${op_DEPS}) add_operator(pixel_shuffle_op extra SRCS pixel_shuffle_op.cc DEPS ${op_DEPS})
add_operator(clip_op extra SRCS clip_op.cc DEPS ${op_DEPS}) add_operator(clip_op extra SRCS clip_op.cc DEPS ${op_DEPS})
add_operator(print_op extra SRCS print_op.cc DEPS ${op_DEPS})
# for OCR specific # for OCR specific
add_operator(while_op extra SRCS while_op.cc DEPS ${op_DEPS}) add_operator(while_op extra SRCS while_op.cc DEPS ${op_DEPS})
......
...@@ -21,15 +21,15 @@ namespace lite { ...@@ -21,15 +21,15 @@ namespace lite {
namespace operators { namespace operators {
bool AssignOpLite::CheckShape() const { bool AssignOpLite::CheckShape() const {
CHECK_OR_FALSE(param_.X); CHECK_OR_FALSE(param_.X || param_.X_array);
CHECK_OR_FALSE(param_.Out); CHECK_OR_FALSE(param_.Out || param_.Out_array);
return true; return true;
} }
bool AssignOpLite::InferShapeImpl() const { bool AssignOpLite::InferShapeImpl() const {
if (param_.X != nullptr) { if (param_.X) {
param_.Out->Resize(param_.X->dims()); param_.Out->Resize(param_.X->dims());
} else if (param_.X_array != nullptr) { } else if (param_.X_array) {
param_.Out_array->resize(param_.Out_array->size()); param_.Out_array->resize(param_.Out_array->size());
} else { } else {
LOG(FATAL) << "x or x_array must be set."; LOG(FATAL) << "x or x_array must be set.";
......
...@@ -20,35 +20,37 @@ namespace paddle { ...@@ -20,35 +20,37 @@ namespace paddle {
namespace lite { namespace lite {
namespace operators { namespace operators {
bool ConditionalBlockOpLite::CheckShape() const { bool ConditionalBlockOp::CheckShape() const {
CHECK_OR_FALSE(param_.cond); CHECK_OR_FALSE(param_.cond);
CHECK_OR_FALSE(param_.sub_block); CHECK_OR_FALSE(param_.program_desc);
CHECK_OR_FALSE(param_.scope); CHECK_OR_FALSE(param_.exec_scope);
return true; return true;
} }
bool ConditionalBlockOpLite::InferShapeImpl() const { return true; } bool ConditionalBlockOp::InferShapeImpl() const { return true; }
bool ConditionalBlockOpLite::AttachImpl(const cpp::OpDesc &op_desc, bool ConditionalBlockOp::AttachImpl(const cpp::OpDesc& op_desc, Scope* scope) {
lite::Scope *scope) {
auto condition = op_desc.Input("Cond").front(); auto condition = op_desc.Input("Cond").front();
param_.cond = scope->FindVar(condition)->GetMutable<lite::Tensor>(); param_.cond = scope->FindVar(condition)->GetMutable<lite::Tensor>();
auto inputs = op_desc.Input("Input"); auto inputs = op_desc.Input("Input");
for (auto var : inputs) { for (const auto& input : inputs) {
param_.x.push_back(scope->FindVar(var)->GetMutable<lite::Tensor>()); auto* var = scope->FindVar(input);
CHECK(var);
param_.inputs.push_back(var->GetMutable<lite::Tensor>());
} }
auto outs = op_desc.Output("Out"); auto outs = op_desc.Output("Out");
for (auto var : outs) { for (const auto& out : outs) {
param_.outs.push_back(scope->FindVar(var)->GetMutable<lite::Tensor>()); auto* var = scope->FindVar(out);
CHECK(var);
param_.outs.push_back(var->GetMutable<lite::Tensor>());
} }
param_.is_scalar_condition = op_desc.GetAttr<bool>("is_scalar_condition"); param_.is_scalar_condition = op_desc.GetAttr<bool>("is_scalar_condition");
// obtain sub_block in core program.cc // obtain sub_block in core program.cc
param_.sub_block = sub_block_; CHECK(param_.program_desc);
param_.scope = scope; param_.block_idx = op_desc.GetAttr<int32_t>("sub_block");
CHECK_GE(param_.block_idx, 0);
param_.exec_scope = scope;
CHECK(param_.exec_scope);
return true; return true;
} }
...@@ -57,4 +59,4 @@ bool ConditionalBlockOpLite::AttachImpl(const cpp::OpDesc &op_desc, ...@@ -57,4 +59,4 @@ bool ConditionalBlockOpLite::AttachImpl(const cpp::OpDesc &op_desc,
} // namespace paddle } // namespace paddle
REGISTER_LITE_OP(conditional_block, REGISTER_LITE_OP(conditional_block,
paddle::lite::operators::ConditionalBlockOpLite); paddle::lite::operators::ConditionalBlockOp);
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "lite/core/op_lite.h" #include "lite/core/op_lite.h"
...@@ -23,27 +24,30 @@ namespace paddle { ...@@ -23,27 +24,30 @@ namespace paddle {
namespace lite { namespace lite {
namespace operators { namespace operators {
class ConditionalBlockOpLite : public OpLite { class ConditionalBlockOp : public OpLite {
public: public:
ConditionalBlockOpLite() {} ConditionalBlockOp() {}
explicit ConditionalBlockOpLite(const std::string &op_type) explicit ConditionalBlockOp(const std::string &op_type) : OpLite(op_type) {}
: OpLite(op_type) {}
bool CheckShape() const override; bool CheckShape() const override;
bool InferShapeImpl() const override; bool InferShapeImpl() const override;
bool AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) override; bool AttachImpl(const cpp::OpDesc &opdesc, Scope *scope) override;
void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); } void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); }
std::string DebugString() const override { return "conditional_block"; } std::string DebugString() const override { return "conditional_block"; }
void SetSubBlock(cpp::BlockDesc *desc) { sub_block_ = desc; } void SetProgramDesc(std::shared_ptr<const cpp::ProgramDesc> program_desc) {
param_.program_desc = program_desc;
}
std::shared_ptr<const cpp::ProgramDesc> GetProgramDesc() {
return param_.program_desc;
}
private: private:
mutable ConditionalBlockParam param_; mutable ConditionalBlockParam param_;
cpp::BlockDesc *sub_block_;
}; };
} // namespace operators } // namespace operators
......
...@@ -90,9 +90,9 @@ struct SubgraphParam : ParamBase { ...@@ -90,9 +90,9 @@ struct SubgraphParam : ParamBase {
std::vector<std::string> output_names{}; std::vector<std::string> output_names{};
std::vector<std::string> input_data_names{}; std::vector<std::string> input_data_names{};
std::vector<std::string> output_data_names{}; std::vector<std::string> output_data_names{};
int sub_block_idx{-1}; int block_idx{-1};
cpp::BlockDesc* sub_block_desc{nullptr}; std::shared_ptr<const cpp::ProgramDesc> program_desc{nullptr};
Scope* scope{nullptr}; Scope* exec_scope{nullptr};
}; };
/// -------------------------- NN operators ------------------------------------ /// -------------------------- NN operators ------------------------------------
...@@ -939,11 +939,10 @@ struct CompareParam : ParamBase { ...@@ -939,11 +939,10 @@ struct CompareParam : ParamBase {
}; };
struct WhileParam : ParamBase { struct WhileParam : ParamBase {
Scope* scope{};
Tensor* cond{}; Tensor* cond{};
cpp::BlockDesc* sub_block{}; int block_idx{-1};
std::vector<Tensor*> x{}; std::shared_ptr<const cpp::ProgramDesc> program_desc{nullptr};
std::vector<Tensor*> outs{}; Scope* exec_scope{nullptr};
}; };
struct TopkParam : ParamBase { struct TopkParam : ParamBase {
...@@ -1454,10 +1453,11 @@ struct MergeLodTensorParam : ParamBase { ...@@ -1454,10 +1453,11 @@ struct MergeLodTensorParam : ParamBase {
struct ConditionalBlockParam : ParamBase { struct ConditionalBlockParam : ParamBase {
const lite::Tensor* cond{}; const lite::Tensor* cond{};
std::vector<lite::Tensor*> x{}; std::vector<lite::Tensor*> inputs{};
std::vector<lite::Tensor*> outs{}; std::vector<lite::Tensor*> outs{};
cpp::BlockDesc* sub_block{}; int block_idx{-1};
Scope* scope{}; std::shared_ptr<const cpp::ProgramDesc> program_desc{nullptr};
Scope* exec_scope{nullptr};
bool is_scalar_condition{}; bool is_scalar_condition{};
}; };
...@@ -1779,6 +1779,22 @@ struct ClipParam : ParamBase { ...@@ -1779,6 +1779,22 @@ struct ClipParam : ParamBase {
float max{}; float max{};
}; };
struct PrintParam : ParamBase {
const lite::Tensor* in{};
lite::Tensor* out{};
std::string name;
int first_n{-1};
std::string message;
int summarize{20};
bool print_tensor_name{true};
bool print_tensor_type{true};
bool print_tensor_shape{true};
bool print_tensor_lod{true};
bool print_tensor_layout{true};
std::string print_phase;
bool is_forward{true};
};
} // namespace operators } // namespace operators
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lite/operators/print_op.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace operators {
bool PrintOp::CheckShape() const {
CHECK_OR_FALSE(param_.in);
CHECK_OR_FALSE(param_.out);
return true;
}
bool PrintOp::InferShapeImpl() const {
param_.out->set_lod(param_.in->lod());
param_.out->Resize(param_.in->dims());
return true;
}
bool PrintOp::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
AttachParam(&param_);
param_.name = op_desc.Input("In").front();
param_.in = scope->FindTensor(param_.name);
param_.out = scope->FindMutableTensor(op_desc.Output("Out").front());
param_.first_n = op_desc.GetAttr<int32_t>("first_n");
param_.message = op_desc.GetAttr<std::string>("message");
param_.summarize = op_desc.GetAttr<int32_t>("summarize");
param_.print_tensor_name = op_desc.GetAttr<bool>("print_tensor_name");
param_.print_tensor_type = op_desc.GetAttr<bool>("print_tensor_type");
param_.print_tensor_shape = op_desc.GetAttr<bool>("print_tensor_shape");
param_.print_tensor_lod = op_desc.GetAttr<bool>("print_tensor_lod");
param_.print_tensor_layout = op_desc.GetAttr<bool>("print_tensor_layout");
param_.print_phase = op_desc.GetAttr<std::string>("print_phase");
param_.is_forward = op_desc.GetAttr<bool>("is_forward");
return true;
}
} // namespace operators
} // namespace lite
} // namespace paddle
REGISTER_LITE_OP(print, paddle::lite::operators::PrintOp);
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <string>
#include <vector>
#include "lite/core/op_lite.h"
#include "lite/core/scope.h"
#include "lite/utils/all.h"
namespace paddle {
namespace lite {
namespace operators {
class PrintOp : public OpLite {
public:
PrintOp() {}
explicit PrintOp(const std::string &op_type) : OpLite(op_type) {}
bool CheckShape() const override;
bool InferShapeImpl() const override;
bool AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) override;
void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); }
std::string DebugString() const override { return "print"; }
private:
mutable PrintParam param_;
};
} // namespace operators
} // namespace lite
} // namespace paddle
...@@ -39,10 +39,11 @@ bool SubgraphOp::AttachImpl(const cpp::OpDesc& op_desc, lite::Scope* scope) { ...@@ -39,10 +39,11 @@ bool SubgraphOp::AttachImpl(const cpp::OpDesc& op_desc, lite::Scope* scope) {
op_desc.GetAttr<std::vector<std::string>>("input_data_names"); op_desc.GetAttr<std::vector<std::string>>("input_data_names");
param_.output_data_names = param_.output_data_names =
op_desc.GetAttr<std::vector<std::string>>("output_data_names"); op_desc.GetAttr<std::vector<std::string>>("output_data_names");
CHECK(param_.sub_block_desc); CHECK(param_.program_desc);
param_.sub_block_idx = op_desc.GetAttr<int32_t>("sub_block"); param_.block_idx = op_desc.GetAttr<int32_t>("sub_block");
param_.scope = scope; CHECK_GE(param_.block_idx, 0);
CHECK(param_.scope); param_.exec_scope = scope;
CHECK(param_.exec_scope);
return true; return true;
} }
......
...@@ -13,14 +13,11 @@ ...@@ -13,14 +13,11 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "lite/core/kernel.h"
#include "lite/core/op_lite.h" #include "lite/core/op_lite.h"
#include "lite/core/scope.h" #include "lite/core/scope.h"
#include "lite/core/tensor.h"
#include "lite/operators/op_params.h"
#include "lite/utils/all.h" #include "lite/utils/all.h"
namespace paddle { namespace paddle {
...@@ -37,14 +34,18 @@ class SubgraphOp : public OpLite { ...@@ -37,14 +34,18 @@ class SubgraphOp : public OpLite {
bool InferShapeImpl() const override; bool InferShapeImpl() const override;
bool AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) override; bool AttachImpl(const cpp::OpDesc &op_desc, Scope *scope) override;
void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); } void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); }
std::string DebugString() const override { return "subgraph"; } std::string DebugString() const override { return "subgraph"; }
void SetSubBlock(cpp::BlockDesc *desc) { param_.sub_block_desc = desc; } void SetProgramDesc(std::shared_ptr<const cpp::ProgramDesc> program_desc) {
cpp::BlockDesc *GetSubBlock() { return param_.sub_block_desc; } param_.program_desc = program_desc;
}
std::shared_ptr<const cpp::ProgramDesc> GetProgramDesc() {
return param_.program_desc;
}
private: private:
mutable SubgraphParam param_; mutable SubgraphParam param_;
......
...@@ -20,31 +20,23 @@ namespace paddle { ...@@ -20,31 +20,23 @@ namespace paddle {
namespace lite { namespace lite {
namespace operators { namespace operators {
bool WhileOpLite::CheckShape() const { bool WhileOp::CheckShape() const {
CHECK_OR_FALSE(param_.sub_block);
CHECK_OR_FALSE(param_.scope);
CHECK_OR_FALSE(param_.cond); CHECK_OR_FALSE(param_.cond);
CHECK_OR_FALSE(param_.program_desc);
CHECK_OR_FALSE(param_.exec_scope);
return true; return true;
} }
bool WhileOpLite::InferShapeImpl() const { return true; } bool WhileOp::InferShapeImpl() const { return true; }
bool WhileOpLite::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
auto inputs = op_desc.Input("X");
auto outs = op_desc.Output("Out");
for (auto var : inputs) {
// param_.x.push_back(scope->FindVar(var)->GetMutable<lite::Tensor>());
}
for (auto var : outs) {
// param_.outs.push_back(scope->FindVar(var)->GetMutable<lite::Tensor>());
}
param_.sub_block = sub_block_;
bool WhileOp::AttachImpl(const cpp::OpDesc &op_desc, Scope *scope) {
auto condition = op_desc.Input("Condition"); auto condition = op_desc.Input("Condition");
param_.cond = scope->FindVar(condition[0])->GetMutable<lite::Tensor>(); param_.cond = scope->FindVar(condition[0])->GetMutable<lite::Tensor>();
param_.scope = scope; CHECK(param_.program_desc);
param_.block_idx = op_desc.GetAttr<int32_t>("sub_block");
CHECK_GE(param_.block_idx, 0);
param_.exec_scope = scope;
CHECK(param_.exec_scope);
return true; return true;
} }
...@@ -52,4 +44,4 @@ bool WhileOpLite::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) { ...@@ -52,4 +44,4 @@ bool WhileOpLite::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
} // namespace lite } // namespace lite
} // namespace paddle } // namespace paddle
REGISTER_LITE_OP(while, paddle::lite::operators::WhileOpLite); REGISTER_LITE_OP(while, paddle::lite::operators::WhileOp);
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "lite/core/op_lite.h" #include "lite/core/op_lite.h"
...@@ -23,24 +24,30 @@ namespace paddle { ...@@ -23,24 +24,30 @@ namespace paddle {
namespace lite { namespace lite {
namespace operators { namespace operators {
class WhileOpLite : public OpLite { class WhileOp : public OpLite {
public: public:
WhileOpLite() {} WhileOp() {}
explicit WhileOpLite(const std::string &op_type) : OpLite(op_type) {} explicit WhileOp(const std::string &op_type) : OpLite(op_type) {}
bool CheckShape() const override; bool CheckShape() const override;
bool InferShapeImpl() const override; bool InferShapeImpl() const override;
bool AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) override; bool AttachImpl(const cpp::OpDesc &opdesc, Scope *scope) override;
void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); } void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); }
std::string DebugString() const override { return "while"; } std::string DebugString() const override { return "while"; }
void SetSubBlock(cpp::BlockDesc *desc) { sub_block_ = desc; }
void SetProgramDesc(std::shared_ptr<const cpp::ProgramDesc> program_desc) {
param_.program_desc = program_desc;
}
std::shared_ptr<const cpp::ProgramDesc> GetProgramDesc() {
return param_.program_desc;
}
private: private:
mutable WhileParam param_; mutable WhileParam param_;
cpp::BlockDesc *sub_block_;
}; };
} // namespace operators } // namespace operators
......
if(LITE_WITH_ARM)
lite_cc_test(test_transformer_with_mask_fp32_arm SRCS test_transformer_with_mask_fp32_arm.cc
DEPS ${lite_model_test_DEPS} paddle_api_full
ARM_DEPS ${arm_kernels}
ARGS --model_dir=${LITE_MODEL_DIR}/transformer_with_mask_fp32 SERIAL)
if(WITH_TESTING)
add_dependencies(test_transformer_with_mask_fp32_arm extern_lite_download_transformer_with_mask_fp32_tar_gz)
endif()
endif()
if(LITE_WITH_XPU) if(LITE_WITH_XPU)
lite_cc_test(test_resnet50_lite_xpu SRCS test_resnet50_lite_xpu.cc lite_cc_test(test_resnet50_lite_xpu SRCS test_resnet50_lite_xpu.cc
DEPS mir_passes lite_api_test_helper paddle_api_full paddle_api_light gflags utils DEPS mir_passes lite_api_test_helper paddle_api_full paddle_api_light gflags utils
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
...@@ -12,6 +12,8 @@ WITH_EXTRA=OFF ...@@ -12,6 +12,8 @@ WITH_EXTRA=OFF
WITH_CV=OFF WITH_CV=OFF
# controls whether to hide log information, default is ON. # controls whether to hide log information, default is ON.
WITH_LOG=ON WITH_LOG=ON
# controls whether to throw the exception when error occurs, default is OFF
WITH_EXCEPTION=OFF
# absolute path of Paddle-Lite. # absolute path of Paddle-Lite.
workspace=$PWD/$(dirname $0)/../../ workspace=$PWD/$(dirname $0)/../../
# options of striping lib according to input model. # options of striping lib according to input model.
...@@ -69,6 +71,7 @@ function make_ios { ...@@ -69,6 +71,7 @@ function make_ios {
-DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \ -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \
-DLITE_WITH_X86=OFF \ -DLITE_WITH_X86=OFF \
-DLITE_WITH_LOG=$WITH_LOG \ -DLITE_WITH_LOG=$WITH_LOG \
-DLITE_WITH_EXCEPTION=$WITH_EXCEPTION \
-DLITE_BUILD_TAILOR=$WITH_STRIP \ -DLITE_BUILD_TAILOR=$WITH_STRIP \
-DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \ -DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \
-DARM_TARGET_ARCH_ABI=$arch \ -DARM_TARGET_ARCH_ABI=$arch \
...@@ -96,6 +99,7 @@ function print_usage { ...@@ -96,6 +99,7 @@ function print_usage {
echo -e "| --arch: (armv8|armv7), default is armv8 |" echo -e "| --arch: (armv8|armv7), default is armv8 |"
echo -e "| --with_cv: (OFF|ON); controls whether to compile cv functions into lib, default is OFF |" echo -e "| --with_cv: (OFF|ON); controls whether to compile cv functions into lib, default is OFF |"
echo -e "| --with_log: (OFF|ON); controls whether to print log information, default is ON |" echo -e "| --with_log: (OFF|ON); controls whether to print log information, default is ON |"
echo -e "| --with_exception: (OFF|ON); controls whether to throw the exception when error occurs, default is OFF |"
echo -e "| --with_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP) |" echo -e "| --with_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP) |"
echo -e "| |" echo -e "| |"
echo -e "| arguments of striping lib according to input model:(armv8, gcc, c++_static) |" echo -e "| arguments of striping lib according to input model:(armv8, gcc, c++_static) |"
...@@ -140,6 +144,10 @@ function main { ...@@ -140,6 +144,10 @@ function main {
WITH_LOG="${i#*=}" WITH_LOG="${i#*=}"
shift shift
;; ;;
--with_exception=*)
WITH_EXCEPTION="${i#*=}"
shift
;;
help) help)
print_usage print_usage
exit 0 exit 0
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册