PReLU tests logic moved to prelu_test_util that can be reused among different backends.
Added new PReLU tests in Metal. PiperOrigin-RevId: 352012792 Change-Id: Idd9c8bfa9989c6df7e5e12ea83e25e721e3f3f2b
This commit is contained in:
parent
b131b30fb5
commit
0e1f4fd484
tensorflow/lite/delegates/gpu
cl/kernels
common/tasks
metal/kernels
@ -409,7 +409,7 @@ cc_test(
|
||||
":cl_test",
|
||||
"//tensorflow/lite/delegates/gpu/common:operations",
|
||||
"//tensorflow/lite/delegates/gpu/common:status",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:prelu",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:prelu_test_util",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
@ -13,8 +13,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==============================================================================*/
|
||||
|
||||
#include "tensorflow/lite/delegates/gpu/common/tasks/prelu.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
@ -22,9 +20,7 @@ limitations under the License.
|
||||
#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/prelu_test_util.h"
|
||||
|
||||
namespace tflite {
|
||||
namespace gpu {
|
||||
@ -32,102 +28,18 @@ namespace cl {
|
||||
namespace {
|
||||
|
||||
TEST_F(OpenCLOperationTest, PReLUAlpha) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<Linear, DataType::FLOAT32> parameters;
|
||||
parameters.shape = Linear(2);
|
||||
parameters.data = {0.5f, -2.0f};
|
||||
attr.alpha = parameters;
|
||||
attr.clip = 0.0;
|
||||
|
||||
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 =
|
||||
CreatePReLU(creation_context_.GetGpuInfo(), 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, 2.0f, -1.0f, 3.0f}));
|
||||
}
|
||||
}
|
||||
auto status = PReLUAlphaTest(&exec_env_);
|
||||
ASSERT_TRUE(status.ok()) << status.error_message();
|
||||
}
|
||||
|
||||
TEST_F(OpenCLOperationTest, PReLUAlphaClip) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<Linear, DataType::FLOAT32> parameters;
|
||||
parameters.shape = Linear(2);
|
||||
parameters.data = {0.5f, -2.0f};
|
||||
attr.alpha = parameters;
|
||||
attr.clip = 0.7f;
|
||||
|
||||
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 =
|
||||
CreatePReLU(creation_context_.GetGpuInfo(), 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, 2.0f, -1.0f, 0.7f}));
|
||||
}
|
||||
}
|
||||
auto status = PReLUAlphaClipTest(&exec_env_);
|
||||
ASSERT_TRUE(status.ok()) << status.error_message();
|
||||
}
|
||||
|
||||
TEST_F(OpenCLOperationTest, PReLUHWCAlpha) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<HWC, DataType::FLOAT32> hwc_tensor;
|
||||
hwc_tensor.shape = HWC(2, 1, 2);
|
||||
hwc_tensor.data = {0.5f, -2.0f, 0.7f, 4.7f};
|
||||
attr.alpha = hwc_tensor;
|
||||
attr.clip = 0.0;
|
||||
|
||||
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 =
|
||||
CreatePReLU(creation_context_.GetGpuInfo(), 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, 2.0f, -1.4f, 3.0f}));
|
||||
}
|
||||
}
|
||||
auto status = PReLUHWCAlphaTest(&exec_env_);
|
||||
ASSERT_TRUE(status.ok()) << status.error_message();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -413,6 +413,19 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "prelu_test_util",
|
||||
testonly = 1,
|
||||
srcs = ["prelu_test_util.cc"],
|
||||
hdrs = ["prelu_test_util.h"],
|
||||
deps = [
|
||||
":prelu",
|
||||
"//tensorflow/lite/delegates/gpu/common:operations",
|
||||
"//tensorflow/lite/delegates/gpu/common:status",
|
||||
"//tensorflow/lite/delegates/gpu/common/task:testing_util",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "quantize_and_dequantize",
|
||||
srcs = ["quantize_and_dequantize.cc"],
|
||||
|
125
tensorflow/lite/delegates/gpu/common/tasks/prelu_test_util.cc
Normal file
125
tensorflow/lite/delegates/gpu/common/tasks/prelu_test_util.cc
Normal file
@ -0,0 +1,125 @@
|
||||
/* Copyright 2021 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/prelu_test_util.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#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/prelu.h"
|
||||
|
||||
namespace tflite {
|
||||
namespace gpu {
|
||||
|
||||
absl::Status PReLUAlphaTest(TestExecutionEnvironment* env) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<Linear, DataType::FLOAT32> parameters;
|
||||
parameters.shape = Linear(2);
|
||||
parameters.data = {0.5f, -2.0f};
|
||||
attr.alpha = parameters;
|
||||
attr.clip = 0.0;
|
||||
|
||||
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 = CreatePReLU(env->GetGpuInfo(), op_def, attr);
|
||||
RETURN_IF_ERROR(env->ExecuteGPUOperation(
|
||||
src_tensor, absl::make_unique<GPUOperation>(std::move(operation)),
|
||||
BHWC(1, 2, 1, 2), &dst_tensor));
|
||||
RETURN_IF_ERROR(
|
||||
PointWiseNear({0.0f, 2.0f, -1.0f, 3.0f}, dst_tensor.data, eps));
|
||||
}
|
||||
}
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status PReLUAlphaClipTest(TestExecutionEnvironment* env) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<Linear, DataType::FLOAT32> parameters;
|
||||
parameters.shape = Linear(2);
|
||||
parameters.data = {0.5f, -2.0f};
|
||||
attr.alpha = parameters;
|
||||
attr.clip = 0.7f;
|
||||
|
||||
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 = CreatePReLU(env->GetGpuInfo(), op_def, attr);
|
||||
RETURN_IF_ERROR(env->ExecuteGPUOperation(
|
||||
src_tensor, absl::make_unique<GPUOperation>(std::move(operation)),
|
||||
BHWC(1, 2, 1, 2), &dst_tensor));
|
||||
RETURN_IF_ERROR(
|
||||
PointWiseNear({0.0f, 2.0f, -1.0f, 0.7f}, dst_tensor.data, eps));
|
||||
}
|
||||
}
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status PReLUHWCAlphaTest(TestExecutionEnvironment* env) {
|
||||
TensorFloat32 src_tensor;
|
||||
src_tensor.shape = BHWC(1, 2, 1, 2);
|
||||
src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
|
||||
|
||||
PReLUAttributes attr;
|
||||
::tflite::gpu::Tensor<HWC, DataType::FLOAT32> hwc_tensor;
|
||||
hwc_tensor.shape = HWC(2, 1, 2);
|
||||
hwc_tensor.data = {0.5f, -2.0f, 0.7f, 4.7f};
|
||||
attr.alpha = hwc_tensor;
|
||||
attr.clip = 0.0;
|
||||
|
||||
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 = CreatePReLU(env->GetGpuInfo(), op_def, attr);
|
||||
RETURN_IF_ERROR(env->ExecuteGPUOperation(
|
||||
src_tensor, absl::make_unique<GPUOperation>(std::move(operation)),
|
||||
BHWC(1, 2, 1, 2), &dst_tensor));
|
||||
RETURN_IF_ERROR(
|
||||
PointWiseNear({0.0f, 2.0f, -1.4f, 3.0f}, dst_tensor.data, eps));
|
||||
}
|
||||
}
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
} // namespace gpu
|
||||
} // namespace tflite
|
34
tensorflow/lite/delegates/gpu/common/tasks/prelu_test_util.h
Normal file
34
tensorflow/lite/delegates/gpu/common/tasks/prelu_test_util.h
Normal file
@ -0,0 +1,34 @@
|
||||
/* Copyright 2021 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_PRELU_TEST_UTIL_H_
|
||||
#define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_PRELU_TEST_UTIL_H_
|
||||
|
||||
#include "tensorflow/lite/delegates/gpu/common/status.h"
|
||||
#include "tensorflow/lite/delegates/gpu/common/task/testing_util.h"
|
||||
|
||||
namespace tflite {
|
||||
namespace gpu {
|
||||
|
||||
absl::Status PReLUAlphaTest(TestExecutionEnvironment* env);
|
||||
|
||||
absl::Status PReLUAlphaClipTest(TestExecutionEnvironment* env);
|
||||
|
||||
absl::Status PReLUHWCAlphaTest(TestExecutionEnvironment* env);
|
||||
|
||||
} // namespace gpu
|
||||
} // namespace tflite
|
||||
|
||||
#endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_PRELU_TEST_UTIL_H_
|
@ -459,6 +459,7 @@ objc_library(
|
||||
deps = [
|
||||
":prelu",
|
||||
":test_util",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:prelu_test_util",
|
||||
],
|
||||
)
|
||||
|
||||
@ -903,6 +904,7 @@ objc_library(
|
||||
"//tensorflow/lite/delegates/gpu/common:types",
|
||||
"//tensorflow/lite/delegates/gpu/common:util",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:add_test_util",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:prelu_test_util",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:quantize_and_dequantize_test_util",
|
||||
"//tensorflow/lite/delegates/gpu/common/tasks:relu_test_util",
|
||||
"//tensorflow/lite/delegates/gpu/metal:common",
|
||||
|
@ -27,6 +27,7 @@ limitations under the License.
|
||||
#include "tensorflow/lite/delegates/gpu/common/util.h"
|
||||
#include "tensorflow/lite/delegates/gpu/metal/compute_task_descriptor.h"
|
||||
#include "tensorflow/lite/delegates/gpu/metal/kernels/test_util.h"
|
||||
#include "tensorflow/lite/delegates/gpu/common/tasks/prelu_test_util.h"
|
||||
|
||||
using ::tflite::gpu::BHWC;
|
||||
using ::tflite::gpu::DataType;
|
||||
@ -42,7 +43,10 @@ using ::tflite::gpu::metal::SingleOpModel;
|
||||
@interface PReLUTest : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation PReLUTest
|
||||
@implementation PReLUTest {
|
||||
tflite::gpu::metal::MetalExecutionEnvironment exec_env_;
|
||||
}
|
||||
|
||||
- (void)setUp {
|
||||
[super setUp];
|
||||
}
|
||||
@ -157,4 +161,19 @@ using ::tflite::gpu::metal::SingleOpModel;
|
||||
XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
|
||||
}
|
||||
|
||||
- (void)testPReLUAlpha {
|
||||
auto status = PReLUAlphaTest(&exec_env_);
|
||||
XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
|
||||
}
|
||||
|
||||
- (void)testPReLUAlphaClip {
|
||||
auto status = PReLUAlphaClipTest(&exec_env_);
|
||||
XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
|
||||
}
|
||||
|
||||
- (void)testPReLUHWCAlpha {
|
||||
auto status = PReLUHWCAlphaTest(&exec_env_);
|
||||
XCTAssertTrue(status.ok(), @"%s", std::string(status.message()).c_str());
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
Reference in New Issue
Block a user