1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * @addtogroup NeuralNetworks 19 * @{ 20 */ 21 22 /** 23 * @file NeuralNetworksExperimentalFeatures.h 24 */ 25 26 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_NEURAL_NETWORKS_EXPERIMENTAL_FEATURES_H 27 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_NEURAL_NETWORKS_EXPERIMENTAL_FEATURES_H 28 29 /****************************************************************** 30 * 31 * IMPORTANT NOTICE: 32 * 33 * This file is part of Android's set of stable system headers 34 * exposed by the Android NDK (Native Development Kit). 35 * 36 * Third-party source AND binary code relies on the definitions 37 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. 38 * 39 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) 40 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS 41 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY 42 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES 43 */ 44 45 #include <stdbool.h> 46 #include <stddef.h> 47 #include <stdint.h> 48 #include <sys/cdefs.h> 49 50 __BEGIN_DECLS 51 52 /** 53 * The Android NNAPI experimental feature level. 54 */ 55 typedef enum { 56 ANEURALNETWORKS_FEATURE_LEVEL_EXPERIMENTAL = 0x7FFFFFFF, 57 } ANeuralNetworksExperimentalFeatureLevelCode; 58 59 /** 60 * Operation types for experimental features. 61 * 62 * The type of an operation in a model. 63 */ 64 typedef enum { 65 /** 66 * Expands a representation of a sparse tensor to a dense tensor. 67 * 68 * To encode a conceptual n-dimensional dense tensor with dims [D0, ..., Dn-1], potentially with 69 * a k-dimensional block (0 <= k <= n) with dims [Dn, ..., Dn+k-1], the format specifies: 70 * * 1: In what order to traverse these dimensions. For example, to store a 2-D matrix in row 71 * major order, the traversal order would be [D0, D1], whereas to store it in column major 72 * order, the traversal order would be [D1, D0]. If the 2-D matrix has a 2-D inner block, 73 * the traversal order could be [D0, D1, D2, D3]. 74 * * 2: How each block dimension in [Dn, ..., Dn+k-1] maps to the original tensor dimension in 75 * [D0, ..., Dn-1]. 76 * * 3: In the traversal order defined above, the format (dense vs. sparse) and index metadata 77 * for each dimension. For a dense dimension, this is just the size of that dimension. For 78 * a sparse dimension, it's the same as the compressed index defined in the Compressed 79 * Sparse Row (CSR) format. 80 * (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) 81 * 82 * The number of inputs to this operation is determined by the number of dimensions (including 83 * the block dimensions) of the sparsity parameters. Currently, the only formats supported are 84 * DENSE and SPARSE_CSR, but additional sparsity formats may be added in later versions of this 85 * operation. 86 * 87 * Supported tensor {@link OperandCode}: 88 * * {@link ANEURALNETWORKS_TENSOR_FLOAT16} 89 * * {@link ANEURALNETWORKS_TENSOR_FLOAT32} 90 * * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM} 91 * * {@link ANEURALNETWORKS_TENSOR_QUANT8_ASYMM} 92 * * {@link ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED} 93 * * {@link ANEURALNETWORKS_TENSOR_BOOL8} 94 * * {@link ANEURALNETWORKS_TENSOR_INT32} 95 * * {@link ANEURALNETWORKS_TENSOR_QUANT16_SYMM} 96 * * {@link ANEURALNETWORKS_TENSOR_QUANT16_ASYMM} 97 * 98 * 99 * Reference: 100 * * This implementation is a modification of the TACO format. 101 * http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf 102 * 103 * Inputs: 104 * * 0: A 1-D tensor representing the compressed sparse tensor data of a conceptual 105 * n-dimensional tensor. 106 * * 1: A 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensor defining the traversal order for 107 * reading the non-zero blocks. For an n-dimensional tensor with dimensions [D0, D1, …, 108 * Dn-1]: if block sparse with a k-dimensional block (0 < k <= n), the traversal order has 109 * n+k elements. The first n elements are still a permutation of [D0, …, Dn-1]. The last k 110 * elements are a permutation of [Dn, …, Dn+k-1], defining how to traverse a block 111 * internally. If not block sparse, the traversal order is just a permutation of [D0, …, 112 * Dn-1]. 113 * * 2: An optional 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensor defining the block map. For 114 * a block sparse n-dimensional tensor with a k-dimensional block (0 < k <= n), it stores 115 * how a block dimension [Dn, …, Dn+k-1] maps to the original tensor dimension in [D0, …, 116 * Dn-1]. For i, j where 0 <= i < j < k, blockMap[i] < blockMap[j]. If not block sparse, 117 * this is null. 118 * * 3: A 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensor with n+k elements defining the format 119 * of each dimension in the traversal order (listed above). The format is either DENSE 120 * (where DENSE = 0) or SPARSE_CSR (where SPARSE_CSR = 1). DENSE means that each coordinate 121 * in this dimension is stored implicitly. SPARSE_CSR means only the coordinates with 122 * non-zero elements are stored. 123 * * 4: A 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensor with n+k elements defining the size of 124 * each dimension or block. The product of all these sizes totals the number of elements in 125 * the dense tensor. First n elements represent the sparse tensor’s shape, and the last k 126 * elements represent the block’s shape. 127 * * 5 ~ (5 + 2 * (n+k)): An optional pair of {@link ANEURALNETWORKS_TENSOR_INT32} tensors which 128 * together specify the sparse indices along that dimension. The first pair of arguments 129 * corresponds to D0, the second to D1, and so on until Dn+k-1. If the dimension is DENSE, 130 * both arguments in the pair are null and the dimension is implicitly specified by the 131 * corresponding element in Input 4. If the dimension is SPARSE_CSR, then we use the pair 132 * of array segments and array indices to encode that dimension: 133 * * * +0: An optional list of n+k input 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensors, 134 * defining the array segments. The array segments represent how to segment the indices 135 * array, each segment corresponds to one element in the previous dimension. Array 136 * segments are interspersed with array indices (listed below), so this input could be 137 * input (5, 5 + 2, …, 5 + 2*(n+k-1)). For i, j where 0 =< i < j, arraySegments[i] <= 138 * arraySegments[j]. Used if the dimension is SPARSE_CSR, omitted if the dimension is 139 * DENSE. 140 * * * +1: An optional list of n+k input 1-D {@link ANEURALNETWORKS_TENSOR_INT32} tensors, 141 * defining the array indices. The array indices represent the index of the non-zero 142 * elements within this dimension (as those in the CSR matrix format, where the first 143 * array is row pointers and the second array is column indices). Array indices are 144 * interspersed with array segments (listed above), so this input could be input (6, 6 + 145 * 2, …, 6 + 2*(n+k-1)). Used if the dimension is SPARSE_CSR, omitted if the dimension 146 * is DENSE. 147 * 148 * Outputs: 149 * * 0: An n-D dense tensor. The output tensor has the same {@link OperandCode} as input 0. 150 */ 151 ANEURALNETWORKS_DENSIFY = 20000, 152 } ANeuralNetworksExperimentalOperationCode; 153 154 __END_DECLS 155 156 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_NEURAL_NETWORKS_EXPERIMENTAL_FEATURES_H 157 158 /** @} */ 159