/* Copyright 2015 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/core/framework/tensor_util.h" #include #include "tensorflow/core/framework/tensor.h" #include "tensorflow/core/framework/types.h" #include "tensorflow/core/lib/core/status_test_util.h" #include "tensorflow/core/platform/test.h" namespace tensorflow { namespace { TEST(TensorUtil, DeepCopy0d) { Tensor x(DT_FLOAT, TensorShape({})); x.scalar()() = 10.0; // Make y a deep copy of x and then change it. Tensor y = tensor::DeepCopy(x); y.scalar()() = 20.0; // x doesn't change EXPECT_EQ(10.0, x.scalar()()); // Change x. x.scalar()() = 30.0; // Y doesn't change. EXPECT_EQ(20.0, y.scalar()()); Tensor z = tensor::DeepCopy(y); // Change y. y.scalar()() = 40.0; // The final states should all be different. EXPECT_EQ(20.0, z.scalar()()); EXPECT_EQ(30.0, x.scalar()()); EXPECT_EQ(40.0, y.scalar()()); // Should have the same shape and type. EXPECT_EQ(TensorShape({}), x.shape()); EXPECT_EQ(TensorShape({}), y.shape()); EXPECT_EQ(TensorShape({}), z.shape()); EXPECT_EQ(DT_FLOAT, x.dtype()); EXPECT_EQ(DT_FLOAT, y.dtype()); EXPECT_EQ(DT_FLOAT, z.dtype()); } TEST(TensorUtil, DeepCopyZeroElements) { Tensor x; Tensor y = tensor::DeepCopy(x); EXPECT_EQ(TensorShape({0}), y.shape()); EXPECT_EQ(DT_FLOAT, y.dtype()); EXPECT_EQ(0, y.NumElements()); } TEST(TensorUtil, DeepCopy) { Tensor x(DT_FLOAT, TensorShape({1})); x.flat()(0) = 10.0; // Make y a deep copy of x and then change it. Tensor y = tensor::DeepCopy(x); y.flat()(0) = 20.0; // x doesn't change EXPECT_EQ(10.0, x.flat()(0)); // Change x. x.flat()(0) = 30.0; // Y doesn't change. EXPECT_EQ(20.0, y.flat()(0)); Tensor z = tensor::DeepCopy(y); // Change y. y.flat()(0) = 40.0; // The final states should all be different. EXPECT_EQ(20.0, z.flat()(0)); EXPECT_EQ(30.0, x.flat()(0)); EXPECT_EQ(40.0, y.flat()(0)); // Should have the same shape and type. EXPECT_EQ(TensorShape({1}), x.shape()); EXPECT_EQ(TensorShape({1}), y.shape()); EXPECT_EQ(TensorShape({1}), z.shape()); EXPECT_EQ(DT_FLOAT, x.dtype()); EXPECT_EQ(DT_FLOAT, y.dtype()); EXPECT_EQ(DT_FLOAT, z.dtype()); // Test string deep copy Tensor str1(DT_STRING, TensorShape({2})); str1.flat()(0) = "foo1"; str1.flat()(1) = "foo2"; Tensor str2 = tensor::DeepCopy(str1); str2.flat()(0) = "bar1"; str2.flat()(1) = "bar2"; EXPECT_NE(str2.flat()(0), str1.flat()(0)); } TEST(TensorUtil, DeepCopySlice) { Tensor x(DT_INT32, TensorShape({10})); x.flat().setConstant(1); // Slice 'x' -- y still refers to the same buffer. Tensor y = x.Slice(2, 6); // Do a deep copy of y, which is a slice. Tensor z = tensor::DeepCopy(y); // Set x to be different. x.flat().setConstant(2); EXPECT_EQ(TensorShape({10}), x.shape()); EXPECT_EQ(TensorShape({4}), y.shape()); EXPECT_EQ(TensorShape({4}), z.shape()); EXPECT_EQ(DT_INT32, x.dtype()); EXPECT_EQ(DT_INT32, y.dtype()); EXPECT_EQ(DT_INT32, z.dtype()); // x and y should now all be '2', but z should be '1'. for (int i = 0; i < 10; ++i) { EXPECT_EQ(2, x.flat()(i)); } for (int i = 0; i < 4; ++i) { EXPECT_EQ(2, y.unaligned_flat()(i)); EXPECT_EQ(1, z.flat()(i)); } } TEST(TensorUtil, Concat) { std::vector sizes = {1, 4, 5}; std::vector to_concat; int64 total_size = 0; int offset = 0; for (size_t entry = 0; entry < sizes.size(); ++entry) { const int64 size = sizes[entry]; Tensor tensor(DT_INT32, TensorShape({size, 2})); for (int i = offset; i < offset + size; ++i) { for (int j = 0; j < 2; ++j) { tensor.matrix()(i - offset, j) = 2 * i + j; } } to_concat.push_back(tensor); total_size += size; offset += size; } Tensor concated; TF_ASSERT_OK(tensor::Concat(to_concat, &concated)); ASSERT_EQ(TensorShape({total_size, 2}), concated.shape()); for (int i = 0; i < total_size; ++i) { for (int j = 0; j < 2; ++j) { EXPECT_EQ(2 * i + j, concated.matrix()(i, j)); } } } TEST(TensorUtil, Split) { Tensor to_split(DT_INT64, TensorShape({10, 2})); for (int i = 0; i < 10; ++i) { for (int j = 0; j < 2; ++j) { to_split.matrix()(i, j) = 2 * i + j; } } std::vector sizes = {1, 4, 5}; std::vector splits; TF_ASSERT_OK(tensor::Split(to_split, sizes, &splits)); ASSERT_EQ(sizes.size(), splits.size()); int offset = 0; for (size_t entry = 0; entry < splits.size(); ++entry) { const int64 size = sizes[entry]; const Tensor& split = splits[entry]; ASSERT_EQ(TensorShape({size, 2}), split.shape()); for (int i = offset; i < offset + size; ++i) { for (int j = 0; j < 2; ++j) { EXPECT_EQ(2 * i + j, split.matrix()(i - offset, j)); } } offset += size; } } TEST(TensorUtil, ConcatSplitStrings) { Tensor x(DT_STRING, TensorShape({4, 3})); for (int i = 0; i < 4 * 3; ++i) { x.flat()(i) = strings::StrCat("foo_", i); } std::vector split; TF_ASSERT_OK(tensor::Split(x, {2, 1, 1}, &split)); Tensor x_round_tripped; TF_ASSERT_OK(tensor::Concat(split, &x_round_tripped)); ASSERT_EQ(x.shape(), x_round_tripped.shape()); for (int i = 0; i < 4 * 3; ++i) { EXPECT_EQ(x.flat()(i), x_round_tripped.flat()(i)); } // Ensure that no memory is being shared between 'x' and 'x_round_tripped'. for (int i = 0; i < 4 * 3; ++i) { x_round_tripped.flat()(i) = strings::StrCat("bar_", i); } for (int i = 0; i < 4 * 3; ++i) { EXPECT_NE(x.flat()(i), x_round_tripped.flat()(i)); } } TEST(TensorProtoUtil, CreatesStringTensorProto) { std::vector values{"a", "b", "c"}; std::vector shape{1, 3}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_STRING\n" "tensor_shape {\n" " dim {\n" " size: 1\n" " }\n" " dim {\n" " size: 3\n" " }\n" "}\n" "string_val: \"a\"\n" "string_val: \"b\"\n" "string_val: \"c\"\n"); } TEST(TensorProtoUtil, CreatesInt32TensorProto) { std::vector values{1, 2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_INT32\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "int_val: 1\n" "int_val: 2\n"); } TEST(TensorProtoUtil, CreatesInt64TensorProto) { std::vector values{1, 2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_INT64\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "int64_val: 1\n" "int64_val: 2\n"); } TEST(TensorProtoUtil, CreatesUInt32TensorProto) { std::vector values{1, 2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_UINT32\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "uint32_val: 1\n" "uint32_val: 2\n"); } TEST(TensorProtoUtil, CreatesUInt64TensorProto) { std::vector values{1, 2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_UINT64\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "uint64_val: 1\n" "uint64_val: 2\n"); } TEST(TensorProtoUtil, CreatesFloatTensorProto) { std::vector values{1.1, 2.2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_FLOAT\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "float_val: 1.1\n" "float_val: 2.2\n"); } TEST(TensorProtoUtil, CreatesDoubleTensorProto) { std::vector values{1.1, 2.2}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_DOUBLE\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "double_val: 1.1\n" "double_val: 2.2\n"); } TEST(TensorProtoUtil, CreatesBoolTensorProto) { std::vector values{true, false}; std::vector shape{2}; auto proto = tensor::CreateTensorProto(values, shape); EXPECT_EQ(proto.DebugString(), "dtype: DT_BOOL\n" "tensor_shape {\n" " dim {\n" " size: 2\n" " }\n" "}\n" "bool_val: true\n" "bool_val: false\n"); } } // namespace } // namespace tensorflow