Skip to content

Commit b3b82cd

Browse files
[executorch][native-vk] Add the Vulkan engine (pytorch#23455)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23419 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/115/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/115/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/114/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/115/orig Differential Revision: [D119601866](https://our.internmc.facebook.com/intern/diff/D119601866/) @diff-train-skip-merge cc @SS-JIA @manuelcandales @digantdesai @cbilgin Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 70112ea commit b3b82cd

8 files changed

Lines changed: 1419 additions & 5 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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-
41
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
52
load(":targets.bzl", "define_common_targets")
63

74
oncall("executorch")
85

6+
# Any targets that should be shared between fbcode and xplat must be defined in
7+
# targets.bzl. This file can contain cell-only targets.
8+
99
non_fbcode_target(_kind = define_common_targets)
1010

1111
fbcode_target(_kind = define_common_targets)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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/VulkanEngine.h>
8+
9+
#include <stdexcept>
10+
#include <string>
11+
#include <utility>
12+
13+
#include <executorch/backends/native/runtime/vulkan/VulkanConstantMaterializationTracker.h>
14+
#include <executorch/backends/native/runtime/vulkan/VulkanEngineExecutable.h>
15+
#include <executorch/backends/vulkan/runtime/graph/GraphConfig.h>
16+
#include <executorch/backends/vulkan/runtime/vk_api/Runtime.h>
17+
18+
namespace ptn {
19+
20+
using vkcompute::GraphConfig;
21+
namespace vkapi = vkcompute::vkapi;
22+
23+
struct VulkanEngineHost::Impl {
24+
vkapi::Adapter* adapter = nullptr;
25+
std::string name = "vulkan";
26+
std::string device_name;
27+
};
28+
29+
struct VulkanEngineContext::Impl {
30+
GraphConfig config;
31+
VulkanConstantMaterializationTracker materializations;
32+
};
33+
34+
VulkanEngineHost::VulkanEngineHost(std::unique_ptr<Impl> impl)
35+
: impl_(std::move(impl)) {}
36+
37+
VulkanEngineHost::~VulkanEngineHost() = default;
38+
39+
std::unique_ptr<VulkanEngineHost> VulkanEngineHost::create() {
40+
std::unique_ptr<Impl> impl = std::make_unique<Impl>();
41+
try {
42+
// Brings up the VkInstance and the default adapter on first call. Note this
43+
// deliberately avoids api::available() / api::context(), which would
44+
// construct a whole global api::Context -- command pool, descriptor pool
45+
// and fences -- that nothing uses, since every ComputeGraph builds its own.
46+
impl->adapter = vkapi::runtime()->get_adapter_p();
47+
} catch (const std::exception& e) {
48+
throw std::runtime_error(
49+
std::string("vulkan: no Vulkan device available (is a driver or "
50+
"SwiftShader present?): ") +
51+
e.what());
52+
}
53+
if (impl->adapter == nullptr) {
54+
throw std::runtime_error("vulkan: no Vulkan device available");
55+
}
56+
impl->device_name = impl->adapter->device_name();
57+
return std::unique_ptr<VulkanEngineHost>(
58+
new VulkanEngineHost(std::move(impl)));
59+
}
60+
61+
const std::string& VulkanEngineHost::name() const {
62+
return impl_->name;
63+
}
64+
65+
const std::string& VulkanEngineHost::device_name() const {
66+
return impl_->device_name;
67+
}
68+
69+
std::unique_ptr<EngineContext> VulkanEngineHost::create_context(
70+
std::shared_ptr<const Program> program,
71+
std::shared_ptr<const Package> package) {
72+
return create_vulkan_context(std::move(program), std::move(package));
73+
}
74+
75+
std::unique_ptr<VulkanEngineContext> VulkanEngineHost::create_vulkan_context(
76+
std::shared_ptr<const Program> program,
77+
std::shared_ptr<const Package> package) {
78+
std::unique_ptr<VulkanEngineContext::Impl> impl =
79+
std::make_unique<VulkanEngineContext::Impl>();
80+
return std::unique_ptr<VulkanEngineContext>(new VulkanEngineContext(
81+
std::move(program), std::move(package), std::move(impl)));
82+
}
83+
84+
VulkanEngineContext::VulkanEngineContext(
85+
std::shared_ptr<const Program> program,
86+
std::shared_ptr<const Package> package,
87+
std::unique_ptr<Impl> impl)
88+
: EngineContext(std::move(program), std::move(package)),
89+
impl_(std::move(impl)) {}
90+
91+
VulkanEngineContext::~VulkanEngineContext() = default;
92+
93+
size_t VulkanEngineContext::unique_constant_bytes() const {
94+
return impl_->materializations.unique_constant_bytes();
95+
}
96+
97+
size_t VulkanEngineContext::materialized_constant_bytes() const {
98+
return impl_->materializations.materialized_constant_bytes();
99+
}
100+
101+
std::unique_ptr<EngineExecutable> VulkanEngineContext::compile_method(
102+
const Method& method) {
103+
return create_vulkan_engine_executable(
104+
method, package(), impl_->materializations, impl_->config);
105+
}
106+
107+
} // namespace ptn
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 <memory>
11+
#include <string>
12+
13+
#include <executorch/backends/native/runtime/engine/Engine.h>
14+
15+
namespace ptn {
16+
17+
class VulkanEngineContext;
18+
19+
// Process-wide Vulkan runtime and adapter selection. The host must outlive the
20+
// contexts and executables it creates.
21+
class VulkanEngineHost final : public EngineHost {
22+
private:
23+
struct Impl;
24+
std::unique_ptr<Impl> impl_;
25+
26+
explicit VulkanEngineHost(std::unique_ptr<Impl> impl);
27+
28+
public:
29+
~VulkanEngineHost() override;
30+
31+
// Bring up (or reuse) the process-wide Vulkan runtime and select its default
32+
// adapter. Throws std::runtime_error when no Vulkan device is available.
33+
static std::unique_ptr<VulkanEngineHost> create();
34+
35+
const std::string& name() const override;
36+
const std::string& device_name() const override;
37+
38+
std::unique_ptr<EngineContext> create_context(
39+
std::shared_ptr<const Program> program,
40+
std::shared_ptr<const Package> package) override;
41+
42+
std::unique_ptr<VulkanEngineContext> create_vulkan_context(
43+
std::shared_ptr<const Program> program,
44+
std::shared_ptr<const Package> package);
45+
};
46+
47+
// Per-program Vulkan state. Each executable owns its ComputeGraph and command
48+
// resources while sharing this context's constant materialization tracker.
49+
class VulkanEngineContext final : public EngineContext {
50+
private:
51+
friend class VulkanEngineHost;
52+
53+
struct Impl;
54+
std::unique_ptr<Impl> impl_;
55+
56+
VulkanEngineContext(
57+
std::shared_ptr<const Program> program,
58+
std::shared_ptr<const Package> package,
59+
std::unique_ptr<Impl> impl);
60+
61+
std::unique_ptr<EngineExecutable> compile_method(
62+
const Method& method) override;
63+
64+
public:
65+
~VulkanEngineContext() override;
66+
67+
// Source-byte accounting across every executable compiled so far.
68+
size_t unique_constant_bytes() const;
69+
size_t materialized_constant_bytes() const;
70+
};
71+
72+
} // namespace ptn

0 commit comments

Comments
 (0)