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_supported_ops.h"
17
18 #include <algorithm>
19 #include <iostream>
20 #include <string>
21 #include <vector>
22
23 #include "absl/strings/str_join.h"
24 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
25 #include "tensorflow/core/framework/kernel_def.pb.h"
26 #include "tensorflow/core/framework/types.h"
27 #include "tensorflow/core/framework/types.pb.h"
28 #include "tensorflow/core/platform/init_main.h"
29 #include "tensorflow/core/util/command_line_flags.h"
30
31 namespace tensorflow {
32 namespace tf2xla {
33 namespace {
34
PrintSupportedOps(const string & device,const string & regen_run)35 void PrintSupportedOps(const string& device, const string& regen_run) {
36 XlaOpRegistry::RegisterCompilationKernels();
37
38 std::vector<const KernelDef*> kdefs =
39 XlaOpRegistry::DeviceKernels(device,
40 /*include_compilation_only_kernels=*/true);
41 std::sort(
42 kdefs.begin(), kdefs.end(),
43 [](const KernelDef* a, const KernelDef* b) { return a->op() < b->op(); });
44
45 std::cout << "**Supported operators for device: " << device << "**\n\n"
46 << "Operator | Type Constraint\n"
47 << "-------- | ---------------" << std::endl;
48 for (const KernelDef* kdef : kdefs) {
49 std::vector<string> constraints;
50 for (const KernelDef::AttrConstraint& constraint : kdef->constraint()) {
51 std::vector<string> types;
52 for (int type : constraint.allowed_values().list().type()) {
53 types.push_back(DataTypeString(static_cast<DataType>(type)));
54 }
55 std::sort(types.begin(), types.end());
56 constraints.push_back("`" + constraint.name() + "={" +
57 absl::StrJoin(types, ",") + "}`");
58 }
59 std::cout << "`" << kdef->op() << "` | "
60 << absl::StrJoin(constraints, "<br>") << std::endl;
61 }
62
63 std::cout << "\nTo regenerate this table, run:\n\n```shell\n"
64 << regen_run << " --device=" << device << "\n```" << std::endl;
65 }
66
67 } // namespace
68
SupportedOpsMain(int argc,char ** argv,const char * regen_run)69 void SupportedOpsMain(int argc, char** argv, const char* regen_run) {
70 std::vector<string> device_names = XlaOpRegistry::BackendNames();
71 std::sort(device_names.begin(), device_names.end());
72
73 // Set up and parse flags.
74 string device;
75 std::vector<Flag> flag_list = {
76 {"device", &device,
77 "Name of the compilation device for which to print supported ops, "
78 "one of: " +
79 absl::StrJoin(device_names, ",")},
80 };
81 string usage = Flags::Usage(argv[0], flag_list);
82 bool parsed_flags_ok = Flags::Parse(&argc, argv, flag_list);
83 QCHECK(parsed_flags_ok) << "\n" << usage;
84 QCHECK(XlaOpRegistry::IsBackendRegistered(device))
85 << "\nUnknown device: " << device << "\n"
86 << usage;
87
88 // Run the program.
89 port::InitMain(usage.c_str(), &argc, &argv);
90 QCHECK(argc == 1) << "\nERROR: This command does not take any arguments "
91 "other than flags\n\n"
92 << usage;
93 PrintSupportedOps(device, regen_run);
94 }
95
96 } // namespace tf2xla
97 } // namespace tensorflow
98