1 | /* Copyright 2016 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 | #include "tensorflow/python/util/kernel_registry.h" |
16 | |
17 | #include "tensorflow/core/framework/node_def.pb.h" |
18 | #include "tensorflow/core/framework/node_def_util.h" |
19 | #include "tensorflow/core/framework/op.h" |
20 | #include "tensorflow/core/framework/op_kernel.h" |
21 | #include "tensorflow/core/framework/types.h" |
22 | #include "tensorflow/core/lib/core/status.h" |
23 | #include "tensorflow/core/util/device_name_utils.h" |
24 | |
25 | namespace tensorflow { |
26 | namespace swig { |
27 | |
28 | string TryFindKernelClass(const string& serialized_node_def) { |
29 | tensorflow::NodeDef node_def; |
30 | if (!node_def.ParseFromString(serialized_node_def)) { |
31 | LOG(WARNING) << "Error parsing node_def" ; |
32 | return "" ; |
33 | } |
34 | |
35 | const tensorflow::OpRegistrationData* op_reg_data; |
36 | auto status = |
37 | tensorflow::OpRegistry::Global()->LookUp(node_def.op(), &op_reg_data); |
38 | if (!status.ok()) { |
39 | LOG(WARNING) << "Op " << node_def.op() << " not found: " << status; |
40 | return "" ; |
41 | } |
42 | AddDefaultsToNodeDef(op_reg_data->op_def, &node_def); |
43 | |
44 | tensorflow::DeviceNameUtils::ParsedName parsed_name; |
45 | if (!tensorflow::DeviceNameUtils::ParseFullName(node_def.device(), |
46 | &parsed_name)) { |
47 | LOG(WARNING) << "Failed to parse device from node_def: " |
48 | << node_def.ShortDebugString(); |
49 | return "" ; |
50 | } |
51 | string class_name = "" ; |
52 | status = tensorflow::FindKernelDef( |
53 | tensorflow::DeviceType(parsed_name.type.c_str()), node_def, |
54 | nullptr /* kernel_def */, &class_name); |
55 | if (!status.ok()) { |
56 | LOG(WARNING) << "Op [" << node_def.op() << "]: " << status; |
57 | } |
58 | return class_name; |
59 | } |
60 | |
61 | } // namespace swig |
62 | } // namespace tensorflow |
63 | |