xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/flex/util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_LITE_DELEGATES_FLEX_UTIL_H_
16 #define TENSORFLOW_LITE_DELEGATES_FLEX_UTIL_H_
17 
18 #include <string>
19 
20 #include "tensorflow/c/c_api_internal.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow/core/platform/statusor.h"
24 #include "tensorflow/lite/c/common.h"
25 #include "tensorflow/lite/util.h"
26 
27 namespace tflite {
28 namespace flex {
29 
30 // Converts a tensorflow:Status into a TfLiteStatus. If the original status
31 // represented an error, reports it using the given 'context'.
32 TfLiteStatus ConvertStatus(TfLiteContext* context,
33                            const tensorflow::Status& status);
34 
35 // Copies the given shape and type of the TensorFlow 'src' tensor into a TF Lite
36 // 'tensor'. Logs an error and returns kTfLiteError if the shape or type can't
37 // be converted.
38 TfLiteStatus CopyShapeAndType(TfLiteContext* context,
39                               const tensorflow::Tensor& src,
40                               TfLiteTensor* tensor);
41 
42 // Returns the TF C API Data type that corresponds to the given TfLiteType.
43 TF_DataType GetTensorFlowDataType(TfLiteType type);
44 
45 // Returns the TfLiteType that corresponds to the given TF C API Data type.
46 TfLiteType GetTensorFlowLiteType(TF_DataType);
47 
48 // Returns the TF type name that corresponds to the given TfLiteType.
49 const char* TfLiteTypeToTfTypeName(TfLiteType type);
50 
51 // Creates a `tensorflow::Tensor` from a TfLiteTensor for non-resource and
52 // non-variant type. Returns error status if the conversion fails.
53 tensorflow::StatusOr<tensorflow::Tensor> CreateTfTensorFromTfLiteTensor(
54     const TfLiteTensor* tflite_tensor);
55 
56 // Returns the encoded string name for a TF Lite resource variable tensor.
57 // This function will return a string in the format:
58 // tflite_resource_variable:resource_id.
59 std::string TfLiteResourceIdentifier(const TfLiteTensor* tensor);
60 
61 // Parses out the resource ID from the given `resource_handle` and sets it
62 // to the corresponding TfLiteTensor. Returns true if succeed.
63 bool GetTfLiteResourceTensorFromResourceHandle(
64     const tensorflow::ResourceHandle& resource_handle, TfLiteTensor* tensor);
65 
66 }  // namespace flex
67 }  // namespace tflite
68 
69 #endif  // TENSORFLOW_LITE_DELEGATES_FLEX_UTIL_H_
70