1 /* Copyright 2016 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 "tensorflow/core/framework/allocator.h"
17 #include "tensorflow/core/framework/fake_input.h"
18 #include "tensorflow/core/framework/node_def_builder.h"
19 #include "tensorflow/core/framework/op_kernel.h"
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/framework/tensor_testutil.h"
22 #include "tensorflow/core/framework/types.h"
23 #include "tensorflow/core/kernels/ops_testutil.h"
24 #include "tensorflow/core/lib/core/status_test_util.h"
25 #include "tensorflow/core/platform/test.h"
26
27 namespace tensorflow {
28
29 namespace {
30
31 class SparseReduceSumOpTest : public OpsTestBase {
32 protected:
33 template <typename T>
MakeOp()34 void MakeOp() {
35 DataType value_type = tensorflow::DataTypeToEnum<T>::value;
36 TF_ASSERT_OK(NodeDefBuilder("sparse_reduce_sum", "SparseReduceSum")
37 .Input(FakeInput(DT_INT64))
38 .Input(FakeInput(value_type))
39 .Input(FakeInput(DT_INT64))
40 .Input(FakeInput(DT_INT32))
41 .Attr("T", value_type)
42 .Finalize(node_def()));
43 TF_ASSERT_OK(InitOp());
44 }
45 };
46
TEST_F(SparseReduceSumOpTest,SimpleReduce)47 TEST_F(SparseReduceSumOpTest, SimpleReduce) {
48 MakeOp<float>();
49
50 // [ 1]
51 // [2 ]
52 // [3 4]
53
54 const auto indices_shape = TensorShape({4, 2});
55 std::initializer_list<int64> in{0, 1, 1, 0, 2, 0, 2, 1};
56 const gtl::ArraySlice<int64> indices(in);
57 std::initializer_list<int64> sh{3, 2};
58 const gtl::ArraySlice<int64> shape(sh);
59
60 AddInputFromArray<int64>(indices_shape, indices);
61 AddInputFromArray<float>(TensorShape({4}), {1, 2, 3, 4});
62 AddInputFromArray<int64>(TensorShape({2}), shape);
63 AddInputFromArray<int32>(TensorShape({1}), {0}); // reduction axes
64
65 TF_ASSERT_OK(RunOpKernel());
66
67 Tensor expected_by_0(allocator(), DT_FLOAT, TensorShape({2}));
68 test::FillValues<float>(&expected_by_0, {5, 5});
69 test::ExpectTensorEqual<float>(expected_by_0, *GetOutput(0));
70 }
71
72 class SparseReduceSumSparseOpTest : public OpsTestBase {
73 protected:
74 template <typename T>
MakeOp()75 void MakeOp() {
76 DataType value_type = tensorflow::DataTypeToEnum<T>::value;
77 TF_ASSERT_OK(
78 NodeDefBuilder("sparse_reduce_sum_sparse", "SparseReduceSumSparse")
79 .Input(FakeInput(DT_INT64))
80 .Input(FakeInput(value_type))
81 .Input(FakeInput(DT_INT64))
82 .Input(FakeInput(DT_INT32))
83 .Attr("T", value_type)
84 .Finalize(node_def()));
85 TF_ASSERT_OK(InitOp());
86 }
87 };
88
TEST_F(SparseReduceSumSparseOpTest,SimpleReduce)89 TEST_F(SparseReduceSumSparseOpTest, SimpleReduce) {
90 MakeOp<float>();
91
92 // [ 2]
93 // [2 ]
94 // [3 4]
95
96 const auto indices_shape = TensorShape({4, 2});
97 std::initializer_list<int64> in{0, 1, 1, 0, 2, 0, 2, 1};
98 const gtl::ArraySlice<int64> indices(in);
99 std::initializer_list<int64> sh{3, 2};
100 const gtl::ArraySlice<int64> shape(sh);
101
102 AddInputFromArray<int64>(indices_shape, indices);
103 AddInputFromArray<float>(TensorShape({4}), {2, 2, 3, 4});
104 AddInputFromArray<int64>(TensorShape({2}), shape);
105 AddInputFromArray<int32>(TensorShape({1}), {0}); // reduction axes
106
107 TF_ASSERT_OK(RunOpKernel());
108
109 const int expected_nnz = 2;
110 Tensor expected_indices(allocator(), DT_INT64,
111 TensorShape({expected_nnz, 1}));
112 test::FillValues<int64>(&expected_indices, {0, 1});
113 test::ExpectTensorEqual<int64>(expected_indices, *GetOutput(0));
114
115 Tensor expected_values(allocator(), DT_FLOAT, TensorShape({2}));
116 test::FillValues<float>(&expected_values, {5, 6});
117 test::ExpectTensorEqual<float>(expected_values, *GetOutput(1));
118
119 Tensor expected_shape(allocator(), DT_INT64, {1});
120 test::FillValues<int64>(&expected_shape, {2});
121 test::ExpectTensorEqual<int64>(expected_shape, *GetOutput(2));
122 }
123
124 } // namespace
125
126 } // namespace tensorflow
127