Skip to content

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

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

#pragma once

#include "snippets_helpers.hpp"


/* The file contains graphs with different MHA-patterns:
 * Skeleton on MHA-pattern is:
 *              \     /
 *              MatMul0
 *                 |
 * Eltwise/Select/Reshape/FakeQuantize
 *                 |
 *              Softmax
 *                 |
 * Eltwise/Select/Reshape/FakeQuantize
 *                  \      /
 *                   MatMul1
 */

namespace ov {
namespace test {
namespace snippets {

/* Graph:
 *       Transpose1[0,2,3,1]  Constant
 *                     \       /
 * Transpose0[0,2,1,3] Multiply [with_mul = true]
 *              \     /
 *              MatMul0                AddParam
 *                 \                    |
 *                  Add <--- Broadcast [with_broadcast = true]
 *                   |
 *                Reshape0 [with_reshape = true]
 *                   |
 *                Softmax
 *                   |
 *                Reshape1 [with_reshape = true]  Transpose2[0,2,1,3]
 *                    \                              /
 *                     MatMul1
 *                   Transpose3[0,2,1,3]
 */
class MHAFunction : public SnippetsFunctionBase {
public:
    explicit MHAFunction(const std::vector<PartialShape>& inputShapes,
                         const std::vector<ov::element::Type>& precisions,
                         bool with_mul = true,
                         bool with_reshape = true,
                         bool with_broadcast = false,
                         bool with_shape_of = false)
        : SnippetsFunctionBase(inputShapes),
          with_mul(with_mul),
          with_reshape(with_reshape),
          with_broadcast(with_broadcast),
          with_shape_of(with_shape_of),
          precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
    }

protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;

    const bool with_mul = true;
    const bool with_reshape = true;
    const bool with_broadcast = false;
    const bool with_shape_of = false;
    const std::vector<ov::element::Type> precisions;
};

class MHAConstBFunction : public SnippetsFunctionBase {
public:
    explicit MHAConstBFunction(const std::vector<PartialShape>& inputShapes,
                               const std::vector<ov::element::Type>& precisions,
                               bool with_mul = true,
                               bool with_reshape = true,
                               bool const_b_matmul0 = false,
                               bool const_b_matmul1 = false)
        : SnippetsFunctionBase(inputShapes),
          with_mul(with_mul),
          with_reshape(with_reshape),
          const_b_matmul0(const_b_matmul0),
          const_b_matmul1(const_b_matmul1),
          precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
        OPENVINO_ASSERT(!const_b_matmul0 || input_shapes[1].is_static(),
                        "MHAConstBFunction expects static shape for MatMul0 constant B");
        OPENVINO_ASSERT(!const_b_matmul1 || input_shapes[3].is_static(),
                        "MHAConstBFunction expects static shape for MatMul1 constant B");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;
    void validate_function(const std::shared_ptr<Model>& f) const override;

    const bool with_mul = true;
    const bool with_reshape = true;
    const bool const_b_matmul0 = false;
    const bool const_b_matmul1 = false;
    const std::vector<ov::element::Type> precisions;
};

// Builds a model with two structurally-identical MHA branches:
//   - Branch 1: Q1/K1/Add1 runtime parameters, V1 CONSTANT
//     → post-softmax Subgraph has are_wei_constant=true → RepackMatMulWeights pre-packs V1
//     → constant_repacked_mask bit is SET
//   - Branch 2: Q2/K2/Add2 runtime parameters, V2 RUNTIME PARAMETER
//     → post-softmax Subgraph has are_wei_constant=false → no pre-packing
//     → constant_repacked_mask bit is CLEAR
class MHATwoConstBFunction : public SnippetsFunctionBase {
public:
    explicit MHATwoConstBFunction(const std::vector<PartialShape>& inputShapes,
                                  const std::vector<ov::element::Type>& precisions,
                                  bool with_reshape = true)
        : SnippetsFunctionBase(inputShapes),
          precisions(precisions),
          with_reshape(with_reshape) {
        OPENVINO_ASSERT(input_shapes.size() == 7, "Got invalid number of input shapes (expected 7)");
        OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
        OPENVINO_ASSERT(input_shapes[6].is_static(),
                        "V shape must be static (used to create V1 constant and V2 parameter)");
    }

protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    // 7 runtime parameters: Q1, K1, Add1, Q2, K2, Add2, V2.
    // V1 is embedded as a Constant (not a runtime Parameter).
    void validate_function(const std::shared_ptr<Model>& f) const override {
        OPENVINO_ASSERT(f != nullptr, "The test requires Model to be defined");
        validate_params_shape({input_shapes[0], input_shapes[1], input_shapes[2],
                               input_shapes[3], input_shapes[4], input_shapes[5],
                               input_shapes[6]},
                              f->get_parameters());
    }

