mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 09:10:35 -06:00
dnn cann backend: add hardswish, layernorm and instasnce norm for cann and bug fix (#24462)
* add hardswish for cann * gemm cann bug fix * fix indentation * cann: add layer norm * cann: add instance norm * add supportBackend * cann: layer norm does not support axis=-1 due to 1d mat issue * disable instance norm for now * fix doc * remove tensor desc initialization for 1D tensor
This commit is contained in:
@@ -6,6 +6,9 @@
|
||||
#include <opencv2/dnn/shape_utils.hpp>
|
||||
#include "./cpu_kernels/fast_norm.hpp"
|
||||
|
||||
// CANN backend
|
||||
#include "../op_cann.hpp"
|
||||
|
||||
// OpenVINO backend
|
||||
#include "../op_inf_engine.hpp"
|
||||
#include "../ie_ngraph.hpp"
|
||||
@@ -41,6 +44,7 @@ public:
|
||||
#endif
|
||||
return backendId == DNN_BACKEND_OPENCV ||
|
||||
backendId == DNN_BACKEND_CUDA;
|
||||
// backendId == DNN_BACKEND_CANN; // not supported due to 1d mat shape issue
|
||||
}
|
||||
|
||||
bool getMemoryShapes(const std::vector<MatShape> &inputs,
|
||||
@@ -169,6 +173,51 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CANN
|
||||
virtual Ptr<BackendNode> initCann(const std::vector<Ptr<BackendWrapper> > &inputs,
|
||||
const std::vector<Ptr<BackendWrapper> > &outputs,
|
||||
const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE {
|
||||
auto input_tensor_wrapper = inputs[0].dynamicCast<CannBackendWrapper>();
|
||||
auto input_tensor_desc = input_tensor_wrapper->getTensorDesc();
|
||||
|
||||
auto scale_tensor_wrapper = inputs[1].dynamicCast<CannBackendWrapper>();
|
||||
auto scale_tensor_desc = scale_tensor_wrapper->getTensorDesc();
|
||||
|
||||
auto bias_tensor_wrapper = inputs[2].dynamicCast<CannBackendWrapper>();
|
||||
auto bias_tensor_desc = bias_tensor_wrapper->getTensorDesc();
|
||||
|
||||
auto last_node = nodes[0].dynamicCast<CannBackendNode>()->getOp();
|
||||
auto scale_node = nodes[1].dynamicCast<CannBackendNode>()->getOp();
|
||||
auto bias_node = nodes[2].dynamicCast<CannBackendNode>()->getOp();
|
||||
|
||||
auto op = std::make_shared<ge::op::InstanceNorm>(name);
|
||||
|
||||
// set attrs
|
||||
op->set_attr_epsilon(epsilon);
|
||||
|
||||
// set inputs
|
||||
// set inputs : x
|
||||
op->set_input_x_by_name(*last_node, input_tensor_wrapper->name.c_str());
|
||||
op->update_input_desc_x(*input_tensor_desc);
|
||||
// set inputs : gamma
|
||||
op->set_input_gamma_by_name((*scale_node), scale_tensor_wrapper->name.c_str());
|
||||
op->update_input_desc_gamma(*scale_tensor_desc);
|
||||
// set inputs : beta
|
||||
op->set_input_beta_by_name(*bias_node, bias_tensor_wrapper->name.c_str());
|
||||
op->update_input_desc_beta(*bias_tensor_desc);
|
||||
|
||||
// set outputs
|
||||
auto output_desc_y = std::make_shared<ge::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
|
||||
op->update_output_desc_y(*output_desc_y);
|
||||
auto output_desc_mean = std::make_shared<ge::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
|
||||
op->update_output_desc_mean(*output_desc_mean);
|
||||
auto output_desc_var = std::make_shared<ge::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
|
||||
op->update_output_desc_variance(*output_desc_var);
|
||||
|
||||
return Ptr<BackendNode>(new CannBackendNode(op));
|
||||
}
|
||||
#endif // HAVE_CANN
|
||||
|
||||
#ifdef HAVE_DNN_NGRAPH
|
||||
virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inputs,
|
||||
const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE {
|
||||
|
||||
Reference in New Issue
Block a user