/* Copyright 2017 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 #include #include #include "tensorflow/core/common_runtime/device.h" #include "tensorflow/core/common_runtime/device_factory.h" #include "tensorflow/core/framework/allocator.h" #include "tensorflow/core/framework/fake_input.h" #include "tensorflow/core/framework/node_def_builder.h" #include "tensorflow/core/framework/op_kernel.h" #include "tensorflow/core/framework/tensor.h" #include "tensorflow/core/framework/types.h" #include "tensorflow/core/kernels/ops_testutil.h" namespace tensorflow { namespace { class SerializeTensorOpTest : public OpsTestBase { protected: template void MakeOp(const TensorShape& input_shape, std::function functor) { TF_ASSERT_OK(NodeDefBuilder("myop", "SerializeTensor") .Input(FakeInput(DataTypeToEnum::value)) .Finalize(node_def())); TF_ASSERT_OK(InitOp()); AddInput(input_shape, functor); } void ParseSerializedWithNodeDef(const NodeDef& parse_node_def, Tensor* serialized, Tensor* parse_output) { std::unique_ptr device( DeviceFactory::NewDevice("CPU", {}, "/job:a/replica:0/task:0")); gtl::InlinedVector inputs; inputs.push_back({nullptr, serialized}); Status status; std::unique_ptr op(CreateOpKernel(DEVICE_CPU, device.get(), cpu_allocator(), parse_node_def, TF_GRAPH_DEF_VERSION, &status)); TF_EXPECT_OK(status); OpKernelContext::Params params; params.device = device.get(); params.inputs = &inputs; params.frame_iter = FrameAndIter(0, 0); params.op_kernel = op.get(); std::vector attrs; test::SetOutputAttrs(¶ms, &attrs); OpKernelContext ctx(¶ms); op->Compute(&ctx); TF_EXPECT_OK(status); *parse_output = *ctx.mutable_output(0); } template void ParseSerializedOutput(Tensor* serialized, Tensor* parse_output) { NodeDef parse; TF_ASSERT_OK(NodeDefBuilder("parse", "ParseTensor") .Input(FakeInput(DT_STRING)) .Attr("out_type", DataTypeToEnum::value) .Finalize(&parse)); ParseSerializedWithNodeDef(parse, serialized, parse_output); } }; TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_half) { MakeOp(TensorShape({10}), [](int x) -> Eigen::half { return static_cast(x / 10.); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_float) { MakeOp(TensorShape({1, 10}), [](int x) -> float { return static_cast(x / 10.); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_double) { MakeOp(TensorShape({5, 5}), [](int x) -> double { return static_cast(x / 10.); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_int64) { MakeOp(TensorShape({2, 3, 4}), [](int x) -> int64 { return static_cast(x - 10); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_int32) { MakeOp(TensorShape({4, 2}), [](int x) -> int32 { return static_cast(x + 7); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_int16) { MakeOp(TensorShape({8}), [](int x) -> int16 { return static_cast(x + 18); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_int8) { MakeOp(TensorShape({2}), [](int x) -> int8 { return static_cast(x + 8); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_uint16) { MakeOp(TensorShape({1, 3}), [](int x) -> uint16 { return static_cast(x + 2); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_uint8) { MakeOp(TensorShape({2, 1, 1}), [](int x) -> uint8 { return static_cast(x + 1); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_complex64) { MakeOp(TensorShape({}), [](int x) -> complex64 { return complex64{static_cast(x / 8.), static_cast(x / 2.)}; }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_complex128) { MakeOp(TensorShape({3}), [](int x) -> complex128 { return complex128{x / 3., x / 2.}; }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_bool) { MakeOp(TensorShape({1}), [](int x) -> bool { return static_cast(x % 2); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } TEST_F(SerializeTensorOpTest, SerializeTensorOpTest_string) { MakeOp(TensorShape({10}), [](int x) -> string { return std::to_string(x / 10.); }); TF_ASSERT_OK(RunOpKernel()); Tensor parse_output; ParseSerializedOutput(GetOutput(0), &parse_output); test::ExpectTensorEqual(parse_output, GetInput(0)); } } // namespace } // namespace tensorflow