tracel-ai/burn · error

Invalid broadcast shapes: Next grad shape {:?}, Previous gra

Error message

Invalid broadcast shapes: Next grad shape {:?}, Previous grad shape {:?}. Expected the shape of the next grad to be 1.

What it means

read_tensor_async in the router interpreter can read Float, Int, and Bool tensors, but has no path for quantized (DType::QFloat — todo!) or any other dtype (unimplemented!). Reading such a tensor's data panics.

Source

Thrown at crates/burn-autodiff/src/ops/base.rs:317

    }

    fn distributed_params(&self) -> Option<DistributedParams> {
        self.ops.node.distributed_params.clone()
    }
}

/// Make sure the grad tensor has the given shape.
///
/// If broadcasting happened during the forward pass, the gradients will be sum along the
/// broadcasted dimension.
pub fn broadcast_shape<B: Backend>(mut grad: FloatTensor<B>, shape: &Shape) -> FloatTensor<B> {
    let shape_grad = grad.shape();
    let ndims = shape_grad.num_dims();

    for i in 0..ndims {
        if shape_grad[i] != shape[i] {
            if shape[i] != 1 {
                panic!(
                    "Invalid broadcast shapes: Next grad shape {:?}, Previous grad shape {:?}. {}",
                    shape, shape_grad, "Expected the shape of the next grad to be 1."
                );
            }
            grad = B::float_sum_dim(grad, i);
        }
    }

    grad
}

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Dequantize the tensor to float first (via dequantize ops on a concrete backend), then read its data.
  2. Avoid read_tensor_async on quantized tensors; keep quantized tensors opaque and read only their float counterparts.
  3. Check tensor.dtype before reading and branch accordingly; wait for upstream QFloat read support if quantized reads are required.

Example fix

// before
let data = q_tensor.into_data(); // panics in read_tensor_async
// after
let f_tensor = my_backend::dequantize(q_tensor, FloatDType::F32);
let data = f_tensor.into_data();
Defensive patterns

Strategy: validation

Validate before calling

fn readable_dtype(dtype: &burn_tensor::DType) -> bool {
    !matches!(dtype, burn_tensor::DType::QFloat(_))
}
// call before read_tensor_async / into_data

Type guard

fn is_quantized(dtype: &burn_tensor::DType) -> bool {
    matches!(dtype, burn_tensor::DType::QFloat(_))
}

Try / catch

std::panic::catch_unwind(std::panic::AssertUnwindSafe(||
    read_tensor_async::<B, _>(ctx, desc)
)).map_err(|_| anyhow::anyhow!("reading quantized tensor data is not supported; dequantize first"))

Prevention

When it happens

Trigger: Calling read_tensor_async (used by into_data/read APIs on routed backends) on a tensor whose dtype is QFloat or otherwise not Float/Int/Bool.

Common situations: Inspecting/serializing a quantized tensor's data through a router backend (e.g. during quantized model export or debugging); quantization support not yet wired into the router's read path.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/b5cb570e6a7cc611. Report an issue: GitHub.