Skip to content

Commit 70112ea

Browse files
[executorch][native-vk] Track Vulkan constant materialization (pytorch#23454)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23418 by @digantdesai ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/114/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/114/head Merge bot PR base: https://github.com/pytorch/executorch/tree/main Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/114/orig Differential Revision: [D119601863](https://our.internmc.facebook.com/intern/diff/D119601863/) @diff-train-skip-merge cc @SS-JIA @manuelcandales @digantdesai @cbilgin Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 5f4b03a commit 70112ea

7 files changed

Lines changed: 198 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Any targets that should be shared between fbcode and xplat must be defined in
2+
# targets.bzl. This file can contain fbcode-only targets.
3+
4+
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
5+
load(":targets.bzl", "define_common_targets")
6+
7+
oncall("executorch")
8+
9+
non_fbcode_target(_kind = define_common_targets)
10+
11+
fbcode_target(_kind = define_common_targets)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 <functional>
10+
#include <stdexcept>
11+
12+
namespace ptn {
13+
14+
size_t VulkanConstantMaterializationTracker::KeyHash::operator()(
15+
const Key& key) const {
16+
const size_t package_hash = std::hash<uint64_t>()(key.package_id);
17+
const size_t owner_hash = std::hash<std::string>()(key.owner);
18+
return package_hash ^
19+
(owner_hash + 0x9e3779b9 + (package_hash << 6) + (package_hash >> 2));
20+
}
21+
22+
bool VulkanConstantMaterializationTracker::record(
23+
uint64_t package_id,
24+
const std::string& owner,
25+
size_t nbytes) {
26+
const auto inserted = entries_.emplace(Key{package_id, owner}, Entry{});
27+
Entry& entry = inserted.first->second;
28+
if (!inserted.second) {
29+
if (entry.nbytes != nbytes) {
30+
throw std::runtime_error(
31+
"vulkan constant tracker: byte count changed for package " +
32+
std::to_string(package_id) + " constant '" + owner + "': recorded " +
33+
std::to_string(entry.nbytes) + ", got " + std::to_string(nbytes));
34+
}
35+
materialized_constant_bytes_ += nbytes;
36+
return true;
37+
}
38+
39+
entry.nbytes = nbytes;
40+
unique_constant_bytes_ += nbytes;
41+
materialized_constant_bytes_ += nbytes;
42+
return false;
43+
}
44+
45+
} // namespace ptn
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
#pragma once
8+
9+
#include <cstddef>
10+
#include <cstdint>
11+
#include <string>
12+
#include <unordered_map>
13+
14+
namespace ptn {
15+
16+
// Accounts for constants materialized by Vulkan executables in one engine
17+
// context.
18+
//
19+
// A package identity plus its canonical owner key remains stable when raw
20+
// weight storage is acquired and released repeatedly.
21+
//
22+
// The tracker does not own or deduplicate Vulkan resources. Each record() call
23+
// represents one executable materializing a constant from its source bytes.
24+
//
25+
// Not thread-safe: the owning engine must not compile methods concurrently.
26+
class VulkanConstantMaterializationTracker {
27+
private:
28+
struct Key {
29+
uint64_t package_id = 0;
30+
std::string owner;
31+
32+
bool operator==(const Key&) const = default;
33+
};
34+
35+
struct KeyHash {
36+
size_t operator()(const Key& key) const;
37+
};
38+
39+
struct Entry {
40+
size_t nbytes = 0;
41+
};
42+
43+
std::unordered_map<Key, Entry, KeyHash> entries_;
44+
size_t unique_constant_bytes_ = 0;
45+
size_t materialized_constant_bytes_ = 0;
46+
47+
public:
48+
// Source bytes across distinct package constants.
49+
size_t unique_constant_bytes() const {
50+
return unique_constant_bytes_;
51+
}
52+
53+
// Source bytes handed to Vulkan materialization, counting repetitions.
54+
size_t materialized_constant_bytes() const {
55+
return materialized_constant_bytes_;
56+
}
57+
58+
size_t num_constants() const {
59+
return entries_.size();
60+
}
61+
62+
// True when an earlier executable materialized the same package constant.
63+
bool record(uint64_t package_id, const std::string& owner, size_t nbytes);
64+
};
65+
66+
} // namespace ptn
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
def define_common_targets():
4+
runtime.cxx_library(
5+
name = "constant_materialization_tracker",
6+
srcs = ["VulkanConstantMaterializationTracker.cpp"],
7+
exported_headers = [
8+
"VulkanConstantMaterializationTracker.h",
9+
],
10+
visibility = ["//executorch/backends/native/..."],
11+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
2+
load(":targets.bzl", "define_common_targets")
3+
4+
oncall("executorch")
5+
6+
non_fbcode_target(_kind = define_common_targets)
7+
8+
fbcode_target(_kind = define_common_targets)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
def define_common_targets():
4+
runtime.cxx_test(
5+
name = "vulkan_constant_materialization_tracker_test",
6+
srcs = ["test_vulkan_constant_materialization_tracker.cpp"],
7+
deps = [
8+
"//executorch/backends/native/runtime/vulkan:constant_materialization_tracker",
9+
],
10+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)