• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #include "tensorflow/lite/kernels/shim/tf_tensor_view.h"
16 
17 #include <utility>
18 
19 #include "absl/status/status.h"
20 #include "absl/status/statusor.h"
21 #include "tensorflow/core/framework/types.pb.h"
22 
23 // Creates a case statement for the switch() clause given the dtype
24 #define CASE_FOR_DTYPE_GIVEN_CPP_DTYPE(TF_DTYPE, CPP_DTYPE) \
25   case TF_DTYPE: {                                          \
26     using DType = typename CPP_DTYPE;                       \
27     return TfTensorView(wrapped_tensor, DType());           \
28   }
29 
30 #define CASE_FOR_DTYPE(TF_DTYPE)           \
31   CASE_FOR_DTYPE_GIVEN_CPP_DTYPE(TF_DTYPE, \
32                                  ::tensorflow::EnumToDataType<TF_DTYPE>::Type)
33 
34 namespace tflite {
35 namespace shim {
36 
37 // ctors
38 
TfTensorView(TfTensorView && o)39 TfTensorView::TfTensorView(TfTensorView &&o) noexcept
40     : TensorView(std::move(o)), shape_data_(std::move(o.shape_data_)) {
41   shape_ = absl::Span<int>(shape_data_);
42 }
43 
TfTensorView(const TfTensorView & o)44 TfTensorView::TfTensorView(const TfTensorView &o)
45     : TensorView(o), shape_data_(o.shape_data_) {
46   shape_ = absl::Span<int>(shape_data_);
47 }
48 
operator =(TfTensorView && o)49 TfTensorView &TfTensorView::operator=(TfTensorView &&o) noexcept {
50   shape_data_ = std::move(o.shape_data_);
51   TensorView::operator=(std::move(o));
52   shape_ = absl::Span<int>(shape_data_);
53   return *this;
54 }
55 
operator =(const TfTensorView & o)56 TfTensorView &TfTensorView::operator=(const TfTensorView &o) {
57   if (&o == this) return *this;
58   TensorView::operator=(o);
59   shape_data_ = o.shape_data_;
60   shape_ = absl::Span<int>(shape_data_);
61   return *this;
62 }
63 
64 template <typename TfTensorType>
65 absl::StatusOr<typename MatchConstNess<TfTensorType, TfTensorView>::Type>
TfTensorViewTemplatizedNew(TfTensorType * wrapped_tensor)66 TfTensorViewTemplatizedNew(TfTensorType *wrapped_tensor) {
67   switch (wrapped_tensor->dtype()) {
68     CASE_FOR_DTYPE(::tensorflow::DT_BOOL);
69     CASE_FOR_DTYPE(::tensorflow::DT_UINT8);
70     CASE_FOR_DTYPE(::tensorflow::DT_UINT64);
71     CASE_FOR_DTYPE(::tensorflow::DT_INT8);
72     CASE_FOR_DTYPE(::tensorflow::DT_INT16);
73     CASE_FOR_DTYPE(::tensorflow::DT_INT32);
74     // Map DT_INT64 to int64_t instead of int64 to have a single int64 datatype.
75     CASE_FOR_DTYPE_GIVEN_CPP_DTYPE(::tensorflow::DT_INT64, std::int64_t);
76     CASE_FOR_DTYPE(::tensorflow::DT_FLOAT);
77     CASE_FOR_DTYPE(::tensorflow::DT_DOUBLE);
78     CASE_FOR_DTYPE(::tensorflow::DT_STRING);
79     default: {
80       return absl::UnimplementedError(
81           absl::StrCat("Unsupported data type: ", wrapped_tensor->dtype()));
82     }
83   }
84 }
85 
86 template <>
New(::tensorflow::Tensor * wrapped_tensor)87 absl::StatusOr<TfTensorView> TensorView::New<::tensorflow::Tensor>(
88     ::tensorflow::Tensor *wrapped_tensor) {
89   return TfTensorViewTemplatizedNew(wrapped_tensor);
90 }
91 
92 template <>
New(const::tensorflow::Tensor * wrapped_tensor)93 absl::StatusOr<const TfTensorView> TensorView::New<const ::tensorflow::Tensor>(
94     const ::tensorflow::Tensor *wrapped_tensor) {
95   return TfTensorViewTemplatizedNew(wrapped_tensor);
96 }
97 
98 }  // namespace shim
99 }  // namespace tflite
100