|
| 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/InsertPrepack.h> |
| 8 | + |
| 9 | +#include <gtest/gtest.h> |
| 10 | + |
| 11 | +namespace ptn::vulkan { |
| 12 | +namespace { |
| 13 | + |
| 14 | +Node placeholder(const char* name, ValueId output) { |
| 15 | + return Node{ |
| 16 | + .name = name, |
| 17 | + .op_kind = OpKind::Placeholder, |
| 18 | + .outputs = {{.value_id = output}}, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +Node binary( |
| 23 | + const char* name, |
| 24 | + const char* target, |
| 25 | + ValueId left, |
| 26 | + ValueId right, |
| 27 | + ValueId output) { |
| 28 | + return Node{ |
| 29 | + .name = name, |
| 30 | + .target = target, |
| 31 | + .inputs = |
| 32 | + { |
| 33 | + {.name = "self", .arg = TensorArg{left}}, |
| 34 | + {.name = "other", .arg = TensorArg{right}}, |
| 35 | + }, |
| 36 | + .outputs = {{.value_id = output}}, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +Method make_method() { |
| 41 | + Method method; |
| 42 | + method.name = "forward"; |
| 43 | + Graph& graph = method.graph; |
| 44 | + graph.values = { |
| 45 | + Value("input", ScalarType::Half, {4}), |
| 46 | + Value("constant", ScalarType::Half, {4}), |
| 47 | + Value("mul", ScalarType::Half, {4}), |
| 48 | + Value("add", ScalarType::Half, {4}), |
| 49 | + }; |
| 50 | + graph.values[0].role = ValueRole::UserInput; |
| 51 | + graph.values[1].role = ValueRole::Parameter; |
| 52 | + graph.nodes = { |
| 53 | + placeholder("input", 0), |
| 54 | + placeholder("constant", 1), |
| 55 | + binary("mul", "torch.ops.aten.mul.Tensor", 0, 1, 2), |
| 56 | + binary("add", "torch.ops.aten.add.Tensor", 2, 1, 3), |
| 57 | + Node{ |
| 58 | + .name = "output", |
| 59 | + .op_kind = OpKind::Output, |
| 60 | + .inputs = {{.arg = TensorArg{3}}}, |
| 61 | + }, |
| 62 | + }; |
| 63 | + graph.input_ids = {0}; |
| 64 | + graph.output_ids = {3}; |
| 65 | + graph.initialize_schedule(); |
| 66 | + graph.rebuild_def_use(); |
| 67 | + method.data_bindings = {{ |
| 68 | + .value_id = 1, |
| 69 | + .role = ValueRole::Parameter, |
| 70 | + .key = "constant", |
| 71 | + }}; |
| 72 | + return method; |
| 73 | +} |
| 74 | + |
| 75 | +// cppcheck-suppress-begin syntaxError |
| 76 | +TEST(InsertPrepackTest, SharesOnePrepackAcrossConsumers) { |
| 77 | + Method method = make_method(); |
| 78 | + |
| 79 | + EXPECT_EQ(insert_prepack_nodes(method), 1); |
| 80 | + |
| 81 | + const Graph& graph = method.graph; |
| 82 | + ASSERT_EQ(graph.values.size(), 5); |
| 83 | + ASSERT_EQ(graph.nodes.size(), 6); |
| 84 | + const Node& prepack = graph.node(5); |
| 85 | + EXPECT_EQ(prepack.target, "torch.ops.et_vk.prepack.default"); |
| 86 | + EXPECT_EQ(prepack.input_value_ids(), std::vector<ValueId>({1})); |
| 87 | + EXPECT_EQ(graph.node(2).input_value_ids(), std::vector<ValueId>({0, 4})); |
| 88 | + EXPECT_EQ(graph.node(3).input_value_ids(), std::vector<ValueId>({2, 4})); |
| 89 | + EXPECT_EQ(graph.value(4).role, ValueRole::ConstantTensor); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(InsertPrepackTest, LeavesSelfPrepackingOpUntouched) { |
| 93 | + Method method = make_method(); |
| 94 | + method.graph.node(2).target = "torch.ops.aten.linear.default"; |
| 95 | + method.graph.node(3).target = "torch.ops.et_vk.embedding_q4gsw.default"; |
| 96 | + |
| 97 | + EXPECT_EQ(insert_prepack_nodes(method), 0); |
| 98 | + EXPECT_EQ(method.graph.values.size(), 4); |
| 99 | + EXPECT_EQ(method.graph.nodes.size(), 5); |
| 100 | +} |
| 101 | + |
| 102 | +TEST(InsertPrepackTest, LeavesMutableBindingUntouched) { |
| 103 | + Method method = make_method(); |
| 104 | + method.data_bindings[0].mutated = true; |
| 105 | + |
| 106 | + EXPECT_EQ(insert_prepack_nodes(method), 0); |
| 107 | + EXPECT_EQ(method.graph.values.size(), 4); |
| 108 | + EXPECT_EQ(method.graph.nodes.size(), 5); |
| 109 | + EXPECT_EQ( |
| 110 | + method.graph.node(2).input_value_ids(), std::vector<ValueId>({0, 1})); |
| 111 | + EXPECT_EQ( |
| 112 | + method.graph.node(3).input_value_ids(), std::vector<ValueId>({2, 1})); |
| 113 | +} |
| 114 | +// cppcheck-suppress-end syntaxError |
| 115 | + |
| 116 | +} // namespace |
| 117 | +} // namespace ptn::vulkan |
0 commit comments