1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OBJECT_READER_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OBJECT_READER_H_
18 
19 #include <cstdint>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/delegates/gpu/common/model.h"
24 #include "tensorflow/lite/delegates/gpu/common/model_builder_helper.h"
25 #include "tensorflow/lite/delegates/gpu/common/status.h"
26 #include "tensorflow/lite/kernels/kernel_util.h"
27 
28 namespace tflite {
29 namespace gpu {
30 
31 // If quantized tensors exist in the graph & quant_conversion_map is non-null,
32 // the mapping between the original tensors (fixed-point) & GPU values (fp) is
33 // stored in quant_conversion_map.
34 class ObjectReader {
35  public:
36   static absl::Status ReadNonConstantTensor(
37       TfLiteContext* context, absl::flat_hash_map<int, Value*>* tensor_to_value,
38       absl::flat_hash_map<int, int>* quant_conversion_map, GraphFloat32* graph,
39       uint32_t tensor_idx, Value** value = nullptr);
40 
41   ObjectReader(GraphFloat32* graph, TfLiteContext* context,
42                const TfLiteNode* node,
43                absl::flat_hash_map<int, Value*>* tensor_to_value,
44                absl::flat_hash_map<int, int>* quant_conversion_map = nullptr)
graph_(graph)45       : graph_(graph),
46         context_(context),
47         node_(node),
48         tensor_to_value_(tensor_to_value),
49         quant_conversion_map_(quant_conversion_map) {}
50 
51   absl::Status ReadValue(uint32_t idx, Value** value);
52 
53   absl::Status ReadValueByTensorIdx(uint32_t tensor_idx, Value** value);
54 
55   int GetNumberOfRuntimeInputs() const;
56 
57   absl::Status GetTensorDims(uint32_t idx, TfLiteIntArray* dimensions) const;
58 
59   template <typename TensorT>
ReadTensor(uint32_t idx,TensorT * t)60   absl::Status ReadTensor(uint32_t idx, TensorT* t) const {
61     if (idx < 0 || idx >= node_->inputs->size) {
62       // If larger, this can be an older model with fewer input tensors than the
63       // current implementation.
64       return absl::OutOfRangeError("Invalid data index found.");
65     }
66     const int32_t tensor_idx = node_->inputs->data[idx];
67     if (tensor_idx < 0) {
68       return absl::InvalidArgumentError(
69           "Invalid data index found. Possibly an unset optional tensor is "
70           "being read.");
71     }
72 
73     const TfLiteTensor* tflite_tensor = context_->tensors + tensor_idx;
74     if (tflite_tensor->sparsity != nullptr) {
75       return absl::InvalidArgumentError("Sparsity is not supported on GPU.");
76     }
77     t->data.resize(NumElements(tflite_tensor));
78     RETURN_IF_ERROR(CreateVectorCopyData(*tflite_tensor, &t->data[0]));
79 
80     // Axis and data layout depend on operation this tensor is used in. So,
81     // postpone resolutions until operations are parsed.
82     t->id = tensor_idx;
83     return SetAllDimensions(tflite_tensor->dims, &t->shape);
84   }
85 
86   absl::Status AddOutput(const Node* node, int id);
87 
88   absl::Status AddOutputs(const Node* node);
89 
90   absl::Status AddInput(const Node* node, uint32_t idx);
91 
92   absl::Status AddUpdate(const Node* node, uint32_t idx);
93 
94   TfLiteTensor* GetInputTensor(int index) const;
95 
96   TfLiteTensor* GetOutputTensor(int index) const;
97 
98   absl::Status VerifyInputsConstsOutputs(const TfLiteNode* node,
99                                          int runtime_inputs, int const_inputs,
100                                          int outputs);
101 
102  private:
103   GraphFloat32* graph_;
104   TfLiteContext* context_;
105   const TfLiteNode* node_;
106   absl::flat_hash_map<int, Value*>* tensor_to_value_;
107   absl::flat_hash_map<int, int>* quant_conversion_map_;
108 };
109 
110 }  // namespace gpu
111 }  // namespace tflite
112 
113 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OBJECT_READER_H_
114