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 #include <android-base/scopeguard.h>
18 #include <gtest/gtest.h>
19 #include <utility>
20 #include <vector>
21 
22 #include "Telemetry.h"
23 #include "TestNeuralNetworksWrapper.h"
24 
25 using android::nn::telemetry::DataClass;
26 using android::nn::test_wrapper::Compilation;
27 using android::nn::test_wrapper::Execution;
28 using android::nn::test_wrapper::Model;
29 using android::nn::test_wrapper::OperandType;
30 using android::nn::test_wrapper::Result;
31 using android::nn::test_wrapper::Type;
32 
33 namespace {
34 
35 typedef float Matrix3x4[3][4];
36 
37 class TelemetryTest : public ::testing::Test {};
38 
TEST_F(TelemetryTest,TestAtomGeneration)39 TEST_F(TelemetryTest, TestAtomGeneration) {
40     std::atomic_uint executions = 0;
41     std::atomic_uint compilations = 0;
42 
43     android::nn::telemetry::registerTelemetryCallbacks(
44             [&compilations](const android::nn::telemetry::DiagnosticCompilationInfo*) {
45                 compilations++;
46             },
47             [&executions](const android::nn::telemetry::DiagnosticExecutionInfo*) {
48                 executions++;
49             });
50 
51     Model modelAdd2;
52     OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4});
53     OperandType scalarType(Type::INT32, {});
54     auto a = modelAdd2.addOperand(&matrixType);
55     auto b = modelAdd2.addOperand(&matrixType);
56     auto c = modelAdd2.addOperand(&matrixType);
57     auto d = modelAdd2.addConstantOperand(&scalarType, ANEURALNETWORKS_FUSED_NONE);
58     modelAdd2.addOperation(ANEURALNETWORKS_ADD, {a, b, d}, {c});
59     modelAdd2.identifyInputsAndOutputs({a, b}, {c});
60     ASSERT_TRUE(modelAdd2.isValid());
61     modelAdd2.finish();
62 
63     Matrix3x4 matrix;
64     memset(&matrix, 0, sizeof(matrix));
65     Compilation compilation(&modelAdd2);
66     compilation.finish();
67     Execution execution(&compilation);
68     ASSERT_EQ(execution.setInput(0, matrix, sizeof(Matrix3x4)), Result::NO_ERROR);
69     ASSERT_EQ(execution.setInput(1, matrix, sizeof(Matrix3x4)), Result::NO_ERROR);
70     ASSERT_EQ(execution.setOutput(0, matrix, sizeof(Matrix3x4)), Result::NO_ERROR);
71     ASSERT_EQ(execution.compute(), Result::NO_ERROR);
72     ASSERT_EQ(executions, 1u);
73     ASSERT_EQ(compilations, 1u);
74 
75     android::nn::telemetry::clearTelemetryCallbacks();
76 }
77 
TEST_F(TelemetryTest,TestEvalDataClass)78 TEST_F(TelemetryTest, TestEvalDataClass) {
79     std::vector<std::pair<DataClass, std::vector<android::nn::OperandType>>> data = {
80             {DataClass::FLOAT32, {android::nn::OperandType::TENSOR_FLOAT32}},
81             {DataClass::FLOAT32,
82              {android::nn::OperandType::TENSOR_FLOAT32, android::nn::OperandType::FLOAT32}},
83             {DataClass::FLOAT32,
84              {android::nn::OperandType::FLOAT32, android::nn::OperandType::TENSOR_FLOAT32}},
85             {DataClass::OTHER, {android::nn::OperandType::FLOAT32}},
86             {DataClass::UNKNOWN, {}},
87             {DataClass::FLOAT16,
88              {android::nn::OperandType::FLOAT32, android::nn::OperandType::TENSOR_FLOAT16,
89               android::nn::OperandType::TENSOR_INT32}},
90             {DataClass::MIXED,
91              {android::nn::OperandType::FLOAT32, android::nn::OperandType::TENSOR_FLOAT16,
92               android::nn::OperandType::TENSOR_FLOAT32}},
93             {DataClass::QUANT,
94              {android::nn::OperandType::FLOAT32, android::nn::OperandType::TENSOR_QUANT8_ASYMM}},
95     };
96 
97     for (auto& pair : data) {
98         DataClass result = DataClass::UNKNOWN;
99         for (auto v : pair.second) {
100             result = android::nn::telemetry::evalDataClass(v, result);
101         }
102         ASSERT_EQ(result, pair.first);
103     }
104 }
105 
106 }  // namespace
107