summaryrefslogtreecommitdiff
path: root/nn/runtime/test/TestGenerated.cpp
diff options
context:
space:
mode:
authorXusong Wang <xusongw@google.com>2019-08-23 12:57:32 -0700
committerXusong Wang <xusongw@google.com>2019-08-26 13:45:42 -0700
commit9a3ae317cd6f2acbd64ec7e4a8afc0ea4e8b481f (patch)
treed242d20c0dd7f69cd31949b90d29bdfbba4fd2f9 /nn/runtime/test/TestGenerated.cpp
parent3d9b0a0b1486a76bb0d1eb3c15ca368af19e0e08 (diff)
downloadml-9a3ae317cd6f2acbd64ec7e4a8afc0ea4e8b481f.tar.gz
Reduce compilation time by dynamic registration.
Prior to this CL, the test generator will generate CTS and VTS test files with TEST_F definitions for each spec file. This CL removes all the generated source codes for test definitions by employing dynamic registration: * In each example file, each generated test model is registered to the TestModelManager from a dummy global variable initialization. * Create value-parameterized test suites to instantiate generated tests from the registered test models. Four test suites are affected: * GeneratedTests.<test_name> -> TestGenerated/GeneratedTests.Test/<test_name> * DynamicOutputShapeTest.<test_name> -> TestGenerated/DynamicOutputShapeTest.Test/<test_name> * GeneratedValidationTests.<test_name> -> TestGenerated/GeneratedValidationTests.Test/<test_name> * ComplianceTest.<test_name> -> TestGenerated/GeneratedComplianceTest.Test/<test_name> Or in CTS * ComputeMode/GeneratedTests.<test_name>/[0-2] -> TestGenerated/GeneratedTests.{Sync,Async,Burst}/<test_name> This CL additionally fixes the issue that DynamicOutputShapeTest and GeneratedValidationTests were missing from CTS. This CL will reduce the NNAPI test compliation time. Before: $ rm -r generated/spec_* && specs/generate_all_tests.sh $ m NeuralNetworksTest_static -j$(nproc) #### build completed successfully (02:27 (mm:ss)) #### After: $ rm -r generated/spec_* && specs/generate_all_tests.sh $ m NeuralNetworksTest_static -j$(nproc) #### build completed successfully (58 seconds) #### Bug: 120601396 Test: NNT_static Change-Id: Ib1d934228ca24fbc9fb01557551d78eff96cb9db
Diffstat (limited to 'nn/runtime/test/TestGenerated.cpp')
-rw-r--r--nn/runtime/test/TestGenerated.cpp94
1 files changed, 81 insertions, 13 deletions
diff --git a/nn/runtime/test/TestGenerated.cpp b/nn/runtime/test/TestGenerated.cpp
index 478844af9..534c46cee 100644
--- a/nn/runtime/test/TestGenerated.cpp
+++ b/nn/runtime/test/TestGenerated.cpp
@@ -26,10 +26,12 @@
#include <fstream>
#include <iostream>
#include <map>
+#include <string>
#include <thread>
#include <vector>
#include "TestHarness.h"
+#include "TestNeuralNetworksWrapper.h"
// Systrace is not available from CTS tests due to platform layering
// constraints. We reuse the NNTEST_ONLY_PUBLIC_API flag, as that should also be
@@ -42,10 +44,46 @@
#define NNTRACE_APP_SWITCH(...)
#endif
-namespace generated_tests {
-using namespace android::nn::test_wrapper;
+#ifdef NNTEST_CTS
+#define NNTEST_COMPUTE_MODE
+#endif
+
+namespace android::nn::generated_tests {
+using namespace test_wrapper;
using namespace test_helper;
+class GeneratedTests : public GeneratedTestBase {
+ protected:
+ void SetUp() override;
+ void TearDown() override;
+
+ std::optional<Compilation> compileModel(const Model* model);
+ void executeWithCompilation(const Compilation* compilation, const TestModel& testModel);
+ void executeOnce(const Model* model, const TestModel& testModel);
+ void executeMultithreadedOwnCompilation(const Model* model, const TestModel& testModel);
+ void executeMultithreadedSharedCompilation(const Model* model, const TestModel& testModel);
+ // Test driver for those generated from ml/nn/runtime/test/spec
+ void execute(const TestModel& testModel);
+
+ std::string mCacheDir;
+ std::vector<uint8_t> mToken;
+ bool mTestCompilationCaching = false;
+ bool mTestDynamicOutputShape = false;
+ bool mExpectFailure = false;
+};
+
+// Tag for the dynamic output shape tests
+class DynamicOutputShapeTest : public GeneratedTests {
+ protected:
+ DynamicOutputShapeTest() { mTestDynamicOutputShape = true; }
+};
+
+// Tag for the generated validation tests
+class GeneratedValidationTests : public GeneratedTests {
+ protected:
+ GeneratedValidationTests() { mExpectFailure = true; }
+};
+
static OperandType getOperandType(const TestOperand& op, bool testDynamicOutputShape) {
auto dims = op.dimensions;
if (testDynamicOutputShape && op.lifetime == TestOperandLifeTime::MODEL_OUTPUT) {
@@ -243,9 +281,7 @@ void GeneratedTests::execute(const TestModel& testModel) {
}
void GeneratedTests::SetUp() {
-#ifdef NNTEST_COMPUTE_MODE
- mOldComputeMode = Execution::setComputeMode(GetParam());
-#endif
+ GeneratedTestBase::SetUp();
char cacheDirTemp[] = "/data/local/tmp/TestCompilationCachingXXXXXX";
char* cacheDir = mkdtemp(cacheDirTemp);
ASSERT_NE(cacheDir, nullptr);
@@ -254,9 +290,6 @@ void GeneratedTests::SetUp() {
}
void GeneratedTests::TearDown() {
-#ifdef NNTEST_COMPUTE_MODE
- Execution::setComputeMode(mOldComputeMode);
-#endif
if (!::testing::Test::HasFailure()) {
// TODO: Switch to std::filesystem::remove_all once libc++fs is made available in CTS.
// Remove the cache directory specified by path recursively.
@@ -265,13 +298,48 @@ void GeneratedTests::TearDown() {
};
nftw(mCacheDir.c_str(), callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
}
+ GeneratedTestBase::TearDown();
}
#ifdef NNTEST_COMPUTE_MODE
-INSTANTIATE_TEST_SUITE_P(ComputeMode, GeneratedTests,
- testing::Values(Execution::ComputeMode::SYNC,
- Execution::ComputeMode::ASYNC,
- Execution::ComputeMode::BURST));
+TEST_P(GeneratedTests, Sync) {
+ const auto oldComputeMode = Execution::setComputeMode(Execution::ComputeMode::SYNC);
+ execute(*mTestModel);
+ Execution::setComputeMode(oldComputeMode);
+}
+
+TEST_P(GeneratedTests, Async) {
+ const auto oldComputeMode = Execution::setComputeMode(Execution::ComputeMode::ASYNC);
+ execute(*mTestModel);
+ Execution::setComputeMode(oldComputeMode);
+}
+
+TEST_P(GeneratedTests, Burst) {
+ const auto oldComputeMode = Execution::setComputeMode(Execution::ComputeMode::BURST);
+ execute(*mTestModel);
+ Execution::setComputeMode(oldComputeMode);
+}
+#else
+TEST_P(GeneratedTests, Test) {
+ execute(*mTestModel);
+}
#endif
-} // namespace generated_tests
+TEST_P(DynamicOutputShapeTest, Test) {
+ execute(*mTestModel);
+}
+
+TEST_P(GeneratedValidationTests, Test) {
+ execute(*mTestModel);
+}
+
+INSTANTIATE_GENERATED_TEST(GeneratedTests,
+ [](const TestModel& testModel) { return !testModel.expectFailure; });
+
+INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest,
+ [](const TestModel& testModel) { return !testModel.expectFailure; });
+
+INSTANTIATE_GENERATED_TEST(GeneratedValidationTests,
+ [](const TestModel& testModel) { return testModel.expectFailure; });
+
+} // namespace android::nn::generated_tests