Skip to content

[ET][Portable] Fix & cleanup op gelu #706

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

Closed
Closed
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
9 changes: 5 additions & 4 deletions kernels/portable/cpu/op_gelu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <cmath>

#include <executorch/kernels/portable/cpu/math_constants.h>
#include <executorch/kernels/portable/cpu/util/activation_ops_util.h>
#include <executorch/kernels/portable/cpu/util/functional_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <executorch/runtime/platform/assert.h>

namespace torch {
namespace executor {
Expand All @@ -28,10 +28,11 @@ Tensor& gelu_out(
Tensor& out) {
(void)ctx;

Error err = resize_tensor(out, in.sizes());
ET_CHECK_MSG(err == Error::Ok, "Could not resize output");
ET_KERNEL_CHECK(
ctx, check_gelu_args(in, approximate, out), InvalidArgument, out);

ET_CHECK_SAME_SHAPE_AND_DTYPE2(in, out);
ET_KERNEL_CHECK(
ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out);

ET_SWITCH_FLOAT_TYPES(in.scalar_type(), ctx, "gelu", CTYPE, [&]() {
if (approximate == "tanh") {
Expand Down
1 change: 1 addition & 0 deletions kernels/portable/cpu/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ _ATEN_OPS = (
name = "op_gelu",
deps = [
":math_constants",
"//executorch/kernels/portable/cpu/util:activation_ops_util",
"//executorch/kernels/portable/cpu/util:functional_util",
],
),
Expand Down
27 changes: 27 additions & 0 deletions kernels/portable/cpu/util/activation_ops_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 <cstring>

#include <executorch/kernels/portable/cpu/util/activation_ops_util.h>

namespace torch {
namespace executor {

bool check_gelu_args(const Tensor& in, string_view approximate, Tensor& out) {
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
ET_LOG_MSG_AND_RETURN_IF_FALSE(
approximate == "tanh" || approximate == "none",
"Invalid approximation format: %.*s for gelu",
static_cast<int>(approximate.length()),
approximate.data());
return true;
}

} // namespace executor
} // namespace torch
19 changes: 19 additions & 0 deletions kernels/portable/cpu/util/activation_ops_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/

#pragma once

#include <executorch/runtime/kernel/kernel_includes.h>

namespace torch {
namespace executor {

bool check_gelu_args(const Tensor& in, string_view approximate, Tensor& out);

} // namespace executor
} // namespace torch
13 changes: 13 additions & 0 deletions kernels/portable/cpu/util/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ def define_common_targets():
TARGETS and BUCK files that call this function.
"""

runtime.cxx_library(
name = "activation_ops_util",
srcs = ["activation_ops_util.cpp"],
exported_headers = [
"activation_ops_util.h",
],
compiler_flags = ["-Wno-missing-prototypes"],
deps = [
"//executorch/runtime/kernel:kernel_includes",
],
visibility = ["//executorch/kernels/portable/cpu/...", "//executorch/kernels/optimized/cpu/..."],
)

runtime.cxx_library(
name = "repeat_util",
srcs = [
Expand Down