1 /* Copyright 2018 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 #include <vector>
17
18 #include "tensorflow/c/c_api.h"
19 #include "tensorflow/c/eager/c_api.h"
20 #include "tensorflow/c/eager/tfe_tensor_debug_info_internal.h"
21 #include "tensorflow/c/eager/tfe_tensorhandle_internal.h"
22 #include "tensorflow/c/tf_status_internal.h"
23 #include "tensorflow/core/common_runtime/eager/tensor_handle.h"
24 #include "tensorflow/core/platform/status.h"
25
26 using tensorflow::string;
27
28 namespace {
29
TensorShapeAsVector(const tensorflow::TensorHandle & handle,tensorflow::Status * status)30 std::vector<tensorflow::int64> TensorShapeAsVector(
31 const tensorflow::TensorHandle& handle, tensorflow::Status* status) {
32 std::vector<tensorflow::int64> shape;
33 int rank = -1;
34 *status = handle.NumDims(&rank);
35 if (!status->ok()) {
36 return shape;
37 }
38 shape.reserve(rank);
39 for (int i = 0; i < rank; ++i) {
40 tensorflow::int64 dim;
41 *status = handle.Dim(i, &dim);
42 if (!status->ok()) {
43 return shape;
44 }
45 shape.push_back(dim);
46 }
47 return shape;
48 }
49
50 } // namespace
51
52 extern "C" {
53
TFE_TensorHandleTensorDebugInfo(TFE_TensorHandle * h,TF_Status * status)54 TF_CAPI_EXPORT extern TFE_TensorDebugInfo* TFE_TensorHandleTensorDebugInfo(
55 TFE_TensorHandle* h, TF_Status* status) {
56 tensorflow::TensorHandle* handle =
57 TensorHandleFromInterface(tensorflow::unwrap(h));
58 const tensorflow::Tensor* tensor;
59 status->status = handle->Tensor(&tensor);
60 if (!status->status.ok()) {
61 return nullptr;
62 }
63
64 std::vector<tensorflow::int64> dev_dims =
65 TensorShapeAsVector(*handle, &status->status);
66 if (!status->status.ok()) {
67 return nullptr;
68 }
69 return new TFE_TensorDebugInfo(dev_dims);
70 }
71
TFE_DeleteTensorDebugInfo(TFE_TensorDebugInfo * debug_info)72 TF_CAPI_EXPORT extern void TFE_DeleteTensorDebugInfo(
73 TFE_TensorDebugInfo* debug_info) {
74 delete debug_info;
75 }
76
TFE_TensorDebugInfoOnDeviceNumDims(TFE_TensorDebugInfo * debug_info)77 TF_CAPI_EXPORT extern int TFE_TensorDebugInfoOnDeviceNumDims(
78 TFE_TensorDebugInfo* debug_info) {
79 return debug_info->dev_dims.size();
80 }
81
TFE_TensorDebugInfoOnDeviceDim(TFE_TensorDebugInfo * debug_info,int dim_index)82 TF_CAPI_EXPORT extern int64_t TFE_TensorDebugInfoOnDeviceDim(
83 TFE_TensorDebugInfo* debug_info, int dim_index) {
84 return debug_info->dev_dims[dim_index];
85 }
86
87 } // extern "C"
88