mirror of
https://github.com/android/ndk-samples
synced 2025-11-07 16:51:54 +08:00
Update nn-samples with dependency APIs.
This commit is contained in:
@@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
|
|||||||
android {
|
android {
|
||||||
compileSdkVersion 'android-R'
|
compileSdkVersion 'android-R'
|
||||||
buildToolsVersion '30.0.0 rc1'
|
buildToolsVersion '30.0.0 rc1'
|
||||||
ndkVersion '21.1.6210238'
|
ndkVersion '21.1.6273396'
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.example.android.basic"
|
applicationId "com.example.android.basic"
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ buildscript {
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:4.1.0-alpha01'
|
classpath 'com.android.tools.build:gradle:4.1.0-alpha02'
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#Wed Mar 04 14:46:09 PST 2020
|
#Tue Mar 17 14:03:57 PDT 2020
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
|
|||||||
android {
|
android {
|
||||||
compileSdkVersion 'android-R'
|
compileSdkVersion 'android-R'
|
||||||
buildToolsVersion '30.0.0 rc1'
|
buildToolsVersion '30.0.0 rc1'
|
||||||
ndkVersion '21.1.6210238'
|
ndkVersion '21.1.6273396'
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.example.android.sequence"
|
applicationId "com.example.android.sequence"
|
||||||
|
|||||||
@@ -557,9 +557,9 @@ bool SimpleSequenceModel::CreateOpaqueMemories() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute a single step of accumulating the geometric progression.
|
* Dispatch a single computation step of accumulating the geometric progression.
|
||||||
*/
|
*/
|
||||||
static bool ComputeSingleStep(ANeuralNetworksCompilation* compilation,
|
static bool DispatchSingleStep(ANeuralNetworksCompilation* compilation,
|
||||||
ANeuralNetworksMemory* sumIn,
|
ANeuralNetworksMemory* sumIn,
|
||||||
uint32_t sumInLength,
|
uint32_t sumInLength,
|
||||||
ANeuralNetworksMemory* stateIn,
|
ANeuralNetworksMemory* stateIn,
|
||||||
@@ -567,7 +567,9 @@ static bool ComputeSingleStep(ANeuralNetworksCompilation* compilation,
|
|||||||
ANeuralNetworksMemory* sumOut,
|
ANeuralNetworksMemory* sumOut,
|
||||||
uint32_t sumOutLength,
|
uint32_t sumOutLength,
|
||||||
ANeuralNetworksMemory* stateOut,
|
ANeuralNetworksMemory* stateOut,
|
||||||
uint32_t stateOutLength) {
|
uint32_t stateOutLength,
|
||||||
|
const ANeuralNetworksEvent* waitFor,
|
||||||
|
ANeuralNetworksEvent** event) {
|
||||||
// Create an ANeuralNetworksExecution object from the compiled model.
|
// Create an ANeuralNetworksExecution object from the compiled model.
|
||||||
ANeuralNetworksExecution* execution;
|
ANeuralNetworksExecution* execution;
|
||||||
int32_t status = ANeuralNetworksExecution_create(compilation, &execution);
|
int32_t status = ANeuralNetworksExecution_create(compilation, &execution);
|
||||||
@@ -642,9 +644,20 @@ static bool ComputeSingleStep(ANeuralNetworksCompilation* compilation,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the execution of the model.
|
// Dispatch the execution of the model.
|
||||||
// Note that the execution here is synchronous.
|
// Note that the execution here is asynchronous with dependencies.
|
||||||
status = ANeuralNetworksExecution_compute(execution);
|
constexpr uint64_t kTimeOutDurationInNs = 100'000'000; // 100 ms
|
||||||
|
const ANeuralNetworksEvent* const* dependencies = nullptr;
|
||||||
|
uint32_t numDependencies = 0;
|
||||||
|
if (waitFor != nullptr) {
|
||||||
|
dependencies = &waitFor;
|
||||||
|
numDependencies = 1;
|
||||||
|
}
|
||||||
|
status = ANeuralNetworksExecution_startComputeWithDependencies(execution,
|
||||||
|
dependencies,
|
||||||
|
numDependencies,
|
||||||
|
kTimeOutDurationInNs,
|
||||||
|
event);
|
||||||
if (status != ANEURALNETWORKS_NO_ERROR) {
|
if (status != ANEURALNETWORKS_NO_ERROR) {
|
||||||
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
|
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
|
||||||
"ANeuralNetworksExecution_compute failed");
|
"ANeuralNetworksExecution_compute failed");
|
||||||
@@ -679,6 +692,9 @@ bool SimpleSequenceModel::Compute(float initialValue,
|
|||||||
fillMemory(sumInFd_, tensorSize_, 0);
|
fillMemory(sumInFd_, tensorSize_, 0);
|
||||||
fillMemory(initialStateFd_, tensorSize_, initialValue);
|
fillMemory(initialStateFd_, tensorSize_, initialValue);
|
||||||
|
|
||||||
|
// The event objects for all computation steps.
|
||||||
|
std::vector<ANeuralNetworksEvent*> events(steps, nullptr);
|
||||||
|
|
||||||
for (uint32_t i = 0; i < steps; i++) {
|
for (uint32_t i = 0; i < steps; i++) {
|
||||||
// We will only use ASharedMemory for boundary step executions, and use
|
// We will only use ASharedMemory for boundary step executions, and use
|
||||||
// opaque memories for intermediate results to minimize the data copying.
|
// opaque memories for intermediate results to minimize the data copying.
|
||||||
@@ -711,7 +727,10 @@ bool SimpleSequenceModel::Compute(float initialValue,
|
|||||||
stateOutMemory = memoryOpaqueStateOut_;
|
stateOutMemory = memoryOpaqueStateOut_;
|
||||||
stateOutLength = 0;
|
stateOutLength = 0;
|
||||||
|
|
||||||
if (!ComputeSingleStep(compilation_,
|
// Dispatch a single computation step with a dependency on the previous step, if any.
|
||||||
|
// The actual computation will start once its dependency has finished.
|
||||||
|
const ANeuralNetworksEvent* waitFor = i == 0 ? nullptr : events[i - 1];
|
||||||
|
if (!DispatchSingleStep(compilation_,
|
||||||
sumInMemory,
|
sumInMemory,
|
||||||
sumInLength,
|
sumInLength,
|
||||||
stateInMemory,
|
stateInMemory,
|
||||||
@@ -719,10 +738,12 @@ bool SimpleSequenceModel::Compute(float initialValue,
|
|||||||
sumOutMemory,
|
sumOutMemory,
|
||||||
sumOutLength,
|
sumOutLength,
|
||||||
stateOutMemory,
|
stateOutMemory,
|
||||||
stateOutLength)) {
|
stateOutLength,
|
||||||
|
waitFor,
|
||||||
|
&events[i])) {
|
||||||
__android_log_print(ANDROID_LOG_ERROR,
|
__android_log_print(ANDROID_LOG_ERROR,
|
||||||
LOG_TAG,
|
LOG_TAG,
|
||||||
"ComputeSingleStep failed for step %d",
|
"DispatchSingleStep failed for step %d",
|
||||||
i);
|
i);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -733,6 +754,9 @@ bool SimpleSequenceModel::Compute(float initialValue,
|
|||||||
std::swap(memoryOpaqueStateIn_, memoryOpaqueStateOut_);
|
std::swap(memoryOpaqueStateIn_, memoryOpaqueStateOut_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since the events are chained, we only need to wait for the last one.
|
||||||
|
ANeuralNetworksEvent_wait(events.back());
|
||||||
|
|
||||||
// Get the results.
|
// Get the results.
|
||||||
float* outputTensorPtr = reinterpret_cast<float*>(
|
float* outputTensorPtr = reinterpret_cast<float*>(
|
||||||
mmap(nullptr,
|
mmap(nullptr,
|
||||||
@@ -743,6 +767,11 @@ bool SimpleSequenceModel::Compute(float initialValue,
|
|||||||
0));
|
0));
|
||||||
*result = outputTensorPtr[0];
|
*result = outputTensorPtr[0];
|
||||||
munmap(outputTensorPtr, tensorSize_ * sizeof(float));
|
munmap(outputTensorPtr, tensorSize_ * sizeof(float));
|
||||||
|
|
||||||
|
// Cleanup event objects.
|
||||||
|
for (auto* event : events) {
|
||||||
|
ANeuralNetworksEvent_free(event);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user