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 // Class and associated machinery for specifying an Op's OpDef and shape 17 // inference function for Op registration. 18 19 #ifndef TENSORFLOW_CORE_FRAMEWORK_OP_DEF_BUILDER_H_ 20 #define TENSORFLOW_CORE_FRAMEWORK_OP_DEF_BUILDER_H_ 21 22 #include <string> 23 #include <vector> 24 #include "tensorflow/core/framework/op_def.pb.h" 25 #include "tensorflow/core/lib/core/status.h" 26 #include "tensorflow/core/lib/core/stringpiece.h" 27 #include "tensorflow/core/platform/macros.h" 28 29 namespace tensorflow { 30 31 class FunctionDefHelper; 32 33 namespace shape_inference { 34 class InferenceContext; 35 } 36 typedef std::function<Status(shape_inference::InferenceContext* c)> 37 OpShapeInferenceFn; 38 39 struct OpRegistrationData { 40 public: OpRegistrationDataOpRegistrationData41 OpRegistrationData() {} OpRegistrationDataOpRegistrationData42 OpRegistrationData(const OpDef& def) : op_def(def) {} 43 OpRegistrationData(const OpDef& def, const OpShapeInferenceFn& fn, 44 bool is_function = false) op_defOpRegistrationData45 : op_def(def), shape_inference_fn(fn), is_function_op(is_function) {} 46 47 OpDef op_def; 48 OpShapeInferenceFn shape_inference_fn; 49 bool is_function_op = false; 50 }; 51 52 // Builder class passed to the REGISTER_OP() macro. 53 class OpDefBuilder { 54 public: 55 // Constructs an OpDef with just the name field set. 56 explicit OpDefBuilder(string op_name); 57 58 // Adds an attr to this OpDefBuilder (and returns *this). The spec has 59 // format "<name>:<type>" or "<name>:<type>=<default>" 60 // where <name> matches regexp [a-zA-Z][a-zA-Z0-9_]* 61 // (by convention only using capital letters for attrs that can be inferred) 62 // <type> can be: 63 // "string", "int", "float", "bool", "type", "shape", or "tensor" 64 // "numbertype", "realnumbertype", "quantizedtype" 65 // (meaning "type" with a restriction on valid values) 66 // "{int32,int64}" or {realnumbertype,quantizedtype,string}" 67 // (meaning "type" with a restriction containing unions of value types) 68 // "{\"foo\", \"bar\n baz\"}", or "{'foo', 'bar\n baz'}" 69 // (meaning "string" with a restriction on valid values) 70 // "list(string)", ..., "list(tensor)", "list(numbertype)", ... 71 // (meaning lists of the above types) 72 // "int >= 2" (meaning "int" with a restriction on valid values) 73 // "list(string) >= 2", "list(int) >= 2" 74 // (meaning "list(string)" / "list(int)" with length at least 2) 75 // <default>, if included, should use the Proto text format 76 // of <type>. For lists use [a, b, c] format. 77 // 78 // Note that any attr specifying the length of an input or output will 79 // get a default minimum of 1 unless the >= # syntax is used. 80 // 81 // TODO(josh11b): Perhaps support restrictions and defaults as optional 82 // extra arguments to Attr() instead of encoding them in the spec string. 83 // TODO(josh11b): Would like to have better dtype handling for tensor attrs: 84 // * Ability to say the type of an input/output matches the type of 85 // the tensor. 86 // * Ability to restrict the type of the tensor like the existing 87 // restrictions for type attrs. 88 // Perhaps by linking the type of the tensor to a type attr? 89 OpDefBuilder& Attr(string spec); 90 91 // Adds an input or output to this OpDefBuilder (and returns *this). 92 // The spec has form "<name>:<type-expr>" or "<name>:Ref(<type-expr>)" 93 // where <name> matches regexp [a-z][a-z0-9_]* and <type-expr> can be: 94 // * For a single tensor: <type> 95 // * For a sequence of tensors with the same type: <number>*<type> 96 // * For a sequence of tensors with different types: <type-list> 97 // Where: 98 // <type> is either one of "float", "int32", "string", ... 99 // or the name of an attr (see above) with type "type". 100 // <number> is the name of an attr with type "int". 101 // <type-list> is the name of an attr with type "list(type)". 102 // TODO(josh11b): Indicate Ref() via an optional argument instead of 103 // in the spec? 104 // TODO(josh11b): SparseInput() and SparseOutput() matching the Python 105 // handling? 106 OpDefBuilder& Input(string spec); 107 OpDefBuilder& Output(string spec); 108 109 // Turns on the indicated boolean flag in this OpDefBuilder (and 110 // returns *this). 111 OpDefBuilder& SetIsCommutative(); 112 OpDefBuilder& SetIsAggregate(); 113 OpDefBuilder& SetIsStateful(); 114 OpDefBuilder& SetAllowsUninitializedInput(); 115 116 // Deprecate the op at a certain GraphDef version. 117 OpDefBuilder& Deprecated(int version, string explanation); 118 119 // Adds docs to this OpDefBuilder (and returns *this). 120 // Docs have the format: 121 // <1-line summary> 122 // <rest of the description> 123 // <name>: <description of name> 124 // <name>: <description of name> 125 // <if long, indent the description on subsequent lines> 126 // Where <name> is the name of an attr, input, or output. Please 127 // wrap docs at 72 columns so that it may be indented in the 128 // generated output. For tensor inputs or outputs (not attrs), you 129 // may start the description with an "=" (like name:= <description>) 130 // to suppress the automatically-generated type documentation in 131 // generated output. 132 #ifndef TF_LEAN_BINARY 133 OpDefBuilder& Doc(string text); 134 #else Doc(string text)135 OpDefBuilder& Doc(string text) { return *this; } 136 #endif 137 138 // Sets the shape function to be used for shape inference. 139 // 140 // Note that currently (October 2016), python code still requires a 141 // RegisterShape call to invoke this; see call_cpp_shape_fn in 142 // python/framework/common_shapes.py 143 OpDefBuilder& SetShapeFn(Status (*fn)(shape_inference::InferenceContext*)); 144 145 // Sets op_reg_data->op_def to the requested OpDef and 146 // op_reg_data->shape_inference_fn to the requested shape inference function, 147 // or returns an error. 148 // Must be called after all of the above methods. 149 // 150 // Note that OpDefBuilder only reports parsing errors. You should also 151 // call ValidateOpDef() to detect other problems. 152 Status Finalize(OpRegistrationData* op_reg_data) const; 153 154 private: 155 friend class FunctionDefHelper; 156 157 // Adds control output to this OpDefBuilder (and returns *this). 158 // The <name> must be a valid node name (matches regexp 159 // [a-zA-Z][a-zA-Z0-9_]*). Named control output can only exist for functions. 160 OpDefBuilder& ControlOutput(string name); 161 op_def()162 OpDef* op_def() { return &op_reg_data_.op_def; } 163 164 OpRegistrationData op_reg_data_; 165 std::vector<string> attrs_; 166 std::vector<string> inputs_; 167 std::vector<string> outputs_; 168 std::vector<string> control_outputs_; 169 string doc_; 170 std::vector<string> errors_; 171 }; 172 173 } // namespace tensorflow 174 175 #endif // TENSORFLOW_CORE_FRAMEWORK_OP_DEF_BUILDER_H_ 176