Skip to content

aten.leakyrelu.default in unary_ops #7975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/activations.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ vec4 hardsigmoid(vec4 tex) {
hardsigmoid(tex.z),
hardsigmoid(tex.w));
}

float leaky_relu(float x, float negative_slope) {
return x * (float(x > 0.0) + negative_slope * float(x <= 0.0));
}

vec4 leaky_relu(vec4 tex, float negative_slope) {
return vec4(
leaky_relu(tex.x, negative_slope),
leaky_relu(tex.y, negative_slope),
leaky_relu(tex.z, negative_slope),
leaky_relu(tex.w, negative_slope));
}
2 changes: 2 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/unary_op.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ unary_op:
OPERATOR: hardswish(X)
- NAME: hardsigmoid
OPERATOR: hardsigmoid(X)
- NAME: leaky_relu
OPERATOR: leaky_relu(X, A)
13 changes: 13 additions & 0 deletions backends/vulkan/runtime/graph/ops/impl/UnaryOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ float get_val_or_inf(ComputeGraph& graph, const ValueRef& val, bool max) {
"hardshrink"); \
}

#define DEFINE_LEAKY_RELU_FN(op_name) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a macro that is only used once?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't use other macros (DEFINE_ACTIVATION_FN for instance) since the func signature is different. Also, I thought maybe other activations (like PReLU) could use the same macro in the future.

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
Expand All @@ -137,6 +148,7 @@ DEFINE_RELU_FN(relu);
DEFINE_HARDSHRINK_FN(hardshrink);
DEFINE_ACTIVATION_FN(hardswish);
DEFINE_ACTIVATION_FN(hardsigmoid);
DEFINE_LEAKY_RELU_FN(leaky_relu);

REGISTER_OPERATORS {
VK_REGISTER_OP(aten.abs.default, abs);
Expand All @@ -155,6 +167,7 @@ REGISTER_OPERATORS {
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);
}

} // namespace vkcompute
1 change: 1 addition & 0 deletions backends/vulkan/test/op_tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ def get_reduce_op_inputs():
"aten.cos.default",
"aten.hardswish.default",
"aten.hardsigmoid.default",
"aten.leaky_relu.default",
]
)
def get_unary_ops_inputs():
Expand Down
Loading