OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Use any to store post ops (#30526)
Maksim Kutakovef365e2649ac
diff --git a/src/plugins/intel_cpu/src/dnnl_postops_composer.cpp b/src/plugins/intel_cpu/src/dnnl_postops_composer.cppindex f292e131cd4..cd04ad39dcf 100644--- a/src/plugins/intel_cpu/src/dnnl_postops_composer.cpp+++ b/src/plugins/intel_cpu/src/dnnl_postops_composer.cpp@@ -1029,8 +1029,8 @@ DnnlPrimitiveAttrs DnnlPostOpsComposer::compose() {for (size_t i = 0; i < postOps.size(); ++i) {const auto& postOp = postOps[i];bool isLastPostOp = (i == (postOps.size() - 1));- // @todo replace dynamic cast with an interface for appending to DNNL postops- if (const auto activation = std::dynamic_pointer_cast<ActivationPostOp>(postOp)) {++ if (const auto activation = std::any_cast<ActivationPostOp>(&postOp)) {if (useLegacyPostOps) {// legacy depthwise post ops often outperform binary post ops// first try to make do with original post ops without binary@@ -1047,7 +1047,7 @@ DnnlPrimitiveAttrs DnnlPostOpsComposer::compose() {continue;}- if (const auto ss = std::dynamic_pointer_cast<ScaleShiftPostOp>(postOp)) {+ if (const auto ss = std::any_cast<ScaleShiftPostOp>(&postOp)) {if (useLegacyPostOps) {// legacy depthwise post ops often outperform binary post ops// first try to make do with original post ops without binary@@ -1063,7 +1063,7 @@ DnnlPrimitiveAttrs DnnlPostOpsComposer::compose() {continue;}- if (const auto fq = std::dynamic_pointer_cast<FakeQuantizePostOp>(postOp)) {+ if (const auto fq = std::any_cast<FakeQuantizePostOp>(&postOp)) {// drop rounding one special residual pattern// TODO: validate this unsafe optimizationauto doRounding = [&]() {@@ -1072,11 +1072,11 @@ DnnlPrimitiveAttrs DnnlPostOpsComposer::compose() {for (size_t j = i + 1; j < postOps.size(); j++) {const auto& nextNode = postOps[j];- if (auto nextEltwiseNode = std::dynamic_pointer_cast<SumPostOp>(nextNode)) {+ if (typeid(SumPostOp) == nextNode.type()) {hasSubsequentSum = true;}- if (auto nextQuantizeNode = std::dynamic_pointer_cast<FakeQuantizePostOp>(nextNode)) {+ if (typeid(FakeQuantizePostOp) == nextNode.type()) {hasSubsequentFQ = true;}}@@ -1105,12 +1105,12 @@ DnnlPrimitiveAttrs DnnlPostOpsComposer::compose() {continue;}- if (const auto sum = std::dynamic_pointer_cast<SumPostOp>(postOp)) {+ if (const auto sum = std::any_cast<SumPostOp>(&postOp)) {appendSum(sum->scale(), sum->zeroPoint(), sum->dataType());continue;}- if (const auto conv = std::dynamic_pointer_cast<DepthwiseConvolutionPostOp>(postOp)) {+ if (const auto conv = std::any_cast<DepthwiseConvolutionPostOp>(&postOp)) {appendDepthwiseConvolution(conv->ih(),conv->iw(),conv->kernel()[1],diff --git a/src/plugins/intel_cpu/src/nodes/conv.cpp b/src/plugins/intel_cpu/src/nodes/conv.cppindex d197b9eadfb..08d43b15b83 100644--- a/src/plugins/intel_cpu/src/nodes/conv.cpp+++ b/src/plugins/intel_cpu/src/nodes/conv.cpp@@ -659,10 +659,9 @@ ExecutorPtr Convolution::createFallbackExecutor() {ConvAttrs fallbackAttrs = m_attrs;PostOps& fallbackPostOps = fallbackAttrs.postOps;// remove sum post-op from fallback post-ops- auto sumPostOp =- std::find_if(fallbackPostOps.begin(), fallbackPostOps.end(), [](const std::shared_ptr<PostOp>& postOp) {- return std::dynamic_pointer_cast<SumPostOp>(postOp);- });+ auto sumPostOp = std::find_if(fallbackPostOps.begin(), fallbackPostOps.end(), [](const auto& postOp) {+ return typeid(SumPostOp) == postOp.type();+ });fallbackPostOps.erase(sumPostOp, fallbackPostOps.end());diff --git a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected.cpp b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected.cppindex b718c346380..22692093255 100644--- a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected.cpp@@ -29,7 +29,7 @@ static bool checkPostOps(const PostOps& postOps) {if (postOps.size() > 1) {return false;}- if (const auto activation = std::dynamic_pointer_cast<ActivationPostOp>(postOps[0])) {+ if (const auto activation = std::any_cast<ActivationPostOp>(&postOps[0])) {if (checkActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()))) {return true;}@@ -49,11 +49,11 @@ static void initFCAttrs(const FCAttrs& attrs,aclfcAttrs.weightsNonTransposed = attrs.weightsNonTransposed;if (!attrs.postOps.empty() && checkPostOps(attrs.postOps)) {- auto activation = std::dynamic_pointer_cast<ActivationPostOp>(attrs.postOps[0]);- fullyConnectedLayerInfo.activation_info = getActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()),- activation->alpha(),- activation->beta(),- activation->gamma());+ const auto& activation = std::any_cast<const ActivationPostOp&>(attrs.postOps[0]);+ fullyConnectedLayerInfo.activation_info = getActivationLayerInfo(convertToEltwiseAlgorithm(activation.type()),+ activation.alpha(),+ activation.beta(),+ activation.gamma());}if (memory.at(ARG_SRC)->getPrecision() != memory.at(ARG_WEI)->getPrecision()) {diff --git a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected_utils.cpp b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected_utils.cppindex 02e8ae8b1ff..5963b6a8d33 100644--- a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected_utils.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_fullyconnected_utils.cpp@@ -274,7 +274,7 @@ MemoryPtr acl_fc_executor::prepareWeightMemory(const MemoryArgs& memory,static bool checkPostOps(const PostOps& postOps) {// Add postopsif (!postOps.empty() && postOps.size() == 1) {- if (const auto activation = std::dynamic_pointer_cast<ActivationPostOp>(postOps[0])) {+ if (const auto activation = std::any_cast<ActivationPostOp>(&postOps[0])) {if (checkActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()))) {return true;}@@ -295,11 +295,11 @@ static void initFCAttrs(const FCAttrs& attrs,aclfcAttrs.weightsNonTransposed = attrs.weightsNonTransposed;if (checkPostOps(attrs.postOps)) {- auto activation = std::dynamic_pointer_cast<ActivationPostOp>(attrs.postOps[0]);- fullyConnectedLayerInfo.activation_info = getActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()),- activation->alpha(),- activation->beta(),- activation->gamma());+ const auto& activation = std::any_cast<const ActivationPostOp&>(attrs.postOps[0]);+ fullyConnectedLayerInfo.activation_info = getActivationLayerInfo(convertToEltwiseAlgorithm(activation.type()),+ activation.alpha(),+ activation.beta(),+ activation.gamma());}if (memory.at(ARG_SRC)->getPrecision() != memory.at(ARG_WEI)->getPrecision()) {diff --git a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_lowp_fullyconnected.cpp b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_lowp_fullyconnected.cppindex 03662fe4e11..0c6bf94bd6c 100644--- a/src/plugins/intel_cpu/src/nodes/executors/acl/acl_lowp_fullyconnected.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/acl/acl_lowp_fullyconnected.cpp@@ -27,8 +27,8 @@ static bool checkPostOps(const PostOps& postOps) {return false;}- const auto activation = std::dynamic_pointer_cast<ActivationPostOp>(postOps[0]);- return checkActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()));+ const auto& activation = std::any_cast<const ActivationPostOp&>(postOps[0]);+ return checkActivationLayerInfo(convertToEltwiseAlgorithm(activation.type()));}static void initFCAttrs(const FCAttrs& attrs,@@ -41,12 +41,11 @@ static void initFCAttrs(const FCAttrs& attrs,aclfcAttrs.weightsNonTransposed = attrs.weightsNonTransposed;if (!attrs.postOps.empty()) {- auto activation = std::dynamic_pointer_cast<ActivationPostOp>(attrs.postOps[0]);- fullyConnectedLayerInfo.set_activation_info(- getActivationLayerInfo(convertToEltwiseAlgorithm(activation->type()),- activation->alpha(),- activation->beta(),- activation->gamma()));+ const auto& activation = std::any_cast<const ActivationPostOp&>(attrs.postOps[0]);+ fullyConnectedLayerInfo.set_activation_info(getActivationLayerInfo(convertToEltwiseAlgorithm(activation.type()),+ activation.alpha(),+ activation.beta(),+ activation.gamma()));}if (memory.at(ARG_SRC)->getPrecision() != memory.at(ARG_WEI)->getPrecision()) {diff --git a/src/plugins/intel_cpu/src/nodes/executors/convolution_implementations.cpp b/src/plugins/intel_cpu/src/nodes/executors/convolution_implementations.cppindex a203ee5af85..25efad6982f 100644--- a/src/plugins/intel_cpu/src/nodes/executors/convolution_implementations.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/convolution_implementations.cpp@@ -66,8 +66,8 @@ struct RequiresFallbackDefault {template <typename PostOpType>[[maybe_unused]] static inline bool hasPostOp(const ConvConfig& config) {const auto& postOps = config.attrs.postOps;- return any_of(postOps.begin(), postOps.end(), [](const std::shared_ptr<PostOp>& postOp) {- return std::dynamic_pointer_cast<PostOpType>(postOp);+ return any_of(postOps.begin(), postOps.end(), [](const auto& postOp) {+ return postOp.type() == typeid(PostOpType);});}diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cppindex 7d9e56edca6..19425940001 100644--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp@@ -826,7 +826,7 @@ std::shared_ptr<DnnlConvolutionPrimitive> DnnlConvolutionPrimitive::create(auto getPaddings = [&attrs](const VectorDims& dataShape, const VectorDims& weightsShape) {const bool fusedDWconv = std::any_of(attrs.postOps.begin(), attrs.postOps.end(), [](const auto& p) {- return std::dynamic_pointer_cast<DepthwiseConvolutionPostOp>(p) != nullptr;+ return p.type() == typeid(DepthwiseConvolutionPostOp);});if (attrs.autoPadding == AutoPaddingType::None || // auto padding disableddiff --git a/src/plugins/intel_cpu/src/post_ops.cpp b/src/plugins/intel_cpu/src/post_ops.cppindex 9ed6854963d..a64be141325 100644--- a/src/plugins/intel_cpu/src/post_ops.cpp+++ b/src/plugins/intel_cpu/src/post_ops.cpp@@ -177,21 +177,21 @@ PostOps getPostOps(const std::vector<NodePtr>& fused, ov::element::Type_t sumDatPostOps ops;auto makeActivationPostOp = [](const std::shared_ptr<node::Eltwise>& eltwise) {- return std::make_shared<ActivationPostOp>(convertToActivationPostOpt(eltwise->getAlgorithm()),- eltwise->getAlpha(),- eltwise->getBeta(),- eltwise->getGamma());+ return std::make_any<ActivationPostOp>(convertToActivationPostOpt(eltwise->getAlgorithm()),+ eltwise->getAlpha(),+ eltwise->getBeta(),+ eltwise->getGamma());};auto makeScaleShiftPostOp = [](const std::shared_ptr<node::Eltwise>& eltwise) {- return std::make_shared<ScaleShiftPostOp>(convertToScaleShiftOpt(eltwise->getAlgorithm()),- eltwise->getScales(),- eltwise->getShifts());+ return std::make_any<ScaleShiftPostOp>(convertToScaleShiftOpt(eltwise->getAlgorithm()),+ eltwise->getScales(),+ eltwise->getShifts());};auto makeSumPostOp = [&](const std::shared_ptr<node::Eltwise>& eltwise) {OPENVINO_ASSERT(sumDataType != ov::element::dynamic, "Sum data type is not defined ", eltwise->getName());- return std::make_shared<SumPostOp>(1.0, 0, sumDataType);+ return std::make_any<SumPostOp>(1.0, 0, sumDataType);};for (const auto& node : fused) {@@ -212,16 +212,16 @@ PostOps getPostOps(const std::vector<NodePtr>& fused, ov::element::Type_t sumDat}if (const auto fq = std::dynamic_pointer_cast<node::FakeQuantize>(node)) {- ops.push_back(std::make_shared<FakeQuantizePostOp>(convertToFqPostOp(fq->getAlgorithm()),- fq->getCropLow(),- fq->getCropHigh(),- fq->getInputScale(),- fq->getInputShift(),- fq->getOutputScale(),- fq->getOutputShift(),- fq->getLevels(),- fq->isInputLowBroadcast(),- fq->isOutputHighBroadcast()));+ ops.push_back(std::make_any<FakeQuantizePostOp>(convertToFqPostOp(fq->getAlgorithm()),+ fq->getCropLow(),+ fq->getCropHigh(),+ fq->getInputScale(),+ fq->getInputShift(),+ fq->getOutputScale(),+ fq->getOutputShift(),+ fq->getLevels(),+ fq->isInputLowBroadcast(),+ fq->isOutputHighBroadcast()));}if (const auto conv = std::dynamic_pointer_cast<node::Convolution>(node)) {@@ -236,7 +236,7 @@ PostOps getPostOps(const std::vector<NodePtr>& fused, ov::element::Type_t sumDatdwWeightsDims[dwWeightsDims.size() - 2]};const auto& strides = conv->getStride();- ops.push_back(std::make_shared<DepthwiseConvolutionPostOp>(ih, iw, kernel, strides));+ ops.push_back(std::make_any<DepthwiseConvolutionPostOp>(ih, iw, kernel, strides));}}diff --git a/src/plugins/intel_cpu/src/post_ops.hpp b/src/plugins/intel_cpu/src/post_ops.hppindex 7e4af5d0e85..6ced928efc8 100644--- a/src/plugins/intel_cpu/src/post_ops.hpp+++ b/src/plugins/intel_cpu/src/post_ops.hpp@@ -4,6 +4,7 @@#pragma once+#include <any>#include <cstddef>#include <cstdint>#include <functional>@@ -19,12 +20,7 @@namespace ov::intel_cpu {-struct PostOp;-using PostOps = std::vector<std::shared_ptr<PostOp>>;--struct PostOp {- virtual ~PostOp() = default;-};+using PostOps = std::vector<std::any>;struct ActivationPostOp;using eltwiseExecutorCreatingStrategy = std::function<ExecutorPtr(const ActivationPostOp&,@@ -33,7 +29,7 @@ using eltwiseExecutorCreatingStrategy = std::function<ExecutorPtr(const Activatistd::vector<MemoryDescPtr>,const PostOps&)>;-struct ActivationPostOp : PostOp {+struct ActivationPostOp {enum class Type : uint8_t {relu,tanh,@@ -90,7 +86,7 @@ private:const float m_gamma;};-struct ScaleShiftPostOp : PostOp {+struct ScaleShiftPostOp {enum class Type : uint8_t {add,subtract,@@ -123,7 +119,7 @@ private:const std::vector<float> m_shifts;};-struct FakeQuantizePostOp : PostOp {+struct FakeQuantizePostOp {enum class Type : uint8_t { binarization, quantization_only, quantization_dequantization };FakeQuantizePostOp(const Type type,@@ -201,7 +197,7 @@ private:bool m_isOutputHighBroadcasted;};-struct DepthwiseConvolutionPostOp : PostOp {+struct DepthwiseConvolutionPostOp {DepthwiseConvolutionPostOp(size_t ih, size_t iw, std::vector<size_t> kernel, std::vector<size_t> strides): m_ih(ih),m_iw(iw),@@ -231,7 +227,7 @@ private:std::vector<size_t> m_strides;};-struct SumPostOp : PostOp {+struct SumPostOp {SumPostOp(float scale, int32_t zero_point, ov::element::Type_t dataType): m_scale(scale),m_zero_point(zero_point),@@ -261,8 +257,6 @@ enum class EltwiseKind : uint8_t {// @todo Binary?};-using PostOps = std::vector<std::shared_ptr<PostOp>>;-EltwiseKind getEltwiseKind(const Algorithm alg);ScaleShiftPostOp::Type convertToScaleShiftOpt(const Algorithm alg);