1 /* 2 * Copyright (c) 2023-2024 Tomeu Vizoso <[email protected]> 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_inlines.h" 11 12 #define MAX_CONFIG_BOS 4 13 14 enum etna_job_type { 15 ETNA_JOB_TYPE_NN, 16 ETNA_JOB_TYPE_TP, 17 }; 18 19 enum etna_ml_tp_type { 20 ETNA_ML_TP_TRANSPOSE, 21 ETNA_ML_TP_DETRANSPOSE, 22 ETNA_ML_TP_RESHUFFLE, 23 }; 24 25 struct etna_ml_subgraph { 26 struct pipe_ml_subgraph base; 27 28 struct util_dynarray operations; 29 30 /* Bother are indexed by tensor index */ 31 struct util_dynarray tensors; /* Contains struct pipe_resource* */ 32 struct util_dynarray offsets; /* These are integers */ 33 }; 34 35 struct etna_vip_instruction { 36 enum etna_job_type type; 37 38 struct etna_bo *configs[MAX_CONFIG_BOS]; 39 struct etna_bo *coefficients; 40 struct pipe_resource *input; 41 struct pipe_resource *output; 42 43 struct etna_bo *kernel; 44 }; 45 46 struct etna_operation { 47 struct list_head link; 48 49 enum etna_job_type type; 50 enum etna_ml_tp_type tp_type; 51 52 bool addition; 53 bool depthwise; 54 bool pointwise; 55 bool pooling_first_pixel; 56 bool padding_same; 57 58 unsigned stride; 59 60 unsigned input_tensor; 61 unsigned input_tensor_size; 62 unsigned add_input_tensor; 63 unsigned input_width; 64 unsigned input_height; 65 unsigned input_channels; 66 uint8_t input_zero_point; 67 float input_scale; 68 69 unsigned output_tensor; 70 unsigned output_width; 71 unsigned output_height; 72 unsigned output_channels; 73 uint8_t output_zero_point; 74 float output_scale; 75 76 struct pipe_resource *weight_tensor; 77 unsigned weight_width; 78 unsigned weight_height; 79 uint8_t weight_zero_point; 80 float weight_scale; 81 82 uint8_t addition_offset; 83 84 struct pipe_resource *bias_tensor; 85 }; 86 87 #define ML_DBG(fmt, ...) \ 88 do { \ 89 if (DBG_ENABLED(ETNA_DBG_ML_MSGS)) \ 90 _debug_printf(fmt, ##__VA_ARGS__); \ 91 } while (0) 92 93 unsigned etna_ml_allocate_tensor(struct etna_ml_subgraph *subgraph); 94 struct pipe_resource *etna_ml_get_tensor(struct etna_ml_subgraph *subgraph, unsigned idx); 95 unsigned etna_ml_get_offset(struct etna_ml_subgraph *subgraph, unsigned idx); 96 97 struct pipe_ml_subgraph * 98 etna_ml_subgraph_create(struct pipe_context *context, 99 const struct pipe_ml_operation *operations, 100 unsigned count); 101 102 void 103 etna_ml_subgraph_invoke(struct pipe_context *context, struct pipe_ml_subgraph *subgraph, 104 struct pipe_tensor *input); 105 106 void 107 etna_ml_subgraph_read_outputs(struct pipe_context *context, struct pipe_ml_subgraph *subgraph, 108 unsigned outputs_count, unsigned output_idxs[], void *outputs[]); 109 110 void 111 etna_ml_subgraph_destroy(struct pipe_context *context, struct pipe_ml_subgraph *subgraph); 112 113 #endif 114