1 /* Copyright 2020 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/jit/force_xla_constants_on_host_pass.h"
17
18 #include "absl/strings/match.h"
19 #include "absl/strings/str_cat.h"
20 #include "tensorflow/cc/framework/ops.h"
21 #include "tensorflow/cc/ops/functional_ops.h"
22 #include "tensorflow/cc/ops/standard_ops.h"
23 #include "tensorflow/compiler/jit/compilability_check_util.h"
24 #include "tensorflow/compiler/jit/defs.h"
25 #include "tensorflow/compiler/jit/test_util.h"
26 #include "tensorflow/core/common_runtime/function.h"
27 #include "tensorflow/core/common_runtime/graph_constructor.h"
28 #include "tensorflow/core/framework/function_testlib.h"
29 #include "tensorflow/core/framework/node_def_util.h"
30 #include "tensorflow/core/graph/graph_def_builder.h"
31 #include "tensorflow/core/lib/core/errors.h"
32 #include "tensorflow/core/lib/core/status_test_util.h"
33 #include "tensorflow/core/platform/test.h"
34 #include "tensorflow/core/public/session_options.h"
35 #include "tensorflow/core/public/version.h"
36
37 namespace tensorflow {
38 namespace {
39
ForceXlaConstantsOnHost(const Scope & s,FunctionLibraryDefinition * flib_def,std::unique_ptr<Graph> * result)40 Status ForceXlaConstantsOnHost(const Scope& s,
41 FunctionLibraryDefinition* flib_def,
42 std::unique_ptr<Graph>* result) {
43 auto graph = std::make_unique<Graph>(OpRegistry::Global());
44 GraphOptimizationPassOptions options;
45 SessionOptions session_options;
46 session_options.env = Env::Default();
47 options.graph = &graph;
48 options.session_options = &session_options;
49 options.flib_def = flib_def;
50 TF_RETURN_IF_ERROR(s.ToGraph(graph.get()));
51 ForceXlaConstantsOnHostPass rewriter;
52 TF_RETURN_IF_ERROR(rewriter.Run(options));
53 *result = std::move(graph);
54 return OkStatus();
55 }
56
TEST(ForceXlaConstantsOnHostPassTest,Simple)57 TEST(ForceXlaConstantsOnHostPassTest, Simple) {
58 GraphDefBuilder b(GraphDefBuilder::kFailImmediately);
59 Scope root = Scope::NewRootScope().ExitOnError();
60 FunctionDefLibrary library;
61
62 FunctionDef called_func =
63 FunctionDefHelper::Create("TransposeCall",
64 /*in_def=*/{"a:float", "b:int32"},
65 /*out_def=*/{"c:float"}, {},
66 {{{"t0"},
67 "Transpose",
68 {"a", "b"},
69 {
70 {"T", DT_FLOAT},
71 {"Tperm", DT_INT32},
72 }}},
73 {{"c", "t0:y:0"}});
74
75 AttrValue true_attribute;
76 true_attribute.set_b(true);
77 (*called_func.mutable_attr())[kXlaMustCompileAttr] = true_attribute;
78 *library.add_function() = called_func;
79 TF_ASSERT_OK(root.graph()->AddFunctionLibrary(library));
80 FunctionLibraryDefinition flib_def(OpRegistry::Global(), library);
81 Output in = ops::Placeholder(root, DT_FLOAT);
82 Output perm = ops::Const(root, {3, 1, 2, 0});
83
84 NameAttrList b_name_attr;
85 b_name_attr.set_name("TransposeCall");
86 ops::PartitionedCall call(root.WithOpName("call"), {in, perm}, {DT_FLOAT},
87 b_name_attr);
88 call.output.front().node()->AddAttr(kXlaMustCompileAttr, true);
89
90 std::unique_ptr<Graph> graph;
91 TF_ASSERT_OK(ForceXlaConstantsOnHost(root, &flib_def, &graph));
92
93 bool found = false;
94 for (Node* node : graph->nodes()) {
95 if (CanCreateXlaKernel(node->def())) {
96 EXPECT_FALSE(found);
97 found = true;
98 std::vector<int32> hostmem_attr;
99 EXPECT_TRUE(TryGetNodeAttr(node->def(), "_input_hostmem", &hostmem_attr));
100 EXPECT_EQ(hostmem_attr.size(), 1);
101 EXPECT_EQ(hostmem_attr[0], 1);
102 }
103 }
104 EXPECT_TRUE(found);
105 }
106
107 } // namespace
108 } // namespace tensorflow
109