1 /* Copyright 2019 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_CORE_COMMON_RUNTIME_EAGER_TENSOR_HANDLE_DATA_H_ 16 #define TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_TENSOR_HANDLE_DATA_H_ 17 18 #include "tensorflow/core/common_runtime/eager/context.h" 19 #include "tensorflow/core/framework/tensor.h" 20 #include "tensorflow/core/lib/core/status.h" 21 22 namespace tensorflow { 23 24 class TensorHandleData { 25 public: ~TensorHandleData()26 virtual ~TensorHandleData() {} 27 28 // Different tensor handles support a set of these calls. In some cases these 29 // are resolved with a Tensor or TensorShape. Typically if the handle is not 30 // ready, none of these are supported operations. 31 virtual Status Tensor(const tensorflow::Tensor** t) const = 0; 32 virtual Status TensorValue(tensorflow::TensorValue* t) = 0; 33 virtual Status Shape(TensorShape* shape) const = 0; 34 virtual Status NumDims(int* num_dims) const = 0; 35 virtual Status Dim(int dim_index, int64* dim) const = 0; 36 virtual Status NumElements(int64* num_elements) const = 0; 37 38 virtual string DebugString() const = 0; 39 }; 40 41 // Local Tensor Handle: Handle to a Tensor present on the local host. 42 class LocalTensorHandleData : public TensorHandleData { 43 public: LocalTensorHandleData(const tensorflow::Tensor & t)44 explicit LocalTensorHandleData(const tensorflow::Tensor& t) : tensor_(t) {} ~LocalTensorHandleData()45 ~LocalTensorHandleData() override {} 46 47 // A local tensor handle should be able to satisfy all of these requests. 48 Status Tensor(const tensorflow::Tensor** t) const override; 49 Status TensorValue(tensorflow::TensorValue* t) override; 50 Status Shape(TensorShape* shape) const override; 51 Status NumDims(int* num_dims) const override; 52 Status Dim(int dim_index, int64* dim) const override; 53 Status NumElements(int64* num_elements) const override; 54 DebugString()55 string DebugString() const override { return tensor_.DebugString(); } 56 57 private: 58 tensorflow::Tensor tensor_; 59 }; 60 61 // Empty Local Tensor Handle: Once the execution is complete this is replaced by 62 // a local tensor handle. 63 class EmptyLocalTensorHandleData : public TensorHandleData { 64 public: EmptyLocalTensorHandleData()65 EmptyLocalTensorHandleData() {} ~EmptyLocalTensorHandleData()66 ~EmptyLocalTensorHandleData() override {} 67 68 // Empty tensor handles are not ready and hence cannot satisfy any of these 69 // requests. 70 Status Tensor(const tensorflow::Tensor** t) const override; 71 Status TensorValue(tensorflow::TensorValue* t) override; 72 Status Shape(TensorShape* shape) const override; 73 Status NumDims(int* num_dims) const override; 74 Status Dim(int dim_index, int64* dim) const override; 75 Status NumElements(int64* num_elements) const override; 76 77 string DebugString() const override; 78 }; 79 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_TENSOR_HANDLE_DATA_H_ 83