Skip to content

Commit 81beb7e

Browse files
[executorch][native-vk] Preserve Vulkan layouts across reductions and views (pytorch#23459)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23423 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/119/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/119/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/118/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/119/orig Differential Revision: [D119601871](https://our.internmc.facebook.com/intern/diff/D119601871/) @diff-train-skip-merge cc @SS-JIA @manuelcandales @digantdesai @cbilgin Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 9123df8 commit 81beb7e

7 files changed

Lines changed: 518 additions & 5 deletions

File tree

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <executorch/backends/native/runtime/graph/utils/GraphUtils.h>
3636
#include <executorch/backends/native/runtime/vulkan/VulkanConstantMaterializationTracker.h>
3737
#include <executorch/backends/native/runtime/vulkan/passes/InsertPrepack.h>
38+
#include <executorch/backends/native/runtime/vulkan/passes/MaterializeViewCopies.h>
3839
#include <executorch/runtime/core/freeable_buffer.h>
3940

4041
#include <executorch/backends/vulkan/runtime/api/api.h>
@@ -150,6 +151,79 @@ bool preserves_packed_layout(std::string_view key) {
150151
return std::ranges::find(kOps, key) != kOps.end();
151152
}
152153

154+
struct Reduce2dLayoutConstraint {
155+
ValueId input_id;
156+
std::array<bool, 3> allowed_packed_dims;
157+
};
158+
159+
std::optional<Reduce2dLayoutConstraint> reduce2d_layout_constraint(
160+
const Graph& graph,
161+
const Node& node) {
162+
constexpr std::array<std::string_view, 4> kReduceOps{
163+
"aten.amax.default",
164+
"aten.amin.default",
165+
"aten.mean.dim",
166+
"aten.sum.dim_IntList",
167+
};
168+
if (std::ranges::find(kReduceOps, registry_key(node.target)) ==
169+
kReduceOps.end()) {
170+
return std::nullopt;
171+
}
172+
173+
const auto input = std::ranges::find_if(node.inputs, [](const auto& arg) {
174+
return arg.name == "self" && arg.arg.kind() == ArgKind::Tensor;
175+
});
176+
const auto dims = std::ranges::find_if(node.inputs, [](const auto& arg) {
177+
return arg.name == "dim" && arg.arg.kind() == ArgKind::IntList;
178+
});
179+
if (input == node.inputs.end() || dims == node.inputs.end() ||
180+
dims->arg.as_int_list().values.size() != 2) {
181+
return std::nullopt;
182+
}
183+
if (!dims->arg.as_int_list().ids.empty()) {
184+
throw std::runtime_error(
185+
"vulkan: symbolic dimensions are not supported for 2D reductions");
186+
}
187+
188+
const ValueId input_id = input->arg.as_tensor().id;
189+
const int64_t ndim =
190+
static_cast<int64_t>(graph.value(input_id).tensor_meta().ndim());
191+
std::array<bool, 3> allowed{true, true, true};
192+
for (int64_t dim : dims->arg.as_int_list().values) {
193+
dim = dim < 0 ? dim + ndim : dim;
194+
if (dim < 0 || dim >= ndim) {
195+
throw std::runtime_error("vulkan: reduction dimension is out of range");
196+
}
197+
const int64_t whcn_dim = ndim - 1 - dim;
198+
if (whcn_dim < static_cast<int64_t>(allowed.size())) {
199+
allowed[static_cast<size_t>(whcn_dim)] = false;
200+
}
201+
}
202+
return Reduce2dLayoutConstraint{input_id, allowed};
203+
}
204+
205+
int32_t packed_dim(utils::GPUMemoryLayout layout) {
206+
if (layout == utils::kWidthPacked) {
207+
return 0;
208+
}
209+
if (layout == utils::kHeightPacked) {
210+
return 1;
211+
}
212+
if (layout == utils::kChannelsPacked) {
213+
return 2;
214+
}
215+
throw std::runtime_error("vulkan: unsupported packed layout");
216+
}
217+
218+
utils::GPUMemoryLayout layout_for_packed_dim(int32_t dim) {
219+
constexpr std::array<utils::GPUMemoryLayout, 3> kLayouts{
220+
utils::kWidthPacked,
221+
utils::kHeightPacked,
222+
utils::kChannelsPacked,
223+
};
224+
return kLayouts.at(static_cast<size_t>(dim));
225+
}
226+
153227
bool exceeds_texture_limit(
154228
const TensorMeta& meta,
155229
utils::GPUMemoryLayout layout,
@@ -162,6 +236,8 @@ bool exceeds_texture_limit(
162236
meta.sizes.begin(), meta.sizes.end(), sizes.end() - meta.sizes.size());
163237
if (layout == utils::kWidthPacked) {
164238
sizes[3] = (sizes[3] + 3) / 4;
239+
} else if (layout == utils::kHeightPacked) {
240+
sizes[2] = (sizes[2] + 3) / 4;
165241
} else if (layout == utils::kChannelsPacked) {
166242
sizes[1] = (sizes[1] + 3) / 4;
167243
}
@@ -543,6 +619,30 @@ class VulkanEngineExecutable final : public EngineExecutable {
543619
}
544620
} while (changed);
545621

622+
for (const NodeId node_id : nodes_) {
623+
const Node& node = g().node(node_id);
624+
if (!node.is_call()) {
625+
continue;
626+
}
627+
const auto constraint = reduce2d_layout_constraint(g(), node);
628+
if (!constraint) {
629+
continue;
630+
}
631+
utils::GPUMemoryLayout required = layout_at(constraint->input_id);
632+
if (!constraint->allowed_packed_dims.at(
633+
static_cast<size_t>(packed_dim(required)))) {
634+
for (int32_t dim = 2; dim >= 0; --dim) {
635+
if (constraint->allowed_packed_dims.at(static_cast<size_t>(dim))) {
636+
required = layout_for_packed_dim(dim);
637+
break;
638+
}
639+
}
640+
}
641+
layout_at(constraint->input_id) = required;
642+
for_each_output_value(
643+
node, [&](ValueId output_id) { layout_at(output_id) = required; });
644+
}
645+
546646
const uint32_t texture_limit =
547647
graph_->context()->adapter_ptr()->max_texture3d_dim();
548648
for (ValueId id = 0; id < static_cast<ValueId>(g().values.size()); ++id) {
@@ -575,6 +675,25 @@ class VulkanEngineExecutable final : public EngineExecutable {
575675
} while (changed);
576676
}
577677

678+
void materialize_packed_views() {
679+
std::vector<ValueId> value_ids;
680+
for (const NodeId node_id : nodes_) {
681+
const Node& node = g().node(node_id);
682+
if (!node.is_call() || registry_key(node.target) != "aten.view.default") {
683+
continue;
684+
}
685+
for_each_output_value(node, [&](ValueId output_id) {
686+
const ValueId source_id = g().value(output_id).alias_id;
687+
if (valid(source_id) && layout_at(output_id) != layout_at(source_id) &&
688+
storage_at(output_id) != utils::kBuffer) {
689+
value_ids.push_back(output_id);
690+
}
691+
});
692+
}
693+
vulkan::materialize_view_copies(method_.graph, value_ids);
694+
validate_graph(method_.graph);
695+
}
696+
578697
// Resolve a bound value's package bytes and verify them against the
579698
// serialized metadata before upload.
580699
executorch::runtime::FreeableBuffer constant_buffer(
@@ -1152,6 +1271,7 @@ class VulkanEngineExecutable final : public EngineExecutable {
11521271
void build(const Package& package) {
11531272
check_mutation_outputs();
11541273
assign_layouts();
1274+
materialize_packed_views();
11551275
detach_in_place_outputs();
11561276
index_output_kinds();
11571277
index_bindings();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/passes/MaterializeViewCopies.h>
8+
9+
#include <algorithm>
10+
11+
#include <executorch/backends/native/runtime/graph/Argument.h>
12+
#include <executorch/backends/native/runtime/graph/Ids.h>
13+
#include <executorch/backends/native/runtime/graph/Node.h>
14+
15+
namespace ptn::vulkan {
16+
namespace {
17+
18+
ValueId alias_root(const Graph& graph, ValueId value_id) {
19+
for (size_t depth = 0; valid(value_id) && depth <= graph.values.size();
20+
++depth) {
21+
const ValueId source_id = graph.value(value_id).alias_id;
22+
if (!valid(source_id)) {
23+
return value_id;
24+
}
25+
value_id = source_id;
26+
}
27+
return kInvalid;
28+
}
29+
30+
bool references_alias_group(
31+
const Graph& graph,
32+
const Argument& argument,
33+
ValueId root_id) {
34+
if (argument.kind() == ArgKind::Tensor) {
35+
return alias_root(graph, argument.as_tensor().id) == root_id;
36+
}
37+
if (argument.kind() == ArgKind::TensorList) {
38+
return std::ranges::any_of(argument.as_tensor_list().ids, [&](ValueId id) {
39+
return alias_root(graph, id) == root_id;
40+
});
41+
}
42+
if (argument.kind() == ArgKind::OptionalTensorList) {
43+
return std::ranges::any_of(
44+
argument.as_optional_tensor_list().ids,
45+
[&](ValueId id) { return alias_root(graph, id) == root_id; });
46+
}
47+
return false;
48+
}
49+
50+
bool has_mutated_alias_use(const Graph& graph, ValueId value_id) {
51+
const ValueId root_id = alias_root(graph, value_id);
52+
return std::ranges::any_of(graph.schedule, [&](NodeId node_id) {
53+
return std::ranges::any_of(
54+
graph.node(node_id).inputs, [&](const NamedArgument& input) {
55+
return input.mutated &&
56+
references_alias_group(graph, input.arg, root_id);
57+
});
58+
});
59+
}
60+
61+
} // namespace
62+
63+
// cppcheck-suppress unusedFunction
64+
size_t materialize_view_copies(
65+
Graph& graph,
66+
const std::vector<ValueId>& value_ids) {
67+
size_t count = 0;
68+
for (const NodeId node_id : graph.schedule) {
69+
Node& node = graph.node(node_id);
70+
if (!node.is_call() || node.target != "torch.ops.aten.view.default" ||
71+
node.outputs.size() != 1 ||
72+
node.outputs[0].kind != OutputValueKind::Tensor ||
73+
!valid(node.outputs[0].value_id)) {
74+
continue;
75+
}
76+
const ValueId output_id = node.outputs[0].value_id;
77+
if (std::ranges::find(value_ids, output_id) == value_ids.end() ||
78+
has_mutated_alias_use(graph, output_id)) {
79+
continue;
80+
}
81+
node.target = "torch.ops.aten.view_copy.default";
82+
graph.value(output_id).alias_id = kInvalid;
83+
++count;
84+
}
85+
return count;
86+
}
87+
88+
} // namespace ptn::vulkan
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 <vector>
11+
12+
#include <executorch/backends/native/runtime/graph/Graph.h>
13+
14+
namespace ptn::vulkan {
15+
16+
size_t materialize_view_copies(
17+
Graph& graph,
18+
const std::vector<ValueId>& value_ids);
19+
20+
} // namespace ptn::vulkan

‎backends/native/runtime/vulkan/targets.bzl‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ def define_common_targets():
1616

1717
runtime.cxx_library(
1818
name = "vulkan_passes",
19-
srcs = ["passes/InsertPrepack.cpp"],
20-
exported_headers = ["passes/InsertPrepack.h"],
19+
srcs = [
20+
"passes/InsertPrepack.cpp",
21+
"passes/MaterializeViewCopies.cpp",
22+
],
23+
exported_headers = [
24+
"passes/InsertPrepack.h",
25+
"passes/MaterializeViewCopies.h",
26+
],
2127
exported_deps = [
2228
"//executorch/backends/native/runtime:method",
2329
"//executorch/backends/native/runtime/graph:graph",

‎backends/native/test/runtime/vulkan/targets.bzl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def define_common_targets():
3939

4040
runtime.cxx_test(
4141
name = "vulkan_passes_test",
42-
srcs = ["test_insert_prepack.cpp"],
42+
srcs = [
43+
"test_insert_prepack.cpp",
44+
"test_materialize_view_copies.cpp",
45+
],
4346
deps = [
4447
"//executorch/backends/native/runtime/vulkan:vulkan_passes",
4548
"//executorch/backends/native/runtime/graph:argument",

0 commit comments

Comments
 (0)