1 /* 2 * Copyright (c) 2023-2024 Tomeu Vizoso <tomeu@tomeuvizoso.net> 3 * SPDX-License-Identifier: MIT 4 */ 5 6 #ifndef H_ETNA_ML 7 #define H_ETNA_ML 8 9 #include "pipe/p_state.h" 10 #include "util/u_dynarray.h" 11 #include "etnaviv_context.h" 12 13 #define MAX_CONFIG_BOS 4 14 15 /* 16 * SWAP - swap value of @a and @b 17 */ 18 #define SWAP(a, b) \ 19 do { \ 20 __typeof(a) __tmp = (a); \ 21 (a) = (b); \ 22 (b) = __tmp; \ 23 } while (0) 24 25 enum etna_job_type { 26 ETNA_JOB_TYPE_NN, 27 ETNA_JOB_TYPE_TP, 28 ETNA_JOB_TYPE_CONCAT, /* Fake operation, won't execute on HW. Hack will go away after the move to NIR. */ 29 ETNA_JOB_TYPE_SPLIT, /* Fake operation, won't execute on HW. Hack will go away after the move to NIR. */ 30 }; 31 32 enum etna_ml_tp_type { 33 ETNA_ML_TP_TRANSPOSE, 34 ETNA_ML_TP_DETRANSPOSE, 35 ETNA_ML_TP_RESHUFFLE, 36 ETNA_ML_TP_PAD, 37 }; 38 39 struct etna_ml_subgraph { 40 struct pipe_ml_subgraph base; 41 42 struct util_dynarray operations; 43 44 /* The three are indexed by tensor index */ 45 struct util_dynarray tensors; /* Contains struct pipe_resource* */ 46 struct util_dynarray offsets; /* These are integers */ 47 struct util_dynarray sizes; /* These are integers */ 48 }; 49 50 struct etna_vip_instruction { 51 enum etna_job_type type; 52 enum etna_ml_tp_type tp_type; 53 54 struct etna_bo *configs[MAX_CONFIG_BOS]; 55 struct etna_bo *coefficients; 56 struct pipe_resource *input; 57 unsigned input_offset; 58 struct pipe_resource *output; 59 unsigned output_offset; 60 61 struct etna_bo *kernel; 62 }; 63 64 #define MAX_TENSORS 10 65 struct etna_operation { 66 struct list_head link; 67 68 enum etna_job_type type; 69 enum etna_ml_tp_type tp_type; 70 71 bool addition; 72 bool depthwise; 73 bool pointwise; 74 bool fully_connected; 75 bool pooling_first_pixel; 76 bool padding_same; 77 bool relu; 78 79 unsigned stride; 80 81 unsigned input_tensors[MAX_TENSORS]; 82 unsigned input_count; 83 unsigned input_tensor_sizes[MAX_TENSORS]; 84 85 /* The following apply to the first input tensor only */ 86 unsigned input_width; 87 unsigned input_height; 88 unsigned input_channels; 89 uint8_t input_zero_point; 90 float input_scale; 91 92 unsigned output_tensors[MAX_TENSORS]; 93 unsigned output_count; 94 unsigned output_tensor_sizes[MAX_TENSORS]; 95 96 /* The following apply to the first output tensor only */ 97 unsigned output_width; 98 unsigned output_height; 99 unsigned output_channels; 100 uint8_t output_zero_point; 101 float output_scale; 102 103 struct pipe_resource *weight_tensor; 104 unsigned weight_width; 105 unsigned weight_height; 106 uint8_t weight_zero_point; 107 float weight_scale; 108 bool weight_signed; 109 110 uint8_t addition_offset; 111 112 struct pipe_resource *bias_tensor; 113 }; 114 115 #define ML_DBG(fmt, ...) \ 116 do { \ 117 if (DBG_ENABLED(ETNA_DBG_ML_MSGS)) \ 118 _debug_printf(fmt, ##__VA_ARGS__); \ 119 } while (0) 120 121 unsigned etna_ml_allocate_tensor(struct etna_ml_subgraph *subgraph); 122 struct pipe_resource *etna_ml_get_tensor(struct etna_ml_subgraph *subgraph, unsigned idx); 123 unsigned etna_ml_get_offset(struct etna_ml_subgraph *subgraph, unsigned idx); 124 unsigned etna_ml_get_size(struct etna_ml_subgraph *subgraph, unsigned idx); 125 126 struct etna_bo *etna_ml_create_bo(struct pipe_context *pctx, size_t size); 127 128 struct pipe_resource *etna_ml_create_resource(struct pipe_context *pctx, size_t size); 129 130 struct etna_core_npu_info *etna_ml_get_core_info(struct etna_context *context); 131 132 struct pipe_ml_subgraph * 133 etna_ml_subgraph_create(struct pipe_context *context, 134 const struct pipe_ml_operation *operations, 135 unsigned count); 136 137 void 138 etna_ml_subgraph_invoke(struct pipe_context *pctx, struct pipe_ml_subgraph *psubgraph, 139 unsigned inputs_count, unsigned input_idxs[], void *inputs[], bool is_signed[]); 140 141 void 142 etna_ml_subgraph_read_outputs(struct pipe_context *context, struct pipe_ml_subgraph *subgraph, 143 unsigned outputs_count, unsigned output_idxs[], void *outputs[], 144 bool is_signed[]); 145 146 void 147 etna_ml_subgraph_destroy(struct pipe_context *context, struct pipe_ml_subgraph *subgraph); 148 149 #endif 150