Skip to content

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

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

#include "openvino/core/memory_util.hpp"

#include <gtest/gtest.h>

#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/data_utils.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/type/element_iterator.hpp"

namespace ov::test {

using MemsizeOverflowParam = std::tuple<element::Type, Shape, std::optional<size_t>>;

class GetMemorySizeOverflowTest : public testing::TestWithParam<MemsizeOverflowParam> {
public:
    static std::string get_test_name(const testing::TestParamInfo<MemsizeOverflowParam>& obj) {
        const auto& [type, shape, exp_size] = obj.param;
        std::ostringstream result;
        result << "type=" << type << "_shape=" << shape
               << "_exp_size=" << (exp_size ? std::to_string(*exp_size) : "overflow");
        return result.str();
    }
};

constexpr auto max_dim = std::numeric_limits<size_t>::max();

INSTANTIATE_TEST_SUITE_P(
    bit_type_precision,
    GetMemorySizeOverflowTest,
    testing::Values(std::make_tuple(element::u1, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u1, Shape{8}, std::optional<size_t>(1)),
                    std::make_tuple(element::u1, Shape{9}, std::optional<size_t>(2)),
                    std::make_tuple(element::u1, Shape{3, 3}, std::optional<size_t>(2)),
                    std::make_tuple(element::u1, Shape{max_dim}, std::optional<size_t>(max_dim / 8 + 1)),
                    std::make_tuple(element::u1, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 8 + 1)),
                    std::make_tuple(element::u1, Shape{2, max_dim}, std::nullopt),
                    std::make_tuple(element::u2, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u2, Shape{4}, std::optional<size_t>(1)),
                    std::make_tuple(element::u2, Shape{5}, std::optional<size_t>(2)),
                    std::make_tuple(element::u2, Shape{3, 3}, std::optional<size_t>(3)),
                    std::make_tuple(element::u2, Shape{max_dim}, std::optional<size_t>(max_dim / 4 + 1)),
                    std::make_tuple(element::u2, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 4 + 1)),
                    std::make_tuple(element::u2, Shape{max_dim - 3}, std::optional<size_t>(max_dim / 4)),
                    std::make_tuple(element::u2, Shape{1, 2, max_dim}, std::nullopt)),
    GetMemorySizeOverflowTest::get_test_name);

INSTANTIATE_TEST_SUITE_P(
    nibble_type_precision,
    GetMemorySizeOverflowTest,
    testing::Values(std::make_tuple(element::u4, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u4, Shape{2}, std::optional<size_t>(1)),
                    std::make_tuple(element::u4, Shape{3}, std::optional<size_t>(2)),
                    std::make_tuple(element::u4, Shape{2, 2}, std::optional<size_t>(2)),
                    std::make_tuple(element::u4, Shape{3, 3, 2}, std::optional<size_t>(9)),
                    std::make_tuple(element::u4, Shape{max_dim}, std::optional<size_t>(max_dim / 2 + 1)),
                    std::make_tuple(element::u4, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 2)),
                    std::make_tuple(element::u4, Shape{2, max_dim}, std::nullopt)),
    GetMemorySizeOverflowTest::get_test_name);

INSTANTIATE_TEST_SUITE_P(
    cross_byte_bit_type_precision,
    GetMemorySizeOverflowTest,
    testing::Values(std::make_tuple(element::u3, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u3, Shape{3}, std::optional<size_t>(2)),
                    std::make_tuple(element::u3, Shape{8}, std::optional<size_t>(3)),
                    std::make_tuple(element::u3, Shape{17}, std::optional<size_t>(7)),
                    // elements_count * bitwidth overflows, but the resulting byte size still fits
                    std::make_tuple(element::u3, Shape{max_dim}, std::optional<size_t>(max_dim / 8 * 3 + 3)),
                    std::make_tuple(element::u3, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 8 * 3 + 3)),
                    std::make_tuple(element::u3, Shape{2, max_dim}, std::nullopt),
                    std::make_tuple(element::u6, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u6, Shape{3}, std::optional<size_t>(3)),
                    std::make_tuple(element::u6, Shape{8}, std::optional<size_t>(6)),
                    std::make_tuple(element::u6, Shape{17}, std::optional<size_t>(13)),
                    std::make_tuple(element::u6, Shape{max_dim}, std::optional<size_t>(max_dim / 8 * 6 + 6)),
                    std::make_tuple(element::u6, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 8 * 6 + 5)),
                    std::make_tuple(element::u6, Shape{1, 2, max_dim}, std::nullopt)),
    GetMemorySizeOverflowTest::get_test_name);

