Skip to content

Commit d1b868e

Browse files
[executorch][native-vk] Use aligned package constants in place (pytorch#23472)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23438 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/132/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/132/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/131/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/132/orig Differential Revision: [D121704329](https://our.internmc.facebook.com/intern/diff/D121704329/) @diff-train-skip-merge Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent ab46560 commit d1b868e

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

‎backends/native/runtime/vulkan/VulkanEngineExecutable.cpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <any>
1111
#include <array>
1212
#include <cmath>
13+
#include <cstdint>
1314
#include <cstring>
1415
#include <deque>
1516
#include <iterator>
@@ -61,6 +62,21 @@ void release_owned_bytes(void* context, void*, size_t) {
6162
delete static_cast<std::shared_ptr<OwnedBytes>*>(context);
6263
}
6364

65+
// Prepack reads constants through typed pointers, so the package's bytes are
66+
// used in place only when aligned to the element size.
67+
std::optional<OwnedBytes> load_constant(
68+
const Package& package,
69+
const ConstantInfo& info) {
70+
std::optional<OwnedBytes> view = package.view_constant(info.owner);
71+
if (view &&
72+
reinterpret_cast<uintptr_t>(view->span().data()) %
73+
element_size(info.dtype) ==
74+
0) {
75+
return view;
76+
}
77+
return package.acquire_constant(info.owner);
78+
}
79+
6480
// Native ScalarType -> ET-VK vkapi::ScalarType. The ids are pinned to the same
6581
// ExecuTorch base, but map explicitly so an unsupported dtype fails by name
6682
// instead of being reinterpreted at another width.
@@ -915,8 +931,7 @@ class VulkanEngineExecutable final : public EngineExecutable {
915931
}
916932
std::shared_ptr<OwnedBytes>& storage = source_constants_[info->owner];
917933
if (!storage) {
918-
std::optional<OwnedBytes> acquired =
919-
package.acquire_constant(info->owner);
934+
std::optional<OwnedBytes> acquired = load_constant(package, *info);
920935
if (!acquired) {
921936
throw std::runtime_error(
922937
"vulkan: package could not load source constant for key '" +

0 commit comments

Comments
 (0)