Skip to content

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

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

#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/test_case.hpp"
#include "common_test_utils/test_control.hpp"
#include "gtest/gtest.h"
#include "onnx_utils.hpp"
#include "openvino/op/abs.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convolution.hpp"
#include "openvino/op/identity.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/relu.hpp"
#include "openvino/op/result.hpp"
#include "openvino/op/slice.hpp"
#include "openvino/op/util/topk_base.hpp"

using namespace ov;
using namespace ov::frontend::onnx::tests;

static std::string s_manifest = onnx_backend_manifest("${MANIFEST}");

using Inputs = std::vector<std::vector<float>>;
using Outputs = std::vector<std::vector<float>>;

template <typename OpType, typename DerivedFromNode>
bool matching_node_found_in_graph(const std::vector<DerivedFromNode>& ops,
                                  const std::string& friendly_name,
                                  const std::unordered_set<std::string>& output_names,
                                  int out_tensor_number = 0) {
    return std::any_of(std::begin(ops), std::end(ops), [&](const DerivedFromNode op) {
        if (const std::shared_ptr<OpType> casted = ov::as_type_ptr<OpType>(op)) {
            const auto& op_friendly_name = casted->get_friendly_name();
            const auto& op_output_names = casted->get_output_tensor(out_tensor_number).get_names();
            if (op_friendly_name == friendly_name && op_output_names == output_names) {
                return true;
            }
        }
        return false;
    });
}

template <typename OpType, typename DerivedFromNode>
std::shared_ptr<OpType> find_by_friendly_name(const std::vector<DerivedFromNode>& ops,
                                              const std::string& friendly_name) {
    const auto it = std::find_if(std::begin(ops), std::end(ops), [&friendly_name](const DerivedFromNode& op) {
        return op->get_friendly_name() == friendly_name && ov::as_type_ptr<OpType>(op) != nullptr;
    });

    if (it != std::end(ops)) {
        return ov::as_type_ptr<OpType>(*it);
    } else {
        return nullptr;
    }
}

