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 "tensorflow/c/c_api_internal.h" 18 #include "tensorflow/lite/delegates/flex/buffer_map_util.h" 19 #include "tensorflow/lite/kernels/internal/compatibility.h" 20 #include "tensorflow/lite/string_type.h" 21 22 namespace tflite { 23 namespace flex { 24 BufferMap()25BufferMap::BufferMap() {} 26 ~BufferMap()27BufferMap::~BufferMap() {} 28 HasTensor(int tensor_index) const29bool BufferMap::HasTensor(int tensor_index) const { 30 return id_to_tensor_.count(tensor_index) != 0; 31 } 32 IsTensorFlowTensor(int tensor_index) const33bool BufferMap::IsTensorFlowTensor(int tensor_index) const { 34 return HasTensor(tensor_index) && owned_by_tf_.count(tensor_index) > 0; 35 } 36 GetTensor(int tensor_index) const37tensorflow::Tensor BufferMap::GetTensor(int tensor_index) const { 38 return id_to_tensor_.at(tensor_index); 39 } 40 GetTensorPtr(int tensor_index) const41const tensorflow::Tensor* BufferMap::GetTensorPtr(int tensor_index) const { 42 auto& tensor = id_to_tensor_.at(tensor_index); 43 return &tensor; 44 } 45 SetFromTfLite(int tensor_index,const TfLiteTensor * tensor)46void BufferMap::SetFromTfLite(int tensor_index, const TfLiteTensor* tensor) { 47 TFLITE_CHECK( 48 SetTfTensorFromTfLite(tensor, &id_to_tensor_[tensor_index]).ok()); 49 if (tensor->type == kTfLiteResource || tensor->type == kTfLiteVariant) { 50 owned_by_tf_.insert(tensor_index); 51 return; 52 } 53 owned_by_tf_.erase(tensor_index); 54 } 55 SetFromTensorFlow(int tensor_index,tensorflow::Tensor tensor)56void BufferMap::SetFromTensorFlow(int tensor_index, tensorflow::Tensor tensor) { 57 id_to_tensor_[tensor_index] = std::move(tensor); 58 owned_by_tf_.insert(tensor_index); 59 } 60 61 } // namespace flex 62 } // namespace tflite 63