    const std::vector<ov::element::Type> precisions;
    const bool with_reshape = true;
};

class MHA2DFunction : public SnippetsFunctionBase {
public:
    explicit MHA2DFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
        : SnippetsFunctionBase(inputShapes), precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
        for (const auto& shape : input_shapes) {
            OPENVINO_ASSERT(shape.rank().is_static() && shape.rank().get_length() == 2, "All input shapes must be 2D");
        }
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;

    const std::vector<ov::element::Type> precisions;
};

/* Graph:
 *       Transpose1[0,2,3,1]  Parameter
 *                     \       /
 * Transpose0[0,2,1,3] Multiply
 *              \     /
 *              MatMul0
 *                 \   /
 *                  Add
 *                Softmax  Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                   Transpose3[0,2,1,3]
 */
class MHAWithDynamicMulFunction : public SnippetsFunctionBase {
public:
    explicit MHAWithDynamicMulFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
        : SnippetsFunctionBase(inputShapes), precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 5, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 5, "Got invalid number of input precisions");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;

    const std::vector<ov::element::Type> precisions;
};

/* Graph:
 *       Transpose1[0,2,1,3]  Constant
 *                     \       /
 * Transpose0[0,2,1,3] Multiply
 *              \     /
 *              MatMul0 [transposed_b = true]
 *                 \   /
 *                  Add
 *                Reshape0
 *                Softmax
 *                Reshape1  Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                   Transpose3[0,2,1,3]
 */
class MHAMatMul0TransposeFunction : public SnippetsFunctionBase {
public:
    explicit MHAMatMul0TransposeFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions,
                                         bool with_reshape = true)
            : SnippetsFunctionBase(inputShapes), with_reshape(with_reshape), precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;

    const bool with_reshape = true;
    const std::vector<ov::element::Type> precisions;
};

/* Graph:
 *             Transpose1[0,2,3,1]  Constant
 *                           \       /
 *       Transpose0[0,2,1,3] Multiply
 *     \               \     /
 * Broadcast  Scalar   MatMul0
 *       \      |      /
 *           Select
 *          Reshape0
 *          Softmax
 *          Reshape1  Transpose2[0,2,1,3]
 *              \      /
 *               MatMul1
 *             Transpose3[0,2,1,3]
 */
class MHASelectFunction : public SnippetsFunctionBase {
public:
    explicit MHASelectFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
        : SnippetsFunctionBase(inputShapes), precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 6, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 6, "Got invalid number of input precisions");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;

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

// Only for tokenization tests since boolean type->u8
// Without Transposes
class MHASelectSplitMFunction : public SnippetsFunctionBase {
public:
    explicit MHASelectSplitMFunction(const std::vector<PartialShape>& inputShapes, const std::vector<Shape>& reshapes)
            : SnippetsFunctionBase(inputShapes), reshapes(reshapes) {
        OPENVINO_ASSERT(input_shapes.size() == 5, "Got invalid number of input shapes");
        OPENVINO_ASSERT(reshapes.size() == 6, "Got invalid number of input precisions");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;

    std::vector<Shape> reshapes;
};

/* Graph:
 *             Constant
 *        \      /
 *        Multiply
 *    \     /
 *    MatMul0
 *       |
 *    Softmax
 *        \      /
 *         MatMul1
 *           |
 *       Transpose3[0,2,1,3]
 */
class MHAWOTransposeOnInputsFunction : public SnippetsFunctionBase {
public:
    explicit MHAWOTransposeOnInputsFunction(const std::vector<PartialShape>& inputShapes) : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 3, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};

/* Graph:
 *    \     /
 *    MatMul0
 *       |
 *    Softmax
 *        \      /
 *         MatMul1
 *           |
 */
class MHAWOTransposeFunction : public SnippetsFunctionBase {
public:
    explicit MHAWOTransposeFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
        : SnippetsFunctionBase(inputShapes), precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 3, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 3, "Got invalid number of input precisions");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;

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

class MHAWOTransposeSplitMFunction : public MHAWOTransposeFunction {
public:
    explicit MHAWOTransposeSplitMFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions,
                                          const std::vector<Shape>& reshapes)
            : MHAWOTransposeFunction(inputShapes, precisions), reshapes(reshapes) {
        OPENVINO_ASSERT(reshapes.size() == 4, "Got invalid number of Reshape shapes");
    }
protected:
    std::shared_ptr<ov::Model> initReference() const override;

