1 /* Copyright 2018 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/delegates/flex/buffer_map.h"
16
17 #include <utility>
18
19 #include "tensorflow/c/c_api_internal.h"
20 #include "tensorflow/lite/delegates/flex/buffer_map_util.h"
21 #include "tensorflow/lite/delegates/flex/util.h"
22 #include "tensorflow/lite/kernels/internal/compatibility.h"
23 #include "tensorflow/lite/string_type.h"
24
25 namespace tflite {
26 namespace flex {
27
BufferMap()28 BufferMap::BufferMap() {}
29
~BufferMap()30 BufferMap::~BufferMap() {}
31
HasTensor(int tensor_index) const32 bool BufferMap::HasTensor(int tensor_index) const {
33 return id_to_tensor_.count(tensor_index) != 0;
34 }
35
IsTensorFlowTensor(int tensor_index) const36 bool BufferMap::IsTensorFlowTensor(int tensor_index) const {
37 return HasTensor(tensor_index) && owned_by_tf_.count(tensor_index) > 0;
38 }
39
GetTensor(int tensor_index) const40 tensorflow::Tensor BufferMap::GetTensor(int tensor_index) const {
41 return id_to_tensor_.at(tensor_index);
42 }
43
GetTensorPtr(int tensor_index) const44 const tensorflow::Tensor* BufferMap::GetTensorPtr(int tensor_index) const {
45 auto& tensor = id_to_tensor_.at(tensor_index);
46 return &tensor;
47 }
48
SetFromTfLite(int tensor_index,const TfLiteTensor * tensor,bool allow_reusing)49 void BufferMap::SetFromTfLite(int tensor_index, const TfLiteTensor* tensor,
50 bool allow_reusing) {
51 TFLITE_CHECK(
52 SetTfTensorFromTfLite(tensor, &id_to_tensor_[tensor_index], allow_reusing)
53 .ok());
54 if (tflite::IsResourceOrVariant(tensor)) {
55 owned_by_tf_.insert(tensor_index);
56 return;
57 }
58 owned_by_tf_.erase(tensor_index);
59 }
60
SetFromTensorFlow(int tensor_index,tensorflow::Tensor tensor)61 void BufferMap::SetFromTensorFlow(int tensor_index, tensorflow::Tensor tensor) {
62 id_to_tensor_[tensor_index] = std::move(tensor);
63 owned_by_tf_.insert(tensor_index);
64 }
65
66 } // namespace flex
67 } // namespace tflite
68