diff options
author | David Gross <dgross@google.com> | 2019-01-23 14:59:10 -0800 |
---|---|---|
committer | David Gross <dgross@google.com> | 2019-01-24 03:55:01 +0000 |
commit | a9eee61faee6b43cc3a6a0b1b5ce211dc42ad003 (patch) | |
tree | 8c08f2e4cd93b8d46af50c8a24ea2ead55fedc83 /nn/runtime/test/TestValidation.cpp | |
parent | eeea25d73105bab339a41f1e948107d300e300f0 (diff) | |
download | ml-a9eee61faee6b43cc3a6a0b1b5ce211dc42ad003.tar.gz |
Update API to allow collecting execution duration.
Test: NeuralNetworksTest_static
Test: --gtest_filter=IntrospectionControlTest.SimpleAddModel with
setprop debug.nn.vlog all; by inspection of logcat, verify that
driver and runtime report the same duration
TODO:
- Ensure duration is reported correctly on error paths and on cpu
fallback
- Check for more misuses of API:
- Ensure that we only request measurement before execution starts
- Ensure that we only request measurement for the special introspection case
- Ensure that we only request duration after execution completes
- Add more extensive timing tests using the introspection/control APIs
Bug: 115390094
Change-Id: I1dd61e4128963a22955ebab18bcdf7293419ca20
Diffstat (limited to 'nn/runtime/test/TestValidation.cpp')
-rw-r--r-- | nn/runtime/test/TestValidation.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/nn/runtime/test/TestValidation.cpp b/nn/runtime/test/TestValidation.cpp index 855850835..5a5e70ff8 100644 --- a/nn/runtime/test/TestValidation.cpp +++ b/nn/runtime/test/TestValidation.cpp @@ -1118,4 +1118,79 @@ TEST_F(ValidationTestInvalidCompilation, CreateExecutionWithInvalidCompilation) ANEURALNETWORKS_BAD_STATE); } +TEST_F(ValidationTestCompilationForDevices, ExecutionTiming) { + ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR); + + // Assume there's a single device. + // TODO: + // - Validate that we fail if there are multiple devices. + // - Validate that we fail if we have Compilation rather than CompilationForDevices. + + ANeuralNetworksExecution* execution; + ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR); + + EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(nullptr, false), + ANEURALNETWORKS_UNEXPECTED_NULL); + EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(nullptr, true), + ANEURALNETWORKS_UNEXPECTED_NULL); + EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, false), + ANEURALNETWORKS_NO_ERROR); + EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, true), ANEURALNETWORKS_NO_ERROR); + + // TODO: + // - Validate that we cannot setMeasureTiming if the execution has started + // - Validate that we cannot getDuration until the execution has finished + + float in0 = 0.0f, in1 = 1.0f, out0 = 0.0f; + int in2 = 0; + ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, &in0, sizeof(in0)), + ANEURALNETWORKS_NO_ERROR); + ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, &in1, sizeof(in1)), + ANEURALNETWORKS_NO_ERROR); + ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, &in2, sizeof(in2)), + ANEURALNETWORKS_NO_ERROR); + ASSERT_EQ(ANeuralNetworksExecution_setOutput(execution, 0, nullptr, &out0, sizeof(out0)), + ANEURALNETWORKS_NO_ERROR); + ASSERT_EQ(ANeuralNetworksExecution_compute(execution), ANEURALNETWORKS_NO_ERROR); + + auto testDuration = [](ANeuralNetworksExecution* e, int32_t durationCode, bool nullDuration) { + SCOPED_TRACE(e); + SCOPED_TRACE(durationCode); + SCOPED_TRACE(nullDuration); + + // Strictly speaking, a duration COULD have this value, but it is + // exceedingly unlikely. We'll use it as an initial value that we expect + // to be modified by getDuration(). + const uint64_t kBogusDuration = UINT64_MAX - 1; + + uint64_t duration = kBogusDuration; + uint64_t* durationPtr = nullDuration ? nullptr : &duration; + + int expectedResultCode = ANEURALNETWORKS_NO_ERROR; + if (e == nullptr | durationPtr == nullptr) { + expectedResultCode = ANEURALNETWORKS_UNEXPECTED_NULL; + } else if (durationCode < 0) { + expectedResultCode = ANEURALNETWORKS_BAD_DATA; + } + + EXPECT_EQ(ANeuralNetworksExecution_getDuration(e, durationCode, durationPtr), + expectedResultCode); + if (expectedResultCode == ANEURALNETWORKS_NO_ERROR) { + EXPECT_NE(duration, kBogusDuration); + } + }; + + std::vector<ANeuralNetworksExecution*> executions = {nullptr, execution}; + std::vector<int32_t> durationCodes = {-1, ANEURALNETWORKS_DURATION_ON_HARDWARE, + ANEURALNETWORKS_DURATION_IN_DRIVER}; + std::vector<bool> nullDurations = {false, true}; + for (auto e : executions) { + for (auto d : durationCodes) { + for (auto n : nullDurations) { + testDuration(e, d, n); + } + } + } +} + } // namespace |