ethosu: handle NULL bias tensor in convolution

PyTorch Conv2d without explicit bias produces a NULL bias_tensor
in the Gallium pipe_ml_operation. Guard against NULL dereferences
in two places:

- ethosu_lower.c: pass NULL to fill_coefs when bias_tensor is NULL
- ethosu_coefs.c: treat missing biases as zero

Fixes crashes when running Conv2d models without bias through the
Ethos-U NPU backend.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40578>
This commit is contained in:
Tomeu Vizoso 2026-03-24 18:02:35 +01:00
parent e0b401aa87
commit f0e4ccf664
2 changed files with 8 additions and 4 deletions

View file

@ -87,12 +87,14 @@ fill_scale_and_biases(struct ethosu_subgraph *subgraph, struct ethosu_operation
uint32_t shift;
int scale = ethosu_quantize_scale(conv_scale, &shift);
uint64_t bias = biases ? biases[i] : 0;
if (ethosu_ml_device(subgraph->base.device)->is_u65)
encode_bias_scale_u65(
biases[i], scale, shift, &(*scales)[idx]);
bias, scale, shift, &(*scales)[idx]);
else
encode_bias_scale_u85(
biases[i], scale, shift, &(*scales)[idx]);
bias, scale, shift, &(*scales)[idx]);
/* Saved for NPU_SET_OFM_SCALE emission in the command stream. */
if (i == 0) {

View file

@ -105,6 +105,8 @@ ethosu_lower_convolution(struct ethosu_subgraph *subgraph,
struct pipe_tensor *input_tensor,
struct ethosu_operation *operation)
{
uint8_t *bias_data = poperation->conv.bias_tensor ? poperation->conv.bias_tensor->data : NULL;
operation->type = ETHOSU_OPERATION_TYPE_CONVOLUTION;
operation->conv.depthwise = is_depthwise(poperation);
@ -148,8 +150,8 @@ ethosu_lower_convolution(struct ethosu_subgraph *subgraph,
allocate_feature_maps(subgraph, operation);
ethosu_sched_operation(subgraph, operation);
fill_coefs(subgraph, operation,
(int32_t *)poperation->conv.bias_tensor->data,
fill_coefs(subgraph, operation, (int32_t *)bias_data,
poperation->conv.weight_tensor->data,
poperation->conv.weight_tensor->dims[0] *
poperation->conv.weight_tensor->dims[1] *