INSTANTIATE_TEST_SUITE_P(
    byte_type_precision,
    GetMemorySizeOverflowTest,
    testing::Values(std::make_tuple(element::u8, Shape{}, std::optional<size_t>(1)),
                    std::make_tuple(element::u8, Shape{8}, std::optional<size_t>(8)),
                    std::make_tuple(element::u8, Shape{9}, std::optional<size_t>(9)),
                    std::make_tuple(element::u8, Shape{3, 3}, std::optional<size_t>(9)),
                    std::make_tuple(element::u8, Shape{max_dim, 3}, std::nullopt),
                    std::make_tuple(element::f64, Shape{}, std::optional<size_t>(8)),
                    std::make_tuple(element::f64, Shape{12}, std::optional<size_t>(96)),
                    std::make_tuple(element::f64, Shape{max_dim / 8}, std::optional<size_t>(max_dim - 7)),
                    std::make_tuple(element::f64, Shape{2, max_dim / 16}, std::optional<size_t>(max_dim - 15)),
                    std::make_tuple(element::f64, Shape{2, max_dim / 16 - 1}, std::optional<size_t>(max_dim - 31)),
                    std::make_tuple(element::f64, Shape{1, max_dim / 8 - 1, 3}, std::nullopt),
                    std::make_tuple(element::string, Shape{}, std::optional<size_t>(sizeof(std::string))),
                    std::make_tuple(element::string, Shape{12}, std::optional<size_t>(12 * sizeof(std::string))),
                    std::make_tuple(element::string, Shape{max_dim}, std::nullopt)),
    GetMemorySizeOverflowTest::get_test_name);

TEST_P(GetMemorySizeOverflowTest, calculate_from_shape) {
    const auto& [type, shape, exp_size] = GetParam();

    EXPECT_EQ(ov::util::get_memory_size_safe(type, shape), exp_size);
}

TEST(GetMemorySizeOverflowTest, zero_number_of_elements) {
    EXPECT_EQ(ov::util::get_memory_size_safe(element::f4e2m1, 0), std::optional<size_t>(0));
    EXPECT_EQ(ov::util::get_memory_size_safe(element::i16, 0), std::optional<size_t>(0));
    EXPECT_EQ(ov::util::get_memory_size_safe(element::string, 0), std::optional<size_t>(0));
}

// element type, memory size, expected max count of elements
using MaxElementsForMemorySizeParam = std::tuple<element::Type, size_t, size_t>;

class GetMaxElementsForMemorySizeTest : public testing::TestWithParam<MaxElementsForMemorySizeParam> {};

TEST_P(GetMaxElementsForMemorySizeTest, calculate_max_elements) {
    const auto& [type, memory_size, exp_elements] = GetParam();
    EXPECT_EQ(ov::util::get_elements_capacity(type, memory_size), exp_elements);
}

INSTANTIATE_TEST_SUITE_P(bit_type_precision,
                         GetMaxElementsForMemorySizeTest,
                         testing::Values(std::make_tuple(element::u1, 0, 0),
                                         std::make_tuple(element::u1, 1, 8),
                                         std::make_tuple(element::u1, 2, 16),
                                         std::make_tuple(element::u1, 3, 24),
                                         std::make_tuple(element::u1, 4, 32),
                                         std::make_tuple(element::u2, 0, 0),
                                         std::make_tuple(element::u2, 1, 4),
                                         std::make_tuple(element::u2, 2, 8),
                                         std::make_tuple(element::u2, 3, 12),
                                         std::make_tuple(element::u2, 4, 16),
                                         // memory size * 8 overflows, but the elements count is still representable
                                         std::make_tuple(element::u2, max_dim / 4, max_dim - 3)));

