• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cc/ops/const_op.h"
17 #include "tensorflow/core/framework/node_def_util.h"
18 #include "tensorflow/core/framework/tensor_testutil.h"
19 #include "tensorflow/core/lib/core/status_test_util.h"
20 #include "tensorflow/core/platform/test.h"
21 
22 namespace tensorflow {
23 
24 namespace {
25 
26 template <typename T>
ExpectNodeEqual(const Node * n,gtl::ArraySlice<T> values,TensorShape shape)27 void ExpectNodeEqual(const Node* n, gtl::ArraySlice<T> values,
28                      TensorShape shape) {
29   EXPECT_TRUE(n->IsConstant());
30   Tensor tensor;
31   TF_EXPECT_OK(GetNodeAttr(n->attrs(), "value", &tensor));
32   DataType dtype;
33   TF_EXPECT_OK(GetNodeAttr(n->attrs(), "dtype", &dtype));
34   EXPECT_EQ(tensor.dtype(), dtype);
35   test::ExpectTensorEqual<T>(tensor, test::AsTensor(values, shape));
36 }
37 
ExpectTypeAndShape(const Node * n,DataType expected_dtype,TensorShape expected_shape)38 void ExpectTypeAndShape(const Node* n, DataType expected_dtype,
39                         TensorShape expected_shape) {
40   EXPECT_TRUE(n->IsConstant());
41   Tensor tensor;
42   TF_EXPECT_OK(GetNodeAttr(n->attrs(), "value", &tensor));
43   DataType dtype;
44   TF_EXPECT_OK(GetNodeAttr(n->attrs(), "dtype", &dtype));
45   EXPECT_EQ(dtype, expected_dtype);
46   EXPECT_EQ(expected_shape, TensorShape(tensor.shape()));
47 }
48 
49 }  // namespace
50 
TEST(ConstOpTest,Basic)51 TEST(ConstOpTest, Basic) {
52   Scope root = Scope::NewRootScope();
53   auto c = ops::Const(root, 42.0f);
54   TF_EXPECT_OK(root.status());
55   EXPECT_EQ(c.op().output_type(0), DT_FLOAT);
56   ExpectNodeEqual<float>(c.node(), {42.0f}, {});
57 }
58 
TEST(ConstOpTest,MultiDim)59 TEST(ConstOpTest, MultiDim) {
60   Scope root = Scope::NewRootScope();
61   auto c = ops::Const(root, {{2.0}, {3.0}});
62   TF_CHECK_OK(root.status());
63   EXPECT_EQ(c.op().output_type(0), DT_DOUBLE);
64   ExpectNodeEqual<double>(c.node(), {2.0, 3.0}, {2, 1});
65 }
66 
TEST(ConstOpTest,Empty)67 TEST(ConstOpTest, Empty) {
68   Scope root = Scope::NewRootScope();
69 
70   auto c1 = ops::Const(root, {});
71   TF_CHECK_OK(root.status());
72   ExpectTypeAndShape(c1.node(), DT_FLOAT, {0});
73 
74   auto c2 = ops::Const(root, {{}});
75   TF_CHECK_OK(root.status());
76   ExpectTypeAndShape(c2.node(), DT_FLOAT, {1, 0});
77 
78   auto c3 = ops::Const(root, {{{}, {}}});
79   TF_CHECK_OK(root.status());
80   ExpectTypeAndShape(c3.node(), DT_FLOAT, {1, 2, 0});
81 
82   auto c4 = ops::Const<int>(root, {{{}}});
83   TF_CHECK_OK(root.status());
84   ExpectTypeAndShape(c4.node(), DT_INT32, {1, 1, 0});
85 
86   ops::Const(root, {{}, {{}}});
87   EXPECT_FALSE(root.status().ok());
88 }
89 
TEST(ConstOpTest,WithExplicitShape)90 TEST(ConstOpTest, WithExplicitShape) {
91   Scope root = Scope::NewRootScope();
92   auto c = ops::Const(root, 42.0, {2, 2});
93   TF_CHECK_OK(root.status());
94   EXPECT_EQ(c.op().output_type(0), DT_DOUBLE);
95   ExpectNodeEqual<double>(c.node(), {42.0, 42.0, 42.0, 42.0}, {2, 2});
96 
97   auto d = ops::Const(root, {"1", "2", "3", "4", "5", "6"}, {2, 3});
98   TF_CHECK_OK(root.status());
99   EXPECT_EQ(d.op().output_type(0), DT_STRING);
100   ExpectNodeEqual<string>(d.node(), {"1", "2", "3", "4", "5", "6"}, {2, 3});
101 }
102 
TEST(ConstOpTest,FromProto)103 TEST(ConstOpTest, FromProto) {
104   Scope root = Scope::NewRootScope();
105   TensorProto proto;
106   proto.set_dtype(DT_DOUBLE);
107   TensorShape({2, 2}).AsProto(proto.mutable_tensor_shape());
108   for (int i = 0; i < 4; ++i) {
109     proto.add_double_val(static_cast<double>(i));
110   }
111   auto c = ops::ConstFromProto(root, proto);
112   TF_CHECK_OK(root.status());
113   EXPECT_EQ(c.op().output_type(0), DT_DOUBLE);
114   ExpectNodeEqual<double>(c.node(), {0.0, 1.0, 2.0, 3.0}, {2, 2});
115 }
116 
TEST(ConstOpTest,InvalidInitializer)117 TEST(ConstOpTest, InvalidInitializer) {
118   Scope root = Scope::NewRootScope();
119   ops::Const(root, {{2.0}, {"df"}});
120   EXPECT_FALSE(root.status().ok());
121 }
122 
TEST(ConstOpTest,Names)123 TEST(ConstOpTest, Names) {
124   Scope root = Scope::NewRootScope();
125   auto c = ops::Const(root, {{2.0}, {3.0}});
126   EXPECT_EQ(c.node()->name(), "Const");
127   auto c_1 = ops::Const(root, {{2.0}, {3.0}});
128   EXPECT_EQ(c_1.node()->name(), "Const_1");
129 
130   auto x = ops::Const(root.WithOpName("x"), 1);
131   EXPECT_EQ(x.node()->name(), "x");
132   auto x_1 = ops::Const(root.WithOpName("x"), 1);
133   EXPECT_EQ(x_1.node()->name(), "x_1");
134 
135   Scope child = root.NewSubScope("c");
136   auto c_y = ops::Const(child.WithOpName("y"), 1);
137   EXPECT_EQ(c_y.node()->name(), "c/y");
138   auto c_y_1 = ops::Const(child.WithOpName("y"), 1);
139   EXPECT_EQ(c_y_1.node()->name(), "c/y_1");
140 }
141 
TEST(ConstOpTest,TemplatedConst)142 TEST(ConstOpTest, TemplatedConst) {
143   Scope root = Scope::NewRootScope();
144   auto c1 = ops::Const<int>(root, {1, 2});
145   ExpectTypeAndShape(c1.node(), DT_INT32, {2});
146 
147   auto c2 = ops::Const<string>(root, {{"this"}, {"is"}, {"a"}, {"constant"}});
148   ExpectTypeAndShape(c2.node(), DT_STRING, {4, 1});
149 }
150 
151 }  // namespace tensorflow
152