1 /* Copyright 2016 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_LIB_CORE_NDARRAY_TENSOR_H_ 17 #define TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_H_ 18 19 #include "tensorflow/c/c_api.h" 20 #include "tensorflow/c/tf_status_helper.h" 21 #include "tensorflow/core/framework/tensor.h" 22 #include "tensorflow/python/lib/core/safe_ptr.h" 23 24 namespace tensorflow { 25 26 Status TF_TensorToMaybeAliasedPyArray(Safe_TF_TensorPtr tensor, 27 PyObject** out_ndarray); 28 29 Status TF_TensorToPyArray(Safe_TF_TensorPtr tensor, PyObject** out_ndarray); 30 31 // Creates a tensor in 'ret' from the input `ndarray`. The returned TF_Tensor 32 // in `ret` may have its own Python reference to `ndarray`s data. After `ret` 33 // is destroyed, this reference must (eventually) be decremented via 34 // ClearDecrefCache(). 35 // `convert_string` indicates whether it has to handle tstring conversion. 36 // Expected to be removed once tstring migration is done. 37 ABSL_MUST_USE_RESULT 38 Status NdarrayToTensor(TFE_Context* ctx, PyObject* ndarray, 39 Safe_TF_TensorPtr* ret); 40 41 // Creates a tensor in 'ret' from the input Ndarray. 42 // TODO(kkb): This is an old conversion function that does not support TFRT. 43 // Currently it's used for session, py_func, and an internal project. Migrate 44 // them. 45 ABSL_MUST_USE_RESULT 46 Status NdarrayToTensor(PyObject* obj, Tensor* ret); 47 48 // Creates a numpy array in 'ret' which either aliases the content of 't' or has 49 // a copy. 50 Status TensorToNdarray(const Tensor& t, PyObject** ret); 51 52 } // namespace tensorflow 53 54 #endif // TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_H_ 55