Skip to content

Commit 25e167f

Browse files
[executorch][native-vk] Propagate dynamic shapes and keep mutation outputs on device (pytorch#23462)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23427 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/122/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/122/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/121/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/122/orig Differential Revision: [D119601872](https://our.internmc.facebook.com/intern/diff/D119601872/) @diff-train-skip-merge Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent db0d023 commit 25e167f

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace {
4848
using vkcompute::ComputeGraph;
4949
using vkcompute::GraphConfig;
5050
using VkRef = vkcompute::ValueRef;
51+
constexpr VkRef kNoOutputStaging = -1;
5152
namespace vkapi = vkcompute::vkapi;
5253
namespace utils = vkcompute::utils;
5354

@@ -528,6 +529,7 @@ class VulkanEngineExecutable final : public EngineExecutable {
528529
}
529530

530531
void execute() override {
532+
graph_->propagate_resize();
531533
graph_->execute();
532534
}
533535

@@ -604,6 +606,30 @@ class VulkanEngineExecutable final : public EngineExecutable {
604606
mark_node_width(n);
605607
}
606608

609+
for (const NodeId nid : nodes_) {
610+
const Node& n = g().node(nid);
611+
if (!n.is_call() || registry_key(n.target) != "aten.index.Tensor") {
612+
continue;
613+
}
614+
const std::vector<ValueId> inputs = n.input_value_ids();
615+
if (inputs.empty() || !valid(inputs.front()) ||
616+
!g().value(inputs.front()).is_tensor() ||
617+
g().value(inputs.front()).tensor_meta().sizes.size() <= 1) {
618+
continue;
619+
}
620+
mark_node_width(n);
621+
for (const ValueId input_id : inputs) {
622+
if (valid(input_id) && g().value(input_id).is_tensor()) {
623+
storage_at(input_id) = utils::kBuffer;
624+
}
625+
}
626+
for_each_output_value(n, [&](ValueId output_id) {
627+
if (g().value(output_id).is_tensor()) {
628+
storage_at(output_id) = utils::kBuffer;
629+
}
630+
});
631+
}
632+
607633
bool changed;
608634
do {
609635
changed = false;
@@ -1331,15 +1357,20 @@ class VulkanEngineExecutable final : public EngineExecutable {
13311357
}
13321358
}
13331359

1334-
// 4. Register the region's outputs, creating device->host staging.
1335-
output_staging_.resize(outputs_.size());
1336-
std::transform(
1337-
outputs_.begin(),
1338-
outputs_.end(),
1339-
output_staging_.begin(),
1340-
[this](ValueId out) {
1341-
return graph_->set_output_tensor(vref_at(out));
1342-
});
1360+
// 4. Register graph outputs. User outputs get host staging; mutation
1361+
// outputs remain device-resident.
1362+
output_staging_.reserve(outputs_.size());
1363+
for (size_t i = 0; i < outputs_.size(); ++i) {
1364+
const VkRef output = vref_at(outputs_[i]);
1365+
const bool is_mutation = !method_.output_specs.empty() &&
1366+
method_.output_specs.at(i).kind != OutputKind::UserOutput;
1367+
if (is_mutation) {
1368+
graph_->set_output_tensor(output, false);
1369+
output_staging_.push_back(kNoOutputStaging);
1370+
} else {
1371+
output_staging_.push_back(graph_->set_output_tensor(output));
1372+
}
1373+
}
13431374

13441375
// 5. Finalize and upload constants. TensorRef owns the remaining shared
13451376
// references and releases each source after its final prepack consumer.

0 commit comments

Comments
 (0)