Skip to content

Commit bbc9690

Browse files
[executorch][native-vk] Materialize dynamic width-packed views (pytorch#23465)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23430 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/125/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/125/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/124/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/125/orig Differential Revision: [D120634800](https://our.internmc.facebook.com/intern/diff/D120634800/) @diff-train-skip-merge Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 866dbd3 commit bbc9690

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,10 @@ class VulkanEngineExecutable final : public EngineExecutable {
757757
}
758758
for_each_output_value(node, [&](ValueId output_id) {
759759
const ValueId source_id = g().value(output_id).alias_id;
760-
if (valid(source_id) && layout_at(output_id) != layout_at(source_id) &&
760+
if (valid(source_id) &&
761+
((!g().value(output_id).tensor_meta().lower_bounds.empty() &&
762+
layout_at(output_id) == utils::kWidthPacked) ||
763+
layout_at(output_id) != layout_at(source_id)) &&
761764
storage_at(output_id) != utils::kBuffer) {
762765
value_ids.push_back(output_id);
763766
}

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,86 @@ std::vector<uint8_t> make_relu_program(const std::vector<int32_t>& dim_order) {
656656
builder, graph, {fbs::CreateOutputSpecDirect(builder, "relu")});
657657
}
658658

659+
std::vector<uint8_t> make_dynamic_view_mm_program() {
660+
flatbuffers::FlatBufferBuilder builder;
661+
const auto input_meta =
662+
create_tensor_meta(builder, fbs::ScalarType::FLOAT, {4, 2}, {1, 2});
663+
const auto weight_meta =
664+
create_tensor_meta(builder, fbs::ScalarType::FLOAT, {2, 1});
665+
const auto output_meta =
666+
create_tensor_meta(builder, fbs::ScalarType::FLOAT, {4, 1}, {1, 1});
667+
const std::vector<flatbuffers::Offset<fbs::TensorValue>> tensor_values = {
668+
fbs::CreateTensorValueDirect(builder, "input", input_meta),
669+
fbs::CreateTensorValueDirect(builder, "matrix", input_meta),
670+
fbs::CreateTensorValueDirect(builder, "weight", weight_meta),
671+
fbs::CreateTensorValueDirect(builder, "product", output_meta),
672+
};
673+
674+
const std::vector<flatbuffers::Offset<fbs::Output>> input_outputs = {
675+
fbs::CreateOutputDirect(builder, "input")};
676+
const std::vector<flatbuffers::Offset<fbs::Output>> weight_outputs = {
677+
fbs::CreateOutputDirect(builder, "weight")};
678+
const std::vector<flatbuffers::Offset<fbs::Output>> view_outputs = {
679+
fbs::CreateOutputDirect(builder, "matrix", "input")};
680+
const std::vector<flatbuffers::Offset<fbs::Output>> product_outputs = {
681+
fbs::CreateOutputDirect(builder, "product")};
682+
const std::vector<flatbuffers::Offset<fbs::NamedArgument>> view_inputs = {
683+
fbs::CreateNamedArgumentDirect(
684+
builder, "self", create_tensor_arg(builder, "input")),
685+
fbs::CreateNamedArgumentDirect(
686+
builder, "size", create_int_list_arg(builder, {-1, 2})),
687+
};
688+
const std::vector<flatbuffers::Offset<fbs::NamedArgument>> mm_inputs = {
689+
fbs::CreateNamedArgumentDirect(
690+
builder, "self", create_tensor_arg(builder, "matrix")),
691+
fbs::CreateNamedArgumentDirect(
692+
builder, "mat2", create_tensor_arg(builder, "weight")),
693+
};
694+
const std::vector<flatbuffers::Offset<fbs::NamedArgument>> output_inputs = {
695+
fbs::CreateNamedArgumentDirect(
696+
builder, "", create_tensor_arg(builder, "product"))};
697+
const std::vector<flatbuffers::Offset<fbs::Node>> nodes = {
698+
fbs::CreateNodeDirect(
699+
builder,
700+
"input",
701+
fbs::OpKind::PLACEHOLDER,
702+
"",
703+
nullptr,
704+
&input_outputs),
705+
fbs::CreateNodeDirect(
706+
builder,
707+
"weight",
708+
fbs::OpKind::PLACEHOLDER,
709+
"",
710+
nullptr,
711+
&weight_outputs),
712+
fbs::CreateNodeDirect(
713+
builder,
714+
"view",
715+
fbs::OpKind::CALL_FUNCTION,
716+
"torch.ops.aten.view.default",
717+
&view_inputs,
718+
&view_outputs),
719+
fbs::CreateNodeDirect(
720+
builder,
721+
"mm",
722+
fbs::OpKind::CALL_FUNCTION,
723+
"torch.ops.aten.mm.default",
724+
&mm_inputs,
725+
&product_outputs),
726+
fbs::CreateNodeDirect(
727+
builder, "output", fbs::OpKind::OUTPUT, "", &output_inputs),
728+
};
729+
const auto graph = fbs::CreateGraph(
730+
builder,
731+
builder.CreateVector(nodes),
732+
create_strings(builder, {"input", "weight"}),
733+
create_strings(builder, {"product"}),
734+
builder.CreateVector(tensor_values));
735+
return finish_program(
736+
builder, graph, {fbs::CreateOutputSpecDirect(builder, "product")});
737+
}
738+
659739
struct CompiledProgram {
660740
std::unique_ptr<VulkanEngineHost> host;
661741
std::shared_ptr<const Program> program;
@@ -969,6 +1049,25 @@ TEST(VulkanEngineTest, ExecutesMeanBeforeWidthPackedView) {
9691049
0, output.data(), output.size(), ScalarType::Float);
9701050
EXPECT_EQ(output, (std::vector<float>{2.5f, 6.5f}));
9711051
}
1052+
1053+
TEST(VulkanEngineTest, ResizesWidthPackedViewOfWidthPackedInput) {
1054+
CompiledProgram compiled = compile_program(make_dynamic_view_mm_program());
1055+
compiled.executable->resize_input(0, {2, 2});
1056+
1057+
const std::vector<float> input = {1.0f, 2.0f, 3.0f, 4.0f};
1058+
const std::vector<float> weight = {10.0f, 1.0f};
1059+
compiled.executable->set_input(
1060+
0, input.data(), input.size(), ScalarType::Float);
1061+
compiled.executable->set_input(
1062+
1, weight.data(), weight.size(), ScalarType::Float);
1063+
compiled.executable->execute();
1064+
1065+
EXPECT_EQ(compiled.executable->output_sizes(0), (std::vector<int64_t>{2, 1}));
1066+
std::vector<float> output(2);
1067+
compiled.executable->get_output(
1068+
0, output.data(), output.size(), ScalarType::Float);
1069+
EXPECT_EQ(output, (std::vector<float>{12.0f, 34.0f}));
1070+
}
9721071
// cppcheck-suppress-end syntaxError
9731072

9741073
} // namespace

0 commit comments

Comments
 (0)