1/* Copyright 2020 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#ifndef TENSORFLOW_C_TENSOR_INTERFACE_H_
17#define TENSORFLOW_C_TENSOR_INTERFACE_H_
18
19#include "tensorflow/core/framework/types.pb.h"
20#include "tensorflow/core/platform/status.h"
21
22namespace tensorflow {
23
24// Abstract interface to a Tensor.
25//
26// This allows us to hide concrete implementations of Tensor from header
27// files. The interface lists the common functionality that must be provided by
28// any concrete implementation. However, in cases where the true concrete class
29// is needed a static_cast can be applied.
30class AbstractTensorInterface {
31 public:
32 // Release any underlying resources, including the interface object.
33 virtual void Release() = 0;
34
35 // Returns tensor dtype.
36 virtual DataType Type() const = 0;
37 // Returns number of dimensions.
38 virtual int NumDims() const = 0;
39 // Returns size of specified dimension
40 virtual int64_t Dim(int dim_index) const = 0;
41 // Returns number of elements across all dimensions.
42 virtual int64_t NumElements() const = 0;
43 // Return size in bytes of the Tensor
44 virtual size_t ByteSize() const = 0;
45 // Returns a pointer to tensor data
46 virtual void* Data() const = 0;
47
48 // Returns if the tensor is aligned
49 virtual bool IsAligned() const = 0;
50 // Returns if their is sole ownership of this Tensor and thus it can be moved.
51 virtual bool CanMove() const = 0;
52
53 virtual std::string SummarizeValue() const = 0;
54
55 protected:
56 virtual ~AbstractTensorInterface() {}
57};
58
59namespace internal {
60struct AbstractTensorInterfaceDeleter {
61 void operator()(AbstractTensorInterface* p) const {
62 if (p != nullptr) {
63 p->Release();
64 }
65 }
66};
67} // namespace internal
68
69using AbstractTensorPtr =
70 std::unique_ptr<AbstractTensorInterface,
71 internal::AbstractTensorInterfaceDeleter>;
72
73} // namespace tensorflow
74
75#endif // TENSORFLOW_C_TENSOR_INTERFACE_H_
76