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/compiler/tf2xla/tf2xla.h"
17 
18 #include "tensorflow/compiler/tf2xla/tf2xla.pb.h"
19 #include "tensorflow/compiler/xla/client/client_library.h"
20 #include "tensorflow/compiler/xla/client/local_client.h"
21 #include "tensorflow/compiler/xla/literal_util.h"
22 #include "tensorflow/compiler/xla/statusor.h"
23 #include "tensorflow/core/framework/attr_value.pb.h"
24 #include "tensorflow/core/framework/attr_value_util.h"
25 #include "tensorflow/core/framework/graph.pb.h"
26 #include "tensorflow/core/framework/node_def.pb.h"
27 #include "tensorflow/core/lib/core/status.h"
28 #include "tensorflow/core/lib/core/status_test_util.h"
29 #include "tensorflow/core/platform/test.h"
30 
31 namespace tensorflow {
32 namespace {
33 
TypeAttrValue(DataType type)34 AttrValue TypeAttrValue(DataType type) {
35   AttrValue attr_value;
36   SetAttrValue(type, &attr_value);
37   return attr_value;
38 }
39 
SumGraph()40 GraphDef SumGraph() {
41   GraphDef graph_def;
42   NodeDef* x = graph_def.add_node();
43   x->set_name("x");
44   x->set_op("Placeholder");
45   (*x->mutable_attr())["dtype"] = TypeAttrValue(DT_INT32);
46   NodeDef* y = graph_def.add_node();
47   y->set_name("y");
48   y->set_op("Placeholder");
49   (*y->mutable_attr())["dtype"] = TypeAttrValue(DT_INT32);
50   NodeDef* sum = graph_def.add_node();
51   sum->set_name("sum");
52   sum->set_op("Add");
53   sum->add_input("x");
54   sum->add_input("y");
55   (*sum->mutable_attr())["T"] = TypeAttrValue(DT_INT32);
56   return graph_def;
57 }
58 
SumConfig()59 tf2xla::Config SumConfig() {
60   tf2xla::Config config;
61   config.add_feed()->mutable_id()->set_node_name("x");
62   config.add_feed()->mutable_id()->set_node_name("y");
63   config.add_fetch()->mutable_id()->set_node_name("sum");
64   return config;
65 }
66 
TEST(ConvertGraphDefToXla,Sum)67 TEST(ConvertGraphDefToXla, Sum) {
68   GraphDef graph_def = SumGraph();
69   tf2xla::Config config = SumConfig();
70 
71   xla::LocalClient* client = xla::ClientLibrary::LocalClientOrDie();
72   xla::Computation computation;
73   TF_EXPECT_OK(ConvertGraphDefToXla(graph_def, config, client, &computation));
74 
75   // Set up arguments.
76   auto x_literal = xla::Literal::CreateR0<int32>(10);
77   auto y_literal = xla::Literal::CreateR0<int32>(32);
78   auto x_global_or = client->TransferToServer(*x_literal);
79   auto y_global_or = client->TransferToServer(*y_literal);
80   TF_EXPECT_OK(x_global_or.status());
81   TF_EXPECT_OK(y_global_or.status());
82   std::unique_ptr<xla::GlobalData> x_global =
83       std::move(x_global_or.ValueOrDie());
84   std::unique_ptr<xla::GlobalData> y_global =
85       std::move(y_global_or.ValueOrDie());
86 
87   // Execute and check result.
88   auto result_or =
89       client->ExecuteAndTransfer(computation, {x_global.get(), y_global.get()});
90   TF_EXPECT_OK(result_or.status());
91   std::unique_ptr<xla::Literal> result = std::move(result_or.ValueOrDie());
92   EXPECT_EQ("(s32[]) (\n42\n)", result->ToString());
93 }
94 
95 }  // namespace
96 }  // namespace tensorflow
97