• 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 <vector>
17 
18 #include "tensorflow/cc/client/client_session.h"
19 #include "tensorflow/cc/ops/standard_ops.h"
20 #include "tensorflow/core/framework/tensor_testutil.h"
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/lib/core/threadpool.h"
23 #include "tensorflow/core/platform/test.h"
24 
25 namespace tensorflow {
26 namespace {
27 
28 using ops::Add;
29 using ops::Const;
30 using ops::Mul;
31 using ops::Placeholder;
32 using ops::Sub;
33 
TEST(ClientSessionTest,Basic)34 TEST(ClientSessionTest, Basic) {
35   Scope root = Scope::NewRootScope();
36   auto c = Const(root, {{1, 1}});
37   ClientSession session(root);
38   std::vector<Tensor> outputs;
39 
40   TF_EXPECT_OK(session.Run({c}, &outputs));
41   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({1, 1}, {1, 2}));
42 }
43 
TEST(ClientSessionTest,Feed)44 TEST(ClientSessionTest, Feed) {
45   Scope root = Scope::NewRootScope();
46   auto a = Placeholder(root, DT_INT32);
47   auto b = Placeholder(root, DT_INT32);
48   auto c = Add(root, a, b);
49   ClientSession session(root);
50   std::vector<Tensor> outputs;
51 
52   TF_EXPECT_OK(session.Run({{a, 1}, {b, 41}}, {c}, &outputs));
53   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({42}, {}));
54 }
55 
TEST(ClientSessionTest,Extend)56 TEST(ClientSessionTest, Extend) {
57   Scope root = Scope::NewRootScope();
58   auto a = Placeholder(root, DT_INT32, Placeholder::Shape({2}));
59   auto c = Add(root, a, {2, 2});
60   ClientSession session(root);
61   std::vector<Tensor> outputs;
62 
63   TF_EXPECT_OK(session.Run({{a, {1, 1}}}, {c}, &outputs));
64   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({3, 3}, {2}));
65 
66   auto d = Add(root, c, {39, 39});
67   outputs.clear();
68   TF_EXPECT_OK(session.Run({{a, {-10, 1}}}, {d}, &outputs));
69   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({31, 42}, {2}));
70 }
71 
TEST(ClientSessionTest,MultiThreaded)72 TEST(ClientSessionTest, MultiThreaded) {
73   Scope root = Scope::NewRootScope();
74   auto a = Add(root, {1, 2}, {3, 4});
75   auto b = Mul(root, {1, 2}, {3, 4});
76   ClientSession session(root);
77   {
78     thread::ThreadPool thread_pool(Env::Default(), "pool", 2);
79     thread_pool.Schedule([&session, a]() {
80       std::vector<Tensor> outputs;
81       TF_EXPECT_OK(session.Run({a}, &outputs));
82       test::ExpectTensorEqual<int>(outputs[0],
83                                    test::AsTensor<int>({4, 6}, {2}));
84     });
85     thread_pool.Schedule([&session, b]() {
86       std::vector<Tensor> outputs;
87       TF_EXPECT_OK(session.Run({b}, &outputs));
88       test::ExpectTensorEqual<int>(outputs[0],
89                                    test::AsTensor<int>({3, 8}, {2}));
90     });
91   }
92   auto c = Sub(root, b, a);
93   std::vector<Tensor> outputs;
94   TF_EXPECT_OK(session.Run({c}, &outputs));
95   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({-1, 2}, {2}));
96 }
97 
TEST(ClientSessionTest,Callable)98 TEST(ClientSessionTest, Callable) {
99   Scope root = Scope::NewRootScope();
100   auto a = Placeholder(root, DT_INT32);
101   auto b = Placeholder(root, DT_INT32);
102   auto c = Add(root, a, b);
103   ClientSession session(root);
104   std::vector<Tensor> outputs;
105 
106   CallableOptions options;
107   options.add_feed(a.node()->name());
108   options.add_feed(b.node()->name());
109   options.add_fetch(c.node()->name());
110   ClientSession::CallableHandle callable;
111   TF_CHECK_OK(session.MakeCallable(options, &callable));
112   TF_EXPECT_OK(session.RunCallable(
113       callable, {test::AsTensor<int>({1}, {}), test::AsTensor<int>({41}, {})},
114       &outputs, nullptr));
115   test::ExpectTensorEqual<int>(outputs[0], test::AsTensor<int>({42}, {}));
116   TF_EXPECT_OK(session.ReleaseCallable(callable));
117 }
118 
119 }  // namespace
120 }  // namespace tensorflow
121