1 /* Copyright 2018 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 #ifndef TENSORFLOW_JAVA_SRC_GEN_CC_OP_SPECS_H_ 17 #define TENSORFLOW_JAVA_SRC_GEN_CC_OP_SPECS_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "tensorflow/core/framework/api_def.pb.h" 23 #include "tensorflow/core/framework/attr_value.pb.h" 24 #include "tensorflow/core/framework/op_def.pb.h" 25 #include "tensorflow/java/src/gen/cc/java_defs.h" 26 27 namespace tensorflow { 28 namespace java { 29 30 constexpr const char kDefaultEndpointPackage[] = "core"; 31 32 class EndpointSpec { 33 public: 34 // A specification for an operation endpoint 35 // 36 // package: package of this endpoint (from which also derives its package) 37 // name: name of this endpoint class 38 // javadoc: the endpoint class documentation 39 // TODO(annarev): hardcode deprecated to false until deprecated is possible EndpointSpec(const string & package,const string & name,const Javadoc & javadoc)40 EndpointSpec(const string& package, const string& name, 41 const Javadoc& javadoc) 42 : package_(package), name_(name), javadoc_(javadoc), deprecated_(false) {} 43 package()44 const string& package() const { return package_; } name()45 const string& name() const { return name_; } javadoc()46 const Javadoc& javadoc() const { return javadoc_; } deprecated()47 bool deprecated() const { return deprecated_; } 48 49 private: 50 const string package_; 51 const string name_; 52 const Javadoc javadoc_; 53 const bool deprecated_; 54 }; 55 56 class ArgumentSpec { 57 public: 58 // A specification for an operation argument 59 // 60 // op_def_name: argument name, as known by TensorFlow core 61 // var: a variable to represent this argument in Java 62 // type: the tensor type of this argument 63 // description: a description of this argument, in javadoc 64 // iterable: true if this argument is a list ArgumentSpec(const string & op_def_name,const Variable & var,const Type & type,const string & description,bool iterable)65 ArgumentSpec(const string& op_def_name, const Variable& var, const Type& type, 66 const string& description, bool iterable) 67 : op_def_name_(op_def_name), 68 var_(var), 69 type_(type), 70 description_(description), 71 iterable_(iterable) {} 72 op_def_name()73 const string& op_def_name() const { return op_def_name_; } var()74 const Variable& var() const { return var_; } type()75 const Type& type() const { return type_; } description()76 const string& description() const { return description_; } iterable()77 bool iterable() const { return iterable_; } 78 79 private: 80 const string op_def_name_; 81 const Variable var_; 82 const Type type_; 83 const string description_; 84 const bool iterable_; 85 }; 86 87 class AttributeSpec { 88 public: 89 // A specification for an operation attribute 90 // 91 // op_def_name: attribute name, as known by TensorFlow core 92 // var: a variable to represent this attribute in Java 93 // type: the type of this attribute 94 // jni_type: the type of this attribute in JNI layer (see OperationBuilder) 95 // description: a description of this attribute, in javadoc 96 // iterable: true if this attribute is a list 97 // default_value: default value for this attribute or nullptr if none. Any 98 // value referenced by this pointer must outlive the lifetime 99 // of the AttributeSpec. This is guaranteed if the value is 100 // issued by an OpDef of the global OpRegistry. AttributeSpec(const string & op_def_name,const Variable & var,const Type & type,const Type & jni_type,const string & description,bool iterable,const AttrValue * default_value)101 AttributeSpec(const string& op_def_name, const Variable& var, 102 const Type& type, const Type& jni_type, 103 const string& description, bool iterable, 104 const AttrValue* default_value) 105 : op_def_name_(op_def_name), 106 var_(var), 107 type_(type), 108 description_(description), 109 iterable_(iterable), 110 jni_type_(jni_type), 111 default_value_(default_value) {} 112 op_def_name()113 const string& op_def_name() const { return op_def_name_; } var()114 const Variable& var() const { return var_; } type()115 const Type& type() const { return type_; } description()116 const string& description() const { return description_; } iterable()117 bool iterable() const { return iterable_; } jni_type()118 const Type& jni_type() const { return jni_type_; } has_default_value()119 bool has_default_value() const { return default_value_ != nullptr; } default_value()120 const AttrValue* default_value() const { return default_value_; } 121 122 private: 123 const string op_def_name_; 124 const Variable var_; 125 const Type type_; 126 const string description_; 127 const bool iterable_; 128 const Type jni_type_; 129 const AttrValue* default_value_; 130 }; 131 132 class OpSpec { 133 public: 134 // Parses an op definition and its API to produce a specification used for 135 // rendering its Java wrapper 136 // 137 // op_def: Op definition 138 // api_def: Op API definition 139 static OpSpec Create(const OpDef& op_def, const ApiDef& api_def); 140 graph_op_name()141 const string& graph_op_name() const { return graph_op_name_; } hidden()142 bool hidden() const { return hidden_; } deprecation_explanation()143 const string& deprecation_explanation() const { 144 return deprecation_explanation_; 145 } endpoints()146 const std::vector<EndpointSpec> endpoints() const { return endpoints_; } inputs()147 const std::vector<ArgumentSpec>& inputs() const { return inputs_; } outputs()148 const std::vector<ArgumentSpec>& outputs() const { return outputs_; } attributes()149 const std::vector<AttributeSpec>& attributes() const { return attributes_; } optional_attributes()150 const std::vector<AttributeSpec>& optional_attributes() const { 151 return optional_attributes_; 152 } 153 154 private: 155 // A specification for an operation 156 // 157 // graph_op_name: name of this op, as known by TensorFlow core engine 158 // hidden: true if this op should not be visible through the Graph Ops API 159 // deprecation_explanation: message to show if all endpoints are deprecated OpSpec(const string & graph_op_name,bool hidden,const string & deprecation_explanation)160 explicit OpSpec(const string& graph_op_name, bool hidden, 161 const string& deprecation_explanation) 162 : graph_op_name_(graph_op_name), 163 hidden_(hidden), 164 deprecation_explanation_(deprecation_explanation) {} 165 166 const string graph_op_name_; 167 const bool hidden_; 168 const string deprecation_explanation_; 169 std::vector<EndpointSpec> endpoints_; 170 std::vector<ArgumentSpec> inputs_; 171 std::vector<ArgumentSpec> outputs_; 172 std::vector<AttributeSpec> attributes_; 173 std::vector<AttributeSpec> optional_attributes_; 174 }; 175 176 } // namespace java 177 } // namespace tensorflow 178 179 #endif // TENSORFLOW_JAVA_SRC_GEN_CC_OP_SPECS_H_ 180