xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/experimental/resource/resource_variable.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_RESOURCE_VARIABLE_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_RESOURCE_RESOURCE_VARIABLE_H_
17 
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/experimental/resource/resource_base.h"
20 
21 namespace tflite {
22 namespace resource {
23 
24 /// WARNING: Experimental interface, subject to change.
25 // A resource variable class. It's similar to TensorFlow Resource
26 // Variable, but it's identified with int32 ID in TFLite (instead of
27 // using Resource handle like TensorFlow).
28 class ResourceVariable : public ResourceBase {
29  public:
30   ResourceVariable();
31   ResourceVariable(ResourceVariable&& other);
32 
33   ResourceVariable(const ResourceVariable&) = delete;
34   ResourceVariable& operator=(const ResourceVariable&) = delete;
35 
36   ~ResourceVariable() override;
37 
38   // Assigns data from a tensor. Copies its type, shape and data over.
39   TfLiteStatus AssignFrom(const TfLiteTensor* tensor);
40 
41   // Get the data tensor stored in the resource variable.
42   // Returns `nullptr` if the variable is never initialized by calling
43   // `AssignFrom`.
GetTensor()44   TfLiteTensor* GetTensor() { return is_initialized_ ? &tensor_ : nullptr; }
45 
46   // Returns true if this resource variable is initialized.
IsInitialized()47   bool IsInitialized() override { return is_initialized_; }
48 
GetMemoryUsage()49   size_t GetMemoryUsage() override {
50     return is_initialized_ ? tensor_.bytes : 0;
51   }
52 
53  private:
54   // The tensor (and its buffer stored in `tensor_.data` is fully owned by
55   // the `ResourceVariable` object.
56   TfLiteTensor tensor_;
57   // True if `AssignFrom` function is every called.
58   // False if and only if `tensor_` is filled with zeros.
59   bool is_initialized_ = false;
60 };
61 
62 // Creates a resource variable, shared among all the subgraphs with the given
63 // resource id if there is an existing one.
64 // WARNING: Experimental interface, subject to change.
65 void CreateResourceVariableIfNotAvailable(ResourceMap* resources,
66                                           int resource_id);
67 
68 // Returns the corresponding resource variable, or nullptr if none.
69 // WARNING: Experimental interface, subject to change.
70 ResourceVariable* GetResourceVariable(ResourceMap* resources, int resource_id);
71 
72 // Returns true if 'tensor' points to a builtin resource.
73 // WARNING: Experimental interface, subject to change.
74 bool IsBuiltinResource(const TfLiteTensor* tensor);
75 
76 }  // namespace resource
77 }  // namespace tflite
78 
79 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_RESOURCE_RESOURCE_VARIABLE_H_
80