|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include <executorch/backends/native/runtime/vulkan/VulkanConstantMaterializationTracker.h> |
| 8 | + |
| 9 | +#include <stdexcept> |
| 10 | + |
| 11 | +#include <gtest/gtest.h> |
| 12 | + |
| 13 | +namespace ptn { |
| 14 | +namespace { |
| 15 | + |
| 16 | +// cppcheck-suppress-begin syntaxError |
| 17 | +TEST(VulkanConstantMaterializationTrackerTest, UsesPackageAndOwnerIdentity) { |
| 18 | + VulkanConstantMaterializationTracker tracker; |
| 19 | + |
| 20 | + EXPECT_FALSE(tracker.record(1, "weight", 4)); |
| 21 | + EXPECT_TRUE(tracker.record(1, "weight", 4)); |
| 22 | + EXPECT_FALSE(tracker.record(2, "weight", 4)); |
| 23 | + EXPECT_FALSE(tracker.record(1, "bias", 2)); |
| 24 | + |
| 25 | + EXPECT_EQ(tracker.num_constants(), 3); |
| 26 | + EXPECT_EQ(tracker.unique_constant_bytes(), 10); |
| 27 | + EXPECT_EQ(tracker.materialized_constant_bytes(), 14); |
| 28 | +} |
| 29 | + |
| 30 | +TEST(VulkanConstantMaterializationTrackerTest, RejectsInconsistentMetadata) { |
| 31 | + VulkanConstantMaterializationTracker tracker; |
| 32 | + EXPECT_FALSE(tracker.record(1, "weight", 4)); |
| 33 | + try { |
| 34 | + tracker.record(1, "weight", 8); |
| 35 | + FAIL() << "expected inconsistent metadata to be rejected"; |
| 36 | + } catch (const std::runtime_error& error) { |
| 37 | + EXPECT_STREQ( |
| 38 | + error.what(), |
| 39 | + "vulkan constant tracker: byte count changed for package 1 constant " |
| 40 | + "'weight': " |
| 41 | + "recorded 4, got 8"); |
| 42 | + } |
| 43 | +} |
| 44 | +// cppcheck-suppress-end syntaxError |
| 45 | + |
| 46 | +} // namespace |
| 47 | +} // namespace ptn |
0 commit comments