tracel-ai/burn · error

Cannot reshape packed tensor: inner dimension {} is not alig

Error message

Cannot reshape packed tensor: inner dimension {} is not aligned with packing factor {num_quants}

What it means

q_reshape reshapes a packed (sub-byte) quantized tensor by dividing the packed dimension size by the packing factor. If the packed dimension's length is not a multiple of the packing factor, the packed values cannot be re-packed losslessly and the op panics with this unimplemented!.

Source

Thrown at crates/burn-cubecl/src/ops/base.rs:349

    out
}

/// Reshape a jit tensor to a new shape
pub fn q_reshape(mut tensor: CubeTensor, shape: Shape) -> CubeTensor {
    let scheme = tensor.scheme();
    let curr_shape = tensor.meta.shape();

    let shape_values = match scheme.store {
        QuantStore::Native => shape.clone(),
        QuantStore::PackedNative(packed_dim) | QuantStore::PackedU32(packed_dim) => {
            let rank = shape.num_dims();
            let mut shape = shape.clone();
            let packed_d = rank - packed_dim - 1;
            let num_quants = scheme.num_quants();

            if !shape[packed_d].is_multiple_of(num_quants) {
                unimplemented!(
                    "Cannot reshape packed tensor: inner dimension {} is not aligned with packing factor {num_quants}",
                    shape[packed_d]
                );
            }

            shape[packed_d] = shape[packed_d].div_ceil(num_quants);
            shape
        }
    };

    let (values, scales) = tensor.quantized_handles().unwrap();
    let analysis_values = reshape_analysis(
        values.meta.shape(),
        Some(values.meta.strides()),
        &shape_values,
    );
    let action_values =
        analysis_values.action(values.meta.shape(), values.meta.strides(), &shape_values);

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Pad or recompute the tensor shape so the packed dimension is a multiple of the packing factor before reshaping
  2. Dequantize, reshape, and re-quantize the tensor
  3. Choose a quantization scheme without sub-byte packing (plain int8) if reshaping is required
  4. Upgrade burn to check whether unpacked reshape support was added

Example fix

// before
t.reshape([7, 32]); // packed dim 7 not multiple of 4 -> panic
// after
t.reshape([8, 32]); // aligned with packing factor 4
Defensive patterns

Strategy: validation

Validate before calling

fn reshape_packed_ok(dim: usize, num_quants: usize) -> bool {
    dim.is_multiple_of(num_quants)
}

Type guard

fn is_packed_aligned(shape: &[usize], packed_dim: usize, scheme: &QuantScheme) -> bool {
    shape[shape.len() - packed_dim - 1] % scheme.num_quants() == 0
}

Try / catch

// unimplemented! panics; validate first:
if is_packed_aligned(&new_shape, packed_dim, &scheme) { t.reshape(new_shape) } else { t.dequantize().reshape(new_shape) }

Prevention

When it happens

Trigger: Calling tensor.reshape() on a quantized tensor with packing (e.g. 2-bit packed into u8) where shape[packed_dim] % num_quants != 0, e.g. reshaping a dim of size 7 with a packing factor of 4.

Common situations: Reshaping quantized tensors produced from data whose last dimension was padded to non-multiple sizes; dynamic shapes computed at runtime that drift from multiples of the packing factor.

Related errors


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