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/core/framework/fake_input.h"
#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/kernels/ops_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"
namespace {
using tensorflow::AllocatorAttributes;
using tensorflow::DT_FLOAT;
using tensorflow::DT_INT32;
using tensorflow::DT_INT64;
using tensorflow::int64;
using tensorflow::int32;
using tensorflow::NodeDefBuilder;
using tensorflow::OpsTestBase;
using tensorflow::Status;
using tensorflow::Tensor;
using tensorflow::TensorShape;
using tensorflow::test::ExpectClose;
using tensorflow::test::FillValues;
using tensorflow::test::AsTensor;
using tensorflow::test::ExpectTensorEqual;
class KPFusedGatherTest : public OpsTestBase {
protected:
void RunValidCase(const TensorShape& data_shape,
const TensorShape& slice_shape,
const std::vector<int32>& begin_val,
const std::vector<int64>& slice_data,
const std::vector<float>& data_data,
const std::vector<int64>& expected_unique,
const std::vector<int32>& expected_indices,
const std::vector<float>& expected_output_data) {
TF_EXPECT_OK(NodeDefBuilder("kp_fused_gather", "KPFusedGather")
.Input(FakeInput(DT_FLOAT))
.Input(FakeInput(DT_INT64))
.Input(FakeInput(DT_INT32))
.Finalize(node_def()));
TF_EXPECT_OK(InitOp());
AddInputFromArray<float>(data_shape, data_data);
AddInputFromArray<int64>(slice_shape, slice_data);
AddInputFromArray<int32>(TensorShape({2}), begin_val);
TF_ASSERT_OK(RunOpKernel());
const Tensor& out_unique = *GetOutput(0);
const Tensor& out_indices = *GetOutput(1);
const Tensor& out_data = *GetOutput(2);
Tensor expected_unique_tensor(
allocator(), DT_INT64,
TensorShape({static_cast<int64>(expected_unique.size())})
);
FillValues<int64>(&expected_unique_tensor, expected_unique);
ExpectTensorEqual<int64>(expected_unique_tensor, out_unique);
Tensor expected_indices_tensor(
allocator(), DT_INT32,
TensorShape({static_cast<int64_t>(expected_indices.size())})
);
FillValues<int32>(&expected_indices_tensor, expected_indices);
ExpectTensorEqual<int32>(expected_indices_tensor, out_indices);
Tensor expected_data_tensor(allocator(), DT_FLOAT,
TensorShape({static_cast<int64>(expected_unique.size()), 12}));
FillValues<float>(&expected_data_tensor, expected_output_data);
ExpectClose(expected_data_tensor, out_data);
}
Status RunOpExpectFailure(const TensorShape& data_shape,
const TensorShape& slice_shape,
const std::vector<int32>& begin_val,
const std::vector<int64>& slice_data,
const std::vector<float>& data_data) {
TF_CHECK_OK(NodeDefBuilder("kp_fused_gather", "KPFusedGather")
.Input(FakeInput(DT_FLOAT))
.Input(FakeInput(DT_INT64))
.Input(FakeInput(DT_INT32))
.Finalize(node_def()));
TF_CHECK_OK(InitOp());
AddInputFromArray<float>(data_shape, data_data);
AddInputFromArray<int64>(slice_shape, slice_data);
AddInputFromArray<int32>(TensorShape({2}), begin_val);
return RunOpKernel();
}
};
TEST_F(KPFusedGatherTest, Valid_NormalInput) {
RunValidCase(
TensorShape({2, 12}),
TensorShape({4, 3}),
{0, 1},
{1, 1, 3,
0, 1, 5,
1, 0, 7,
0, 1, 9},
{1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f,
13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f},
{1, 0},
{0, 0, 1, 0},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
);
}
TEST_F(KPFusedGatherTest, Invalid_DataDimsNot2) {
std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f};
Status s = RunOpExpectFailure(
TensorShape({4}),
TensorShape({2, 2}),
{0, 0},
{0, 1, 2, 3},
data
);
EXPECT_FALSE(s.ok());
EXPECT_TRUE(absl::StrContains(s.message(), "Embedding table shape must be 2D"));
}
TEST_F(KPFusedGatherTest, Invalid_SliceInputDimsNot2) {
std::vector<float> data(2 * 12, 1.0f);
Status s = RunOpExpectFailure(
TensorShape({2, 12}),
TensorShape({4}),
{0, 0},
{0, 1, 2, 3},
data
);
EXPECT_FALSE(s.ok());
EXPECT_TRUE(absl::StrContains(s.message(), "Input key must be 2D"));
}
TEST_F(KPFusedGatherTest, Invalid_BeginColOutOfRange) {
std::vector<float> data(2 * 12, 1.0f);
Status s = RunOpExpectFailure(
TensorShape({2, 12}),
TensorShape({2, 2}),
{0, 2},
{0, 1, 2, 3},
data
);
EXPECT_FALSE(s.ok());
EXPECT_TRUE(absl::StrContains(s.message(),"slice cols out of keys range"));
}
TEST_F(KPFusedGatherTest, Invalid_IndexOutOfRangeInData) {
std::vector<float> data(2 * 12, 1.0f);
Status s = RunOpExpectFailure(
TensorShape({2, 12}),
TensorShape({2, 2}),
{0, 0},
{0, 1,
2, 3},
data
);
EXPECT_FALSE(s.ok());
EXPECT_TRUE(absl::StrContains(s.message(),"idx out of table range"));
}
}