-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathSymIntOps.cpp
More file actions
225 lines (179 loc) · 6.3 KB
/
SymIntOps.cpp
File metadata and controls
225 lines (179 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/vulkan/runtime/graph/ops/ExecuteNode.h>
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h>
namespace vkcompute {
//
// sym_size
//
void sym_size_impl(ComputeGraph* graph, const std::vector<ValueRef>& args) {
const ValueRef in_tensor = args.at(0);
const ValueRef dim = args.at(1);
const ValueRef out_symint = args.at(2);
const int64_t dim_val = graph->extract_scalar<int64_t>(dim);
const int64_t size_at_dim = graph->size_at<int64_t>(dim_val, in_tensor);
graph->set_symint(out_symint, static_cast<int32_t>(size_at_dim));
}
void resize_sym_size_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)args; // Unused parameter
sym_size_impl(graph, resize_args);
}
/*
* This operator takes a tensor and an integer dimension as inputs, and produces
* a symint as output. The symint's value is the size of the tensor at the
* specified dimension.
*/
void sym_size_int(ComputeGraph& graph, const std::vector<ValueRef>& args) {
sym_size_impl(&graph, args);
graph.execute_nodes().emplace_back(
new ExecuteNode(resize_sym_size_node, args));
}
//
// binary operators
//
void sym_add_impl(ComputeGraph* graph, const std::vector<ValueRef>& args) {
const ValueRef a = args.at(0);
const ValueRef b = args.at(1);
const ValueRef out = args.at(2);
const int32_t a_val = graph->read_symint(a);
const int32_t b_val = graph->read_symint(b);
const int32_t result = a_val + b_val;
graph->set_symint(out, result);
}
void resize_sym_add_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)args; // Unused parameter
sym_add_impl(graph, resize_args);
}
/*
* This operator takes two symints as inputs and produces a symint as output.
* The output symint's value is the sum of the two input symints.
*/
void sym_add(ComputeGraph& graph, const std::vector<ValueRef>& args) {
sym_add_impl(&graph, args);
graph.execute_nodes().emplace_back(
new ExecuteNode(resize_sym_add_node, args));
}
void sym_sub_impl(ComputeGraph* graph, const std::vector<ValueRef>& args) {
const ValueRef a = args.at(0);
const ValueRef b = args.at(1);
const ValueRef out = args.at(2);
const int32_t a_val = graph->read_symint(a);
const int32_t b_val = graph->read_symint(b);
const int32_t result = a_val - b_val;
graph->set_symint(out, result);
}
void resize_sym_sub_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)args;
sym_sub_impl(graph, resize_args);
}
void sym_sub(ComputeGraph& graph, const std::vector<ValueRef>& args) {
sym_sub_impl(&graph, args);
graph.execute_nodes().emplace_back(
new ExecuteNode(resize_sym_sub_node, args));
}
void sym_floordiv_impl(ComputeGraph* graph, const std::vector<ValueRef>& args) {
const ValueRef a = args.at(0);
const ValueRef b = args.at(1);
const ValueRef out = args.at(2);
const int32_t a_val = graph->read_symint(a);
const int32_t b_val = graph->read_symint(b);
// Floor division: round towards negative infinity
const int32_t result = (a_val ^ b_val) < 0 && a_val % b_val != 0
? a_val / b_val - 1
: a_val / b_val;
graph->set_symint(out, result);
}
void resize_sym_floordiv_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)args;
sym_floordiv_impl(graph, resize_args);
}
void sym_floordiv(ComputeGraph& graph, const std::vector<ValueRef>& args) {
sym_floordiv_impl(&graph, args);
graph.execute_nodes().emplace_back(
new ExecuteNode(resize_sym_floordiv_node, args));
}
void sym_mul_impl(ComputeGraph* graph, const std::vector<ValueRef>& args) {
const ValueRef a = args.at(0);
const ValueRef b = args.at(1);
const ValueRef out = args.at(2);
const int32_t a_val = graph->read_symint(a);
const int32_t b_val = graph->read_symint(b);
const int32_t result = a_val * b_val;
graph->set_symint(out, result);
}
void resize_sym_mul_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)args;
sym_mul_impl(graph, resize_args);
}
void sym_mul(ComputeGraph& graph, const std::vector<ValueRef>& args) {
sym_mul_impl(&graph, args);
graph.execute_nodes().emplace_back(
new ExecuteNode(resize_sym_mul_node, args));
}
void select_as_symint_impl(
ComputeGraph* graph,
const std::vector<ArgGroup>& unused,
const std::vector<ValueRef>& args) {
(void)unused; // Unused parameter
const ValueRef x = args.at(0);
const ValueRef dim = args.at(1);
const ValueRef index = args.at(2);
const ValueRef out = args.at(3);
const int64_t dim_val = graph->extract_scalar<int64_t>(dim);
int64_t index_val = graph->extract_scalar<int64_t>(index);
const std::vector<int64_t> x_sizes = graph->sizes_of(x);
const vkapi::ScalarType x_dtype = graph->dtype_of(x);
if (index_val < 0) {
index_val += x_sizes[dim_val];
}
const StagingPtr x_staging = graph->get_staging(graph->staging_of(x));
int32_t x_val;
switch (x_dtype) {
case vkapi::ScalarType::Int:
x_val = x_staging->select_element_at_dim<int32_t>(
x_sizes, dim_val, index_val);
break;
case vkapi::ScalarType::Long:
x_val = static_cast<int32_t>(x_staging->select_element_at_dim<int64_t>(
x_sizes, dim_val, index_val));
break;
default:
VK_THROW("Unsupported dtype for select_as_symint");
}
graph->set_symint(out, x_val);
}
void select_as_symint(ComputeGraph& graph, const std::vector<ValueRef>& args) {
select_as_symint_impl(&graph, {}, args);
graph.execute_nodes().emplace_back(new ExecuteNode(
select_as_symint_impl, args, {}, "select_as_symint", true));
graph.set_has_data_dependent_shapes();
}
REGISTER_OPERATORS {
VK_REGISTER_OP(sym_size.int, sym_size_int);
VK_REGISTER_OP(add, sym_add);
VK_REGISTER_OP(sub, sym_sub);
VK_REGISTER_OP(floordiv, sym_floordiv);
VK_REGISTER_OP(mul, sym_mul);
VK_REGISTER_OP(et_vk.select_as_symint.default, select_as_symint);
}
} // namespace vkcompute