From edc5c06e3a8af7576c1190d85e0293d1299954a5 Mon Sep 17 00:00:00 2001 From: hong19860320 <9973393+hong19860320@users.noreply.github.com> Date: Thu, 2 Jan 2020 10:06:17 +0800 Subject: [PATCH] [LITE][XPU] Supporting llvm and xpu device target (#2711) --- lite/backends/xpu/device.cc | 7 +++++-- lite/backends/xpu/device.h | 22 ++++++++++++++++++---- lite/kernels/xpu/bridges/utility.cc | 2 +- lite/kernels/xpu/subgraph_compute.cc | 2 +- lite/tools/build_xpu.sh | 5 +++++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lite/backends/xpu/device.cc b/lite/backends/xpu/device.cc index dbf88ff833..badde878ad 100644 --- a/lite/backends/xpu/device.cc +++ b/lite/backends/xpu/device.cc @@ -36,8 +36,11 @@ std::unique_ptr Device::Build( } xtcl::xNetwork network = builder->FinalizeNetwork(xtcl::relay::TupleNode::make(all_outs)); - auto target = xtcl::Target::Create(device_name_); - auto compiler = xtcl::network::xTensorCompiler(network, target); + auto target = xtcl::NullValue(); + if (!target_.empty()) { + target = xtcl::Target::Create(target_); + } + xtcl::network::xTensorCompiler compiler(network, target); compiler.SetParams(*params); // Set the data of constant tensors compiler.Build(); VLOG(3) << "[XPU] Build done"; diff --git a/lite/backends/xpu/device.h b/lite/backends/xpu/device.h index bf9a8bf76a..6de18d5466 100644 --- a/lite/backends/xpu/device.h +++ b/lite/backends/xpu/device.h @@ -15,6 +15,7 @@ #pragma once #include +#include #include #include #include @@ -30,7 +31,18 @@ class Device { static Device x; return x; } - Device() {} + Device() { + char* name = std::getenv("XPU_DEVICE_NAME"); + if (name) { + name_ = std::string(name); + } + // XPU_DEVICE_TARGET for XPU model building, which supports 'llvm' and 'xpu + // -libs=xdnn' + char* target = std::getenv("XPU_DEVICE_TARGET"); + if (target) { + target_ = std::string(target); + } + } // Build the XPU graph to the XPU runtime, return the XPU runtime which can be // used to run inference. @@ -39,10 +51,12 @@ class Device { xtcl::network::xTensorCompiler::ParamNDArrayMap* params, std::vector* outputs); + const std::string name() const { return name_; } + const std::string target() const { return target_; } + private: - // Keep reserved fields - int device_id_{0}; - std::string device_name_{"llvm"}; + std::string name_{""}; + std::string target_{""}; }; } // namespace xpu diff --git a/lite/kernels/xpu/bridges/utility.cc b/lite/kernels/xpu/bridges/utility.cc index 79fad7c8b4..0295ae790f 100644 --- a/lite/kernels/xpu/bridges/utility.cc +++ b/lite/kernels/xpu/bridges/utility.cc @@ -103,7 +103,7 @@ DLDeviceType CvtDLDeviceType(TargetType in_type) { out_type = kDLGPU; break; case TARGET(kXPU): - out_type = kDLCPU; + out_type = static_cast(kDLXPU); break; default: LOG(FATAL) << "[XPU] Can not convert target type(" << TargetToStr(in_type) diff --git a/lite/kernels/xpu/subgraph_compute.cc b/lite/kernels/xpu/subgraph_compute.cc index 298f6f0e50..15df4f80ca 100644 --- a/lite/kernels/xpu/subgraph_compute.cc +++ b/lite/kernels/xpu/subgraph_compute.cc @@ -175,7 +175,7 @@ int SubgraphEngine::LaunchDeviceProgram() { // Update the data pointer of DLTensor to track the origin input tensors device_itensors_[i].data = const_cast(origin_itensors_[i]->raw_data()); - device_program_->SetInputZeroCopy(device_inames_[i], &device_itensors_[i]); + device_program_->SetInput(device_inames_[i], &device_itensors_[i]); } // Run the XPU model auto GetCurrentUS = []() -> double { diff --git a/lite/tools/build_xpu.sh b/lite/tools/build_xpu.sh index 9f28274471..fdf287501e 100755 --- a/lite/tools/build_xpu.sh +++ b/lite/tools/build_xpu.sh @@ -104,6 +104,11 @@ function main { build_xpu shift ;; + full_publish) + TARGET_NAME=publish_inference + build_xpu + shift + ;; *) # unknown option print_usage -- GitLab