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