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