1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
26using tensorflow::string;
27
28namespace {
29
30std::vector<int64_t> TensorShapeAsVector(const tensorflow::TensorHandle& handle,
31 tensorflow::Status* status) {
32 std::vector<int64_t> 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 int64_t 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
52extern "C" {
53
54TF_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<int64_t> dev_dims = TensorShapeAsVector(*handle, &status->status);
65 if (!status->status.ok()) {
66 return nullptr;
67 }
68 return new TFE_TensorDebugInfo(dev_dims);
69}
70
71TF_CAPI_EXPORT extern void TFE_DeleteTensorDebugInfo(
72 TFE_TensorDebugInfo* debug_info) {
73 delete debug_info;
74}
75
76TF_CAPI_EXPORT extern int TFE_TensorDebugInfoOnDeviceNumDims(
77 TFE_TensorDebugInfo* debug_info) {
78 return debug_info->dev_dims.size();
79}
80
81TF_CAPI_EXPORT extern int64_t TFE_TensorDebugInfoOnDeviceDim(
82 TFE_TensorDebugInfo* debug_info, int dim_index) {
83 return debug_info->dev_dims[dim_index];
84}
85
86} // extern "C"
87