1 | /* Copyright 2015 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 | // TODO(josh11b): Probably not needed for OpKernel authors, so doesn't |
17 | // need to be as publicly accessible as other files in framework/. |
18 | |
19 | #ifndef TENSORFLOW_CORE_FRAMEWORK_OP_DEF_UTIL_H_ |
20 | #define TENSORFLOW_CORE_FRAMEWORK_OP_DEF_UTIL_H_ |
21 | |
22 | #include <string> |
23 | |
24 | #include "tensorflow/core/framework/api_def.pb.h" |
25 | #include "tensorflow/core/framework/op_def.pb.h" |
26 | #include "tensorflow/core/lib/core/status.h" |
27 | #include "tensorflow/core/lib/core/stringpiece.h" |
28 | #include "tensorflow/core/platform/protobuf.h" |
29 | |
30 | namespace tensorflow { |
31 | |
32 | // Performs a consistency check across the fields of the op_def. |
33 | Status ValidateOpDef(const OpDef& op_def); |
34 | |
35 | // Check if an op is deprecated at the given GraphDef version. If the op is |
36 | // deprecated at a future version, a warning will be logged. |
37 | Status CheckOpDeprecation(const OpDef& op_def, int graph_def_version); |
38 | |
39 | // Validates that attr_value satisfies the type and constraints from attr. |
40 | // REQUIRES: attr has already been validated. |
41 | Status ValidateAttrValue(const AttrValue& attr_value, |
42 | const OpDef::AttrDef& attr); |
43 | |
44 | // The following search through op_def for an attr with the indicated name. |
45 | // Returns nullptr if no such attr is found. |
46 | const OpDef::AttrDef* FindAttr(StringPiece name, const OpDef& op_def); |
47 | OpDef::AttrDef* FindAttrMutable(StringPiece name, OpDef* op_def); |
48 | |
49 | // Searches op_def for input argument with the indicated name. |
50 | // Returns nullptr if no such attr is found. |
51 | const OpDef::ArgDef* FindInputArg(StringPiece name, const OpDef& op_def); |
52 | |
53 | // Searches api_def for input argument with the indicated name. |
54 | // Returns nullptr if no such attr is found. |
55 | const ApiDef::Arg* FindInputArg(StringPiece name, const ApiDef& api_def); |
56 | |
57 | // Produce a human-readable version of an op_def that is more concise |
58 | // than a text-format proto. Excludes descriptions. |
59 | std::string SummarizeOpDef(const OpDef& op_def); |
60 | |
61 | // Returns an error if new_op is not backwards-compatible with (more |
62 | // accepting than) old_op. |
63 | // REQUIRES: old_op and new_op must pass validation. |
64 | Status OpDefCompatible(const OpDef& old_op, const OpDef& new_op); |
65 | |
66 | // Returns an error if any attr in penultimate_op that is not in old_op |
67 | // has a different default value in new_op. In general it is not safe |
68 | // to change the default for an attr that has been added to an op. |
69 | Status OpDefAddedDefaultsUnchanged(const OpDef& old_op, |
70 | const OpDef& penultimate_op, |
71 | const OpDef& new_op); |
72 | |
73 | // Returns an error if the default value for any attr is removed or modified |
74 | // in new_op compared to old_op. Adding new default values is safe, and does |
75 | // not raise an error. |
76 | Status OpDefAttrDefaultsUnchanged(const OpDef& old_op, const OpDef& new_op); |
77 | |
78 | // Remove all docs from *op_def / *op_list. |
79 | void RemoveDescriptionsFromOpDef(OpDef* op_def); |
80 | void RemoveDescriptionsFromOpList(OpList* op_list); |
81 | |
82 | // Remove docs from *op_def but leave explanations of deprecations. |
83 | void RemoveNonDeprecationDescriptionsFromOpDef(OpDef* op_def); |
84 | |
85 | // Returns true if `a1` is equal to `a2`. |
86 | // Equality includes all the fields. |
87 | bool AttrDefEqual(const OpDef::AttrDef& a1, const OpDef::AttrDef& a2); |
88 | |
89 | // Returns hash of `a` that is consistent with AttrDefEqual. |
90 | uint64 AttrDefHash(const OpDef::AttrDef& a); |
91 | |
92 | // Returns true if all AttrDefs in `a1` equal corresponding AttrDefs in |
93 | // `a2`. Correspondence is established by name. |
94 | bool RepeatedAttrDefEqual(const protobuf::RepeatedPtrField<OpDef::AttrDef>& a1, |
95 | const protobuf::RepeatedPtrField<OpDef::AttrDef>& a2); |
96 | |
97 | // Returns hash of `a` that is consistent with RepeatedAttrDefEqual |
98 | uint64 RepeatedAttrDefHash(const protobuf::RepeatedPtrField<OpDef::AttrDef>& a); |
99 | |
100 | // Returns true if `o1` is equal to `o2`. |
101 | // Equality includes all the fields. OpDef.attr field is treated as a set. |
102 | bool OpDefEqual(const OpDef& o1, const OpDef& o2); |
103 | |
104 | // Returns hash of `o` that is consistent with AttrDefEqual. |
105 | uint64 OpDefHash(const OpDef& o); |
106 | |
107 | } // namespace tensorflow |
108 | |
109 | #endif // TENSORFLOW_CORE_FRAMEWORK_OP_DEF_UTIL_H_ |
110 | |