Relu test logic moved to gpu/common/tasks.

PiperOrigin-RevId: 344296799
Change-Id: Ic033efa5401afd8823f48f7ad4ce47c720aaa66e
diff --git a/tensorflow/lite/delegates/gpu/cl/kernels/BUILD b/tensorflow/lite/delegates/gpu/cl/kernels/BUILD
index 9502839..cf7d0e6 100644
--- a/tensorflow/lite/delegates/gpu/cl/kernels/BUILD
+++ b/tensorflow/lite/delegates/gpu/cl/kernels/BUILD
@@ -461,7 +461,7 @@
         ":cl_test",
         "//tensorflow/lite/delegates/gpu/common:operations",
         "//tensorflow/lite/delegates/gpu/common:status",
-        "//tensorflow/lite/delegates/gpu/common/tasks:relu",
+        "//tensorflow/lite/delegates/gpu/common/tasks:relu_test_util",
         "@com_google_googletest//:gtest_main",
     ],
 )
diff --git a/tensorflow/lite/delegates/gpu/cl/kernels/relu_test.cc b/tensorflow/lite/delegates/gpu/cl/kernels/relu_test.cc
index e66d16d..dd17c71 100644
--- a/tensorflow/lite/delegates/gpu/cl/kernels/relu_test.cc
+++ b/tensorflow/lite/delegates/gpu/cl/kernels/relu_test.cc
@@ -13,8 +13,6 @@
 limitations under the License.
 ==============================================================================*/
 
-#include "tensorflow/lite/delegates/gpu/common/tasks/relu.h"
-
 #include <vector>
 
 #include <gmock/gmock.h>
@@ -22,132 +20,22 @@
 #include "tensorflow/lite/delegates/gpu/cl/kernels/cl_test.h"
 #include "tensorflow/lite/delegates/gpu/common/operations.h"
 #include "tensorflow/lite/delegates/gpu/common/status.h"
