1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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_FRAMEWORK_OP_DEF_UTIL_H_
20#define TENSORFLOW_FRAMEWORK_OP_DEF_UTIL_H_
21
22#include <string>
23#include "tensorflow/core/framework/op_def.pb.h"
24#include "tensorflow/core/lib/core/status.h"
25#include "tensorflow/core/platform/protobuf.h"
26
27namespace tensorflow {
28
29// Performs a consistency check across the fields of the op_def.
30Status ValidateOpDef(const OpDef& op_def);
31
32// Check if an op is deprecated at the given GraphDef version.  If the op is
33// deprecated at a future version, a warning will be logged.
34Status CheckOpDeprecation(const OpDef& op_def, int graph_def_version);
35
36// Validates that attr_value satisfies the type and constraints from attr.
37// REQUIRES: attr has already been validated.
38Status ValidateAttrValue(const AttrValue& attr_value,
39                         const OpDef::AttrDef& attr);
40
41// The following search through op_def for an attr with the indicated name.
42// Returns nullptr if no such attr is found.
43const OpDef::AttrDef* FindAttr(StringPiece name, const OpDef& op_def);
44OpDef::AttrDef* FindAttrMutable(StringPiece name, OpDef* op_def);
45
46// Searches op_def for input argument with the indicated name.
47// Returns nullptr if no such attr is found.
48const OpDef::ArgDef* FindInputArg(StringPiece name, const OpDef& op_def);
49
50// Produce a human-readable version of an op_def that is more concise
51// than a text-format proto.  Excludes descriptions.
52string SummarizeOpDef(const OpDef& op_def);
53
54// Returns an error if new_op is not backwards-compatible with (more
55// accepting than) old_op.
56// REQUIRES: old_op and new_op must pass validation.
57Status OpDefCompatible(const OpDef& old_op, const OpDef& new_op);
58
59// Returns an error if any attr in penultimate_op that is not in old_op
60// has a different default value in new_op.  In general it is not safe
61// to change the default for an attr that has been added to an op.
62Status OpDefAddedDefaultsUnchanged(const OpDef& old_op,
63                                   const OpDef& penultimate_op,
64                                   const OpDef& new_op);
65
66// Returns an error if the default value for any attr is added/removed/modified
67// in new_op compared to old_op.
68Status OpDefAttrDefaultsUnchanged(const OpDef& old_op, const OpDef& new_op);
69
70// Remove all docs from *op_def / *op_list.
71void RemoveDescriptionsFromOpDef(OpDef* op_def);
72void RemoveDescriptionsFromOpList(OpList* op_list);
73
74// Remove docs from *op_def but leave explanations of deprecations.
75void RemoveNonDeprecationDescriptionsFromOpDef(OpDef* op_def);
76
77// Returns true if `a1` is equal to `a2`.
78// Equality includes all the fields.
79bool AttrDefEqual(const OpDef::AttrDef& a1, const OpDef::AttrDef& a2);
80
81// Returns hash of `a` that is consistent with AttrDefEqual.
82uint64 AttrDefHash(const OpDef::AttrDef& a);
83
84// Returns true if all AttrDefs in `a1` equal corresponding AttrDefs in
85// `a2`. Correspondence is established by name.
86bool RepeatedAttrDefEqual(const protobuf::RepeatedPtrField<OpDef::AttrDef>& a1,
87                          const protobuf::RepeatedPtrField<OpDef::AttrDef>& a2);
88
89// Returns hash of `a` that is consistent with RepeatedAttrDefEqual
90uint64 RepeatedAttrDefHash(const protobuf::RepeatedPtrField<OpDef::AttrDef>& a);
91
92// Returns true if `o1` is equal to `o2`.
93// Equality includes all the fields. OpDef.attr field is treated as a set.
94bool OpDefEqual(const OpDef& o1, const OpDef& o2);
95
96// Returns hash of `o` that is consistent with AttrDefEqual.
97uint64 OpDefHash(const OpDef& o);
98
99}  // namespace tensorflow
100
101#endif  // TENSORFLOW_FRAMEWORK_OP_DEF_UTIL_H_
102