summaryrefslogtreecommitdiff
path: root/neuralnetworks/utils/adapter/aidl/src/Burst.cpp
blob: 4fabb206358e49b38f38c09e3414a58f48e44dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Burst.h"

#include <android-base/logging.h>
#include <android-base/thread_annotations.h>
#include <android/binder_auto_utils.h>
#include <nnapi/IBurst.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/aidl/Conversions.h>
#include <nnapi/hal/aidl/Utils.h>

#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <utility>
#include <variant>

namespace aidl::android::hardware::neuralnetworks::adapter {
namespace {

using Value = Burst::ThreadSafeMemoryCache::Value;

template <typename Type>
auto convertInput(const Type& object) -> decltype(nn::convert(std::declval<Type>())) {
    auto result = nn::convert(object);
    if (!result.has_value()) {
        result.error().code = nn::ErrorStatus::INVALID_ARGUMENT;
    }
    return result;
}

nn::Duration makeDuration(int64_t durationNs) {
    return nn::Duration(std::chrono::nanoseconds(durationNs));
}

nn::GeneralResult<nn::OptionalDuration> makeOptionalDuration(int64_t durationNs) {
    if (durationNs < -1) {
        return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid duration " << durationNs;
    }
    return durationNs < 0 ? nn::OptionalDuration{} : makeDuration(durationNs);
}

nn::GeneralResult<nn::OptionalTimePoint> makeOptionalTimePoint(int64_t durationNs) {
    if (durationNs < -1) {
        return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid time point " << durationNs;
    }
    return durationNs < 0 ? nn::OptionalTimePoint{} : nn::TimePoint(makeDuration(durationNs));
}

std::vector<nn::IBurst::OptionalCacheHold> ensureAllMemoriesAreCached(
        nn::Request* request, const std::vector<int64_t>& memoryIdentifierTokens,
        const nn::IBurst& burst, const Burst::ThreadSafeMemoryCache& cache) {
    std::vector<nn::IBurst::OptionalCacheHold> holds;
    holds.reserve(memoryIdentifierTokens.size());

    for (size_t i = 0; i < memoryIdentifierTokens.size(); ++i) {
        const auto& pool = request->pools[i];
        const auto token = memoryIdentifierTokens[i];
        constexpr int64_t kNoToken = -1;
        if (token == kNoToken || !std::holds_alternative<nn::SharedMemory>(pool)) {
            continue;
        }

        const auto& memory = std::get<nn::SharedMemory>(pool);
        auto [storedMemory, hold] = cache.add(token, memory, burst);

        request->pools[i] = std::move(storedMemory);
        holds.push_back(std::move(hold));
    }

    return holds;
}

nn::ExecutionResult<ExecutionResult> executeSynchronously(
        const nn::IBurst& burst, const Burst::ThreadSafeMemoryCache& cache, const Request& request,
        const std::vector<int64_t>& memoryIdentifierTokens, bool measureTiming, int64_t deadlineNs,
        int64_t loopTimeoutDurationNs) {
    if (request.pools.size() != memoryIdentifierTokens.size()) {
        return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
               << "request.pools.size() != memoryIdentifierTokens.size()";
    }
    if (!std::all_of(memoryIdentifierTokens.begin(), memoryIdentifierTokens.end(),
                     [](int64_t token) { return token >= -1; })) {
        return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid memoryIdentifierTokens";
    }

    auto nnRequest = NN_TRY(convertInput(request));
    const auto nnMeasureTiming = measureTiming ? nn::MeasureTiming::YES : nn::MeasureTiming::NO;
    const auto nnDeadline = NN_TRY(makeOptionalTimePoint(deadlineNs));
    const auto nnLoopTimeoutDuration = NN_TRY(makeOptionalDuration(loopTimeoutDurationNs));

    const auto hold = ensureAllMemoriesAreCached(&nnRequest, memoryIdentifierTokens, burst, cache);

    const auto result =
            burst.execute(nnRequest, nnMeasureTiming, nnDeadline, nnLoopTimeoutDuration);

    if (!result.ok() && result.error().code == nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
        const auto& [message, code, outputShapes] = result.error();
        return ExecutionResult{.outputSufficientSize = false,
                               .outputShapes = utils::convert(outputShapes).value(),
                               .timing = {.timeInDriverNs = -1, .timeOnDeviceNs = -1}};
    }

    const auto& [outputShapes, timing] = NN_TRY(result);
    return ExecutionResult{.outputSufficientSize = true,
                           .outputShapes = utils::convert(outputShapes).value(),
                           .timing = utils::convert(timing).value()};
}

}  // namespace

Value Burst::ThreadSafeMemoryCache::add(int64_t token, const nn::SharedMemory& memory,
                                        const nn::IBurst& burst) const {
    std::lock_guard guard(mMutex);
    if (const auto it = mCache.find(token); it != mCache.end()) {
        return it->second;
    }
    auto hold = burst.cacheMemory(memory);
    auto [it, _] = mCache.emplace(token, std::make_pair(memory, std::move(hold)));
    return it->second;
}

void Burst::ThreadSafeMemoryCache::remove(int64_t token) const {
    std::lock_guard guard(mMutex);
    mCache.erase(token);
}

Burst::Burst(nn::SharedBurst burst) : kBurst(std::move(burst)) {
    CHECK(kBurst != nullptr);
}

ndk::ScopedAStatus Burst::executeSynchronously(const Request& request,
                                               const std::vector<int64_t>& memoryIdentifierTokens,
                                               bool measureTiming, int64_t deadlineNs,
                                               int64_t loopTimeoutDurationNs,
                                               ExecutionResult* executionResult) {
    auto result =
            adapter::executeSynchronously(*kBurst, kMemoryCache, request, memoryIdentifierTokens,
                                          measureTiming, deadlineNs, loopTimeoutDurationNs);
    if (!result.has_value()) {
        auto [message, code, _] = std::move(result).error();
        const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
        return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
                static_cast<int32_t>(aidlCode), message.c_str());
    }
    *executionResult = std::move(result).value();
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Burst::releaseMemoryResource(int64_t memoryIdentifierToken) {
    if (memoryIdentifierToken < -1) {
        return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
                static_cast<int32_t>(ErrorStatus::INVALID_ARGUMENT),
                "Invalid memoryIdentifierToken");
    }
    kMemoryCache.remove(memoryIdentifierToken);
    return ndk::ScopedAStatus::ok();
}

}  // namespace aidl::android::hardware::neuralnetworks::adapter