1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include <functional> 17 #include <memory> 18 19 #include "tensorflow/core/framework/allocator.h" 20 #include "tensorflow/core/framework/fake_input.h" 21 #include "tensorflow/core/framework/node_def_builder.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/framework/types.h" 25 #include "tensorflow/core/framework/types.pb.h" 26 #include "tensorflow/core/kernels/ops_testutil.h" 27 #include "tensorflow/core/kernels/ops_util.h" 28 #include "tensorflow/core/lib/core/status_test_util.h" 29 #include "tensorflow/core/lib/strings/strcat.h" 30 #include "tensorflow/core/platform/logging.h" 31 #include "tensorflow/core/platform/test.h" 32 33 namespace tensorflow { 34 namespace { 35 36 class DynamicStitchOpTest : public OpsTestBase { 37 protected: 38 void MakeOp(int n, DataType dt) { 39 TF_ASSERT_OK(NodeDefBuilder("myop", "DynamicStitch") 40 .Input(FakeInput(n, DT_INT32)) 41 .Input(FakeInput(n, dt)) 42 .Finalize(node_def())); 43 TF_ASSERT_OK(InitOp()); 44 } 45 }; 46 47 TEST_F(DynamicStitchOpTest, Simple_OneD) { 48 MakeOp(2, DT_FLOAT); 49 50 // Feed and run 51 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 7}); 52 AddInputFromArray<int32>(TensorShape({5}), {1, 6, 2, 3, 5}); 53 AddInputFromArray<float>(TensorShape({3}), {0, 40, 70}); 54 AddInputFromArray<float>(TensorShape({5}), {10, 60, 20, 30, 50}); 55 TF_ASSERT_OK(RunOpKernel()); 56 57 // Check the output. 58 Tensor expected(allocator(), DT_FLOAT, TensorShape({8})); 59 test::FillValues<float>(&expected, {0, 10, 20, 30, 40, 50, 60, 70}); 60 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 61 } 62 63 TEST_F(DynamicStitchOpTest, Simple_TwoD) { 64 MakeOp(3, DT_FLOAT); 65 66 // Feed and run 67 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 7}); 68 AddInputFromArray<int32>(TensorShape({2}), {1, 6}); 69 AddInputFromArray<int32>(TensorShape({3}), {2, 3, 5}); 70 AddInputFromArray<float>(TensorShape({3, 2}), {0, 1, 40, 41, 70, 71}); 71 AddInputFromArray<float>(TensorShape({2, 2}), {10, 11, 60, 61}); 72 AddInputFromArray<float>(TensorShape({3, 2}), {20, 21, 30, 31, 50, 51}); 73 TF_ASSERT_OK(RunOpKernel()); 74 75 // Check the output. 76 Tensor expected(allocator(), DT_FLOAT, TensorShape({8, 2})); 77 test::FillValues<float>(&expected, {0, 1, 10, 11, 20, 21, 30, 31, 40, 41, 50, 78 51, 60, 61, 70, 71}); 79 test::ExpectTensorEqual<float>(expected, *GetOutput(0)); 80 } 81 82 TEST_F(DynamicStitchOpTest, Error_IndicesMultiDimensional) { 83 MakeOp(2, DT_FLOAT); 84 85 // Feed and run 86 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 7}); 87 AddInputFromArray<int32>(TensorShape({1, 5}), {1, 6, 2, 3, 5}); 88 AddInputFromArray<float>(TensorShape({3}), {0, 40, 70}); 89 AddInputFromArray<float>(TensorShape({5}), {10, 60, 20, 30, 50}); 90 Status s = RunOpKernel(); 91 EXPECT_TRUE(StringPiece(s.ToString()) 92 .contains("data[1].shape = [5] does not start with " 93 "indices[1].shape = [1,5]")) 94 << s; 95 } 96 97 TEST_F(DynamicStitchOpTest, Error_DataNumDimsMismatch) { 98 MakeOp(2, DT_FLOAT); 99 100 // Feed and run 101 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 7}); 102 AddInputFromArray<int32>(TensorShape({5}), {1, 6, 2, 3, 5}); 103 AddInputFromArray<float>(TensorShape({3}), {0, 40, 70}); 104 AddInputFromArray<float>(TensorShape({1, 5}), {10, 60, 20, 30, 50}); 105 Status s = RunOpKernel(); 106 EXPECT_TRUE(StringPiece(s.ToString()) 107 .contains("data[1].shape = [1,5] does not start with " 108 "indices[1].shape = [5]")) 109 << s; 110 } 111 112 TEST_F(DynamicStitchOpTest, Error_DataDimSizeMismatch) { 113 MakeOp(2, DT_FLOAT); 114 115 // Feed and run 116 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 5}); 117 AddInputFromArray<int32>(TensorShape({4}), {1, 6, 2, 3}); 118 AddInputFromArray<float>(TensorShape({3, 1}), {0, 40, 70}); 119 AddInputFromArray<float>(TensorShape({4, 2}), 120 {10, 11, 60, 61, 20, 21, 30, 31}); 121 Status s = RunOpKernel(); 122 EXPECT_TRUE(StringPiece(s.ToString()) 123 .contains("Need data[0].shape[1:] = data[1].shape[1:], " 124 "got data[0].shape = [3,1], data[1].shape = [4,2]")) 125 << s; 126 } 127 128 TEST_F(DynamicStitchOpTest, Error_DataAndIndicesSizeMismatch) { 129 MakeOp(2, DT_FLOAT); 130 131 // Feed and run 132 AddInputFromArray<int32>(TensorShape({3}), {0, 4, 7}); 133 AddInputFromArray<int32>(TensorShape({5}), {1, 6, 2, 3, 5}); 134 AddInputFromArray<float>(TensorShape({3}), {0, 40, 70}); 135 AddInputFromArray<float>(TensorShape({4}), {10, 60, 20, 30}); 136 Status s = RunOpKernel(); 137 EXPECT_TRUE( 138 StringPiece(s.ToString()) 139 .contains( 140 "data[1].shape = [4] does not start with indices[1].shape = [5]")) 141 << s; 142 } 143 144 } // namespace 145 } // namespace tensorflow 146