// Copyright (C) 2018-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 // #include "extension.hpp" #include "cpu_kernel.hpp" #include "op.hpp" #ifdef OPENCV_IMPORT_ENABLED #include "fft_kernel.hpp" #include "fft_op.hpp" #endif #include #ifdef NGRAPH_ONNX_IMPORT_ENABLED #include #endif #include #include #include #include using namespace TemplateExtension; //! [extension:ctor] Extension::Extension() { #ifdef NGRAPH_ONNX_IMPORT_ENABLED ngraph::onnx_import::register_operator(Operation::type_info.name, 1, "custom_domain", [](const ngraph::onnx_import::Node& node) -> ngraph::OutputVector { ngraph::OutputVector ng_inputs {node.get_ng_inputs()}; int64_t add = node.get_attribute_value("add"); return {std::make_shared(ng_inputs.at(0), add)}; }); #ifdef OPENCV_IMPORT_ENABLED ngraph::onnx_import::register_operator(FFTOp::type_info.name, 1, "custom_domain", [](const ngraph::onnx_import::Node& node) -> ngraph::OutputVector { ngraph::OutputVector ng_inputs {node.get_ng_inputs()}; bool inverse = node.get_attribute_value("inverse"); return {std::make_shared(ng_inputs.at(0), inverse)}; }); #endif #endif } //! [extension:ctor] //! [extension:dtor] Extension::~Extension() { #ifdef NGRAPH_ONNX_IMPORT_ENABLED ngraph::onnx_import::unregister_operator(Operation::type_info.name, 1, "custom_domain"); #ifdef OPENCV_IMPORT_ENABLED ngraph::onnx_import::unregister_operator(FFTOp::type_info.name, 1, "custom_domain"); #endif // OPENCV_IMPORT_ENABLED #endif // NGRAPH_ONNX_IMPORT_ENABLED } //! [extension:dtor] //! [extension:GetVersion] void Extension::GetVersion(const InferenceEngine::Version*& versionInfo) const noexcept { static InferenceEngine::Version ExtensionDescription = { {1, 0}, // extension API version "1.0", "template_ext" // extension description message }; versionInfo = &ExtensionDescription; } //! [extension:GetVersion] //! [extension:getOpSets] std::map Extension::getOpSets() { std::map opsets; ngraph::OpSet opset; opset.insert(); #ifdef OPENCV_IMPORT_ENABLED opset.insert(); #endif opsets["custom_opset"] = opset; return opsets; } //! [extension:getOpSets] //! [extension:getImplTypes] std::vector Extension::getImplTypes(const std::shared_ptr& node) { if (std::dynamic_pointer_cast(node)) { return {"CPU"}; } #ifdef OPENCV_IMPORT_ENABLED if (std::dynamic_pointer_cast(node)) { return {"CPU"}; } #endif return {}; } //! [extension:getImplTypes] //! [extension:getImplementation] InferenceEngine::ILayerImpl::Ptr Extension::getImplementation(const std::shared_ptr& node, const std::string& implType) { if (implType == "CPU") { if (std::dynamic_pointer_cast(node)) { return std::make_shared(node); } #ifdef OPENCV_IMPORT_ENABLED if (std::dynamic_pointer_cast(node) && implType == "CPU") { return std::make_shared(node); } #endif } return nullptr; } //! [extension:getImplementation] //! [extension:CreateExtension] // Generate exported function IE_DEFINE_EXTENSION_CREATE_FUNCTION(Extension) //! [extension:CreateExtension] INFERENCE_EXTENSION_API(InferenceEngine::StatusCode) InferenceEngine::CreateExtension(InferenceEngine::IExtension*& ext, InferenceEngine::ResponseDesc* resp) noexcept { try { ext = new Extension(); return OK; } catch (std::exception& ex) { if (resp) { std::string err = ((std::string) "Couldn't create extension: ") + ex.what(); err.copy(resp->msg, 255); } return InferenceEngine::GENERAL_ERROR; } }