Skip to content

Replaced hardshrink implementation by a pure python one. #913

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
wants to merge 7 commits into from
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
20 changes: 9 additions & 11 deletions tensorflow_addons/activations/hardshrink.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
# ==============================================================================

import tensorflow as tf
from tensorflow_addons.utils.resource_loader import LazySO

_activation_so = LazySO("custom_ops/activations/_activation_ops.so")


@tf.keras.utils.register_keras_serializable(package='Addons')
Expand All @@ -34,12 +31,13 @@ def hardshrink(x, lower=-0.5, upper=0.5):
Returns:
A `Tensor`. Has the same type as `x`.
"""
if lower > upper:
raise ValueError("The value of lower is {} and should"
" not be higher than the value "
"variable upper, which is {} .".format(lower, upper))
x = tf.convert_to_tensor(x)
return _activation_so.ops.addons_hardshrink(x, lower, upper)


@tf.RegisterGradient("Addons>Hardshrink")
def _hardshrink_grad(op, grad):
return _activation_so.ops.addons_hardshrink_grad(grad, op.inputs[0],
op.get_attr("lower"),
op.get_attr("upper"))
mask_lower = x < lower
mask_upper = upper < x
mask = tf.logical_or(mask_lower, mask_upper)
mask = tf.cast(mask, x.dtype)
return x * mask
3 changes: 1 addition & 2 deletions tensorflow_addons/activations/hardshrink_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
@test_utils.run_all_in_graph_and_eager_modes
class HardshrinkTest(tf.test.TestCase, parameterized.TestCase):
def test_invalid(self):
with self.assertRaisesOpError(
"lower must be less than or equal to upper."): # pylint: disable=bad-continuation
with self.assertRaises(ValueError):
y = hardshrink(tf.ones(shape=(1, 2, 3)), lower=2.0, upper=-2.0)
self.evaluate(y)

Expand Down
5 changes: 0 additions & 5 deletions tensorflow_addons/custom_ops/activations/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ custom_op_library(
srcs = [
"cc/kernels/gelu_op.cc",
"cc/kernels/gelu_op.h",
"cc/kernels/hardshrink_op.cc",
"cc/kernels/hardshrink_op.h",
"cc/kernels/lisht_op.cc",
"cc/kernels/lisht_op.h",
"cc/kernels/mish_op.cc",
Expand All @@ -20,7 +18,6 @@ custom_op_library(
"cc/kernels/tanhshrink_op.cc",
"cc/kernels/tanhshrink_op.h",
"cc/ops/gelu_op.cc",
"cc/ops/hardshrink_op.cc",
"cc/ops/lisht_op.cc",
"cc/ops/mish_op.cc",
"cc/ops/softshrink_op.cc",
Expand All @@ -29,8 +26,6 @@ custom_op_library(
cuda_srcs = [
"cc/kernels/gelu_op.h",
"cc/kernels/gelu_op_gpu.cu.cc",
"cc/kernels/hardshrink_op.h",
"cc/kernels/hardshrink_op_gpu.cu.cc",
"cc/kernels/lisht_op.h",
"cc/kernels/lisht_op_gpu.cu.cc",
"cc/kernels/mish_op.h",
Expand Down

This file was deleted.

144 changes: 0 additions & 144 deletions tensorflow_addons/custom_ops/activations/cc/kernels/hardshrink_op.h

This file was deleted.

This file was deleted.

41 changes: 0 additions & 41 deletions tensorflow_addons/custom_ops/activations/cc/ops/hardshrink_op.cc

This file was deleted.