-
-using ::testing::FloatNear;
-using ::testing::Pointwise;
+#include "tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.h"
 
 namespace tflite {
 namespace gpu {
 namespace cl {
-namespace {
 
 TEST_F(OpenCLOperationTest, ReLUNoClipNoAlpha) {
-  TensorFloat32 src_tensor;
-  src_tensor.shape = BHWC(1, 2, 1, 2);
-  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
-
-  ReLUAttributes attr;
-  attr.alpha = 0.0f;
-  attr.clip = 0.0f;
-
-  for (auto storage : env_.GetSupportedStorages()) {
-    for (auto precision : env_.GetSupportedPrecisions()) {
-      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
-      OperationDef op_def;
-      op_def.precision = precision;
-      auto data_type = DeduceDataTypeFromPrecision(precision);
-      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
-      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
-      TensorFloat32 dst_tensor;
-      GPUOperation operation = CreateReLU(op_def, attr);
-      ASSERT_OK(ExecuteGPUOperation(
-          src_tensor, creation_context_,
-          absl::make_unique<GPUOperation>(std::move(operation)),
-          BHWC(1, 2, 1, 2), &dst_tensor));
-      EXPECT_THAT(dst_tensor.data,
-                  Pointwise(FloatNear(eps), {0.0f, 0.8f, 0.0f, 3.2f}));
-    }
-  }
+  ReLUNoClipNoAlphaTest(&exec_env_);
 }
 
-TEST_F(OpenCLOperationTest, ReLUClip) {
-  TensorFloat32 src_tensor;
-  src_tensor.shape = BHWC(1, 2, 1, 2);
-  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
+TEST_F(OpenCLOperationTest, ReLUClip) { ReLUClipTest(&exec_env_); }
 
-  ReLUAttributes attr;
-  attr.alpha = 0.0f;
-  attr.clip = 0.9f;
+TEST_F(OpenCLOperationTest, ReLUAlpha) { ReLUAlphaTest(&exec_env_); }
 
-  for (auto storage : env_.GetSupportedStorages()) {
-    for (auto precision : env_.GetSupportedPrecisions()) {
-      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
-      OperationDef op_def;
-      op_def.precision = precision;
-      auto data_type = DeduceDataTypeFromPrecision(precision);
-      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
-      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
-      TensorFloat32 dst_tensor;
-      GPUOperation operation = CreateReLU(op_def, attr);
-      ASSERT_OK(ExecuteGPUOperation(
-          src_tensor, creation_context_,
-          absl::make_unique<GPUOperation>(std::move(operation)),
-          BHWC(1, 2, 1, 2), &dst_tensor));
-      EXPECT_THAT(dst_tensor.data,
-                  Pointwise(FloatNear(eps), {0.0f, 0.8f, 0.0f, 0.9f}));
-    }
-  }
-}
+TEST_F(OpenCLOperationTest, ReLUAlphaClip) { ReLUAlphaClipTest(&exec_env_); }
 
-TEST_F(OpenCLOperationTest, ReLUAlpha) {
-  TensorFloat32 src_tensor;
-  src_tensor.shape = BHWC(1, 2, 1, 2);
-  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
-
-  ReLUAttributes attr;
-  attr.alpha = 0.5f;
-  attr.clip = 0.0f;
-
-  for (auto storage : env_.GetSupportedStorages()) {
-    for (auto precision : env_.GetSupportedPrecisions()) {
-      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
-      OperationDef op_def;
-      op_def.precision = precision;
-      auto data_type = DeduceDataTypeFromPrecision(precision);
-      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
-      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
-      TensorFloat32 dst_tensor;
-      GPUOperation operation = CreateReLU(op_def, attr);
-      ASSERT_OK(ExecuteGPUOperation(
-          src_tensor, creation_context_,
-          absl::make_unique<GPUOperation>(std::move(operation)),
-          BHWC(1, 2, 1, 2), &dst_tensor));
-      EXPECT_THAT(dst_tensor.data,
-                  Pointwise(FloatNear(eps), {-0.25f, 0.8f, -0.3f, 3.2f}));
-    }
-  }
-}
-
-TEST_F(OpenCLOperationTest, ReLUAlphaClip) {
-  TensorFloat32 src_tensor;
-  src_tensor.shape = BHWC(1, 2, 1, 2);
-  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
-
-  ReLUAttributes attr;
-  attr.alpha = 0.5f;
-  attr.clip = 0.5f;
-
-  for (auto storage : env_.GetSupportedStorages()) {
-    for (auto precision : env_.GetSupportedPrecisions()) {
-      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
-      OperationDef op_def;
-      op_def.precision = precision;
-      auto data_type = DeduceDataTypeFromPrecision(precision);
-      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
-      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
-      TensorFloat32 dst_tensor;
-      GPUOperation operation = CreateReLU(op_def, attr);
-      ASSERT_OK(ExecuteGPUOperation(
-          src_tensor, creation_context_,
-          absl::make_unique<GPUOperation>(std::move(operation)),
-          BHWC(1, 2, 1, 2), &dst_tensor));
-      EXPECT_THAT(dst_tensor.data,
-                  Pointwise(FloatNear(eps), {-0.25f, 0.5f, -0.3f, 0.5f}));
-    }
-  }
-}
-
-}  // namespace
 }  // namespace cl
 }  // namespace gpu
 }  // namespace tflite
diff --git a/tensorflow/lite/delegates/gpu/common/task/testing_util.h b/tensorflow/lite/delegates/gpu/common/task/testing_util.h
index efe3518..9f4609f 100644
--- a/tensorflow/lite/delegates/gpu/common/task/testing_util.h
+++ b/tensorflow/lite/delegates/gpu/common/task/testing_util.h
@@ -47,6 +47,23 @@
       std::unique_ptr<GPUOperation>&& operation,
       const std::vector<BHWC>& dst_sizes,
       const std::vector<TensorFloat32*>& dst_cpu) = 0;
