Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

7.0 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "shared_test_classes/base/snippets_test_utils.hpp"
#include "snippets_helpers.hpp"

namespace ov {
namespace test {
namespace snippets {

typedef std::tuple<std::vector<InputShape>,         // Input shapes
                   std::vector<ov::element::Type>,  // Input Element types
                   ov::element::Type,               // Inference precision
                   bool,                            // With Multiply
                   size_t,                          // Expected num nodes
                   size_t,                          // Expected num subgraphs
                   std::string,                     // Target Device
                   ov::AnyMap                       // Config
                   >
    MHAParams;

typedef std::tuple<std::vector<InputShape>,         // Input shapes
                   std::vector<ov::element::Type>,  // Input Element types
                   ov::element::Type,               // Inference precision
                   bool,                            // With Multiply
                   bool,                            // MatMul0 with const B
                   bool,                            // MatMul1 with const B
                   size_t,                          // Expected num nodes
                   size_t,                          // Expected num subgraphs
                   std::string,                     // Target Device
                   ov::AnyMap                       // Config
                   >
    MHAConstBParams;

typedef std::tuple<std::vector<InputShape>,         // Input shapes
                   std::vector<ov::element::Type>,  // Input Element types
                   ov::element::Type,               // Inference precision
                   size_t,                          // Expected num nodes
                   size_t,                          // Expected num subgraphs
                   std::string,                     // Target Device
                   ov::AnyMap                       // Config
                   >
MHAWithDynamicMulParams;

class MHABase :  virtual public SnippetsTestsCommon {
protected:
    void SetUp() override;
    void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
    void init_thresholds() override;
    virtual std::shared_ptr<SnippetsFunctionBase> get_subgraph() const = 0;
    virtual void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) = 0;

    std::vector<ov::element::Type> m_input_types;
};

class MHA : public testing::WithParamInterface<ov::test::snippets::MHAParams>,
            virtual public MHABase {
public:
    static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAParams>& obj);

protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
    void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;

    void init_thresholds() override;
};

class MHAConstB : public testing::WithParamInterface<ov::test::snippets::MHAConstBParams>,
                  virtual public MHABase {
public:
    static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAConstBParams>& obj);

protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
    void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;
    void init_thresholds() override;

    bool m_with_mul = false;
    bool m_const_b_matmul0 = false;
    bool m_const_b_matmul1 = false;
};

// Regression test for cache-collision fix for static shapes.
// Builds a model with two structurally-identical MHA branches that differ ONLY in how V is
// provided: branch 1 uses a Constant V (→ pre-packed at compile time, constant_repacked_mask=1)
// while branch 2 uses a runtime Parameter V (→ no pre-packing, constant_repacked_mask=0).
class MHATwoConstB : public MHAConstB {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHA2D : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWithBroadcast : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWithShapeOfBroadcast : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWithShapeOfReshape : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHASelect : public MHA {
protected:
    void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWOTransposeOnInputs : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWOTranspose : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAMulAdd : public MHA {
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHATransposedB : public MHA {
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAINT8MatMul : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
    void init_thresholds() override;
};

class MHAQuantMatMul0 : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAFQAfterMatMul : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAFQ : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
    void init_thresholds() override;
};

class MHAWithExtractedReshape : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHARankUpgradeToReductionReshape : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

class MHAWithDynamicMul : public testing::WithParamInterface<ov::test::snippets::MHAWithDynamicMulParams>,
                          virtual public MHABase {
public:
    static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAWithDynamicMulParams>& obj);

protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
    void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;
};

class MHAWithThreadCount : public MHA {
public:
    constexpr static size_t default_thread_count = 0;
    static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAParams>& obj);

protected:
    void compile_model() override;
};

class MHASharedKV : public MHA {
protected:
    std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};

}  // namespace snippets
}  // namespace test
}  // namespace ov