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 #ifndef TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ 16 #define TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ 17 18 // Must be included first. 19 #include "tensorflow/python/lib/core/numpy.h" 20 21 #include <functional> 22 23 #include "tensorflow/c/c_api.h" 24 #include "tensorflow/core/framework/types.pb.h" 25 #include "tensorflow/core/lib/core/status.h" 26 27 namespace tensorflow { 28 29 // Destructor passed to TF_NewTensor when it reuses a numpy buffer. Stores a 30 // pointer to the pyobj in a buffer to be dereferenced later when we're actually 31 // holding the GIL. Data and len are ignored. 32 void DelayedNumpyDecref(void* data, size_t len, void* obj); 33 34 // Actually dereferences cached numpy arrays. REQUIRES being called while 35 // holding the GIL. 36 void ClearDecrefCache(); 37 38 // Creates a numpy array with shapes specified by dim_size and dims and content 39 // in data. The array does not own the memory, and destructor will be called to 40 // release it. If the status is not ok the caller is responsible for releasing 41 // the memory. 42 Status ArrayFromMemory(int dim_size, npy_intp* dims, void* data, DataType dtype, 43 std::function<void()> destructor, PyObject** result); 44 45 // Converts TF_DataType to the corresponding numpy type. 46 Status TF_DataType_to_PyArray_TYPE(TF_DataType tf_datatype, 47 int* out_pyarray_type); 48 49 } // namespace tensorflow 50 51 #endif // TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ 52