1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
25namespace tensorflow {
26namespace {
27
28using ops::Add;
29using ops::Const;
30using ops::Mul;
31using ops::Placeholder;
32using ops::Sub;
33
34TEST(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
44TEST(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
56TEST(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
72TEST(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
98}  // namespace
99}  // namespace tensorflow
100