• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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/framework/types.pb.h"
24 #include "tensorflow/core/kernels/ops_testutil.h"
25 #include "tensorflow/core/kernels/ops_util.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 #include "tensorflow/core/platform/test.h"
28 
29 namespace tensorflow {
30 namespace {
31 
32 class RangeOpTest : public OpsTestBase {
33  protected:
MakeOp(DataType input_type)34   void MakeOp(DataType input_type) {
35     TF_ASSERT_OK(NodeDefBuilder("myop", "Range")
36                      .Input(FakeInput(input_type))
37                      .Input(FakeInput(input_type))
38                      .Input(FakeInput(input_type))
39                      .Finalize(node_def()));
40     TF_ASSERT_OK(InitOp());
41   }
42 };
43 
44 class LinSpaceOpTest : public OpsTestBase {
45  protected:
MakeOp(DataType input_type,DataType index_type)46   void MakeOp(DataType input_type, DataType index_type) {
47     TF_ASSERT_OK(NodeDefBuilder("myop", "LinSpace")
48                      .Input(FakeInput(input_type))
49                      .Input(FakeInput(input_type))
50                      .Input(FakeInput(index_type))
51                      .Finalize(node_def()));
52     TF_ASSERT_OK(InitOp());
53   }
54 };
55 
TEST_F(RangeOpTest,Simple_D32)56 TEST_F(RangeOpTest, Simple_D32) {
57   MakeOp(DT_INT32);
58 
59   // Feed and run
60   AddInputFromArray<int32>(TensorShape({}), {0});
61   AddInputFromArray<int32>(TensorShape({}), {10});
62   AddInputFromArray<int32>(TensorShape({}), {2});
63   TF_ASSERT_OK(RunOpKernel());
64 
65   // Check the output
66   Tensor expected(allocator(), DT_INT32, TensorShape({5}));
67   test::FillValues<int32>(&expected, {0, 2, 4, 6, 8});
68   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
69 }
70 
TEST_F(RangeOpTest,Simple_Float)71 TEST_F(RangeOpTest, Simple_Float) {
72   MakeOp(DT_FLOAT);
73 
74   // Feed and run
75   AddInputFromArray<float>(TensorShape({}), {0.5});
76   AddInputFromArray<float>(TensorShape({}), {2});
77   AddInputFromArray<float>(TensorShape({}), {0.3});
78   TF_ASSERT_OK(RunOpKernel());
79 
80   // Check the output
81   Tensor expected(allocator(), DT_FLOAT, TensorShape({5}));
82   test::FillValues<float>(&expected, {0.5, 0.8, 1.1, 1.4, 1.7});
83   test::ExpectTensorEqual<float>(expected, *GetOutput(0));
84 }
85 
TEST_F(RangeOpTest,Large_Double)86 TEST_F(RangeOpTest, Large_Double) {
87   MakeOp(DT_DOUBLE);
88 
89   // Feed and run
90   AddInputFromArray<double>(TensorShape({}), {0.0});
91   AddInputFromArray<double>(TensorShape({}), {10000});
92   AddInputFromArray<double>(TensorShape({}), {0.5});
93   TF_ASSERT_OK(RunOpKernel());
94 
95   // Check the output
96   Tensor expected(allocator(), DT_DOUBLE, TensorShape({20000}));
97   std::vector<double> result;
98   for (int32_t i = 0; i < 20000; ++i) result.push_back(i * 0.5);
99   test::FillValues<double>(&expected, gtl::ArraySlice<double>(result));
100   test::ExpectTensorEqual<double>(expected, *GetOutput(0));
101 }
102 
TEST_F(LinSpaceOpTest,Simple_D32)103 TEST_F(LinSpaceOpTest, Simple_D32) {
104   MakeOp(DT_FLOAT, DT_INT32);
105 
106   // Feed and run
107   AddInputFromArray<float>(TensorShape({}), {3.0});
108   AddInputFromArray<float>(TensorShape({}), {7.0});
109   AddInputFromArray<int32>(TensorShape({}), {3});
110   TF_ASSERT_OK(RunOpKernel());
111 
112   // Check the output
113   Tensor expected(allocator(), DT_FLOAT, TensorShape({3}));
114   test::FillValues<float>(&expected, {3.0, 5.0, 7.0});
115   test::ExpectTensorEqual<float>(expected, *GetOutput(0));
116 }
117 
TEST_F(LinSpaceOpTest,Exact_Endpoints)118 TEST_F(LinSpaceOpTest, Exact_Endpoints) {
119   MakeOp(DT_FLOAT, DT_INT32);
120 
121   // Feed and run. The particular values 0., 1., and 42 are chosen to test that
122   // the last value is not calculated via an intermediate delta as (1./41)*41,
123   // because for IEEE 32-bit floats that returns 0.99999994 != 1.0.
124   AddInputFromArray<float>(TensorShape({}), {0.0});
125   AddInputFromArray<float>(TensorShape({}), {1.0});
126   AddInputFromArray<int32>(TensorShape({}), {42});
127   TF_ASSERT_OK(RunOpKernel());
128 
129   // Check the output
130   Tensor output = *GetOutput(0);
131   float expected_start = 0.0;
132   float start = output.flat<float>()(0);
133   EXPECT_EQ(expected_start, start) << expected_start << " vs. " << start;
134   float expected_stop = 1.0;
135   float stop = output.flat<float>()(output.NumElements() - 1);
136   EXPECT_EQ(expected_stop, stop) << expected_stop << " vs. " << stop;
137 }
138 
TEST_F(LinSpaceOpTest,Single_D64)139 TEST_F(LinSpaceOpTest, Single_D64) {
140   MakeOp(DT_FLOAT, DT_INT64);
141 
142   // Feed and run
143   AddInputFromArray<float>(TensorShape({}), {9.0});
144   AddInputFromArray<float>(TensorShape({}), {100.0});
145   AddInputFromArray<int64>(TensorShape({}), {1});
146   TF_ASSERT_OK(RunOpKernel());
147 
148   // Check the output
149   Tensor expected(allocator(), DT_FLOAT, TensorShape({1}));
150   test::FillValues<float>(&expected, {9.0});
151   test::ExpectTensorEqual<float>(expected, *GetOutput(0));
152 }
153 
TEST_F(LinSpaceOpTest,Simple_Double)154 TEST_F(LinSpaceOpTest, Simple_Double) {
155   MakeOp(DT_DOUBLE, DT_INT32);
156 
157   // Feed and run
158   AddInputFromArray<double>(TensorShape({}), {5.0});
159   AddInputFromArray<double>(TensorShape({}), {6.0});
160   AddInputFromArray<int32>(TensorShape({}), {6});
161   TF_ASSERT_OK(RunOpKernel());
162 
163   // Check the output
164   Tensor expected(allocator(), DT_DOUBLE, TensorShape({6}));
165   test::FillValues<double>(&expected, {5.0, 5.2, 5.4, 5.6, 5.8, 6.0});
166   test::ExpectTensorEqual<double>(expected, *GetOutput(0));
167 }
168 
169 }  // namespace
170 }  // namespace tensorflow
171