<commit_before># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import sys

import pytest
from absl.testing import parameterized

import numpy as np
import tensorflow as tf
from tensorflow_addons.activations.hardshrink import _hardshrink_custom_op
from tensorflow_addons.activations.hardshrink import _hardshrink_py
from tensorflow_addons.utils import test_utils


def test_invalid():
    with pytest.raises(
        tf.errors.OpError, match="lower must be less than or equal to upper."
    ):
        y = _hardshrink_custom_op(tf.ones(shape=(1, 2, 3)), lower=2.0, upper=-2.0)
        y.numpy()


@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
def test_hardshrink(dtype):
    x = tf.constant([-2.0, -0.5, 0.0, 0.5, 2.0], dtype=dtype)
    expected_result = tf.constant([-2.0, 0.0, 0.0, 0.0, 2.0], dtype=dtype)
    test_utils.assert_allclose_according_to_type(
        _hardshrink_custom_op(x), expected_result
    )

    expected_result = tf.constant([-2.0, 0.0, 0.0, 0.0, 2.0], dtype=dtype)
    test_utils.assert_allclose_according_to_type(
        _hardshrink_custom_op(x, lower=-1.0, upper=1.0), expected_result
    )


@test_utils.run_all_in_graph_and_eager_modes
class HardshrinkTest(tf.test.TestCase, parameterized.TestCase):
    @parameterized.named_parameters(("float32", np.float32), ("float64", np.float64))
    def test_same_as_py_func(self, dtype):
        np.random.seed(1234)
        for _ in range(20):
            self.verify_funcs_are_equivalent(dtype)

    def verify_funcs_are_equivalent(self, dtype):
        x_np = np.random.uniform(-10, 10, size=(4, 4)).astype(dtype)
        x = tf.convert_to_tensor(x_np)
        lower = np.random.uniform(-10, 10)
        upper = lower + np.random.uniform(0, 10)

        with tf.GradientTape(persistent=True) as t:
            t.watch(x)
            y_native = _hardshrink_custom_op(x, lower, upper)
            y_py = _hardshrink_py(x, lower, upper)

        self.assertAllCloseAccordingToType(y_native, y_py)

        grad_native = t.gradient(y_native, x)
        grad_py = t.gradient(y_py, x)

        self.assertAllCloseAccordingToType(grad_native, grad_py)

<commit_msg>Moved test out of run all in graph and eager mode in hardshrink. (#1395)<commit_after># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import sys

import pytest

import numpy as np
import tensorflow as tf
from tensorflow_addons.activations.hardshrink import _hardshrink_custom_op
from tensorflow_addons.activations.hardshrink import _hardshrink_py
from tensorflow_addons.utils import test_utils


def test_invalid():
    with pytest.raises(
        tf.errors.OpError, match="lower must be less than or equal to upper."
    ):
        y = _hardshrink_custom_op(tf.ones(shape=(1, 2, 3)), lower=2.0, upper=-2.0)
        y.numpy()


@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
def test_hardshrink(dtype):
    x = tf.constant([-2.0, -0.5, 0.0, 0.5, 2.0], dtype=dtype)
    expected_result = tf.constant([-2.0, 0.0, 0.0, 0.0, 2.0], dtype=dtype)
    test_utils.assert_allclose_according_to_type(
        _hardshrink_custom_op(x), expected_result
    )

    expected_result = tf.constant([-2.0, 0.0, 0.0, 0.0, 2.0], dtype=dtype)
    test_utils.assert_allclose_according_to_type(
        _hardshrink_custom_op(x, lower=-1.0, upper=1.0), expected_result
    )


@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_same_as_py_func(dtype):
    np.random.seed(1234)
    for _ in range(20):
        verify_funcs_are_equivalent(dtype)


def verify_funcs_are_equivalent(dtype):
    x_np = np.random.uniform(-10, 10, size=(4, 4)).astype(dtype)
    x = tf.convert_to_tensor(x_np)
    lower = np.random.uniform(-10, 10)
    upper = lower + np.random.uniform(0, 10)

    with tf.GradientTape(persistent=True) as t:
        t.watch(x)
        y_native = _hardshrink_custom_op(x, lower, upper)
        y_py = _hardshrink_py(x, lower, upper)

    test_utils.assert_allclose_according_to_type(y_native, y_py)

    grad_native = t.gradient(y_native, x)
    grad_py = t.gradient(y_py, x)

    test_utils.assert_allclose_according_to_type(grad_native, grad_py)


{'fraction_non_alphanumeric': 0.13305037829857908, 'fraction_numerical': 0.030263886325890384, 'mean_word_length': 3.9005424954792045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3100536', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1795, 'n_words': 456}