1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_RESOURCE_LOOKUP_INTERFACES_H_ 16 #define TENSORFLOW_LITE_EXPERIMENTAL_RESOURCE_LOOKUP_INTERFACES_H_ 17 18 #include <unordered_map> 19 20 #include "tensorflow/lite/c/common.h" 21 #include "tensorflow/lite/experimental/resource/lookup_util.h" 22 #include "tensorflow/lite/experimental/resource/resource_base.h" 23 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 24 #include "tensorflow/lite/string_util.h" 25 26 namespace tflite { 27 namespace resource { 28 29 /// WARNING: Experimental interface, subject to change. 30 // A resource hash table interface. It's similar to TensorFlow core's 31 // LookupInterface class. But it's identified with int32 ID in TFLite (instead 32 // of using Resource handle like TensorFlow). 33 class LookupInterface : public ResourceBase { 34 public: 35 virtual TfLiteStatus Lookup(TfLiteContext* context, const TfLiteTensor* keys, 36 TfLiteTensor* values, 37 const TfLiteTensor* default_value) = 0; 38 virtual TfLiteStatus Import(TfLiteContext* context, const TfLiteTensor* keys, 39 const TfLiteTensor* values) = 0; 40 virtual size_t Size() = 0; 41 42 virtual TfLiteType GetKeyType() const = 0; 43 virtual TfLiteType GetValueType() const = 0; 44 virtual TfLiteStatus CheckKeyAndValueTypes(TfLiteContext* context, 45 const TfLiteTensor* keys, 46 const TfLiteTensor* values) = 0; 47 }; 48 49 // Creates an resource hash table, shared among all the subgraphs with the 50 // given resource id if there is an existing one. 51 // WARNING: Experimental interface, subject to change. 52 void CreateHashtableResourceIfNotAvailable(ResourceMap* resources, 53 int resource_id, 54 TfLiteType key_dtype, 55 TfLiteType value_dtype); 56 57 // Returns the corresponding resource hash table, or nullptr if none. 58 // WARNING: Experimental interface, subject to change. 59 LookupInterface* GetHashtableResource(ResourceMap* resources, int resource_id); 60 61 } // namespace resource 62 } // namespace tflite 63 64 #endif // TENSORFLOW_LITE_EXPERIMENTAL_RESOURCE_LOOKUP_INTERFACES_H_ 65