INSTANTIATE_TEST_SUITE_P(nibble_type_precision,
                         GetMaxElementsForMemorySizeTest,
                         testing::Values(std::make_tuple(element::i4, 0, 0),
                                         std::make_tuple(element::u4, 0, 0),
                                         std::make_tuple(element::u4, 1, 2),
                                         std::make_tuple(element::u4, 2, 4),
                                         std::make_tuple(element::i4, 3, 6),
                                         std::make_tuple(element::i4, 4, 8),
                                         // memory size * 8 overflows, but the elements count is still representable
                                         std::make_tuple(element::u4, max_dim / 2, max_dim - 1)));

INSTANTIATE_TEST_SUITE_P(cross_byte_bit_type_precision,
                         GetMaxElementsForMemorySizeTest,
                         testing::Values(std::make_tuple(element::u3, 0, 0),
                                         std::make_tuple(element::u3, 1, 2),
                                         std::make_tuple(element::u3, 2, 5),
                                         std::make_tuple(element::u3, 3, 8),
                                         std::make_tuple(element::u3, 5, 13),
                                         std::make_tuple(element::u3, 6, 16),
                                         std::make_tuple(element::u3, 11, 29),
                                         std::make_tuple(element::u3, 12, 32),
                                         std::make_tuple(element::u6, 0, 0),
                                         std::make_tuple(element::u6, 1, 1),
                                         std::make_tuple(element::u6, 2, 2),
                                         std::make_tuple(element::u6, 3, 4),
                                         std::make_tuple(element::u6, 4, 5),
                                         std::make_tuple(element::u6, 5, 6),
                                         std::make_tuple(element::u6, 6, 8),
                                         std::make_tuple(element::u6, 11, 14),
                                         std::make_tuple(element::u6, 12, 16),
                                         // memory size * 8 overflows, but the elements count is still representable
                                         std::make_tuple(element::u3, 3 * (max_dim / 8), 8 * (max_dim / 8)),
                                         std::make_tuple(element::u6, 6 * (max_dim / 16), 8 * (max_dim / 16))));

INSTANTIATE_TEST_SUITE_P(byte_type_precision,
                         GetMaxElementsForMemorySizeTest,
                         testing::Values(std::make_tuple(element::u8, 0, 0),
                                         std::make_tuple(element::u8, 1, 1),
                                         std::make_tuple(element::u8, 2, 2),
                                         std::make_tuple(element::u8, 3, 3),
                                         std::make_tuple(element::u8, 4, 4),
                                         std::make_tuple(element::i16, 0, 0),
                                         std::make_tuple(element::i16, 1, 0),
                                         std::make_tuple(element::i16, 2, 1),
                                         std::make_tuple(element::i16, 3, 1),
                                         std::make_tuple(element::i16, 4, 2),
                                         std::make_tuple(element::f32, 0, 0),
                                         std::make_tuple(element::f32, 1, 0),
                                         std::make_tuple(element::f32, 4, 1),
                                         std::make_tuple(element::f32, 7, 1),
                                         std::make_tuple(element::f32, 8, 2),
                                         std::make_tuple(element::f32, 15, 3),
                                         std::make_tuple(element::f32, 16, 4),
                                         std::make_tuple(element::f64, 0, 0),
                                         std::make_tuple(element::f64, 1, 0),
                                         std::make_tuple(element::f64, 7, 0),
                                         std::make_tuple(element::f64, 8, 1),
                                         std::make_tuple(element::f64, 15, 1),
                                         std::make_tuple(element::f64, 16, 2),
                                         std::make_tuple(element::f64, 24, 3),
                                         std::make_tuple(element::f64, 32, 4),
                                         std::make_tuple(element::f64, 33, 4)));

INSTANTIATE_TEST_SUITE_P(other_types,
                         GetMaxElementsForMemorySizeTest,
                         testing::Values(std::make_tuple(element::dynamic, 0, 0),
                                         std::make_tuple(element::dynamic, 1, 0),
                                         std::make_tuple(element::dynamic, 100, 0),
                                         std::make_tuple(element::string, 0, 0),
                                         std::make_tuple(element::string, sizeof(std::string), 1),
                                         std::make_tuple(element::string, 2 * sizeof(std::string), 2),
                                         std::make_tuple(element::string, 3 * sizeof(std::string), 3),
                                         std::make_tuple(element::string, 4 * sizeof(std::string), 4)));