OPENVINO_TEST(onnx_tensor_names, simple_model) {
    const auto model = convert_model("tensor_names.onnx");

    const auto ops = model->get_ordered_ops();
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Parameter>(ops, "input", {"input"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v16::Identity>(ops, "identity_node_on_input", {"identity_on_input"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Relu>(ops, "relu", {"relu_t"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Abs>(ops, "abs", {"abs_t"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v16::Identity>(ops, "final_output", {"final_output"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Result>(model->get_results(),
                                                             "final_output/sink_port_0",
                                                             {"final_output"}));
}

OPENVINO_TEST(onnx_tensor_names, node_multiple_outputs) {
    const auto model = convert_model("top_k.onnx");

    const auto ops = model->get_ordered_ops();
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Parameter>(ops, "x", {"x"}));
    EXPECT_TRUE(matching_node_found_in_graph<ov::op::util::TopKBase>(ops, "indices", {"values"}, 0));
    EXPECT_TRUE(matching_node_found_in_graph<ov::op::util::TopKBase>(ops, "indices", {"indices"}, 1));

    const auto results = model->get_results();
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Result>(results, "values/sink_port_0", {"values"}));
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Result>(results, "indices/sink_port_0", {"indices"}));
}

OPENVINO_TEST(onnx_tensor_names, subgraph_with_multiple_nodes) {
    const auto model = convert_model("max_pool_transposed.onnx");

    // after the import the 2 Result objects are connected to 2 distinct nodes (MaxPool & Transpose)
    // the original MaxPool operator in the model doesn't have its name set
    const auto ops = model->get_ordered_ops();

    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "y/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "y");

    const auto result2 = find_by_friendly_name<op::v0::Result>(ops, "z/sink_port_0");
    EXPECT_NE(result2, nullptr);
    EXPECT_EQ(result2->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");
}

OPENVINO_TEST(onnx_tensor_names, simple_multiout_operator) {
    const auto model = convert_model("max_pool_simple.onnx");

    const auto ops = model->get_ordered_ops();

    // in this case both Results are connected directly to the MaxPool node
    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "y/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");

    const auto result2 = find_by_friendly_name<op::v0::Result>(ops, "z/sink_port_0");
    EXPECT_NE(result2, nullptr);
    EXPECT_EQ(result2->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");
}

OPENVINO_TEST(onnx_tensor_names, simple_multiout_named_operator) {
    const auto model = convert_model("max_pool_simple_named.onnx");

    const auto ops = model->get_ordered_ops();

    // in this case both Results are connected directly to the MaxPool node
    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "y/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");

    const auto result2 = find_by_friendly_name<op::v0::Result>(ops, "z/sink_port_0");
    EXPECT_NE(result2, nullptr);
    EXPECT_EQ(result2->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");
}

OPENVINO_TEST(onnx_tensor_names, subgraph_with_multiple_nodes_named) {
    const auto model = convert_model("max_pool_transposed_named.onnx");

    const auto ops = model->get_ordered_ops();

    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "y/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "y");

    const auto result2 = find_by_friendly_name<op::v0::Result>(ops, "z/sink_port_0");
    EXPECT_NE(result2, nullptr);
    EXPECT_EQ(result2->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "z");
}

OPENVINO_TEST(onnx_tensor_names, subgraph_conv_with_bias) {
    const auto model = convert_model("conv_with_strides_padding_bias.onnx");

    const auto ops = model->get_ordered_ops();

    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "D/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "D");
    EXPECT_EQ(result1->input(0).get_source_output().get_names(), std::unordered_set<std::string>({"D"}));

    EXPECT_NE(nullptr, find_by_friendly_name<op::v1::Convolution>(ops, "D/WithoutBiases"));
}

OPENVINO_TEST(onnx_tensor_names, subgraph_gemm_with_bias) {
    const auto model = convert_model("gemm_abc.onnx");

    const auto ops = model->get_ordered_ops();

    const auto result1 = find_by_friendly_name<op::v0::Result>(ops, "y/sink_port_0");
    EXPECT_NE(result1, nullptr);
    EXPECT_EQ(result1->input(0).get_source_output().get_node_shared_ptr()->get_friendly_name(), "y");
    EXPECT_EQ(result1->input(0).get_source_output().get_names(), std::unordered_set<std::string>({"y"}));

    EXPECT_NE(nullptr, find_by_friendly_name<op::v0::MatMul>(ops, "y/WithoutBiases"));
}

OPENVINO_TEST(onnx_tensor_names, matmulnbits_b_name_preserved) {
    // MatMulNBits repacks B into a low-bit Constant; it must keep the ONNX initializer name "b_Q4".
    const auto model = convert_model("com.microsoft/matmulnbits_3x4.onnx");

    const auto ops = model->get_ordered_ops();

    // Repacked weight keeps "b_Q4" as friendly_name and tensor name.
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_Q4", {"b_Q4"}));

    // It is the u4 repacked weight, not the original initializer.
    const auto packed_b = find_by_friendly_name<op::v0::Constant>(ops, "b_Q4");
    ASSERT_NE(packed_b, nullptr);
    EXPECT_EQ(packed_b->get_element_type(), element::u4);
}

OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved) {
    // MatMulNBits repacks the zero-point into a low-bit Constant; it must retain the ONNX initializer name "b_zp".
    const auto model = convert_model("com.microsoft/matmulnbits_3x32_zp.onnx");

    const auto ops = model->get_ordered_ops();

    // Repacked zero-point keeps "b_zp" as friendly_name and tensor name.
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_zp", {"b_zp"}));

    // It is the u4 repacked zero-point (bits=4), not the original uint8 initializer.
    const auto packed_zp = find_by_friendly_name<op::v0::Constant>(ops, "b_zp");
    ASSERT_NE(packed_zp, nullptr);
    EXPECT_EQ(packed_zp->get_element_type(), element::u4);
}

OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved_unaligned) {
    // Same as matmulnbits_zp_name_preserved, but n_blocks_per_col (3) is not a multiple of num_per_byte
    // (2 for bits=4). The unpacked Constant has a distinct suffixed name and no padding Slice.
    const auto model = convert_model("com.microsoft/matmulnbits_3x48_zp_unaligned.onnx");

    const auto ops = model->get_ordered_ops();

    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_zp_aligned", {"b_zp_aligned"}));

    const auto repacked_zp = find_by_friendly_name<op::v0::Constant>(ops, "b_zp_aligned");
    ASSERT_NE(repacked_zp, nullptr);
    EXPECT_EQ(repacked_zp->get_element_type(), element::u4);
    const Shape expected_zp_shape{3, 3, 1};
    EXPECT_EQ(repacked_zp->get_shape(), expected_zp_shape);
    EXPECT_FALSE(std::any_of(ops.begin(), ops.end(), [](const std::shared_ptr<Node>& node) {
        return ov::as_type_ptr<op::v8::Slice>(node) != nullptr;
    }));
}

OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved_sametype) {
    // zero_point has the same element type as A (float), so it takes the "not packed" branch that
    // builds converted_zero_points directly as a Constant of A's type. Must also keep the ONNX
    // initializer name.
    const auto model = convert_model("com.microsoft/matmulnbits_3x32_zp_sametype.onnx");

    const auto ops = model->get_ordered_ops();

    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_zp", {"b_zp"}));

    const auto packed_zp = find_by_friendly_name<op::v0::Constant>(ops, "b_zp");
    ASSERT_NE(packed_zp, nullptr);
    EXPECT_EQ(packed_zp->get_element_type(), element::f32);
}

OPENVINO_TEST(onnx_tensor_names, matmulnbits_shared_b_name_collision_resolved) {
    // One B feeding two MatMulNBits creates two same-named Constants; ResolveNameCollisions (in
    // normalize) keeps "b_Q4" on one and suffixes the other.
    const auto model = convert_model("com.microsoft/matmulnbits_shared_b.onnx");

    const auto ops = model->get_ordered_ops();

    // Collect the two named u4 weights directly: counting and uniqueness can't be expressed by the
    // bool/first-match helpers. This model has no zero-point input, so the only named u4 constants are
    // the two repacked B weights (the default zero-point is unnamed).
    std::vector<std::shared_ptr<op::v0::Constant>> packed_weights;
    for (const auto& op : ops) {
        const auto c = ov::as_type_ptr<op::v0::Constant>(op);
        if (c && c->get_element_type() == element::u4 && !c->get_output_tensor(0).get_names().empty()) {
            packed_weights.push_back(c);
        }
    }
    ASSERT_EQ(packed_weights.size(), 2u);

    // Names are unique after normalization.
    std::unordered_set<std::string> friendly_names;
    std::unordered_set<std::string> tensor_names;
    for (const auto& c : packed_weights) {
        friendly_names.insert(c->get_friendly_name());
        const auto& names = c->get_output_tensor(0).get_names();
        tensor_names.insert(names.begin(), names.end());
    }
    EXPECT_EQ(friendly_names.size(), 2u);
    EXPECT_EQ(tensor_names.size(), 2u);

    // Exactly one keeps "b_Q4" so weight sharing still matches.
    EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_Q4", {"b_Q4"}));
    EXPECT_EQ(friendly_names.count("b_Q4"), 1u);
    EXPECT_EQ(tensor_names.count("b_Q4"), 1u);
}