summaryrefslogtreecommitdiff
path: root/nn/common/include/nnapi/Types.h
blob: 5adab6bfa1281acb971eaa557e1aa5303f3a357f (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (C) 2020 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.
 */

#ifndef ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPES_H
#define ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPES_H

#include <android-base/expected.h>
#include <android-base/unique_fd.h>

#include <array>
#include <chrono>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>

#include "nnapi/OperandTypes.h"
#include "nnapi/OperationTypes.h"
#include "nnapi/Result.h"

namespace android::nn {

// Forward declarations

class IBuffer;
class IDevice;
class IPreparedModel;

// Default values

constexpr uint64_t kNoTiming = std::numeric_limits<uint64_t>::max();
constexpr float kDefaultExecTime = std::numeric_limits<float>::max();
constexpr float kDefaultPowerUsage = std::numeric_limits<float>::max();
constexpr uint32_t kByteSizeOfCacheToken = 32;
constexpr uint32_t kMaxNumberOfCacheFiles = 32;
constexpr uint8_t kExtensionTypeBits = 16;
constexpr uint8_t kExtensionPrefixBits = 16;

// Aliases

using AlignedData = std::max_align_t;
using SharedBuffer = std::shared_ptr<const IBuffer>;
using SharedDevice = std::shared_ptr<const IDevice>;
using SharedPreparedModel = std::shared_ptr<const IPreparedModel>;

// Canonical types

enum class DeviceStatus {
    AVAILABLE = 0,
    BUSY = 1,
    OFFLINE = 2,
    UNKNOWN = 3,
};

enum class ExecutionPreference {
    LOW_POWER = 0,
    FAST_SINGLE_ANSWER = 1,
    SUSTAINED_SPEED = 2,
    DEFAULT = FAST_SINGLE_ANSWER,
};

enum class DeviceType {
    UNKNOWN = 0,
    OTHER = 1,
    CPU = 2,
    GPU = 3,
    ACCELERATOR = 4,
};

enum class MeasureTiming {
    NO = 0,
    YES = 1,
};

enum class Priority {
    LOW = 0,
    MEDIUM = 1,
    HIGH = 2,
    DEFAULT = MEDIUM,
};

// TODO: Should more errors from NeuralNetworks.h be incorporated? The left name shows errors that
// appear in NeuralNetworks.h but not in the HAL, and the right column shows what these values could
// map to:
// * OUT_OF_MEMORY ==> GENERAL_FAILURE / RESOURCE_EXHAUSTED_*
// * INCOMPLETE ==> GENERAL_FAILURE
// * UNEXPECTED_NULL ==> INVALID_ARGUMENT
// * UNMAPPABLE ==> GENERAL_FAILURE
// * BAD_STATE ==> INVALID_ARGUMENT
enum class ErrorStatus {
    NONE = 0,
    DEVICE_UNAVAILABLE = 1,
    GENERAL_FAILURE = 2,
    OUTPUT_INSUFFICIENT_SIZE = 3,
    INVALID_ARGUMENT = 4,
    MISSED_DEADLINE_TRANSIENT = 5,
    MISSED_DEADLINE_PERSISTENT = 6,
    RESOURCE_EXHAUSTED_TRANSIENT = 7,
    RESOURCE_EXHAUSTED_PERSISTENT = 8,
    DEAD_OBJECT = 10000,
};

struct GeneralError {
    std::string message;
    ErrorStatus code = ErrorStatus::GENERAL_FAILURE;
};

template <typename Type>
using GeneralResult = base::expected<Type, GeneralError>;

enum class FusedActivationFunc : int32_t {
    NONE = 0,
    RELU = 1,
    RELU1 = 2,
    RELU6 = 3,
};

using Dimension = uint32_t;
using Dimensions = std::vector<Dimension>;

using CacheToken = std::array<uint8_t, kByteSizeOfCacheToken>;

struct OutputShape {
    std::vector<uint32_t> dimensions;
    bool isSufficient = false;
};

struct ExecutionError {
    std::string message;
    ErrorStatus code = ErrorStatus::GENERAL_FAILURE;
    // OutputShapes for code == OUTPUT_INSUFFICIENT_SIZE
    std::vector<OutputShape> outputShapes = {};
};

template <typename Type>
using ExecutionResult = base::expected<Type, ExecutionError>;

struct Timing {
    uint64_t timeOnDevice = kNoTiming;
    uint64_t timeInDriver = kNoTiming;
};

struct Capabilities {
    struct PerformanceInfo {
        float execTime = kDefaultExecTime;
        float powerUsage = kDefaultPowerUsage;
    };
    struct OperandPerformance {
        OperandType type{};
        PerformanceInfo info;
    };
    class OperandPerformanceTable {
       public:
        static Result<OperandPerformanceTable> create(
                std::vector<OperandPerformance> operandPerformances);

        PerformanceInfo lookup(OperandType type) const;
        const std::vector<OperandPerformance>& asVector() const;

       private:
        explicit OperandPerformanceTable(std::vector<OperandPerformance> operandPerformances);
        std::vector<OperandPerformance> mSorted;
    };

    PerformanceInfo relaxedFloat32toFloat16PerformanceScalar;
    PerformanceInfo relaxedFloat32toFloat16PerformanceTensor;
    OperandPerformanceTable operandPerformance;
    PerformanceInfo ifPerformance;
    PerformanceInfo whilePerformance;
};

struct Extension {
    struct OperandTypeInformation {
        uint16_t type = 0;
        bool isTensor = false;
        uint32_t byteSize = 0;
    };

    std::string name;
    std::vector<OperandTypeInformation> operandTypes;
};

struct Operation {
    OperationType type{};
    std::vector<uint32_t> inputs;
    std::vector<uint32_t> outputs;
};

struct DataLocation {
    std::variant<const void*, void*> pointer;
    uint32_t poolIndex = 0;
    uint32_t offset = 0;
    uint32_t length = 0;
};

struct Operand {
    enum class LifeTime {
        TEMPORARY_VARIABLE = 0,
        SUBGRAPH_INPUT = 1,
        SUBGRAPH_OUTPUT = 2,
        CONSTANT_COPY = 3,
        CONSTANT_REFERENCE = 4,
        NO_VALUE = 5,
        SUBGRAPH = 6,
        POINTER = 7,
    };
    using NoParams = std::monostate;
    struct SymmPerChannelQuantParams {
        std::vector<float> scales;
        uint32_t channelDim = 0;
    };
    using ExtensionParams = std::vector<uint8_t>;
    using ExtraParams = std::variant<NoParams, SymmPerChannelQuantParams, ExtensionParams>;

    OperandType type{};
    Dimensions dimensions;
    float scale = 0.0f;
    int32_t zeroPoint = 0;
    LifeTime lifetime{};
    DataLocation location;
    ExtraParams extraParams;
};

struct Handle {
    std::vector<base::unique_fd> fds;
    std::vector<int> ints;
};

using SharedHandle = std::shared_ptr<const Handle>;

struct Memory {
    SharedHandle handle;
    size_t size = 0;
    std::string name;
};

struct Model {
    struct Subgraph {
        std::vector<Operand> operands;
        std::vector<Operation> operations;
        std::vector<uint32_t> inputIndexes;
        std::vector<uint32_t> outputIndexes;
    };
    class OperandValues {
       public:
        OperandValues();
        OperandValues(const uint8_t* data, size_t length);

        DataLocation append(const uint8_t* data, size_t length);

        const uint8_t* data() const;
        size_t size() const;

       private:
        std::vector<AlignedData> mData;
    };
    struct ExtensionNameAndPrefix {
        std::string name;
        uint16_t prefix = 0;
    };

    Subgraph main;
    std::vector<Subgraph> referenced;
    OperandValues operandValues;
    std::vector<Memory> pools;
    bool relaxComputationFloat32toFloat16 = false;
    std::vector<ExtensionNameAndPrefix> extensionNameToPrefix;
};

struct BufferDesc {
    Dimensions dimensions;
};

struct BufferRole {
    uint32_t modelIndex = 0;
    uint32_t ioIndex = 0;
    float frequency = 0.0f;
};

struct Request {
    struct Argument {
        enum class LifeTime {
            POOL = 0,
            NO_VALUE = 1,
            POINTER = 2,
        };

        LifeTime lifetime{};
        DataLocation location;
        Dimensions dimensions;
    };
    enum class MemoryDomainToken : uint32_t {};
    using MemoryPool = std::variant<Memory, MemoryDomainToken, SharedBuffer>;

    std::vector<Argument> inputs;
    std::vector<Argument> outputs;
    std::vector<MemoryPool> pools;
};

// Representation of sync_fence.
class SyncFence {
   public:
    static SyncFence createAsSignaled();
    static SyncFence create(base::unique_fd fd);
    static Result<SyncFence> create(SharedHandle syncFence);

    // The function syncWait() has the same semantics as the system function
    // ::sync_wait(), except that the syncWait() return value is semantically
    // richer.
    enum class FenceState {
        ACTIVE,    // fence has not been signaled
        SIGNALED,  // fence has been signaled
        ERROR,     // fence has been placed in the error state
        UNKNOWN,   // either bad argument passed to syncWait(), or internal error
    };
    using Timeout = std::chrono::duration<int, std::milli>;
    using OptionalTimeout = std::optional<Timeout>;

    FenceState syncWait(OptionalTimeout optionalTimeout) const;

    SharedHandle getSharedHandle() const;
    bool hasFd() const;
    int getFd() const;

   private:
    explicit SyncFence(SharedHandle syncFence);

    SharedHandle mSyncFence;
};

using Clock = std::chrono::steady_clock;

using TimePoint = std::chrono::time_point<Clock, std::chrono::nanoseconds>;
using OptionalTimePoint = std::optional<TimePoint>;

using TimeoutDuration = std::chrono::nanoseconds;
using OptionalTimeoutDuration = std::optional<TimeoutDuration>;

enum class Version { ANDROID_OC_MR1, ANDROID_P, ANDROID_Q, ANDROID_R, CURRENT_RUNTIME };

}  // namespace android::nn

#endif  // ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPES_H