dnn: add the CANN backend (#22634)

* cann backend impl v1

* cann backend impl v2: use opencv parsers to build models for cann

* adjust fc according to the new transA and transB

* put cann net in cann backend node and reuse forwardLayer

* use fork() to create a child process and compile cann model

* remove legacy code

* remove debug code

* fall bcak to CPU backend if there is one layer not supoorted by CANN backend

* fix netInput forward
This commit is contained in:
Yuantao Feng
2022-12-21 14:04:41 +08:00
committed by GitHub
parent a08c98cdfb
commit a2b3acfc6e
34 changed files with 2208 additions and 28 deletions

View File

@@ -43,6 +43,7 @@
#include "../op_cuda.hpp"
#include "../op_inf_engine.hpp"
#include "../ie_ngraph.hpp"
#include "../op_cann.hpp"
#ifdef HAVE_CUDA
#include "../cuda4dnn/primitives/reshape.hpp"
@@ -68,7 +69,8 @@ public:
return true;
#endif
return backendId == DNN_BACKEND_OPENCV ||
backendId == DNN_BACKEND_CUDA;
backendId == DNN_BACKEND_CUDA ||
backendId == DNN_BACKEND_CANN;
}
bool getMemoryShapes(const std::vector<MatShape> &inputs,
@@ -118,6 +120,28 @@ public:
inputs[i].copyTo(outputs[i]);
}
#ifdef HAVE_CANN
virtual Ptr<BackendNode> initCann(const std::vector<Ptr<BackendWrapper> > &inputsWrapper, const int index, const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
auto x = inputsWrapper[0].dynamicCast<CannBackendWrapper>();
auto x_desc = x->getTensorDesc();
auto op_x = nodes[0].dynamicCast<CannBackendNode>()->getOp();
auto output_desc = std::make_shared<ge::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
// create operator
std::string op_name = cv::format("identity_%d", index);
auto op = std::make_shared<ge::op::Identity>(op_name);
// set inputs
op->set_input_x_by_name(*op_x, "y");
op->update_input_desc_x(*x_desc);
// set output
op->update_output_desc_y(*output_desc);
return Ptr<BackendNode>(new CannBackendNode(op));
}
#endif
#ifdef HAVE_DNN_NGRAPH
virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inputs,