From f6c35443921500dea1f6206311c6c8d98d97aa4b Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 13 Nov 2024 07:41:46 +0100 Subject: [PATCH] etnaviv/ml: Zero all BOs A few bugs due to uninitialized buffers have cropped up. For now let's zero them all and see if we want to do something else when we get concerned about compilation times. Reviewed-by: Philipp Zabel Part-of: --- src/gallium/drivers/etnaviv/etnaviv_ml.c | 31 +++++++++++++++++-- src/gallium/drivers/etnaviv/etnaviv_ml.h | 4 +++ src/gallium/drivers/etnaviv/etnaviv_ml_nn.c | 23 +++++--------- .../drivers/etnaviv/etnaviv_ml_nn_v7.c | 5 +-- .../drivers/etnaviv/etnaviv_ml_nn_v8.c | 6 ++-- src/gallium/drivers/etnaviv/etnaviv_ml_tp.c | 15 ++------- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml.c b/src/gallium/drivers/etnaviv/etnaviv_ml.c index f0f028e5d44..4813c2d0e98 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml.c +++ b/src/gallium/drivers/etnaviv/etnaviv_ml.c @@ -56,14 +56,41 @@ etna_ml_create_tensor(struct etna_ml_subgraph *subgraph, unsigned idx, unsigned return; } - res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, size); + res = etna_ml_create_resource(context, size); tensors[idx] = res; ML_DBG("created resource %p for tensor %d with size %d\n", res, idx, size); } +struct etna_bo * +etna_ml_create_bo(struct pipe_context *pctx, size_t size) +{ + struct etna_context *ctx = etna_context(pctx); + struct etna_bo *bo = etna_bo_new(ctx->screen->dev, + size, + DRM_ETNA_GEM_CACHE_WC); + + etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); + struct etna_nn_params *map = etna_bo_map(bo); + memset(map, 0, size); + etna_bo_cpu_fini(bo); + + return bo; +} + +struct pipe_resource * +etna_ml_create_resource(struct pipe_context *pctx, size_t size) +{ + struct pipe_resource *res = pipe_buffer_create(pctx->screen, 0, PIPE_USAGE_DEFAULT, size); + void *ptr = etna_bo_map(etna_resource(res)->bo); + memset(ptr, 0, pipe_buffer_size(res)); + + return res; +} + struct etna_core_npu_info * -etna_ml_get_core_info(struct etna_context *context) { +etna_ml_get_core_info(struct etna_context *context) +{ struct etna_screen *screen = context->screen; struct etna_core_info *info = etna_gpu_get_core_info(screen->npu); return &info->npu; diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml.h b/src/gallium/drivers/etnaviv/etnaviv_ml.h index 477d7633893..48e40250fec 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml.h +++ b/src/gallium/drivers/etnaviv/etnaviv_ml.h @@ -105,6 +105,10 @@ unsigned etna_ml_allocate_tensor(struct etna_ml_subgraph *subgraph); struct pipe_resource *etna_ml_get_tensor(struct etna_ml_subgraph *subgraph, unsigned idx); unsigned etna_ml_get_offset(struct etna_ml_subgraph *subgraph, unsigned idx); +struct etna_bo *etna_ml_create_bo(struct pipe_context *pctx, size_t size); + +struct pipe_resource *etna_ml_create_resource(struct pipe_context *pctx, size_t size); + struct etna_core_npu_info *etna_ml_get_core_info(struct etna_context *context); struct pipe_ml_subgraph * diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml_nn.c b/src/gallium/drivers/etnaviv/etnaviv_ml_nn.c index ba4070dcd7d..7cb88f3fe76 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml_nn.c +++ b/src/gallium/drivers/etnaviv/etnaviv_ml_nn.c @@ -191,8 +191,7 @@ pointwise_to_2x2(struct etna_ml_subgraph *subgraph, struct etna_operation *opera struct pipe_context *context = subgraph->base.context; uint8_t *input = map_resource(operation->weight_tensor); unsigned new_size = operation->output_channels * 2 * 2 * operation->input_channels; - struct pipe_resource *output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, - new_size); + struct pipe_resource *output_res = etna_ml_create_resource(context, new_size); uint8_t *output = map_resource(output_res); for (unsigned channel = 0; channel < operation->output_channels; channel++) { @@ -218,8 +217,7 @@ expand_depthwise(struct etna_ml_subgraph *subgraph, struct etna_operation *opera struct pipe_context *context = subgraph->base.context; uint8_t *input = map_resource(operation->weight_tensor); unsigned new_size = operation->output_channels * operation->weight_width * operation->weight_height * operation->input_channels; - struct pipe_resource *output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, - new_size); + struct pipe_resource *output_res = etna_ml_create_resource(context, new_size); uint8_t *output = map_resource(output_res); /* Lower depthwise convolution to regular convolution, as the hardware doesn't support those */ @@ -247,8 +245,7 @@ reorder_for_hw_depthwise(struct etna_ml_subgraph *subgraph, struct etna_operatio { struct pipe_context *context = subgraph->base.context; uint8_t *input = map_resource(operation->weight_tensor); - struct pipe_resource *output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, - pipe_buffer_size(operation->weight_tensor)); + struct pipe_resource *output_res = etna_ml_create_resource(context, pipe_buffer_size(operation->weight_tensor)); uint8_t (*output)[operation->weight_width * operation->weight_height] = (void *)map_resource(output_res); for (int i = 0; i < operation->weight_height * operation->weight_width * operation->output_channels; i++) { @@ -285,7 +282,7 @@ transpose(struct etna_ml_subgraph *subgraph, struct etna_operation *operation) new_size = operation->output_channels * operation->weight_width * \ operation->weight_height * input_channels; - output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, new_size); + output_res = etna_ml_create_resource(context, new_size); output = map_resource(output_res); uint8_t (*input)[operation->weight_width][operation->weight_height][input_channels] = map; @@ -379,7 +376,7 @@ strided_to_normal(struct etna_ml_subgraph *subgraph, struct etna_operation *oper operation->weight_height = DIV_ROUND_UP(operation->weight_height, operation->stride); new_size = operation->output_channels * operation->weight_width * operation->weight_height * operation->input_channels; - output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, new_size); + output_res = etna_ml_create_resource(context, new_size); output = map_resource(output_res); unsigned wdims_out[4] = {operation->output_channels, operation->weight_width, operation->weight_height, operation->input_channels}; @@ -556,7 +553,7 @@ etna_ml_lower_add(struct etna_ml_subgraph *subgraph, operation->output_zero_point = poperation->output_tensor->zero_point; operation->output_scale = poperation->output_tensor->scale; - operation->weight_tensor = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, 8); + operation->weight_tensor = etna_ml_create_resource(context, 8); operation->weight_width = 2; operation->weight_height = 2; operation->weight_zero_point = 0x0; @@ -564,10 +561,9 @@ etna_ml_lower_add(struct etna_ml_subgraph *subgraph, operation->addition_offset = compute_addition_offset(poperation->add.input_tensor->scale, poperation->input_tensor->scale, operation->weight_scale); uint8_t *weight_map = map_resource(operation->weight_tensor); - memset(weight_map, 0, pipe_buffer_size(operation->weight_tensor)); weight_map[0] = compute_weight_add(poperation->add.input_tensor->scale, poperation->input_tensor->scale, operation->weight_scale); - operation->bias_tensor = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, 4); + operation->bias_tensor = etna_ml_create_resource(context, 4); int32_t *bias_map = map_resource(operation->bias_tensor); bias_map[0] = compute_bias_add(poperation->add.input_tensor->scale, poperation->input_tensor->scale, poperation->add.input_tensor->zero_point, poperation->input_tensor->zero_point, @@ -624,9 +620,7 @@ create_nn_config(struct etna_ml_subgraph *subgraph, const struct etna_operation unsigned nn_core_count = etna_ml_get_core_info(ctx)->nn_core_count; unsigned nn_core_version = ctx->screen->specs.nn_core_version; unsigned oc_sram_size = etna_ml_get_core_info(ctx)->on_chip_sram_size; - struct etna_bo *bo = etna_bo_new(ctx->screen->dev, - sizeof(struct etna_nn_params), - DRM_ETNA_GEM_CACHE_WC); + struct etna_bo *bo = etna_ml_create_bo(context, sizeof(struct etna_nn_params)); unsigned input_width = operation->input_width; unsigned input_height = operation->input_height; unsigned input_channels = operation->input_channels; @@ -646,7 +640,6 @@ create_nn_config(struct etna_ml_subgraph *subgraph, const struct etna_operation etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); struct etna_nn_params *map = etna_bo_map(bo); - memset(map, 0, sizeof(*map)); map->layer_type = 0x0; map->no_z_offset = nn_core_version == 8; map->prelu = 0x0; diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v7.c b/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v7.c index 3dddfc1c9e9..918a4f934ff 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v7.c +++ b/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v7.c @@ -529,14 +529,11 @@ etna_ml_create_coeffs_v7(struct etna_ml_subgraph *subgraph, const struct etna_op bo_size = calculate_weight_bo_size(subgraph, operation); zrl_bits = calculate_zrl_bits(subgraph, operation); - struct etna_bo *compressed = etna_bo_new(ctx->screen->dev, - bo_size, - DRM_ETNA_GEM_CACHE_WC); + struct etna_bo *compressed = etna_ml_create_bo(context, bo_size); etna_bo_cpu_prep(compressed, DRM_ETNA_PREP_WRITE); uint32_t *map = etna_bo_map(compressed); - memset(map, 0, bo_size); uint32_t *header = map; map += header_size / 4; diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v8.c b/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v8.c index bb93eadeab3..6840301285c 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v8.c +++ b/src/gallium/drivers/etnaviv/etnaviv_ml_nn_v8.c @@ -159,8 +159,7 @@ reorder_for_hw_depthwise(struct etna_ml_subgraph *subgraph, struct etna_operatio { struct pipe_context *context = subgraph->base.context; uint8_t *input = map_resource(operation->weight_tensor); - struct pipe_resource *output_res = pipe_buffer_create(context->screen, 0, PIPE_USAGE_DEFAULT, - pipe_buffer_size(operation->weight_tensor)); + struct pipe_resource *output_res = etna_ml_create_resource(context, pipe_buffer_size(operation->weight_tensor)); uint8_t (*output)[operation->weight_width * operation->weight_height] = (void *)map_resource(output_res); for (int i = 0; i < operation->weight_height * operation->weight_width * operation->output_channels; i++) { @@ -697,7 +696,7 @@ create_bo(struct etna_ml_subgraph *subgraph, const struct etna_operation *operat unsigned tail_size = 64; max_size = header_size + cores_used * body_size + tail_size; - return etna_bo_new(ctx->screen->dev, max_size, DRM_ETNA_GEM_CACHE_WC); + return etna_ml_create_bo(context, max_size); } static void @@ -806,7 +805,6 @@ etna_ml_create_coeffs_v8(struct etna_ml_subgraph *subgraph, const struct etna_op uint8_t symbol_map[8]; etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); - memset(header, 0, sizeof(*header)); calculate_symbol_map(subgraph, operation, symbol_map); header->symbol_map = pack_symbol_map(symbol_map); diff --git a/src/gallium/drivers/etnaviv/etnaviv_ml_tp.c b/src/gallium/drivers/etnaviv/etnaviv_ml_tp.c index bb324e412c7..df20bc96b40 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_ml_tp.c +++ b/src/gallium/drivers/etnaviv/etnaviv_ml_tp.c @@ -244,10 +244,7 @@ set_default_tp_config(struct etna_tp_params *map) static struct etna_bo * create_transpose_config(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation) { - struct etna_context *ctx = etna_context(subgraph->base.context); - struct etna_bo *bo = etna_bo_new(ctx->screen->dev, - sizeof(struct etna_tp_params), - DRM_ETNA_GEM_CACHE_WC); + struct etna_bo *bo = etna_ml_create_bo(subgraph->base.context, sizeof(struct etna_tp_params)); etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); @@ -293,13 +290,10 @@ create_transpose_config(struct etna_ml_subgraph *subgraph, const struct etna_ope static struct etna_bo * create_detranspose_config(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation) { - struct etna_context *ctx = etna_context(subgraph->base.context); unsigned input_width = operation->input_width; unsigned input_height = operation->input_height; unsigned input_channels = operation->input_channels; - struct etna_bo *bo = etna_bo_new(ctx->screen->dev, - sizeof(struct etna_tp_params), - DRM_ETNA_GEM_CACHE_WC); + struct etna_bo *bo = etna_ml_create_bo(subgraph->base.context, sizeof(struct etna_tp_params)); etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); @@ -416,10 +410,7 @@ static struct etna_bo * create_reshuffle_config(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, unsigned tp_core, unsigned tp_cores_used) { - struct etna_context *ctx = etna_context(subgraph->base.context); - struct etna_bo *bo = etna_bo_new(ctx->screen->dev, - sizeof(struct etna_tp_params), - DRM_ETNA_GEM_CACHE_WC); + struct etna_bo *bo = etna_ml_create_bo(subgraph->base.context, sizeof(struct etna_tp_params)); unsigned input_width = operation->input_width; unsigned input_height = operation->input_height; unsigned output_width = operation->output_width;