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 %ignore ""; 17 18 %rename("%s") TFE_NewContext; 19 %rename("%s") TFE_DeleteContext; 20 %rename("%s") TFE_ContextListDevices; 21 %rename("%s") TFE_ContextAddFunction; 22 %rename("%s") TFE_ContextAddFunctionDef; 23 %rename("%s") TFE_ContextEnableRunMetadata; 24 %rename("%s") TFE_ContextDisableRunMetadata; 25 %rename("%s") TFE_ContextExportRunMetadata; 26 %rename("%s") TFE_ContextClearCaches; 27 %rename("%s") TFE_ContextGetDevicePlacementPolicy; 28 %rename("%s") TFE_ContextSetThreadLocalDevicePlacementPolicy; 29 %rename("%s") TFE_OpNameGetAttrType; 30 %rename("%s") TFE_Py_InitEagerTensor; 31 %rename("%s") TFE_Py_RegisterExceptionClass; 32 %rename("%s") TFE_Py_RegisterFallbackExceptionClass; 33 %rename("%s") TFE_Py_Execute; 34 %rename("%s") TFE_Py_FastPathExecute; 35 %rename("%s") TFE_Py_UID; 36 %rename("%s") TFE_Py_TapeSetNew; 37 %rename("%s") TFE_Py_TapeSetRemove; 38 %rename("%s") TFE_Py_TapeSetStopOnThread; 39 %rename("%s") TFE_Py_TapeSetRestartOnThread; 40 %rename("%s") TFE_Py_TapeSetIsEmpty; 41 %rename("%s") TFE_Py_TapeSetShouldRecord; 42 %rename("%s") TFE_Py_TapeSetWatch; 43 %rename("%s") TFE_Py_TapeSetDeleteTrace; 44 %rename("%s") TFE_Py_TapeSetRecordOperation; 45 %rename("%s") TFE_Py_TapeSetWatchVariable; 46 %rename("%s") TFE_Py_TapeGradient; 47 %rename("%s") TFE_Py_TapeWatchedVariables; 48 %rename("%s") TFE_NewContextOptions; 49 %rename("%s") TFE_ContextOptionsSetConfig; 50 %rename("%s") TFE_ContextOptionsSetDevicePlacementPolicy; 51 %rename("%s") TFE_DeleteContextOptions; 52 %rename("%s") TFE_Py_TensorShapeSlice; 53 54 %{ 55 #include "tensorflow/python/eager/pywrap_tfe.h" 56 %} 57 58 %typemap(in) (const void* proto) { 59 char* c_string; 60 Py_ssize_t py_size; 61 // PyBytes_AsStringAndSize() does not copy but simply interprets the input 62 if (PyBytes_AsStringAndSize($input, &c_string, &py_size) == -1) { 63 // Python has raised an error (likely TypeError or UnicodeEncodeError). 64 SWIG_fail; 65 } 66 $1 = static_cast<void*>(c_string); 67 } 68 69 %typemap(out) TF_DataType { 70 $result = PyInt_FromLong($1); 71 } 72 73 %typemap(out) int64_t { 74 $result = PyInt_FromLong($1); 75 } 76 77 %typemap(out) TF_AttrType { 78 $result = PyInt_FromLong($1); 79 } 80 81 %typemap(in, numinputs=0) unsigned char* is_list (unsigned char tmp) { 82 $1 = &tmp; 83 } 84 85 %typemap(argout) unsigned char* is_list { 86 if (*$1 == 1) { 87 PyObject* list = PyList_New(1); 88 PyList_SetItem(list, 0, $result); 89 $result = list; 90 } 91 } 92 93 %typemap(in) const char* serialized_function_def { 94 $1 = TFE_GetPythonString($input); 95 } 96 97 %typemap(in) const char* device_name { 98 if ($input == Py_None) { 99 $1 = nullptr; 100 } else { 101 $1 = TFE_GetPythonString($input); 102 } 103 } 104 105 %typemap(in) const char* op_name { 106 $1 = TFE_GetPythonString($input); 107 } 108 109 %typemap(in) (TFE_Context*) { 110 $1 = (TFE_Context*)PyCapsule_GetPointer($input, nullptr); 111 112 } 113 %typemap(out) (TFE_Context*) { 114 if ($1 == nullptr) { 115 SWIG_fail; 116 } else { 117 $result = PyCapsule_New($1, nullptr, TFE_DeleteContextCapsule); 118 } 119 } 120 121 %rename("%s") TFE_ContextDevicePlacementPolicy; 122 %rename("%s") TFE_DEVICE_PLACEMENT_EXPLICIT; 123 %rename("%s") TFE_DEVICE_PLACEMENT_WARN; 124 %rename("%s") TFE_DEVICE_PLACEMENT_SILENT; 125 %rename("%s") TFE_DEVICE_PLACEMENT_SILENT_FOR_INT32; 126 127 %include "tensorflow/c/eager/c_api.h" 128 129 %typemap(in) TFE_InputTensorHandles* inputs (TFE_InputTensorHandles temp) { 130 $1 = &temp; 131 if ($input != Py_None) { 132 if (!PyList_Check($input)) { 133 SWIG_exception_fail(SWIG_TypeError, 134 "must provide a list of Tensors as inputs"); 135 } 136 Py_ssize_t len = PyList_Size($input); 137 $1->resize(len); 138 for (Py_ssize_t i = 0; i < len; ++i) { 139 PyObject* elem = PyList_GetItem($input, i); 140 if (!elem) { 141 SWIG_fail; 142 } 143 if (EagerTensor_CheckExact(elem)) { 144 (*$1)[i] = EagerTensor_Handle(elem); 145 } else { 146 SWIG_exception_fail(SWIG_TypeError, 147 "provided list of inputs contains objects other " 148 "than 'EagerTensor'"); 149 } 150 } 151 } 152 } 153 154 // Temporary for the argout 155 %typemap(in) TFE_OutputTensorHandles* outputs (TFE_OutputTensorHandles temp) { 156 if (!PyInt_Check($input)) { 157 SWIG_exception_fail(SWIG_TypeError, 158 "expected an integer value (size of the number of " 159 "outputs of the operation)"); 160 } 161 $1 = &temp; 162 $1->resize(PyInt_AsLong($input), nullptr); 163 } 164 165 // Create new Status object. 166 %typemap(in, numinputs=0) TF_Status *out_status { 167 $1 = TF_NewStatus(); 168 } 169 170 %typemap(freearg) (TF_Status* out_status) { 171 TF_DeleteStatus($1); 172 } 173 174 %typemap(argout) (TFE_OutputTensorHandles* outputs, TF_Status* out_status) { 175 if (MaybeRaiseExceptionFromTFStatus($2, nullptr)) { 176 SWIG_fail; 177 } else { 178 int num_outputs = $1->size(); 179 $result = PyList_New(num_outputs); 180 for (int i = 0; i < num_outputs; ++i) { 181 PyObject *output; 182 output = EagerTensorFromHandle($1->at(i)); 183 PyList_SetItem($result, i, output); 184 } 185 } 186 } 187 188 // SWIG usually unwraps the tuple that the native Python/C interface generates. 189 // Since we wanted to have a function with a variable length of arguments, we 190 // used the native Python/C interface directly (which by default supports 191 // passing all arguments as a tuple). 192 %native(TFE_Py_FastPathExecute) TFE_Py_FastPathExecute_C; 193 194 %include "tensorflow/python/eager/pywrap_tfe.h" 195 196 // Clear all typemaps. 197 %typemap(out) TF_DataType; 198 %typemap(out) int64_t; 199 %typemap(out) TF_AttrType; 200 %typemap(in, numinputs=0) TF_Status *out_status; 201 %typemap(argout) unsigned char* is_list; 202 %typemap(in) (TFE_Context*); 203 %typemap(out) (TFE_Context*); 204 %typemap(in) TFE_OutputTensorHandles* outputs (TFE_OutputTensorHandles temp); 205 %typemap(in, numinputs=0) TF_Status *out_status; 206 %typemap(freearg) (TF_Status* out_status); 207 %typemap(argout) (TFE_OutputTensorHandles* outputs, TF_Status* out_status); 208 %typemap(in) (const void* proto); 209