1 /* Copyright 2017 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_PYTHON_FRAMEWORK_PYTHON_OP_GEN_INTERNAL_H_ 17 #define TENSORFLOW_PYTHON_FRAMEWORK_PYTHON_OP_GEN_INTERNAL_H_ 18 19 #include <unordered_map> 20 21 #include "tensorflow/core/framework/api_def.pb.h" 22 #include "tensorflow/core/framework/attr_value.pb.h" 23 #include "tensorflow/core/framework/op_def.pb.h" 24 #include "tensorflow/core/platform/types.h" 25 26 namespace tensorflow { 27 namespace python_op_gen_internal { 28 29 // Returns true if s is a Python keyword or built-in. 30 bool IsPythonReserved(const string& s); 31 32 // Whether the op should be prefixed with underscore. 33 bool IsOpWithUnderscorePrefix(const string& s); 34 35 // Add a _ to the end of s if necessary to avoid a Python keyword or built-in. 36 string AvoidPythonReserved(const string& s); 37 38 // Convert an AttrValue with type `type` to the Python representation for 39 // that value. 40 string AttrValueToPython(const string& type, const AttrValue& value, 41 const string& dtype_module = "tf."); 42 43 void GenerateLowerCaseOpName(const string& str, string* result); 44 45 string DataTypeToPython(DataType dtype, const string& dtype_module); 46 47 // Names that corresponds to a single input parameter. 48 class ParamNames { 49 public: 50 // Create param based on Arg. ParamNames(const string & name,const string & rename_to)51 ParamNames(const string& name, const string& rename_to) : name_(name) { 52 rename_to_ = AvoidPythonReserved(rename_to); 53 } 54 55 // Get original parameter name. GetName()56 string GetName() const { return name_; } 57 58 // Get the name to rename the parameter to. Note that AvoidPythonReserved 59 // has already been applied. GetRenameTo()60 string GetRenameTo() const { return rename_to_; } 61 62 private: 63 // Original parameter name. 64 string name_; 65 // API name for this parameter. 66 string rename_to_; 67 }; 68 69 class GenPythonOp { 70 public: 71 GenPythonOp(const OpDef& op_def, const ApiDef& api_def, 72 const string& function_name); 73 virtual ~GenPythonOp(); 74 75 virtual string Code(); 76 77 protected: 78 // Print: def Function(parameters): 79 void AddDefLine(const string& function_name, const string& parameters); 80 void AddDefLine(const string& parameters); 81 82 // Format the Op's descriptions so that it can be a Python docstring. 83 void AddDocStringDescription(); 84 85 void AddDocStringArgs(); 86 void AddDocStringInputs(); 87 void AddDocStringAttrs(); 88 void AddDocStringNameArg(); 89 void AddOutputGlobals(); 90 void AddDocStringOutputs(); 91 void AddBody(const string& prefix); 92 void AddBodyNoReturn(const string& apply_prefix); 93 void AddExport(); 94 95 // From constructor arguments 96 const OpDef& op_def_; 97 const ApiDef& api_def_; 98 const string function_name_; 99 const int num_outs_; 100 101 // Return value from Code() is prelude_ + result_. 102 string prelude_; // Code before function definition 103 string result_; // Function definition 104 105 // Map from attr name to the first input arg it is inferred from 106 std::unordered_map<string, string> inferred_attrs_; 107 108 // The names of the non-inferred attrs, in parameter order 109 std::vector<string> attrs_; 110 111 // All parameters, including inputs & non-inferred attrs, required and those 112 // with defaults, except "name" 113 std::vector<ParamNames> param_names_; 114 }; 115 116 } // namespace python_op_gen_internal 117 } // namespace tensorflow 118 119 #endif // TENSORFLOW_PYTHON_FRAMEWORK_PYTHON_OP_GEN_INTERNAL_H_ 120