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 // Must be included first.
20 #include "tensorflow/python/lib/core/numpy.h"
21 
22 #include "tensorflow/c/c_api.h"
23 #include "tensorflow/c/tf_status_helper.h"
24 #include "tensorflow/core/framework/tensor.h"
25 #include "tensorflow/python/lib/core/safe_ptr.h"
26 
27 namespace tensorflow {
28 
29 Status TF_TensorToPyArray(Safe_TF_TensorPtr tensor, PyObject** out_ndarray);
30 
31 // Converts the given numpy ndarray to a (safe) TF_Tensor. The returned
32 // TF_Tensor in `out_tensor` may have its own Python reference to `ndarray`s
33 // data. After `out_tensor` is destroyed, this reference must (eventually) be
34 // decremented via ClearDecrefCache().
35 //
36 // `out_tensor` must be non-null. Caller retains ownership of `ndarray`.
37 Status PyArrayToTF_Tensor(PyObject* ndarray, Safe_TF_TensorPtr* out_tensor);
38 
39 // Creates a tensor in 'ret' from the input Ndarray.
40 Status NdarrayToTensor(PyObject* obj, Tensor* ret);
41 
42 // Creates a numpy array in 'ret' which either aliases the content of 't' or has
43 // a copy.
44 Status TensorToNdarray(const Tensor& t, PyObject** ret);
45 
46 }  // namespace tensorflow
47 
48 #endif  // TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_H_
49