1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TELEMETRY_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TELEMETRY_H
19 
20 #include <functional>
21 #include <string>
22 
23 #include "CompilationBuilder.h"
24 #include "ExecutionBuilder.h"
25 
26 namespace android::nn::telemetry {
27 
28 // Generate telemetry event on successful compilation
29 void onCompilationFinish(CompilationBuilder* c, int resultCode);
30 
31 // Generate telemetry event on successful execution
32 void onExecutionFinish(ExecutionBuilder* e, ExecutionMode executionMode, int resultCode);
33 
34 // Data class of inputs and outputs
35 enum class DataClass {
36     UNKNOWN = 0,
37     OTHER = 1,
38     FLOAT32 = 2,
39     FLOAT16 = 3,
40     QUANT = 4,
41     MIXED = 5,
42 };
43 
44 // Infer data class of operand set
45 DataClass evalDataClass(const OperandType& op, DataClass previousDataClass);
46 
47 // Get the ID that identifies a single session of client interacting with NNAPI runtime.
48 int32_t getSessionId();
49 
50 struct DiagnosticCompilationInfo {
51     // The hash of the model architecture (without weights).
52     const uint8_t* modelArchHash;
53     // The device IDs as a comma-concatenated string.
54     const std::string deviceId;
55     // The error code during compilation.
56     int32_t errorCode;
57     // Data class of the input to the model.
58     DataClass inputDataClass;
59     // Data class of the output from the model.
60     DataClass outputDataClass;
61     // Duration of the compilation in the runtime.
62     // UINT64_MAX indicates no timing information is available.
63     uint64_t compilationTimeNanos;
64     // Did the compilation fallback to the CPU?
65     bool fallbackToCpuFromError;
66     // Is the client compiling with explicit set of devices?
67     bool introspectionEnabled;
68     // Is caching enabled?
69     bool cacheEnabled;
70     // Is control flow used?
71     bool hasControlFlow;
72     // Are dynamic tensors used?
73     bool hasDynamicTemporaries;
74 };
75 
76 struct DiagnosticExecutionInfo {
77     // The hash of the model architecture (without weights).
78     const uint8_t* modelArchHash;
79     // The device IDs as a comma-concatenated string.
80     const std::string deviceId;
81     // Execution mode (e.g. Sync, Burst)
82     ExecutionMode executionMode;
83     // Data class of the input to the model.
84     DataClass inputDataClass;
85     // Data class of the output from the model.
86     DataClass outputDataClass;
87     // The error code during compilation.
88     int32_t errorCode;
89     // Duration of the execution in the runtime.
90     // UINT64_MAX indicates no timing information is available.
91     uint64_t durationRuntimeNanos;
92     // Duration of the execution in the service driver.
93     // UINT64_MAX indicates no timing information is available.
94     uint64_t durationDriverNanos;
95     // Duration of the execution running on the hardware.
96     // UINT64_MAX indicates no timing information is available.
97     uint64_t durationHardwareNanos;
98     // Is the client compiling with explicit set of devices?
99     bool introspectionEnabled;
100     // Is caching enabled?
101     bool cacheEnabled;
102     // Is control flow used?
103     bool hasControlFlow;
104     // Are dynamic tensors used?
105     bool hasDynamicTemporaries;
106 };
107 
108 void registerTelemetryCallbacks(std::function<void(const DiagnosticCompilationInfo*)> compilation,
109                                 std::function<void(const DiagnosticExecutionInfo*)> execution);
110 void clearTelemetryCallbacks();
111 
112 }  // namespace android::nn::telemetry
113 
114 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TELEMETRY_H
115