Skip to content

Commit 1ec95bf

Browse files
[executorch][native-vk] Skip dead tensor values when building the Vulkan graph (pytorch#23470)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23436 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/130/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/130/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/129/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/130/orig Differential Revision: [D121704327](https://our.internmc.facebook.com/intern/diff/D121704327/) @diff-train-skip-merge cc @SS-JIA @manuelcandales @digantdesai @cbilgin Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 9bfc99b commit 1ec95bf

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ bool exceeds_texture_limit(
277277
return sizes[3] > limit || sizes[2] > limit || sizes[0] * sizes[1] > limit;
278278
}
279279

280+
// Load-time fusions leave the values they replace behind with no producer and
281+
// no consumers.
282+
bool is_active(const Value& value) {
283+
return valid(value.producer_id) || !value.consumer_ids.empty() ||
284+
value.role == ValueRole::Buffer;
285+
}
286+
280287
bool requires_bias_tensor(std::string_view key) {
281288
return key == "aten.linear.default" || key == "aten.convolution.default";
282289
}
@@ -1191,6 +1198,9 @@ class VulkanEngineExecutable final : public EngineExecutable {
11911198
return graph_->add_value_list(std::move(refs));
11921199
}
11931200
case ValueKind::Tensor: {
1201+
if (!is_active(v)) {
1202+
return none_ref_;
1203+
}
11941204
const TensorMeta& m = v.tensor_meta();
11951205
const std::vector<int64_t>& sizes = m.sizes;
11961206
const auto rope = v.attrs.find(vulkan::kRopeTableAttr);
@@ -1634,9 +1644,7 @@ class VulkanEngineExecutable final : public EngineExecutable {
16341644
if (binding != nullptr && (binding->has_data || binding->mutated)) {
16351645
continue;
16361646
}
1637-
const bool active = valid(value.producer_id) ||
1638-
!value.consumer_ids.empty() || value.role == ValueRole::Buffer;
1639-
if (!active) {
1647+
if (!is_active(value)) {
16401648
continue;
16411649
}
16421650
requests.push_back(AllocationRequest{

‎backends/native/test/runtime/vulkan/test_vulkan_engine.cpp‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,19 @@ std::vector<uint8_t> make_add_program(
130130
fbs::ScalarType dtype,
131131
const std::vector<int64_t>& shape = {4},
132132
const std::vector<int64_t>& lower_bounds = {},
133-
bool include_mutation_output = false) {
133+
bool include_mutation_output = false,
134+
const std::vector<int64_t>& unused_shape = {}) {
134135
flatbuffers::FlatBufferBuilder builder;
135136
const auto meta = create_tensor_meta(builder, dtype, shape, lower_bounds);
136-
const std::vector<flatbuffers::Offset<fbs::TensorValue>> tensor_values = {
137+
std::vector<flatbuffers::Offset<fbs::TensorValue>> tensor_values = {
137138
fbs::CreateTensorValueDirect(builder, "left", meta),
138139
fbs::CreateTensorValueDirect(builder, "right", meta),
139140
fbs::CreateTensorValueDirect(builder, "sum", meta),
140141
};
142+
if (!unused_shape.empty()) {
143+
tensor_values.push_back(fbs::CreateTensorValueDirect(
144+
builder, "unused", create_tensor_meta(builder, dtype, unused_shape)));
145+
}
141146

142147
const std::vector<flatbuffers::Offset<fbs::Output>> left_outputs = {
143148
fbs::CreateOutputDirect(builder, "left")};
@@ -828,6 +833,23 @@ TEST(VulkanEngineTest, RejectsUserInputMutation) {
828833
}
829834
}
830835

836+
TEST(VulkanEngineTest, DoesNotMaterializeUnusedTensors) {
837+
CompiledProgram compiled = compile_program(make_add_program(
838+
fbs::ScalarType::FLOAT, {4}, {}, false, {1 << 18, 1 << 19}));
839+
const std::vector<float> left = {1.0f, 2.0f, 3.0f, 4.0f};
840+
const std::vector<float> right = {10.0f, 20.0f, 30.0f, 40.0f};
841+
compiled.executable->set_input(
842+
0, left.data(), left.size(), ScalarType::Float);
843+
compiled.executable->set_input(
844+
1, right.data(), right.size(), ScalarType::Float);
845+
compiled.executable->execute();
846+
847+
std::vector<float> output(4);
848+
compiled.executable->get_output(
849+
0, output.data(), output.size(), ScalarType::Float);
850+
EXPECT_EQ(output, (std::vector<float>{11.0f, 22.0f, 33.0f, 44.0f}));
851+
}
852+
831853
TEST(VulkanEngineTest, ResizesInputsWithinSerializedBounds) {
832854
CompiledProgram compiled =
833855
compile_program(make_add_program(fbs::ScalarType::FLOAT, {4}, {1}));

0 commit comments

Comments
 (0)