    std::vector<ov::Shape> reshapes;
};

/* Graph:
 * Transpose0[0,2,1,3] Transpose1[0,2,3,1]
 *              \     /
 *              MatMul0
 *            FakeQuantize i8
 *                 \   /
 *                  Add
 *                Softmax   Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                   FakeQuantize i8
 *                  Transpose3[0,2,1,3]
 */
class MHAFQAfterMatMulFunction : public SnippetsFunctionBase {
public:
    explicit MHAFQAfterMatMulFunction(const std::vector<PartialShape>& inputShapes)
            : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};

/* Graph:
 *   FakeQuantize i8      FakeQuantize i8
 * Transpose0[0,2,1,3] Transpose1[0,2,3,1]
 *              \     /
 *              MatMul0
 *            FakeQuantize i8
 *                 \   /
 *                  Add
 *                Softmax    FakeQuantize i8
 *            FakeQuantize u8 Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                  FakeQuantize i8
 *                  Transpose3[0,2,1,3]
 */
class MHAINT8MatMulFunction : public SnippetsFunctionBase {
public:
    explicit MHAINT8MatMulFunction(const std::vector<PartialShape>& inputShapes)
            : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};

/* Graph:
 *   FakeQuantize i8      Transpose1[0,2,3,1]
 * Transpose0[0,2,1,3] FakeQuantize i8
 *              \     /
 *              MatMul0
 *                 \   /
 *                  Add
 *                Softmax   Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                  FakeQuantize i8
 *                  Transpose3[0,2,1,3]
 */
class MHAQuantMatMul0Function : public SnippetsFunctionBase {
public:
    explicit MHAQuantMatMul0Function(const std::vector<PartialShape>& inputShapes)
            : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};


/* Graph:
 *                                          Constant
 *   FakeQuantize u8      FakeQuantize u8   Convert
 * Transpose0[0,2,1,3] Transpose1[0,2,3,1]  Multiply
 *                  \             \         /
 *                   \              Multiply
 *                    \         FakeQuantize f32
 *                     \         /
 *                       MatMul0
 *                     FakeQuantize f32   FakeQuantize u8
 *                                   \     /
 *                                     Add
 *                                   Softmax  Transpose2[0,2,1,3]
 *                                       \      /
 *                                        MatMul1
 *                                     FakeQuantize u8
 *                                     Transpose3[0,2,1,3]
 * Note: Check a lot of different FQ (the both quantized and floating) - buffers with different size and precision
 */
class MHAFQFunction : public SnippetsFunctionBase {
public:
    explicit MHAFQFunction(const std::vector<PartialShape>& inputShapes)
            : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};

// Only for tokenization! The graph is after LPT: contains TypeRelaxed ops
/* Graph:
 *   FakeQuantize i8      FakeQuantize i8
 * Transpose0[0,2,1,3] Transpose1[0,2,3,1]
 *              \     /
 *              MatMul0
 *            FakeQuantize i8
 *                 \   /
 *                  Add
 *                  Mul (DeQuantize)
 *                Reshape0
 *                Softmax
 *                Reshape1   FakeQuantize i8
 *            FakeQuantize u8 Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                  FakeQuantize i8
 *                  Transpose3[0,2,1,3]
 */
class MHAINT8MatMulTypeRelaxedFunction : public SnippetsFunctionBase {
public:
    explicit MHAINT8MatMulTypeRelaxedFunction(const std::vector<PartialShape>& inputShapes)
            : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;
};