using AlignTestParam = std::tuple<uintptr_t, size_t, size_t>;

class AlignTest : public ::testing::TestWithParam<AlignTestParam> {};

INSTANTIATE_TEST_SUITE_P(AlignTestSuite,
                         AlignTest,
                         testing::Values(AlignTestParam{0, 0, 0},
                                         AlignTestParam{20, 0, 0},
                                         AlignTestParam{0, 64, 0},
                                         AlignTestParam{20, 64, 44},
                                         AlignTestParam{64, 64, 0},
                                         AlignTestParam{65, 64, 63},
                                         AlignTestParam{128, 64, 0},
                                         AlignTestParam{130, 64, 62},
                                         AlignTestParam{0, 100, 0},
                                         AlignTestParam{63, 100, 37},
                                         AlignTestParam{130, 100, 70}),
                         testing::PrintToStringParamName());

TEST_P(AlignTest, align_padding_size) {
    const auto& [pos, aligment, expected] = GetParam();

    const auto pad = ov::util::align_padding_size(aligment, pos);

    EXPECT_EQ(pad, expected);
}

TEST(memory_util, can_compute_byte_strides) {
    EXPECT_TRUE(ov::util::can_compute_byte_strides(element::f32, Shape{}));
    EXPECT_TRUE(ov::util::can_compute_byte_strides(element::f32, Shape{2, 3, 5}));
    // sub-byte types require the last dimension's packed row to end on a byte boundary
    EXPECT_TRUE(ov::util::can_compute_byte_strides(element::u4, Shape{2, 3, 8}));
    EXPECT_FALSE(ov::util::can_compute_byte_strides(element::u4, Shape{2, 3, 3}));
    EXPECT_TRUE(ov::util::can_compute_byte_strides(element::u1, Shape{8}));
    EXPECT_FALSE(ov::util::can_compute_byte_strides(element::u1, Shape{10}));

    // a bitwidth that does not evenly divide a byte can never be byte-aligned
    EXPECT_FALSE(ov::util::can_compute_byte_strides(element::u3, Shape{8}));

    // sub-byte scalar (rank 0) has no last dimension to align
    EXPECT_FALSE(ov::util::can_compute_byte_strides(element::u4, Shape{}));
}

TEST(memory_util, get_dense_byte_strides) {
    EXPECT_EQ(ov::util::get_dense_byte_strides(element::f32, Shape{2, 3, 5}), Strides({60, 20, 4}));

    // last dimension is one packed row (3 bytes); outer strides count whole rows
    EXPECT_EQ(ov::util::get_dense_byte_strides(element::u4, Shape{2, 3, 8}), Strides({12, 4, 1}));

    // zero-sized last dimension collapses all strides to 0
    EXPECT_EQ(ov::util::get_dense_byte_strides(element::u4, Shape{2, 3, 0}), Strides({0, 0, 0}));
}

TEST(memory_util, get_byte_memory_shape) {
    // byte-granular types are returned unchanged
    EXPECT_EQ(ov::util::get_byte_memory_shape(element::f32, Shape{2, 3, 5}), Shape({2, 3, 5}));

    // sub-byte types: last dimension becomes its packed byte count
    EXPECT_EQ(ov::util::get_byte_memory_shape(element::u4, Shape{2, 3, 8}), Shape({2, 3, 4}));
    EXPECT_EQ(ov::util::get_byte_memory_shape(element::u1, Shape{2, 16}), Shape({2, 2}));

    // higher rank: only the last dimension changes, every outer dimension is copied unchanged
    EXPECT_EQ(ov::util::get_byte_memory_shape(element::u4, Shape{1, 2, 4095, 1, 512}), Shape({1, 2, 4095, 1, 256}));

    // sub-byte scalar (rank 0) has no last dimension to convert
    EXPECT_EQ(ov::util::get_byte_memory_shape(element::u4, Shape{}), Shape{});
}
}  // namespace ov::test