1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_LITE_DELEGATES_TELEMETRY_H_
16#define TENSORFLOW_LITE_DELEGATES_TELEMETRY_H_
17
18#include <cstdint>
19#include <limits>
20
21#include "tensorflow/lite/c/common.h"
22#include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
23
24// This file implements utilities for delegate telemetry. These enable
25// representation and reporting of hardware-specific configurations, status
26// codes, etc.
27// These APIs are for internal use *only*, and should be modified with care to
28// avoid incompatibilities between delegates & runtime.
29// WARNING: This is an experimental feature that is subject to change.
30namespace tflite {
31namespace delegates {
32
33// Used to identify specific events for tflite::Profiler.
34constexpr char kDelegateSettingsTag[] = "delegate_settings";
35constexpr char kDelegateStatusTag[] = "delegate_status";
36
37// Defines the delegate or hardware-specific 'namespace' that a status code
38// belongs to. For example, GPU delegate errors might be belong to TFLITE_GPU,
39// while OpenCL-specific ones might be TFLITE_GPU_CL.
40enum class DelegateStatusSource {
41 NONE = 0,
42 TFLITE_GPU = 1,
43 TFLITE_NNAPI = 2,
44 TFLITE_HEXAGON = 3,
45 TFLITE_XNNPACK = 4,
46 TFLITE_COREML = 5,
47 MAX_NUM_SOURCES = std::numeric_limits<int32_t>::max(),
48};
49
50// DelegateStatus defines a namespaced status with a combination of
51// DelegateStatusSource & the corresponding fine-grained 32-bit code. Used to
52// convert to/from a 64-bit representation as follows:
53//
54// delegates::DelegateStatus status(
55// delegates::DelegateStatusSource::TFLITE_NNAPI,
56// ANEURALNETWORKS_OP_FAILED);
57// int64_t code = status.full_status();
58//
59// auto parsed_status = delegates::DelegateStatus(code);
60class DelegateStatus {
61 public:
62 DelegateStatus() : DelegateStatus(DelegateStatusSource::NONE, 0) {}
63 explicit DelegateStatus(int32_t code)
64 : DelegateStatus(DelegateStatusSource::NONE, code) {}
65 explicit DelegateStatus(int64_t full_status)
66 : DelegateStatus(
67 static_cast<DelegateStatusSource>(
68 full_status >> 32 &
69 static_cast<int32_t>(DelegateStatusSource::MAX_NUM_SOURCES)),
70 static_cast<int32_t>(full_status &
71 std::numeric_limits<int32_t>::max())) {}
72 DelegateStatus(DelegateStatusSource source, int32_t code)
73 : source_(static_cast<int32_t>(source)), code_(code) {}
74
75 // Return the detailed full status encoded as a int64_t value.
76 int64_t full_status() const {
77 return static_cast<int64_t>(source_) << 32 | code_;
78 }
79
80 DelegateStatusSource source() const {
81 return static_cast<DelegateStatusSource>(source_);
82 }
83
84 int32_t code() const { return code_; }
85
86 private:
87 // value of a DelegateStatusSource, like DelegateStatusSource::TFLITE_GPU
88 int32_t source_;
89 // value of a status code, like kTfLiteOk.
90 int32_t code_;
91};
92
93// Used by delegates to report their configuration/settings to TFLite.
94// Calling this method adds a new GENERAL_RUNTIME_INSTRUMENTATION_EVENT to
95// the runtime Profiler.
96TfLiteStatus ReportDelegateSettings(TfLiteContext* context,
97 TfLiteDelegate* delegate,
98 const TFLiteSettings& settings);
99
100// Used by delegates to report their status to the TFLite runtime.
101// Calling this method adds a new GENERAL_RUNTIME_INSTRUMENTATION_EVENT to
102// the runtime Profiler.
103TfLiteStatus ReportDelegateStatus(TfLiteContext* context,
104 TfLiteDelegate* delegate,
105 const DelegateStatus& status);
106
107} // namespace delegates
108} // namespace tflite
109
110#endif // TENSORFLOW_LITE_DELEGATES_TELEMETRY_H_
111