/* Graph:
 * Transpose0[0,2,1,3] Transpose1[0,2,3,1]
 *              \     /
 *              MatMul0
 *                 \
 *                Multiply
 *                  Add
 *                Softmax   Transpose2[0,2,1,3]
 *                    \      /
 *                     MatMul1
 *                   Transpose3[0,2,1,3]
 */
class MHAMulAddFunction : public SnippetsFunctionBase {
public:
    explicit MHAMulAddFunction(const std::vector<PartialShape>& inputShapes) : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 3, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
};

/* Graph:
 *       Transpose/Parameter
 *    \     /
 *    MatMul0 [transposed_b = true/false]
 *       |
 *    Softmax
 *        \      /
 *         MatMul1
 *           |
 */
class MHATransposedInputFunction : public SnippetsFunctionBase {
public:
    explicit MHATransposedInputFunction(const std::vector<PartialShape>& inputShapes,
                                        bool transposed_b = false,
                                        std::vector<int64_t> order = {},
                                        bool transpose_b_native_support = false)
        : SnippetsFunctionBase(inputShapes),
          m_transposed_b(transposed_b),
          m_order(order),
          m_transpose_b_native_support(transpose_b_native_support) {
        OPENVINO_ASSERT(input_shapes.size() == 3, "Got invalid number of input shapes");
    }

protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;

    bool m_transposed_b = false;
    std::vector<int64_t> m_order = {};
    bool m_transpose_b_native_support = false;
};

/* Graph:
 *           input0   input1
 *              \     /
 *              MatMul0   input2
 *                 |        | 
 *              Reshape  Reshape (optional)
 *                 |    /
 *              Eltwise1  input3
 *                 |     /
 *              Eltwise2
 *                 |
 *              Reshape
 *                 |
 *              Softmax
 *                 |       input4
 *                  \      /
 *                   MatMul1
 */
class MHAWithExtractedReshapeFunction : public SnippetsFunctionBase {
public:
    explicit MHAWithExtractedReshapeFunction(const std::vector<PartialShape>& inputShapes, const bool add_2nd_reshape)
        : SnippetsFunctionBase(inputShapes), add_2nd_reshape(add_2nd_reshape) {
        OPENVINO_ASSERT(input_shapes.size() == 5, "Got invalid number of input shapes");
    }
protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;
private:
    bool add_2nd_reshape = false;
};

/* Graph:
 *           input0   input1
 *              \     /
 *              MatMul0  input2
 *                 |     /
 *              Eltwise1
 *                 |
 *              Reshape input3
 *                 |    /
 *              Eltwise2
 *                 |
 *              Reshape
 *                 |
 *              Softmax
 *                 |       input4
 *                  \      /
 *                   MatMul1
 */
class MHARankUpgradeToReductionFunction : public SnippetsFunctionBase {
public:
    explicit MHARankUpgradeToReductionFunction(const std::vector<PartialShape>& inputShapes)
        : SnippetsFunctionBase(inputShapes) {
        OPENVINO_ASSERT(input_shapes.size() == 5, "Got invalid number of input shapes");
        bool are_static_shapes = std::all_of(input_shapes.cbegin(), input_shapes.cend(), [](const PartialShape& shape) {
            return shape.is_static();
        });
        OPENVINO_ASSERT(are_static_shapes, "Expect static shape, got dynamic shape");
    }

protected:
    std::shared_ptr<ov::Model> initOriginal() const override;
    std::shared_ptr<ov::Model> initReference() const override;
};

/* Graph:
 * input0   input1
 *    \     /  \
 *    MatMul0   \
 *       |       \
 *    Softmax     \
 *       \       /
 *       MatMul1
 * Note: This is a MHA pattern with shared K and V inputs, duplicating one of the python TF tests
 */
class MHASharedKVFunction : public SnippetsFunctionBase {
public:
    explicit MHASharedKVFunction(const std::vector<PartialShape>& inputShapes,
                                 const std::vector<ov::element::Type>& precisions)
        : SnippetsFunctionBase(inputShapes),
          precisions(precisions) {
        OPENVINO_ASSERT(input_shapes.size() == 2, "Got invalid number of input shapes");
        OPENVINO_ASSERT(precisions.size() == 2, "Got invalid number of input precisions");
    }

protected:
    std::shared_ptr<ov::Model> initOriginal() const override;

    const std::vector<ov::element::Type> precisions;
};

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