-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathUnaryOp.cpp
More file actions
186 lines (165 loc) · 7.41 KB
/
UnaryOp.cpp
File metadata and controls
186 lines (165 loc) · 7.41 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
/*
* 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/OperatorRegistry.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h>
namespace vkcompute {
constexpr float kDummyFloat = -1.0f;
const std::string kClampShaderName = "clamp";
void resize_unary_op_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& extra_args) {
(void)extra_args;
const ValueRef out = args.at(0).refs.at(0);
const ValueRef self = args.at(1).refs.at(0);
const std::vector<int64_t> self_sizes = graph->sizes_of(self);
graph->virtual_resize(out, self_sizes);
}
void add_unary_op_node(
ComputeGraph& graph,
const ValueRef in,
const float min,
const float max,
const ValueRef out,
const std::string& op_name) {
std::string kernel_name(op_name);
add_dtype_suffix(kernel_name, graph.dtype_of(out));
add_storage_type_suffix(kernel_name, graph.storage_type_of(out));
const utils::vec2 min_max = {min, max};
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
graph,
VK_KERNEL_FROM_STR(kernel_name),
default_pick_global_wg_size,
default_pick_local_wg_size,
// Inputs and Outputs
{{out, vkapi::kWrite}, {in, vkapi::kRead}},
// Shader params buffers
{},
// Push Constants
{
graph.is_buffer_storage(out) ? graph.numel_pc_of(out)
: graph.logical_limits_pc_of(out),
PushConstantDataInfo(&min_max, sizeof(min_max)),
},
// pcs,
// Specialization Constants
{},
// Resize Args
{},
// Resizing Logic
resize_unary_op_node));
}
float get_val_or_inf(ComputeGraph& graph, const ValueRef& val, bool max) {
if (!graph.val_is_none(val)) {
return graph.extract_scalar<float>(val);
}
return max ? std::numeric_limits<float>::infinity()
: -std::numeric_limits<float>::infinity();
}
#define DEFINE_ACTIVATION_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node( \
graph, args[0], kDummyFloat, kDummyFloat, args[1], #op_name); \
}
#define DEFINE_CLAMP_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node( \
graph, \
args[0], \
get_val_or_inf(graph, args[1], /*max = */ false), \
get_val_or_inf(graph, args[2], /*max = */ true), \
args[3], \
kClampShaderName); \
}
#define DEFINE_RELU_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node( \
graph, \
args[0], \
0, \
std::numeric_limits<float>::infinity(), \
args[1], \
kClampShaderName); \
}
#define DEFINE_RELU6_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node(graph, args[0], 0, 6, args[1], kClampShaderName); \
}
#define DEFINE_HARDSHRINK_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node( \
graph, \
args[0], \
get_val_or_inf(graph, args[1], /*max = */ false), \
-get_val_or_inf(graph, args[1], /*max = */ true), \
args[2], \
"hardshrink"); \
}
#define DEFINE_LEAKY_RELU_FN(op_name) \
void op_name(ComputeGraph& graph, const std::vector<ValueRef>& args) { \
return add_unary_op_node( \
graph, \
args[0], \
get_val_or_inf(graph, args[1], /*neg slope*/ false), \
kDummyFloat, \
args[2], \
"leaky_relu"); \
}
void gelu(ComputeGraph& graph, const std::vector<ValueRef>& args) {
// args[1] is the `approximate` string
// https://fburl.com/code/9omngmyo
// currently only `approximate = "tanh"` is supported
return add_unary_op_node(
graph, args[0], kDummyFloat, kDummyFloat, args[2], "gelu");
}
DEFINE_ACTIVATION_FN(abs);
DEFINE_ACTIVATION_FN(cos);
DEFINE_ACTIVATION_FN(exp);
DEFINE_ACTIVATION_FN(neg);
DEFINE_ACTIVATION_FN(sigmoid);
DEFINE_ACTIVATION_FN(sin);
DEFINE_ACTIVATION_FN(sqrt);
DEFINE_ACTIVATION_FN(rsqrt);
DEFINE_ACTIVATION_FN(tanh);
DEFINE_CLAMP_FN(clamp);
DEFINE_CLAMP_FN(hardtanh);
DEFINE_RELU_FN(relu);
DEFINE_RELU6_FN(relu6);
DEFINE_HARDSHRINK_FN(hardshrink);
DEFINE_ACTIVATION_FN(hardswish);
DEFINE_ACTIVATION_FN(hardsigmoid);
DEFINE_LEAKY_RELU_FN(leaky_relu);
DEFINE_ACTIVATION_FN(round);
DEFINE_ACTIVATION_FN(bitwise_not);
REGISTER_OPERATORS {
VK_REGISTER_OP(aten.abs.default, abs);
VK_REGISTER_OP(aten.clamp.default, clamp);
VK_REGISTER_OP(aten.cos.default, cos);
VK_REGISTER_OP(aten.exp.default, exp);
VK_REGISTER_OP(aten.gelu.default, gelu);
VK_REGISTER_OP(aten.hardtanh.default, hardtanh);
VK_REGISTER_OP(aten.neg.default, neg);
VK_REGISTER_OP(aten.relu.default, relu);
VK_REGISTER_OP(aten.relu6.default, relu6);
VK_REGISTER_OP(aten.sigmoid.default, sigmoid);
VK_REGISTER_OP(aten.sin.default, sin);
VK_REGISTER_OP(aten.sqrt.default, sqrt);
VK_REGISTER_OP(aten.rsqrt.default, rsqrt);
VK_REGISTER_OP(aten.tanh.default, tanh);
VK_REGISTER_OP(aten.hardshrink.default, hardshrink);
VK_REGISTER_OP(aten.hardswish.default, hardswish);
VK_REGISTER_OP(aten.hardsigmoid.default, hardsigmoid);
VK_REGISTER_OP(aten.leaky_relu.default, leaky_relu);
VK_REGISTER_OP(aten.round.default, round);
VK_REGISTER_OP(aten.bitwise_not.default, bitwise_not);
}
} // namespace vkcompute