+
+  absl::Status ExecuteGPUOperation(const TensorFloat32& src_cpu,
+                                   std::unique_ptr<GPUOperation>&& operation,
+                                   const BHWC& dst_size,
+                                   TensorFloat32* result) {
+    return ExecuteGPUOperation(std::vector<TensorFloat32>{src_cpu},
+                               std::move(operation), dst_size, result);
+  }
+
+  absl::Status ExecuteGPUOperation(const std::vector<TensorFloat32>& src_cpu,
+                                   std::unique_ptr<GPUOperation>&& operation,
+                                   const BHWC& dst_size,
+                                   TensorFloat32* result) {
+    return ExecuteGPUOperation(
+        std::vector<TensorFloat32>{src_cpu}, std::move(operation),
+        std::vector<BHWC>{dst_size}, std::vector<TensorFloat32*>{result});
+  }
 };
 
 }  // namespace gpu
diff --git a/tensorflow/lite/delegates/gpu/common/tasks/BUILD b/tensorflow/lite/delegates/gpu/common/tasks/BUILD
index bbfb503..c78509a 100644
--- a/tensorflow/lite/delegates/gpu/common/tasks/BUILD
+++ b/tensorflow/lite/delegates/gpu/common/tasks/BUILD
@@ -444,6 +444,20 @@
 )
 
 cc_library(
+    name = "relu_test_util",
+    testonly = 1,
+    srcs = ["relu_test_util.cc"],
+    hdrs = ["relu_test_util.h"],
+    deps = [
+        ":relu",
+        "//tensorflow/lite/delegates/gpu/common:operations",
+        "//tensorflow/lite/delegates/gpu/common:status",
+        "//tensorflow/lite/delegates/gpu/common/task:testing_util",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
     name = "reshape",
     srcs = ["reshape.cc"],
     hdrs = ["reshape.h"],
diff --git a/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.cc b/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.cc
new file mode 100644
index 0000000..ea89912
--- /dev/null
+++ b/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.cc
@@ -0,0 +1,155 @@
+/* Copyright 2020 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.
+==============================================================================*/
+
+#include "tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.h"
+
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include "tensorflow/lite/delegates/gpu/common/operations.h"
+#include "tensorflow/lite/delegates/gpu/common/status.h"
+#include "tensorflow/lite/delegates/gpu/common/task/testing_util.h"
+#include "tensorflow/lite/delegates/gpu/common/tasks/relu.h"
+
+namespace tflite {
+namespace gpu {
+
+void ReLUNoClipNoAlphaTest(TestExecutionEnvironment* env) {
+  TensorFloat32 src_tensor;
+  src_tensor.shape = BHWC(1, 2, 1, 2);
+  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
+
+  ReLUAttributes attr;
+  attr.alpha = 0.0f;
+  attr.clip = 0.0f;
+
+  for (auto storage : env->GetSupportedStorages()) {
+    for (auto precision : env->GetSupportedPrecisions()) {
+      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
+      OperationDef op_def;
+      op_def.precision = precision;
+      auto data_type = DeduceDataTypeFromPrecision(precision);
+      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
+      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
+      TensorFloat32 dst_tensor;
+      GPUOperation operation = CreateReLU(op_def, attr);
+      ASSERT_TRUE(env->ExecuteGPUOperation(
+                         src_tensor,
+                         absl::make_unique<GPUOperation>(std::move(operation)),
+                         BHWC(1, 2, 1, 2), &dst_tensor)
+                      .ok());
+      EXPECT_THAT(dst_tensor.data,
+                  testing::Pointwise(testing::FloatNear(eps),
+                                     {0.0f, 0.8f, 0.0f, 3.2f}));
+    }
+  }
+}
+
+void ReLUClipTest(TestExecutionEnvironment* env) {
+  TensorFloat32 src_tensor;
+  src_tensor.shape = BHWC(1, 2, 1, 2);
+  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
+
+  ReLUAttributes attr;
+  attr.alpha = 0.0f;
+  attr.clip = 0.9f;
+
+  for (auto storage : env->GetSupportedStorages()) {
+    for (auto precision : env->GetSupportedPrecisions()) {
+      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
+      OperationDef op_def;
+      op_def.precision = precision;
+      auto data_type = DeduceDataTypeFromPrecision(precision);
+      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
+      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
+      TensorFloat32 dst_tensor;
+      GPUOperation operation = CreateReLU(op_def, attr);
+      ASSERT_TRUE(env->ExecuteGPUOperation(
+                         src_tensor,
+                         absl::make_unique<GPUOperation>(std::move(operation)),
+                         BHWC(1, 2, 1, 2), &dst_tensor)
+                      .ok());
+      EXPECT_THAT(dst_tensor.data,
+                  testing::Pointwise(testing::FloatNear(eps),
+                                     {0.0f, 0.8f, 0.0f, 0.9f}));
+    }
+  }
+}
+
+void ReLUAlphaTest(TestExecutionEnvironment* env) {
+  TensorFloat32 src_tensor;
+  src_tensor.shape = BHWC(1, 2, 1, 2);
+  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
+
+  ReLUAttributes attr;
+  attr.alpha = 0.5f;
+  attr.clip = 0.0f;
+
+  for (auto storage : env->GetSupportedStorages()) {
+    for (auto precision : env->GetSupportedPrecisions()) {
+      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
+      OperationDef op_def;
+      op_def.precision = precision;
+      auto data_type = DeduceDataTypeFromPrecision(precision);
+      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
+      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
+      TensorFloat32 dst_tensor;
+      GPUOperation operation = CreateReLU(op_def, attr);
+      ASSERT_TRUE(env->ExecuteGPUOperation(
+                         src_tensor,
+                         absl::make_unique<GPUOperation>(std::move(operation)),
+                         BHWC(1, 2, 1, 2), &dst_tensor)
+                      .ok());
+      EXPECT_THAT(dst_tensor.data,
+                  testing::Pointwise(testing::FloatNear(eps),
+                                     {-0.25f, 0.8f, -0.3f, 3.2f}));
+    }
+  }
+}
+
+void ReLUAlphaClipTest(TestExecutionEnvironment* env) {
+  TensorFloat32 src_tensor;
+  src_tensor.shape = BHWC(1, 2, 1, 2);
+  src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
+
+  ReLUAttributes attr;
+  attr.alpha = 0.5f;
+  attr.clip = 0.5f;
+
+  for (auto storage : env->GetSupportedStorages()) {
+    for (auto precision : env->GetSupportedPrecisions()) {
+      const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
+      OperationDef op_def;
+      op_def.precision = precision;
+      auto data_type = DeduceDataTypeFromPrecision(precision);
+      op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
+      op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
+      TensorFloat32 dst_tensor;
+      GPUOperation operation = CreateReLU(op_def, attr);
+      ASSERT_TRUE(env->ExecuteGPUOperation(
+                         src_tensor,
+                         absl::make_unique<GPUOperation>(std::move(operation)),
+                         BHWC(1, 2, 1, 2), &dst_tensor)
+                      .ok());
+      EXPECT_THAT(dst_tensor.data,
+                  testing::Pointwise(testing::FloatNear(eps),
+                                     {-0.25f, 0.5f, -0.3f, 0.5f}));
+    }
+  }
+}
+
+}  // namespace gpu
+}  // namespace tflite
diff --git a/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.h b/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.h
new file mode 100644
index 0000000..402974b
--- /dev/null
+++ b/tensorflow/lite/delegates/gpu/common/tasks/relu_test_util.h
@@ -0,0 +1,32 @@
+/* Copyright 2020 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.
+==============================================================================*/
+
+#ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_RELU_TEST_UTIL_H_
+#define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_RELU_TEST_UTIL_H_
+
+#include "tensorflow/lite/delegates/gpu/common/task/testing_util.h"
+
+namespace tflite {
+namespace gpu {
+
+void ReLUNoClipNoAlphaTest(TestExecutionEnvironment* env);
+void ReLUClipTest(TestExecutionEnvironment* env);
+void ReLUAlphaTest(TestExecutionEnvironment* env);
+void ReLUAlphaClipTest(TestExecutionEnvironment* env);
+
+}  // namespace gpu
+}  // namespace tflite
+
+#endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_RELU_TEST_UTIL_H_